Skip to content

Commit d02693f

Browse files
Merge branch 'development' of https://github.com/codeinwp/visualizer into issue-655
2 parents 55f9023 + d2413ef commit d02693f

File tree

11 files changed

+326
-70
lines changed

11 files changed

+326
-70
lines changed

classes/Visualizer/Module.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function _getDataAs( $chart_id, $type ) {
190190
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
191191
$rows = array();
192192
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
193-
$data = unserialize( $chart->post_content );
193+
$data = self::get_chart_data( $chart, $type, false );
194194
if ( ! empty( $series ) ) {
195195
$row = array();
196196
foreach ( $series as $array ) {
@@ -698,4 +698,30 @@ public static final function get_features_for_license( $plan ) {
698698
}
699699
}
700700

701+
/**
702+
* Gets the chart content after common manipulations.
703+
*/
704+
public static function get_chart_data( $chart, $type, $run_filter = true ) {
705+
// change HTML entities
706+
$data = unserialize( html_entity_decode( $chart->post_content ) );
707+
$altered = array();
708+
foreach ( $data as $index => $array ) {
709+
if ( ! is_array( $index ) ) {
710+
foreach ( $array as &$datum ) {
711+
if ( is_string( $datum ) ) {
712+
$datum = stripslashes( $datum );
713+
}
714+
}
715+
$altered[ $index ] = $array;
716+
}
717+
}
718+
// if something goes wrong and the end result is empty, be safe and use the original data
719+
if ( empty( $altered ) ) {
720+
$altered = $data;
721+
}
722+
if ( $run_filter ) {
723+
return apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, $altered, $chart->ID, $type );
724+
}
725+
return $altered;
726+
}
701727
}

classes/Visualizer/Module/Admin.php

Lines changed: 111 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function __construct( Visualizer_Plugin $plugin ) {
6363
$this->_addFilter( 'visualizer_get_chart_counts', 'getChartCountsByTypeAndMeta' );
6464
$this->_addFilter( 'visualizer_feedback_review_trigger', 'feedbackReviewTrigger' );
6565

66+
// screen pagination
67+
$this->_addFilter( 'set-screen-option', 'setScreenOptions', 10, 3 );
68+
6669
// revision support.
6770
$this->_addFilter( 'wp_revisions_to_keep', 'limitRevisions', null, 10, 2 );
6871
$this->_addAction( '_wp_put_post_revision', 'addRevision', null, 10, 1 );
@@ -707,6 +710,94 @@ public function registerAdminMenu() {
707710
array( $this, 'renderSupportPage' )
708711
);
709712
remove_submenu_page( Visualizer_Plugin::NAME, Visualizer_Plugin::NAME );
713+
714+
add_action( "load-{$this->_libraryPage}", array( $this, 'addScreenOptions' ) );
715+
}
716+
717+
/**
718+
* Adds the screen options for pagination.
719+
*/
720+
function addScreenOptions() {
721+
$screen = get_current_screen();
722+
723+
// bail if it's some other page.
724+
if ( ! is_object( $screen ) || $screen->id !== $this->_libraryPage ) {
725+
return;
726+
}
727+
728+
$args = array(
729+
'label' => __( 'Number of charts per page:', 'visualizer' ),
730+
'default' => 6,
731+
'option' => 'visualizer_per_page_library',
732+
);
733+
add_screen_option( 'per_page', $args );
734+
}
735+
736+
/**
737+
* Returns the screen option for pagination.
738+
*/
739+
function setScreenOptions( $status, $option, $value ) {
740+
if ( 'visualizer_per_page_library' === $option ) {
741+
return $value;
742+
}
743+
}
744+
745+
/**
746+
* Adds the display filters to be used in the meta_query while displaying the charts.
747+
*/
748+
private function getDisplayFilters( &$query_args ) {
749+
$query = array();
750+
751+
// add chart type filter to the query arguments
752+
$type = filter_input( INPUT_GET, 'type' );
753+
if ( $type && in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
754+
$query[] = array(
755+
'key' => Visualizer_Plugin::CF_CHART_TYPE,
756+
'value' => $type,
757+
'compare' => '=',
758+
);
759+
}
760+
761+
// add chart library filter to the query arguments
762+
$library = filter_input( INPUT_GET, 'library' );
763+
if ( $library ) {
764+
$query[] = array(
765+
'key' => Visualizer_Plugin::CF_CHART_LIBRARY,
766+
'value' => $library,
767+
'compare' => '=',
768+
);
769+
}
770+
771+
// add date filter to the query arguments
772+
$date = filter_input( INPUT_GET, 'date' );
773+
$possible = array_keys( Visualizer_Plugin::getSupportedDateFilter() );
774+
if ( $date && in_array( $date, $possible, true ) ) {
775+
$query_args['date_query'] = array(
776+
'after' => "$date -1 day",
777+
'inclusive' => true,
778+
);
779+
}
780+
781+
// add source filter to the query arguments
782+
$source = filter_input( INPUT_GET, 'source' );
783+
if ( $source ) {
784+
$source = ucwords( $source );
785+
$source = "Visualizer_Source_{$source}";
786+
$query[] = array(
787+
'key' => Visualizer_Plugin::CF_SOURCE,
788+
'value' => $source,
789+
'compare' => '=',
790+
);
791+
}
792+
793+
$query_args['meta_query'] = $query;
794+
795+
$orderby = filter_input( INPUT_GET, 'orderby' );
796+
$order = filter_input( INPUT_GET, 'order' );
797+
if ( $orderby ) {
798+
$query_args['order_by'] = $orderby;
799+
}
800+
$query_args['order'] = empty( $order ) ? 'desc' : $order;
710801
}
711802

712803
/**
@@ -730,25 +821,30 @@ private function getQuery() {
730821
),
731822
)
732823
);
824+
825+
$per_page = 6;
826+
$screen = get_current_screen();
827+
if ( $screen ) {
828+
// retrieve the per_page option
829+
$screen_option = $screen->get_option( 'per_page', 'option' );
830+
// retrieve the value stored for the current user
831+
$user = get_current_user_id();
832+
$per_page = get_user_meta( $user, $screen_option, true );
833+
if ( empty( $per_page ) || $per_page < 1 ) {
834+
// nothing set, get the default value
835+
$per_page = $screen->get_option( 'per_page', 'default' );
836+
}
837+
}
838+
733839
// the initial query arguments to fetch charts
734840
$query_args = array(
735841
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
736-
'posts_per_page' => 6,
842+
'posts_per_page' => $per_page,
737843
'paged' => $page,
738844
);
739-
// add chart type filter to the query arguments
740-
$filter = filter_input( INPUT_GET, 'type' );
741-
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
742-
$query_args['meta_query'] = array(
743-
array(
744-
'key' => Visualizer_Plugin::CF_CHART_TYPE,
745-
'value' => $filter,
746-
'compare' => '=',
747-
),
748-
);
749-
} else {
750-
$filter = 'all';
751-
}
845+
846+
$this->getDisplayFilters( $query_args );
847+
752848
// Added by Ash/Upwork
753849
$filterByMeta = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
754850
if ( $filterByMeta ) {
@@ -830,7 +926,7 @@ public function renderLibraryPage() {
830926
}
831927

832928
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
833-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
929+
$data = self::get_chart_data( $chart, $type );
834930

835931
$library = $this->load_chart_type( $chart->ID );
836932

classes/Visualizer/Module/Chart.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private function _getChartArray( WP_Post $chart = null ) {
358358
}
359359
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
360360
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
361-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
361+
$data = self::get_chart_data( $chart, $type );
362362
$library = $this->load_chart_type( $chart->ID );
363363

364364
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
@@ -905,10 +905,11 @@ private function handleCSVasString( $data, $editor_type ) {
905905
}
906906
// don't use fpucsv here because we need to just dump the data
907907
// minus the empty rows
908-
// and if any row contains ' or ", fputcsv will mess it up
909908
// because fputcsv needs to tokenize
910-
// and let's standardize the CSV enclosure
911-
$row = str_replace( "'", VISUALIZER_CSV_ENCLOSURE, $row );
909+
// we can standardize the CSV enclosure here and replace all ' with "
910+
// we can assume that if there are an even number of ' they should be changed to "
911+
// but that will screw up words like fo'c'sle
912+
// so here let's just assume ' will NOT be used for enclosures
912913
fwrite( $handle, $row );
913914
fwrite( $handle, PHP_EOL );
914915
}

classes/Visualizer/Module/Frontend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function renderChart( $atts ) {
253253
}
254254

255255
// handle data filter hooks
256-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
256+
$data = self::get_chart_data( $chart, $type );
257257
if ( ! empty( $atts['data'] ) ) {
258258
$data = apply_filters( $atts['data'], $data, $chart->ID, $type );
259259
}

classes/Visualizer/Plugin.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ class Visualizer_Plugin {
123123
*/
124124
private $_modules = array();
125125

126+
/**
127+
* Returns the date queries supported in the library date filter.
128+
*/
129+
public static function getSupportedDateFilter() {
130+
return apply_filters( 'visualizer_filter_by_date', array( '' => __( 'All dates', 'visualizer' ), 'yesterday' => __( 'Yesterday', 'visualizer' ), 'last week' => __( 'Last Week', 'visualizer' ), 'last month' => __( 'Last Month', 'visualizer' ), 'last year' => __( 'Last Year', 'visualizer' ) ) );
131+
}
132+
126133
/**
127134
* Private constructor.
128135
*

classes/Visualizer/Render/Layout.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ public static function _renderJsonScreen( $args ) {
141141
$root = get_post_meta( $id, Visualizer_Plugin::CF_JSON_ROOT, true );
142142
$paging = get_post_meta( $id, Visualizer_Plugin::CF_JSON_PAGING, true );
143143
$headers = get_post_meta( $id, Visualizer_Plugin::CF_JSON_HEADERS, true );
144-
if ( empty( $headers['method'] ) ) {
144+
if ( empty( $headers ) ) {
145+
$headers = array();
146+
}
147+
if ( $headers && empty( $headers['method'] ) ) {
145148
$headers['method'] = 'get';
146149
}
147150
$methods = apply_filters( 'visualizer_json_request_methods', array( 'GET', 'POST' ) );
@@ -174,7 +177,7 @@ class="visualizer-input json-form-element">
174177
<div>
175178
<select name="method" class="json-form-element">
176179
<?php foreach ( $methods as $method ) { ?>
177-
<option value="<?php echo $method; ?>" <?php selected( $headers['method'], $method ); ?>><?php echo $method; ?></option>
180+
<option value="<?php echo $method; ?>" <?php $headers && selected( $headers['method'], $method ); ?>><?php echo $method; ?></option>
178181
<?php } ?>
179182
</select>
180183
</div>
@@ -339,7 +342,7 @@ public static function _renderTextEditor( $args ) {
339342
$csv = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, array(), $chart_id, 'csv-display' );
340343
$data = '';
341344
if ( ! empty( $csv ) && isset( $csv['string'] ) ) {
342-
$data = str_replace( PHP_EOL, "\n", $csv['string'] );
345+
$data = str_replace( PHP_EOL, "\n", stripslashes( $csv['string'] ) );
343346
}
344347
?>
345348
<div class="viz-simple-editor-type viz-text-editor">
@@ -368,7 +371,7 @@ public static function _renderEditorTable( $args ) {
368371
}
369372
$chart = get_post( $chart_id );
370373
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
371-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( str_replace( "'", "\'", html_entity_decode( $chart->post_content ) ) ), $type );
374+
$data = Visualizer_Module::get_chart_data( $chart, $type );
372375
} else {
373376
$headers = array_keys( $data[0] );
374377
}

0 commit comments

Comments
 (0)