@@ -61,6 +61,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
61
61
62
62
$ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_FETCH_DB_DATA , 'getQueryData ' );
63
63
$ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_SAVE_DB_QUERY , 'saveQuery ' );
64
+ $ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY , 'saveFilter ' );
64
65
}
65
66
66
67
/**
@@ -94,7 +95,7 @@ public function getCharts() {
94
95
$ filter = filter_input ( INPUT_GET , 'filter ' , FILTER_SANITIZE_STRING );
95
96
}
96
97
97
- if ( $ filter && in_array ( $ filter , Visualizer_Plugin::getChartTypes () ) ) {
98
+ if ( $ filter && in_array ( $ filter , Visualizer_Plugin::getChartTypes (), true ) ) {
98
99
$ query_args ['meta_query ' ] = array (
99
100
array (
100
101
'key ' => Visualizer_Plugin::CF_CHART_TYPE ,
@@ -201,7 +202,7 @@ public static function _sendResponse( $results ) {
201
202
* @access public
202
203
*/
203
204
public function deleteChart () {
204
- $ is_post = $ _SERVER ['REQUEST_METHOD ' ] == 'POST ' ;
205
+ $ is_post = $ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ;
205
206
$ input_method = $ is_post ? INPUT_POST : INPUT_GET ;
206
207
$ chart_id = $ success = false ;
207
208
$ nonce = wp_verify_nonce ( filter_input ( $ input_method , 'nonce ' ) );
@@ -219,7 +220,7 @@ public function deleteChart() {
219
220
);
220
221
if ( $ chart_id ) {
221
222
$ chart = get_post ( $ chart_id );
222
- $ success = $ chart && $ chart ->post_type == Visualizer_Plugin::CPT_VISUALIZER ;
223
+ $ success = $ chart && $ chart ->post_type === Visualizer_Plugin::CPT_VISUALIZER ;
223
224
}
224
225
}
225
226
if ( $ success ) {
@@ -236,6 +237,34 @@ public function deleteChart() {
236
237
exit ;
237
238
}
238
239
240
+ /**
241
+ * Delete charts that are still in auto-draft mode.
242
+ */
243
+ private function deleteOldCharts () {
244
+ $ query = new WP_Query (
245
+ array (
246
+ 'post_type ' => Visualizer_Plugin::CPT_VISUALIZER ,
247
+ 'post_status ' => 'auto-draft ' ,
248
+ 'fields ' => 'ids ' ,
249
+ 'update_post_meta_cache ' => false ,
250
+ 'update_post_term_cache ' => false ,
251
+ 'posts_per_page ' => 50 ,
252
+ 'date_query ' => array (
253
+ array (
254
+ 'before ' => 'today ' ,
255
+ ),
256
+ ),
257
+ )
258
+ );
259
+
260
+ if ( $ query ->have_posts () ) {
261
+ $ ids = array ();
262
+ while ( $ query ->have_posts () ) {
263
+ wp_delete_post ( $ query ->next_post (), true );
264
+ }
265
+ }
266
+ }
267
+
239
268
/**
240
269
* Renders appropriate page for chart builder. Creates new auto draft chart
241
270
* if no chart has been specified.
@@ -248,7 +277,8 @@ public function renderChartPages() {
248
277
defined ( 'IFRAME_REQUEST ' ) || define ( 'IFRAME_REQUEST ' , 1 );
249
278
// check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
250
279
$ chart_id = isset ( $ _GET ['chart ' ] ) ? filter_var ( $ _GET ['chart ' ], FILTER_VALIDATE_INT ) : '' ;
251
- if ( ! $ chart_id || ! ( $ chart = get_post ( $ chart_id ) ) || $ chart ->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
280
+ if ( ! $ chart_id || ! ( $ chart = get_post ( $ chart_id ) ) || $ chart ->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
281
+ $ this ->deleteOldCharts ();
252
282
$ default_type = isset ( $ _GET ['type ' ] ) && ! empty ( $ _GET ['type ' ] ) ? $ _GET ['type ' ] : 'line ' ;
253
283
$ source = new Visualizer_Source_Csv ( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples ' . DIRECTORY_SEPARATOR . $ default_type . '.csv ' );
254
284
$ source ->fetch ();
@@ -406,8 +436,8 @@ private function _handleDataAndSettingsPage() {
406
436
if ( isset ( $ _POST ['map_api_key ' ] ) ) {
407
437
update_option ( 'visualizer-map-api-key ' , $ _POST ['map_api_key ' ] );
408
438
}
409
- if ( $ _SERVER ['REQUEST_METHOD ' ] == 'POST ' && isset ( $ _GET ['nonce ' ] ) && wp_verify_nonce ( $ _GET ['nonce ' ] ) ) {
410
- if ( $ this ->_chart ->post_status == 'auto-draft ' ) {
439
+ if ( $ _SERVER ['REQUEST_METHOD ' ] === 'POST ' && isset ( $ _GET ['nonce ' ] ) && wp_verify_nonce ( $ _GET ['nonce ' ] ) ) {
440
+ if ( $ this ->_chart ->post_status === 'auto-draft ' ) {
411
441
$ this ->_chart ->post_status = 'publish ' ;
412
442
413
443
// ensure that a revision is not created. If a revision is created it will have the proper data and the parent of the revision will have default data.
@@ -434,7 +464,7 @@ private function _handleDataAndSettingsPage() {
434
464
$ sidebar ->__data = $ data ['data ' ];
435
465
} else {
436
466
$ sidebar = apply_filters ( 'visualizer_pro_chart_type_sidebar ' , '' , $ data );
437
- if ( $ sidebar != '' ) {
467
+ if ( $ sidebar !== '' ) {
438
468
$ sidebar ->__series = $ data ['series ' ];
439
469
$ sidebar ->__data = $ data ['data ' ];
440
470
}
@@ -488,10 +518,10 @@ private function _handleDataAndSettingsPage() {
488
518
$ render ->custom_css = $ data ['css ' ];
489
519
$ render ->sidebar = $ sidebar ;
490
520
if ( filter_input ( INPUT_GET , 'library ' , FILTER_VALIDATE_BOOLEAN ) ) {
491
- $ render ->button = filter_input ( INPUT_GET , 'action ' ) == Visualizer_Plugin::ACTION_EDIT_CHART
521
+ $ render ->button = filter_input ( INPUT_GET , 'action ' ) === Visualizer_Plugin::ACTION_EDIT_CHART
492
522
? esc_html__ ( 'Save Chart ' , 'visualizer ' )
493
523
: esc_html__ ( 'Create Chart ' , 'visualizer ' );
494
- if ( filter_input ( INPUT_GET , 'action ' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) {
524
+ if ( filter_input ( INPUT_GET , 'action ' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) {
495
525
$ render ->cancel_button = esc_html__ ( 'Cancel ' , 'visualizer ' );
496
526
}
497
527
} else {
@@ -514,9 +544,9 @@ private function _handleDataAndSettingsPage() {
514
544
*/
515
545
private function _handleTypesPage () {
516
546
// process post request
517
- if ( $ _SERVER ['REQUEST_METHOD ' ] == 'POST ' && wp_verify_nonce ( filter_input ( INPUT_POST , 'nonce ' ) ) ) {
547
+ if ( $ _SERVER ['REQUEST_METHOD ' ] === 'POST ' && wp_verify_nonce ( filter_input ( INPUT_POST , 'nonce ' ) ) ) {
518
548
$ type = filter_input ( INPUT_POST , 'type ' );
519
- if ( in_array ( $ type , Visualizer_Plugin::getChartTypes () ) ) {
549
+ if ( in_array ( $ type , Visualizer_Plugin::getChartTypes (), true ) ) {
520
550
// save new chart type
521
551
update_post_meta ( $ this ->_chart ->ID , Visualizer_Plugin::CF_CHART_TYPE , $ type );
522
552
// if the chart has default data, update it with appropriate default data for new type
@@ -579,7 +609,7 @@ public function uploadData() {
579
609
// check chart, if chart exists
580
610
// do not use filter_input as it does not work for phpunit test cases, use filter_var instead
581
611
$ chart_id = isset ( $ _GET ['chart ' ] ) ? filter_var ( $ _GET ['chart ' ], FILTER_VALIDATE_INT ) : '' ;
582
- if ( ! $ chart_id || ! ( $ chart = get_post ( $ chart_id ) ) || $ chart ->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
612
+ if ( ! $ chart_id || ! ( $ chart = get_post ( $ chart_id ) ) || $ chart ->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
583
613
if ( ! $ can_die ) {
584
614
return ;
585
615
}
@@ -593,7 +623,9 @@ public function uploadData() {
593
623
594
624
if ( ! isset ( $ _POST ['chart_data_src ' ] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $ _POST ['chart_data_src ' ] ) {
595
625
// delete the filters in case this chart is being uploaded from other data sources
596
- delete_post_meta ( $ chart_id , 'visualizer-filter-config ' );
626
+ delete_post_meta ( $ chart_id , Visualizer_Plugin::CF_FILTER_CONFIG );
627
+ delete_post_meta ( $ chart_id , '__transient- ' . Visualizer_Plugin::CF_FILTER_CONFIG );
628
+ delete_post_meta ( $ chart_id , '__transient- ' . Visualizer_Plugin::CF_DB_QUERY );
597
629
598
630
// delete "import from db" specific parameters.
599
631
delete_post_meta ( $ chart_id , Visualizer_Plugin::CF_DB_QUERY );
@@ -607,10 +639,11 @@ public function uploadData() {
607
639
if ( isset ( $ _POST ['vz-import-time ' ] ) ) {
608
640
apply_filters ( 'visualizer_pro_chart_schedule ' , $ chart_id , $ _POST ['remote_data ' ], $ _POST ['vz-import-time ' ] );
609
641
}
642
+ // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
610
643
} elseif ( isset ( $ _FILES ['local_data ' ] ) && $ _FILES ['local_data ' ]['error ' ] == 0 ) {
611
644
$ source = new Visualizer_Source_Csv ( $ _FILES ['local_data ' ]['tmp_name ' ] );
612
645
} elseif ( isset ( $ _POST ['chart_data ' ] ) && strlen ( $ _POST ['chart_data ' ] ) > 0 ) {
613
- $ source = apply_filters ( 'visualizer_pro_handle_chart_data ' , $ _POST ['chart_data ' ], '' );
646
+ $ source = apply_filters ( 'visualizer_pro_handle_chart_data ' , $ _POST ['chart_data ' ], '' , $ chart_id , $ _POST );
614
647
} else {
615
648
$ render ->message = esc_html__ ( 'CSV file with chart data was not uploaded. Please, try again. ' , 'visualizer ' );
616
649
}
@@ -672,7 +705,7 @@ public function cloneChart() {
672
705
);
673
706
if ( $ chart_id ) {
674
707
$ chart = get_post ( $ chart_id );
675
- $ success = $ chart && $ chart ->post_type == Visualizer_Plugin::CPT_VISUALIZER ;
708
+ $ success = $ chart && $ chart ->post_type === Visualizer_Plugin::CPT_VISUALIZER ;
676
709
}
677
710
}
678
711
$ redirect = wp_get_referer ();
@@ -847,4 +880,33 @@ public function saveQuery() {
847
880
defined ( 'WP_TESTS_DOMAIN ' ) ? wp_die () : exit ();
848
881
}
849
882
}
883
+
884
+
885
+ /**
886
+ * Saves the filter query and the schedule.
887
+ *
888
+ * @access public
889
+ */
890
+ public function saveFilter () {
891
+ check_ajax_referer ( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION , 'security ' );
892
+
893
+ $ chart_id = filter_input (
894
+ INPUT_GET ,
895
+ 'chart ' ,
896
+ FILTER_VALIDATE_INT ,
897
+ array (
898
+ 'options ' => array (
899
+ 'min_range ' => 1 ,
900
+ ),
901
+ )
902
+ );
903
+
904
+ $ hours = $ _POST ['refresh ' ];
905
+
906
+ do_action ( 'visualizer_save_filter ' , $ chart_id , $ hours );
907
+
908
+ if ( ! ( defined ( 'VISUALIZER_DO_NOT_DIE ' ) && VISUALIZER_DO_NOT_DIE ) ) {
909
+ defined ( 'WP_TESTS_DOMAIN ' ) ? wp_die () : exit ();
910
+ }
911
+ }
850
912
}
0 commit comments