PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.1.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.1.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
← All changes | classes/Visualizer/Module/Chart.php +308 -24 3.0.83.1.3 View file →
@@ -57,13 +57,19 @@
57 57 $this->_addAjaxAction( Visualizer_Plugin::ACTION_EDIT_CHART, 'renderChartPages' );
58 58 $this->_addAjaxAction( Visualizer_Plugin::ACTION_UPLOAD_DATA, 'uploadData' );
59 59 $this->_addAjaxAction( Visualizer_Plugin::ACTION_CLONE_CHART, 'cloneChart' );
60 60 $this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' );
61 +
62 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_FETCH_DB_DATA, 'getQueryData' );
63 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_DB_QUERY, 'saveQuery' );
64 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' );
61 65 }
62 66
63 67 /**
64 68 * Fetches charts from database.
65 69 *
70 + * This method is also called from the media pop-up (classic editor: create a post and add chart from insert content).
71 + *
66 72 * @since 1.0.0
67 73 *
68 74 * @access public
69 75 */
@@ -71,9 +77,12 @@
71 77 $query_args = array(
72 78 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
73 79 'posts_per_page' => 9,
74 80 'paged' => filter_input(
75 - INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
81 + INPUT_GET,
82 + 'page',
83 + FILTER_VALIDATE_INT,
84 + array(
76 85 'options' => array(
77 86 'min_range' => 1,
78 87 'default' => 1,
79 88 ),
@@ -100,8 +109,18 @@
100 109 while ( $query->have_posts() ) {
101 110 $chart = $query->next_post();
102 111 $chart_data = $this->_getChartArray( $chart );
103 112 $chart_data['id'] = $chart->ID;
113 + $chart_data['library'] = $this->load_chart_type( $chart->ID );
114 + $css = '';
115 + $settings = $chart_data['settings'];
116 + $arguments = $this->get_inline_custom_css( 'visualizer-chart-' . $chart->ID, $settings );
117 + if ( ! empty( $arguments ) ) {
118 + $css = $arguments[0];
119 + $settings = $arguments[1];
120 + }
121 + $chart_data['settings'] = $settings;
122 + $chart_data['css'] = $css;
104 123 $charts[] = $chart_data;
105 124 }
106 125 self::_sendResponse(
107 126 array(
@@ -129,14 +148,33 @@
129 148 }
130 149 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
131 150 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
132 151 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
152 + $library = $this->load_chart_type( $chart->ID );
133 153
154 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
155 + $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
156 + if ( ! empty( $atts['settings'] ) ) {
157 + $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
158 + }
159 +
160 + $css = '';
161 + $arguments = $this->get_inline_custom_css( 'visualizer-' . $chart->ID, $settings );
162 + if ( ! empty( $arguments ) ) {
163 + $css = $arguments[0];
164 + $settings = $arguments[1];
165 + }
166 +
167 + $date_formats = Visualizer_Source::get_date_formats_if_exists( $series, $data );
168 +
134 169 return array(
135 170 'type' => $type,
136 171 'series' => $series,
137 - 'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ),
172 + 'settings' => $settings,
138 173 'data' => $data,
174 + 'library' => $library,
175 + 'css' => $css,
176 + 'date_formats' => $date_formats,
139 177 );
140 178 }
141 179
142 180 /**
@@ -170,9 +208,12 @@
170 208 $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) );
171 209 $capable = current_user_can( 'delete_posts' );
172 210 if ( $nonce && $capable ) {
173 211 $chart_id = filter_input(
174 - $input_method, 'chart', FILTER_VALIDATE_INT, array(
212 + $input_method,
213 + 'chart',
214 + FILTER_VALIDATE_INT,
215 + array(
175 216 'options' => array(
176 217 'min_range' => 1,
177 218 ),
178 219 )
@@ -196,8 +237,36 @@
196 237 exit;
197 238 }
198 239
199 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 +
268 + /**
200 269 * Renders appropriate page for chart builder. Creates new auto draft chart
201 270 * if no chart has been specified.
202 271 *
203 272 * @since 1.0.0
@@ -208,8 +277,9 @@
208 277 defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
209 278 // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
210 279 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
211 280 if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
281 + $this->deleteOldCharts();
212 282 $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
213 283 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
214 284 $source->fetch();
215 285 $chart_id = wp_insert_post(
@@ -226,9 +296,11 @@
226 296 add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
227 297 add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
228 298 add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
229 299 add_post_meta(
230 - $chart_id, Visualizer_Plugin::CF_SETTINGS, array(
300 + $chart_id,
301 + Visualizer_Plugin::CF_SETTINGS,
302 + array(
231 303 'focusTarget' => 'datum',
232 304 )
233 305 );
234 306 do_action( 'visualizer_pro_new_chart_defaults', $chart_id );
@@ -235,8 +307,11 @@
235 307 }
236 308 wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
237 309 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
238 310 }
311 +
312 + $this->load_chart_type( $chart_id );
313 +
239 314 // enqueue and register scripts and styles
240 315 wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
241 316 wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
242 317
@@ -241,22 +316,25 @@
241 316 wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
242 317
243 318 wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION );
244 319 wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION, true );
245 - wp_register_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
246 - wp_register_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
320 + wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
247 321 wp_register_script(
248 - 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
249 - 'google-jsapi-old',
250 - 'google-jsapi-new',
251 - 'visualizer-frame',
252 - ), Visualizer_Plugin::VERSION, true
322 + 'visualizer-render',
323 + VISUALIZER_ABSURL . 'js/render-facade.js',
324 + apply_filters( 'visualizer_assets_render', array( 'visualizer-frame', 'visualizer-customization' ), false ),
325 + Visualizer_Plugin::VERSION,
326 + true
253 327 );
254 328 wp_register_script(
255 - 'visualizer-preview', VISUALIZER_ABSURL . 'js/preview.js', array(
329 + 'visualizer-preview',
330 + VISUALIZER_ABSURL . 'js/preview.js',
331 + array(
256 332 'wp-color-picker',
257 333 'visualizer-render',
258 - ), Visualizer_Plugin::VERSION, true
334 + ),
335 + Visualizer_Plugin::VERSION,
336 + true
259 337 );
260 338 // added by Ash/Upwork
261 339 if ( VISUALIZER_PRO ) {
262 340 global $Visualizer_Pro;
@@ -297,8 +375,62 @@
297 375 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
298 376 }
299 377
300 378 /**
379 + * Load code editor assets.
380 + */
381 + private function loadCodeEditorAssets() {
382 + global $wp_version;
383 +
384 + if ( ! VISUALIZER_PRO ) {
385 + return;
386 + }
387 +
388 + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping();
389 +
390 + // data tables assets.
391 + wp_register_script( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
392 + wp_register_style( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css', array(), Visualizer_Plugin::VERSION );
393 + wp_register_style( 'visualizer-datatables-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION );
394 +
395 + wp_enqueue_script( 'visualizer-datatables' );
396 + wp_enqueue_style( 'visualizer-datatables-ui' );
397 +
398 + if ( version_compare( $wp_version, '4.9.0', '<' ) ) {
399 + // code mirror assets.
400 + wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
401 + wp_register_script( 'visualizer-codemirror-placeholder', '//codemirror.net/addon/display/placeholder.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
402 + wp_register_script( 'visualizer-codemirror-matchbrackets', '//codemirror.net/addon/edit/matchbrackets.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
403 + wp_register_script( 'visualizer-codemirror-closebrackets', '//codemirror.net/addon/edit/closebrackets.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
404 + wp_register_script( 'visualizer-codemirror-sql', '//codemirror.net/mode/sql/sql.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
405 + wp_register_script( 'visualizer-codemirror-sql-hint', '//codemirror.net/addon/hint/sql-hint.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
406 + wp_register_script( 'visualizer-codemirror-hint', '//codemirror.net/addon/hint/show-hint.js', array( 'visualizer-codemirror-sql', 'visualizer-codemirror-sql-hint', 'visualizer-codemirror-placeholder', 'visualizer-codemirror-matchbrackets', 'visualizer-codemirror-closebrackets' ), Visualizer_Plugin::VERSION );
407 + wp_register_style( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.css', array(), Visualizer_Plugin::VERSION );
408 + wp_register_style( 'visualizer-codemirror-hint', '//codemirror.net/addon/hint/show-hint.css', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
409 +
410 + wp_enqueue_script( 'visualizer-codemirror-hint' );
411 + wp_enqueue_style( 'visualizer-codemirror-hint' );
412 + } else {
413 + wp_enqueue_code_editor(
414 + array(
415 + 'type' => 'sql',
416 + 'codemirror' => array(
417 + 'autofocus' => true,
418 + 'lineWrapping' => true,
419 + 'dragDrop' => false,
420 + 'matchBrackets' => true,
421 + 'autoCloseBrackets' => true,
422 + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ),
423 + 'hintOptions' => array( 'tables' => $table_col_mapping ),
424 + ),
425 + )
426 + );
427 + }
428 +
429 + return $table_col_mapping;
430 + }
431 +
432 + /**
301 433 * Handle data and settings page
302 434 */
303 435 private function _handleDataAndSettingsPage() {
304 436 if ( isset( $_POST['map_api_key'] ) ) {
@@ -306,11 +438,18 @@
306 438 }
307 439 if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
308 440 if ( $this->_chart->post_status == 'auto-draft' ) {
309 441 $this->_chart->post_status = 'publish';
442 +
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.
444 + // we do not want any difference in data so disable revisions temporarily.
445 + add_filter( 'wp_revisions_to_keep', '__return_false' );
310 446 wp_update_post( $this->_chart->to_array() );
311 447 }
312 - update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST );
448 + // save meta data only when it is NOT being canceled.
449 + if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) {
450 + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST );
451 + }
313 452 $render = new Visualizer_Render_Page_Send();
314 453 $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID );
315 454 wp_iframe( array( $render, 'render' ) );
316 455
@@ -330,15 +469,20 @@
330 469 $sidebar->__data = $data['data'];
331 470 }
332 471 }
333 472 unset( $data['settings']['width'], $data['settings']['height'] );
334 - wp_enqueue_style( 'visualizer-frame' );
335 473 wp_enqueue_style( 'wp-color-picker' );
336 474 wp_enqueue_style( 'visualizer-frame' );
337 475 wp_enqueue_script( 'visualizer-preview' );
476 + wp_enqueue_script( 'visualizer-chosen' );
338 477 wp_enqueue_script( 'visualizer-render' );
478 +
479 + $table_col_mapping = $this->loadCodeEditorAssets();
480 +
339 481 wp_localize_script(
340 - 'visualizer-render', 'visualizer', array(
482 + 'visualizer-render',
483 + 'visualizer',
484 + array(
341 485 'l10n' => array(
342 486 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
343 487 'loading' => esc_html__( 'Loading...', 'visualizer' ),
344 488 ),
@@ -343,25 +487,35 @@
343 487 'loading' => esc_html__( 'Loading...', 'visualizer' ),
344 488 ),
345 489 'charts' => array(
346 490 'canvas' => $data,
491 + 'id' => $this->_chart->ID,
347 492 ),
348 493 'language' => $this->get_language(),
349 494 'map_api_key' => get_option( 'visualizer-map-api-key' ),
350 - 'ajax' => array(
495 + 'ajax' => array(
351 496 'url' => admin_url( 'admin-ajax.php' ),
352 497 'nonces' => array(
353 498 'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ),
499 + 'db_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ),
354 500 ),
355 501 'actions' => array(
356 502 'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA,
503 + 'db_get_data' => Visualizer_Plugin::ACTION_FETCH_DB_DATA,
357 504 ),
358 505 ),
506 + 'db_query' => array(
507 + 'tables' => $table_col_mapping,
508 + ),
509 + 'is_pro' => VISUALIZER_PRO,
510 + 'page_type' => 'chart',
359 511 )
360 512 );
513 +
361 514 $render = new Visualizer_Render_Page_Data();
362 515 $render->chart = $this->_chart;
363 516 $render->type = $data['type'];
517 + $render->custom_css = $data['css'];
364 518 $render->sidebar = $sidebar;
365 519 if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) {
366 520 $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART
367 521 ? esc_html__( 'Save Chart', 'visualizer' )
@@ -467,9 +621,15 @@
467 621 }
468 622
469 623 if ( ! isset( $_POST['chart_data_src'] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $_POST['chart_data_src'] ) {
470 624 // delete the filters in case this chart is being uploaded from other data sources
471 - delete_post_meta( $chart_id, 'visualizer-filter-config' );
625 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG );
626 + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_FILTER_CONFIG );
627 + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_DB_QUERY );
628 +
629 + // delete "import from db" specific parameters.
630 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
631 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
472 632 }
473 633
474 634 $source = null;
475 635 $render = new Visualizer_Render_Page_Update();
@@ -480,19 +640,33 @@
480 640 }
481 641 } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
482 642 $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
483 643 } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
484 - $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' );
644 + $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '', $chart_id, $_POST );
485 645 } else {
486 646 $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
487 647 }
488 648 if ( $source ) {
489 649 if ( $source->fetch() ) {
490 - $chart->post_content = $source->getData();
650 + $content = $source->getData();
651 + $populate = true;
652 + if ( is_string( $content ) && is_array( unserialize( $content ) ) ) {
653 + $json = unserialize( $content );
654 + // if source exists, so should data. if source exists but data is blank, do not populate the chart.
655 + // if we populate the data even if it is empty, the chart will show "Table has no columns".
656 + if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) {
657 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ), 'warn', __FILE__, __LINE__ );
658 + $populate = false;
659 + }
660 + }
661 + if ( $populate ) {
662 + $chart->post_content = $content;
663 + }
491 664 wp_update_post( $chart->to_array() );
492 665 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
493 666 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
494 667 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
668 + $render->id = $chart->ID;
495 669 $render->data = json_encode( $source->getRawData() );
496 670 $render->series = json_encode( $source->getSeries() );
497 671 } else {
498 672 $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
@@ -517,9 +691,12 @@
517 691 $nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART );
518 692 $capable = current_user_can( 'edit_posts' );
519 693 if ( $nonce && $capable ) {
520 694 $chart_id = filter_input(
521 - INPUT_GET, 'chart', FILTER_VALIDATE_INT, array(
695 + INPUT_GET,
696 + 'chart',
697 + FILTER_VALIDATE_INT,
698 + array(
522 699 'options' => array(
523 700 'min_range' => 1,
524 701 ),
525 702 )
@@ -549,9 +726,10 @@
549 726 $redirect = add_query_arg(
550 727 array(
551 728 'page' => 'visualizer',
552 729 'type' => filter_input( INPUT_GET, 'type' ),
553 - ), admin_url( 'upload.php' )
730 + ),
731 + admin_url( 'upload.php' )
554 732 );
555 733 }
556 734 }
557 735 wp_redirect( $redirect );
@@ -569,9 +747,11 @@
569 747 check_ajax_referer( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION, 'security' );
570 748 $capable = current_user_can( 'edit_posts' );
571 749 if ( $capable ) {
572 750 $chart_id = isset( $_GET['chart'] ) ? filter_var(
573 - $_GET['chart'], FILTER_VALIDATE_INT, array(
751 + $_GET['chart'],
752 + FILTER_VALIDATE_INT,
753 + array(
574 754 'options' => array(
575 755 'min_range' => 1,
576 756 ),
577 757 )
@@ -602,9 +782,11 @@
602 782 unset( $data['settings']['width'], $data['settings']['height'] );
603 783 wp_enqueue_style( 'visualizer-frame' );
604 784 wp_enqueue_script( 'visualizer-render' );
605 785 wp_localize_script(
606 - 'visualizer-render', 'visualizer', array(
786 + 'visualizer-render',
787 + 'visualizer',
788 + array(
607 789 'l10n' => array(
608 790 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
609 791 'loading' => esc_html__( 'Loading...', 'visualizer' ),
610 792 ),
@@ -622,5 +804,107 @@
622 804 $this->_addAction( 'admin_head', 'renderFlattrScript' );
623 805 wp_iframe( array( $render, 'render' ) );
624 806 }
625 807
808 + /**
809 + * Returns the data for the query.
810 + *
811 + * @access public
812 + */
813 + public function getQueryData() {
814 + check_ajax_referer( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION, 'security' );
815 +
816 + $params = wp_parse_args( $_POST['params'] );
817 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
818 + $html = $source->fetch( true );
819 + $error = '';
820 + if ( empty( $html ) ) {
821 + $error = $source->get_error();
822 + wp_send_json_error( array( 'msg' => $error ) );
823 + }
824 + wp_send_json_success( array( 'table' => $html ) );
825 + }
826 +
827 + /**
828 + * Saves the query and the schedule.
829 + *
830 + * @access public
831 + */
832 + public function saveQuery() {
833 + check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION, 'security' );
834 +
835 + $chart_id = filter_input(
836 + INPUT_GET,
837 + 'chart',
838 + FILTER_VALIDATE_INT,
839 + array(
840 + 'options' => array(
841 + 'min_range' => 1,
842 + ),
843 + )
844 + );
845 +
846 + $render = new Visualizer_Render_Page_Update();
847 + if ( $chart_id ) {
848 + $params = wp_parse_args( $_POST['params'] );
849 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
850 + $source->fetch( false );
851 + $error = $source->get_error();
852 + if ( empty( $error ) ) {
853 + $hours = $_POST['refresh'];
854 + update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, stripslashes( $params['query'] ) );
855 + update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
856 + update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
857 + update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, $hours );
858 + update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
859 +
860 + $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
861 + $schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
862 + update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $schedules );
863 +
864 + wp_update_post(
865 + array(
866 + 'ID' => $chart_id,
867 + 'post_content' => $source->getData(),
868 + )
869 + );
870 + $render->data = json_encode( $source->getRawData() );
871 + $render->series = json_encode( $source->getSeries() );
872 + } else {
873 + $render->message = $error;
874 + }
875 + }
876 + $render->render();
877 + if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
878 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
879 + }
880 + }
881 +
882 +
883 + /**
884 + * Saves the filter query and the schedule.
885 + *
886 + * @access public
887 + */
888 + public function saveFilter() {
889 + check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION, 'security' );
890 +
891 + $chart_id = filter_input(
892 + INPUT_GET,
893 + 'chart',
894 + FILTER_VALIDATE_INT,
895 + array(
896 + 'options' => array(
897 + 'min_range' => 1,
898 + ),
899 + )
900 + );
901 +
902 + $hours = $_POST['refresh'];
903 +
904 + do_action( 'visualizer_save_filter', $chart_id, $hours );
905 +
906 + if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
907 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
908 + }
909 + }
626 910 }