Skip to content

Commit 612a90e

Browse files
Merge branch 'development' of https://github.com/codeinwp/visualizer into issue-315
2 parents dc7915c + 6c5fdf1 commit 612a90e

27 files changed

+275
-92
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Visualizer_Gutenberg_Block {
4646
* Returns an instance of this class.
4747
*/
4848
public static function get_instance() {
49-
if ( null == self::$instance ) {
49+
if ( null === self::$instance ) {
5050
self::$instance = new Visualizer_Gutenberg_Block();
5151
}
5252
return self::$instance;
@@ -297,19 +297,19 @@ public function format_chart_data( $data, $series ) {
297297
continue;
298298
}
299299

300-
if ( $row['type'] == 'number' ) {
300+
if ( $row['type'] === 'number' ) {
301301
foreach ( $data as $o => $col ) {
302302
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
303303
}
304304
}
305305

306-
if ( $row['type'] == 'boolean' ) {
306+
if ( $row['type'] === 'boolean' ) {
307307
foreach ( $data as $o => $col ) {
308308
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
309309
}
310310
}
311311

312-
if ( $row['type'] == 'timeofday' ) {
312+
if ( $row['type'] === 'timeofday' ) {
313313
foreach ( $data as $o => $col ) {
314314
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
315315
if ( $date ) {
@@ -323,7 +323,7 @@ public function format_chart_data( $data, $series ) {
323323
}
324324
}
325325

326-
if ( $row['type'] == 'string' ) {
326+
if ( $row['type'] === 'string' ) {
327327
foreach ( $data as $o => $col ) {
328328
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
329329
}

classes/Visualizer/Module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function _getDataAs( $chart_id, $type ) {
159159
$success = false;
160160
if ( $chart_id ) {
161161
$chart = get_post( $chart_id );
162-
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
162+
$success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
163163
}
164164
if ( $success ) {
165165
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );

classes/Visualizer/Module/Admin.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function restoreRevision( $post_id, $revision_id ) {
133133
* @access public
134134
*/
135135
public function init() {
136+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
136137
if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) && 'true' == get_user_option( 'rich_editing' ) ) {
137138
$this->_addFilter( 'mce_external_languages', 'add_tinymce_lang', 10, 1 );
138139
$this->_addFilter( 'mce_external_plugins', 'tinymce_plugin', 10, 1 );
@@ -465,7 +466,7 @@ private static function handleDeprecatedCharts( $types, $enabledOnly, $get2Darra
465466
*/
466467
public function renderTemplates() {
467468
global $pagenow;
468-
if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) {
469+
if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) {
469470
return;
470471
}
471472
$render = new Visualizer_Render_Templates();
@@ -485,7 +486,7 @@ public function renderTemplates() {
485486
* @param string $suffix The current page suffix.
486487
*/
487488
public function enqueueLibraryScripts( $suffix ) {
488-
if ( $suffix == $this->_libraryPage ) {
489+
if ( $suffix === $this->_libraryPage ) {
489490
wp_enqueue_style( 'visualizer-library', VISUALIZER_ABSURL . 'css/library.css', array(), Visualizer_Plugin::VERSION );
490491
$this->_addFilter( 'media_upload_tabs', 'setupVisualizerTab' );
491492
wp_enqueue_media();
@@ -579,7 +580,7 @@ private function getQuery() {
579580
);
580581
// add chart type filter to the query arguments
581582
$filter = filter_input( INPUT_GET, 'type' );
582-
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
583+
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
583584
$query_args['meta_query'] = array(
584585
array(
585586
'key' => Visualizer_Plugin::CF_CHART_TYPE,
@@ -631,7 +632,7 @@ public function renderLibraryPage() {
631632
);
632633
// add chart type filter to the query arguments
633634
$filter = filter_input( INPUT_GET, 'type' );
634-
if ( ! ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) ) {
635+
if ( ! ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) ) {
635636
$filter = 'all';
636637
}
637638

@@ -739,7 +740,7 @@ public function renderLibraryPage() {
739740
* @return array Updated array of action links.
740741
*/
741742
public function getPluginActionLinks( $links, $file ) {
742-
if ( $file == plugin_basename( VISUALIZER_BASEFILE ) ) {
743+
if ( $file === plugin_basename( VISUALIZER_BASEFILE ) ) {
743744
array_unshift(
744745
$links,
745746
sprintf(
@@ -766,7 +767,7 @@ public function getPluginActionLinks( $links, $file ) {
766767
* @return array Updated array of plugin meta links.
767768
*/
768769
public function getPluginMetaLinks( $plugin_meta, $plugin_file ) {
769-
if ( $plugin_file == plugin_basename( VISUALIZER_BASEFILE ) ) {
770+
if ( $plugin_file === plugin_basename( VISUALIZER_BASEFILE ) ) {
770771
// knowledge base link
771772
$plugin_meta[] = sprintf(
772773
'<a href="https://github.com/codeinwp/visualizer/wiki" target="_blank">%s</a>',

classes/Visualizer/Module/Chart.php

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
6161

6262
$this->_addAjaxAction( Visualizer_Plugin::ACTION_FETCH_DB_DATA, 'getQueryData' );
6363
$this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_DB_QUERY, 'saveQuery' );
64+
$this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' );
6465
}
6566

6667
/**
@@ -94,7 +95,7 @@ public function getCharts() {
9495
$filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING );
9596
}
9697

97-
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
98+
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
9899
$query_args['meta_query'] = array(
99100
array(
100101
'key' => Visualizer_Plugin::CF_CHART_TYPE,
@@ -201,7 +202,7 @@ public static function _sendResponse( $results ) {
201202
* @access public
202203
*/
203204
public function deleteChart() {
204-
$is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
205+
$is_post = $_SERVER['REQUEST_METHOD'] === 'POST';
205206
$input_method = $is_post ? INPUT_POST : INPUT_GET;
206207
$chart_id = $success = false;
207208
$nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) );
@@ -219,7 +220,7 @@ public function deleteChart() {
219220
);
220221
if ( $chart_id ) {
221222
$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;
223224
}
224225
}
225226
if ( $success ) {
@@ -236,6 +237,34 @@ public function deleteChart() {
236237
exit;
237238
}
238239

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+
239268
/**
240269
* Renders appropriate page for chart builder. Creates new auto draft chart
241270
* if no chart has been specified.
@@ -248,7 +277,8 @@ public function renderChartPages() {
248277
defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
249278
// check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
250279
$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();
252282
$default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
253283
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
254284
$source->fetch();
@@ -406,8 +436,8 @@ private function _handleDataAndSettingsPage() {
406436
if ( isset( $_POST['map_api_key'] ) ) {
407437
update_option( 'visualizer-map-api-key', $_POST['map_api_key'] );
408438
}
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' ) {
411441
$this->_chart->post_status = 'publish';
412442

413443
// 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() {
434464
$sidebar->__data = $data['data'];
435465
} else {
436466
$sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data );
437-
if ( $sidebar != '' ) {
467+
if ( $sidebar !== '' ) {
438468
$sidebar->__series = $data['series'];
439469
$sidebar->__data = $data['data'];
440470
}
@@ -488,10 +518,10 @@ private function _handleDataAndSettingsPage() {
488518
$render->custom_css = $data['css'];
489519
$render->sidebar = $sidebar;
490520
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
492522
? esc_html__( 'Save Chart', 'visualizer' )
493523
: 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 ) {
495525
$render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
496526
}
497527
} else {
@@ -514,9 +544,9 @@ private function _handleDataAndSettingsPage() {
514544
*/
515545
private function _handleTypesPage() {
516546
// 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' ) ) ) {
518548
$type = filter_input( INPUT_POST, 'type' );
519-
if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) {
549+
if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
520550
// save new chart type
521551
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type );
522552
// if the chart has default data, update it with appropriate default data for new type
@@ -579,7 +609,7 @@ public function uploadData() {
579609
// check chart, if chart exists
580610
// do not use filter_input as it does not work for phpunit test cases, use filter_var instead
581611
$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 ) {
583613
if ( ! $can_die ) {
584614
return;
585615
}
@@ -593,7 +623,9 @@ public function uploadData() {
593623

594624
if ( ! isset( $_POST['chart_data_src'] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $_POST['chart_data_src'] ) {
595625
// 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 );
597629

598630
// delete "import from db" specific parameters.
599631
delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
@@ -607,10 +639,11 @@ public function uploadData() {
607639
if ( isset( $_POST['vz-import-time'] ) ) {
608640
apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] );
609641
}
642+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
610643
} elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
611644
$source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
612645
} 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 );
614647
} else {
615648
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
616649
}
@@ -672,7 +705,7 @@ public function cloneChart() {
672705
);
673706
if ( $chart_id ) {
674707
$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;
676709
}
677710
}
678711
$redirect = wp_get_referer();
@@ -847,4 +880,33 @@ public function saveQuery() {
847880
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
848881
}
849882
}
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+
}
850912
}

classes/Visualizer/Module/Frontend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function renderChart( $atts ) {
207207
);
208208

209209
// if empty id or chart does not exists, then return empty string
210-
if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
210+
if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
211211
return '';
212212
}
213213

0 commit comments

Comments
 (0)