PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.2
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 +552 -73 3.1.33.4.2 View file →
@@ -60,12 +60,222 @@
60 60 $this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' );
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 +
65 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_ROOTS, 'getJsonRoots' );
66 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_DATA, 'getJsonData' );
67 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_DATA, 'setJsonData' );
68 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE, 'setJsonSchedule' );
69 +
64 70 $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' );
71 +
72 + $this->_addFilter( 'visualizer_get_sidebar', 'getSidebar', 10, 2 );
73 +
65 74 }
66 75
67 76 /**
77 + * Generates the HTML of the sidebar for the chart.
78 + *
79 + * @since ?
80 + *
81 + * @access public
82 + */
83 + public function getSidebar( $sidebar, $chart_id ) {
84 + $chart = get_post( $chart_id );
85 + $data = $this->_getChartArray( $chart );
86 + $sidebar = '';
87 + $sidebar_class = $this->load_chart_class_name( $chart_id );
88 + if ( class_exists( $sidebar_class, true ) ) {
89 + $sidebar = new $sidebar_class( $data['settings'] );
90 + $sidebar->__series = $data['series'];
91 + $sidebar->__data = $data['data'];
92 + } else {
93 + $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data );
94 + if ( $sidebar !== '' ) {
95 + $sidebar->__series = $data['series'];
96 + $sidebar->__data = $data['data'];
97 + }
98 + }
99 + return str_replace( "'", '"', $sidebar->__toString() );
100 + }
101 +
102 + /**
103 + * Sets the schedule for how JSON-endpoint charts should be updated.
104 + *
105 + * @since ?
106 + *
107 + * @access public
108 + */
109 + public function setJsonSchedule() {
110 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION, 'security' );
111 +
112 + $chart_id = filter_input(
113 + INPUT_POST,
114 + 'chart',
115 + FILTER_VALIDATE_INT,
116 + array(
117 + 'options' => array(
118 + 'min_range' => 1,
119 + ),
120 + )
121 + );
122 +
123 + if ( ! $chart_id ) {
124 + wp_send_json_error();
125 + }
126 +
127 + $time = filter_input(
128 + INPUT_POST,
129 + 'time',
130 + FILTER_VALIDATE_INT,
131 + array(
132 + 'options' => array(
133 + 'min_range' => -1,
134 + ),
135 + )
136 + );
137 +
138 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE );
139 +
140 + if ( -1 < $time ) {
141 + add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time );
142 + }
143 + wp_send_json_success();
144 + }
145 +
146 + /**
147 + * Get the root elements for JSON-endpoint.
148 + *
149 + * @since ?
150 + *
151 + * @access public
152 + */
153 + public function getJsonRoots() {
154 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION, 'security' );
155 +
156 + $params = wp_parse_args( $_POST['params'] );
157 +
158 + $source = new Visualizer_Source_Json( $params );
159 +
160 + $roots = $source->fetchRoots();
161 + if ( empty( $roots ) ) {
162 + wp_send_json_error( array( 'msg' => $source->get_error() ) );
163 + }
164 +
165 + wp_send_json_success( array( 'url' => $params['url'], 'roots' => $roots ) );
166 + }
167 +
168 + /**
169 + * Get the data for the JSON-endpoint corresponding to the chosen root.
170 + *
171 + * @since ?
172 + *
173 + * @access public
174 + */
175 + public function getJsonData() {
176 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION, 'security' );
177 +
178 + $params = wp_parse_args( $_POST['params'] );
179 +
180 + $chart_id = $params['chart'];
181 +
182 + if ( empty( $chart_id ) ) {
183 + wp_die();
184 + }
185 +
186 + $source = new Visualizer_Source_Json( $params );
187 + $source->fetch();
188 + $data = $source->getRawData();
189 +
190 + if ( empty( $data ) ) {
191 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
192 + }
193 +
194 + $data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false );
195 + wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) );
196 + }
197 +
198 + /**
199 + * Updates the database with the correct post parameters for JSON-endpoint charts.
200 + *
201 + * @since ?
202 + *
203 + * @access public
204 + */
205 + public function setJsonData() {
206 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION, 'security' );
207 +
208 + $params = $_POST;
209 + $chart_id = $_GET['chart'];
210 +
211 + if ( empty( $chart_id ) ) {
212 + wp_die();
213 + }
214 +
215 + $chart = get_post( $chart_id );
216 +
217 + $source = new Visualizer_Source_Json( $params );
218 + $source->fetchFromEditableTable();
219 +
220 + $content = $source->getData();
221 + $chart->post_content = $content;
222 + wp_update_post( $chart->to_array() );
223 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
224 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
225 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
226 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
227 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
228 +
229 + delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS );
230 + $headers = array( 'method' => $params['method'] );
231 + if ( ! empty( $params['auth'] ) ) {
232 + $headers['auth'] = $params['auth'];
233 + } elseif ( ! empty( $params['username'] ) && ! empty( $params['password'] ) ) {
234 + $headers['auth'] = array( 'username' => $params['username'], 'password' => $params['password'] );
235 + }
236 +
237 + add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers );
238 +
239 + delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING );
240 + if ( ! empty( $params['paging'] ) ) {
241 + add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] );
242 + }
243 +
244 + $time = filter_input(
245 + INPUT_POST,
246 + 'time',
247 + FILTER_VALIDATE_INT,
248 + array(
249 + 'options' => array(
250 + 'min_range' => -1,
251 + ),
252 + )
253 + );
254 +
255 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE );
256 +
257 + if ( -1 < $time ) {
258 + add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time );
259 + }
260 +
261 + // delete other source specific parameters.
262 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
263 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
264 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_URL );
265 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_SCHEDULE );
266 +
267 + $render = new Visualizer_Render_Page_Update();
268 + $render->id = $chart->ID;
269 + $render->data = json_encode( $source->getRawData() );
270 + $render->series = json_encode( $source->getSeries() );
271 + $render->render();
272 +
273 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
274 + }
275 +
276 +
277 + /**
68 278 * Fetches charts from database.
69 279 *
70 280 * This method is also called from the media pop-up (classic editor: create a post and add chart from insert content).
71 281 *
@@ -94,9 +304,9 @@
94 304 // 'filter' is from the modal from the add media button.
95 305 $filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING );
96 306 }
97 307
98 - if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
308 + if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
99 309 $query_args['meta_query'] = array(
100 310 array(
101 311 'key' => Visualizer_Plugin::CF_CHART_TYPE,
102 312 'value' => $filter,
@@ -201,9 +411,9 @@
201 411 *
202 412 * @access public
203 413 */
204 414 public function deleteChart() {
205 - $is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
415 + $is_post = $_SERVER['REQUEST_METHOD'] === 'POST';
206 416 $input_method = $is_post ? INPUT_POST : INPUT_GET;
207 417 $chart_id = $success = false;
208 418 $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) );
209 419 $capable = current_user_can( 'delete_posts' );
@@ -219,9 +429,9 @@
219 429 )
220 430 );
221 431 if ( $chart_id ) {
222 432 $chart = get_post( $chart_id );
223 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
433 + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
224 434 }
225 435 }
226 436 if ( $success ) {
227 437 wp_delete_post( $chart_id, true );
@@ -232,9 +442,9 @@
232 442 'success' => $success,
233 443 )
234 444 );
235 445 }
236 - wp_redirect( wp_get_referer() );
446 + wp_redirect( remove_query_arg( 'vaction', wp_get_referer() ) );
237 447 exit;
238 448 }
239 449
240 450 /**
@@ -276,9 +486,9 @@
276 486 public function renderChartPages() {
277 487 defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
278 488 // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
279 489 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
280 - if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
490 + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
281 491 $this->deleteOldCharts();
282 492 $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
283 493 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
284 494 $source->fetch();
@@ -295,8 +505,9 @@
295 505 add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type );
296 506 add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
297 507 add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
298 508 add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
509 + add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
299 510 add_post_meta(
300 511 $chart_id,
301 512 Visualizer_Plugin::CF_SETTINGS,
302 513 array(
@@ -308,16 +519,23 @@
308 519 wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
309 520 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
310 521 }
311 522
312 - $this->load_chart_type( $chart_id );
523 + $lib = $this->load_chart_type( $chart_id );
313 524
525 + // the alpha color picker (RGBA) is not supported by google.
526 + $color_picker_dep = 'wp-color-picker';
527 + if ( in_array( $lib, array( 'chartjs', 'datatables' ), true ) && ! wp_script_is( 'wp-color-picker-alpha', 'registered' ) ) {
528 + wp_register_script( 'wp-color-picker-alpha', VISUALIZER_ABSURL . 'js/lib/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), Visualizer_Plugin::VERSION );
529 + $color_picker_dep = 'wp-color-picker-alpha';
530 + }
531 +
314 532 // enqueue and register scripts and styles
315 533 wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
316 534 wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
317 535
318 536 wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION );
319 - wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION, true );
537 + wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen', 'jquery-ui-accordion' ), Visualizer_Plugin::VERSION, true );
320 538 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
321 539 wp_register_script(
322 540 'visualizer-render',
323 541 VISUALIZER_ABSURL . 'js/render-facade.js',
@@ -328,19 +546,23 @@
328 546 wp_register_script(
329 547 'visualizer-preview',
330 548 VISUALIZER_ABSURL . 'js/preview.js',
331 549 array(
332 - 'wp-color-picker',
550 + $color_picker_dep,
333 551 'visualizer-render',
334 552 ),
335 553 Visualizer_Plugin::VERSION,
336 554 true
337 555 );
338 - // added by Ash/Upwork
339 - if ( VISUALIZER_PRO ) {
556 + wp_register_script( 'visualizer-editor-simple', VISUALIZER_ABSURL . 'js/simple-editor.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
557 +
558 + do_action( 'visualizer_add_scripts' );
559 +
560 + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
340 561 global $Visualizer_Pro;
341 562 $Visualizer_Pro->_addScriptsAndStyles();
342 563 }
564 +
343 565 // dispatch pages
344 566 $this->_chart = get_post( $chart_id );
345 567 $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer';
346 568
@@ -377,25 +599,26 @@
377 599
378 600 /**
379 601 * Load code editor assets.
380 602 */
381 - private function loadCodeEditorAssets() {
603 + private function loadCodeEditorAssets( $chart_id ) {
382 604 global $wp_version;
383 605
384 - if ( ! VISUALIZER_PRO ) {
606 + $wp_scripts = wp_scripts();
607 +
608 + // data tables assets.
609 + wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
610 + wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION );
611 + wp_register_style( 'visualizer-jquery-ui', sprintf( '//ajax.googleapis.com/ajax/libs/jqueryui/%s/themes/smoothness/jquery-ui.css', $wp_scripts->registered['jquery-ui-core']->ver ), array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION );
612 + wp_enqueue_script( 'visualizer-datatables' );
613 + wp_enqueue_style( 'visualizer-jquery-ui' );
614 +
615 + if ( ! Visualizer_Module::is_pro() ) {
385 616 return;
386 617 }
387 618
388 - $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping();
619 + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( $chart_id );
389 620
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 621 if ( version_compare( $wp_version, '4.9.0', '<' ) ) {
399 622 // code mirror assets.
400 623 wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
401 624 wp_register_script( 'visualizer-codemirror-placeholder', '//codemirror.net/addon/display/placeholder.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
@@ -435,10 +658,10 @@
435 658 private function _handleDataAndSettingsPage() {
436 659 if ( isset( $_POST['map_api_key'] ) ) {
437 660 update_option( 'visualizer-map-api-key', $_POST['map_api_key'] );
438 661 }
439 - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
440 - if ( $this->_chart->post_status == 'auto-draft' ) {
662 + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
663 + if ( $this->_chart->post_status === 'auto-draft' ) {
441 664 $this->_chart->post_status = 'publish';
442 665
443 666 // 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 667 // we do not want any difference in data so disable revisions temporarily.
@@ -447,8 +670,24 @@
447 670 }
448 671 // save meta data only when it is NOT being canceled.
449 672 if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) {
450 673 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST );
674 +
675 + // we will keep a parameter called 'internal_title' that will be set to the given title or, if empty, the chart ID
676 + // this will help in searching with the chart id.
677 + $settings = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
678 + $title = null;
679 + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
680 + $title = $settings['title'];
681 + if ( is_array( $title ) ) {
682 + $title = $settings['title']['text'];
683 + }
684 + }
685 + if ( empty( $title ) ) {
686 + $title = $this->_chart->ID;
687 + }
688 + $settings['internal_title'] = $title;
689 + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings );
451 690 }
452 691 $render = new Visualizer_Render_Page_Send();
453 692 $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID );
454 693 wp_iframe( array( $render, 'render' ) );
@@ -456,9 +695,9 @@
456 695 return;
457 696 }
458 697 $data = $this->_getChartArray();
459 698 $sidebar = '';
460 - $sidebar_class = 'Visualizer_Render_Sidebar_Type_' . ucfirst( $data['type'] );
699 + $sidebar_class = $this->load_chart_class_name( $this->_chart->ID );
461 700 if ( class_exists( $sidebar_class, true ) ) {
462 701 $sidebar = new $sidebar_class( $data['settings'] );
463 702 $sidebar->__series = $data['series'];
464 703 $sidebar->__data = $data['data'];
@@ -463,14 +702,14 @@
463 702 $sidebar->__series = $data['series'];
464 703 $sidebar->__data = $data['data'];
465 704 } else {
466 705 $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data );
467 - if ( $sidebar != '' ) {
706 + if ( $sidebar !== '' ) {
468 707 $sidebar->__series = $data['series'];
469 708 $sidebar->__data = $data['data'];
470 709 }
471 710 }
472 - unset( $data['settings']['width'], $data['settings']['height'] );
711 + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
473 712 wp_enqueue_style( 'wp-color-picker' );
474 713 wp_enqueue_style( 'visualizer-frame' );
475 714 wp_enqueue_script( 'visualizer-preview' );
476 715 wp_enqueue_script( 'visualizer-chosen' );
@@ -475,17 +714,36 @@
475 714 wp_enqueue_script( 'visualizer-preview' );
476 715 wp_enqueue_script( 'visualizer-chosen' );
477 716 wp_enqueue_script( 'visualizer-render' );
478 717
479 - $table_col_mapping = $this->loadCodeEditorAssets();
718 + if ( Visualizer_Module::can_show_feature( 'simple-editor' ) ) {
719 + wp_enqueue_script( 'visualizer-editor-simple' );
720 + wp_localize_script(
721 + 'visualizer-editor-simple',
722 + 'visualizer1',
723 + array(
724 + 'ajax' => array(
725 + 'url' => admin_url( 'admin-ajax.php' ),
726 + 'nonces' => array(
727 + ),
728 + 'actions' => array(
729 + ),
730 + ),
731 + )
732 + );
733 + }
480 734
735 + $table_col_mapping = $this->loadCodeEditorAssets( $this->_chart->ID );
736 +
481 737 wp_localize_script(
482 738 'visualizer-render',
483 739 'visualizer',
484 740 array(
485 741 'l10n' => array(
486 - 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
742 + 'invalid_source' => esc_html__( 'You have entered an invalid URL. Please provide a valid URL.', 'visualizer' ),
487 743 'loading' => esc_html__( 'Loading...', 'visualizer' ),
744 + 'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
745 + 'select_columns' => esc_html__( 'Please select a few columns to include in the chart.', 'visualizer' ),
488 746 ),
489 747 'charts' => array(
490 748 'canvas' => $data,
491 749 'id' => $this->_chart->ID,
@@ -496,19 +754,28 @@
496 754 'url' => admin_url( 'admin-ajax.php' ),
497 755 'nonces' => array(
498 756 'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ),
499 757 'db_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ),
758 + 'json_get_roots' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION ),
759 + 'json_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ),
760 + 'json_set_schedule' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ),
500 761 ),
501 762 'actions' => array(
502 763 'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA,
503 764 'db_get_data' => Visualizer_Plugin::ACTION_FETCH_DB_DATA,
765 + 'json_get_roots' => Visualizer_Plugin::ACTION_JSON_GET_ROOTS,
766 + 'json_get_data' => Visualizer_Plugin::ACTION_JSON_GET_DATA,
767 + 'json_set_schedule' => Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE,
504 768 ),
505 769 ),
506 770 'db_query' => array(
507 771 'tables' => $table_col_mapping,
508 772 ),
509 - 'is_pro' => VISUALIZER_PRO,
773 + 'is_pro' => Visualizer_Module::is_pro(),
510 774 'page_type' => 'chart',
775 + 'json_tag_separator' => Visualizer_Source_Json::TAG_SEPARATOR,
776 + 'json_tag_separator_view' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW,
777 + 'is_front' => false,
511 778 )
512 779 );
513 780
514 781 $render = new Visualizer_Render_Page_Data();
@@ -516,21 +783,25 @@
516 783 $render->type = $data['type'];
517 784 $render->custom_css = $data['css'];
518 785 $render->sidebar = $sidebar;
519 786 if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) {
520 - $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART
787 + $render->button = filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART
521 788 ? esc_html__( 'Save Chart', 'visualizer' )
522 789 : esc_html__( 'Create Chart', 'visualizer' );
523 - if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) {
790 + if ( filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) {
524 791 $render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
525 792 }
526 793 } else {
527 794 $render->button = esc_attr__( 'Insert Chart', 'visualizer' );
528 795 }
529 - if ( VISUALIZER_PRO ) {
796 +
797 + do_action( 'visualizer_enqueue_scripts_and_styles', $data, $this->_chart->ID );
798 +
799 + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
530 800 global $Visualizer_Pro;
531 - $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
801 + $Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
532 802 }
803 +
533 804 $this->_addAction( 'admin_head', 'renderFlattrScript' );
534 805 wp_iframe( array( $render, 'render' ) );
535 806 }
536 807
@@ -542,13 +813,21 @@
542 813 * @access private
543 814 */
544 815 private function _handleTypesPage() {
545 816 // process post request
546 - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) {
817 + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) {
547 818 $type = filter_input( INPUT_POST, 'type' );
548 - if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) {
819 + $library = filter_input( INPUT_POST, 'chart-library' );
820 + if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
821 + if ( empty( $library ) ) {
822 + // library cannot be empty.
823 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Chart library empty while creating the chart! Aborting...', 'error', __FILE__, __LINE__ );
824 + return;
825 + }
826 +
549 827 // save new chart type
550 828 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type );
829 + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, $library );
551 830 // if the chart has default data, update it with appropriate default data for new type
552 831 if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) {
553 832 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' );
554 833 $source->fetch();
@@ -555,8 +834,11 @@
555 834 $this->_chart->post_content = $source->getData();
556 835 wp_update_post( $this->_chart->to_array() );
557 836 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
558 837 }
838 +
839 + Visualizer_Module_Utility::set_defaults( $this->_chart );
840 +
559 841 // redirect to next tab
560 842 // changed by Ash/Upwork
561 843 wp_redirect( add_query_arg( 'tab', 'settings' ) );
562 844
@@ -564,9 +846,9 @@
564 846 }
565 847 }
566 848 $render = new Visualizer_Render_Page_Types();
567 849 $render->type = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
568 - $render->types = Visualizer_Module_Admin::_getChartTypesLocalized();
850 + $render->types = Visualizer_Module_Admin::_getChartTypesLocalized( false, false, false, 'types' );
569 851 $render->chart = $this->_chart;
570 852 wp_enqueue_style( 'visualizer-frame' );
571 853 wp_enqueue_script( 'visualizer-frame' );
572 854 wp_iframe( array( $render, 'render' ) );
@@ -582,10 +864,122 @@
582 864 */
583 865 public function renderFlattrScript() {
584 866 echo '';
585 867 }
586 - // changed by Ash/Upwork
868 +
587 869 /**
870 + * Processes the CSV that is sent in the request as a string.
871 + *
872 + * @since 3.2.0
873 + */
874 + private function handleCSVasString( $data, $editor_type ) {
875 + $source = null;
876 +
877 + switch ( $editor_type ) {
878 + case 'text':
879 + // data coming in from the text editor.
880 + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
881 + $handle = fopen( $tmpfile, 'w' );
882 + $values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) );
883 + if ( $values ) {
884 + foreach ( $values as $row ) {
885 + if ( empty( $row ) ) {
886 + continue;
887 + }
888 + // don't use fpucsv here because we need to just dump the data
889 + // minus the empty rows
890 + // and if any row contains ' or ", fputcsv will mess it up
891 + // because fputcsv needs to tokenize
892 + // and let's standardize the CSV enclosure
893 + $row = str_replace( "'", VISUALIZER_CSV_ENCLOSURE, $row );
894 + fwrite( $handle, $row );
895 + fwrite( $handle, PHP_EOL );
896 + }
897 + }
898 + $source = new Visualizer_Source_Csv( $tmpfile );
899 + fclose( $handle );
900 + break;
901 + case 'table':
902 + // Import from Chart
903 + // fall-through.
904 + case 'excel':
905 + // data coming in from the excel editor.
906 + $source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
907 + break;
908 + }
909 + return $source;
910 + }
911 +
912 + /**
913 + * Parses the data uploaded as an HTML table.
914 + *
915 + * @since 3.2.0
916 + *
917 + * @access private
918 + */
919 + private function handleTabularData() {
920 + $csv = array();
921 + // the datatable mentions the headers twice, so lets remove the duplicates.
922 + $headers = array_unique( array_filter( $_POST['header'] ) );
923 + $types = $_POST['type'];
924 +
925 + // capture all the indexes that correspond to excluded columns.
926 + $exclude = array();
927 + $index = 0;
928 + foreach ( $types as $type ) {
929 + if ( empty( $type ) ) {
930 + $exclude[] = $index;
931 + }
932 + $index++;
933 + }
934 +
935 + // when N headers are being renamed, the number of headers increases by N
936 + // because of the way datatable duplicates header information
937 + // so unset the headers that have been renamed.
938 + if ( count( $headers ) !== count( $types ) ) {
939 + $to = count( $headers );
940 + for ( $i = count( $types ); $i < $to; $i++ ) {
941 + unset( $headers[ $i + 1 ] );
942 + }
943 + }
944 +
945 + $columns = array();
946 + for ( $i = 0; $i < count( $headers ); $i++ ) {
947 + if ( ! isset( $_POST[ 'data' . $i ] ) ) {
948 + continue;
949 + }
950 + $columns[ $i ] = $_POST[ 'data' . $i ];
951 + }
952 +
953 + $csv[] = $headers;
954 + $csv[] = $types;
955 + for ( $j = 0; $j < count( $columns[0] ); $j++ ) {
956 + $row = array();
957 + for ( $i = 0; $i < count( $headers ); $i++ ) {
958 + $row[] = sanitize_text_field( $columns[ $i ][ $j ] );
959 + }
960 + $csv[] = $row;
961 + }
962 +
963 + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
964 + $handle = fopen( $tmpfile, 'w' );
965 +
966 + if ( $csv ) {
967 + $index = 0;
968 + foreach ( $csv as $row ) {
969 + // remove all the cells corresponding to the excluded headers.
970 + foreach ( $exclude as $j ) {
971 + unset( $row[ $j ] );
972 + }
973 + fputcsv( $handle, $row );
974 + }
975 + }
976 + $source = new Visualizer_Source_Csv( $tmpfile );
977 + fclose( $handle );
978 + return $source;
979 + }
980 +
981 + /**
588 982 * Parses uploaded CSV file and saves new data for the chart.
589 983 *
590 984 * @since 1.0.0
591 985 *
@@ -607,9 +1001,9 @@
607 1001
608 1002 // check chart, if chart exists
609 1003 // do not use filter_input as it does not work for phpunit test cases, use filter_var instead
610 1004 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
611 - if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
1005 + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
612 1006 if ( ! $can_die ) {
613 1007 return;
614 1008 }
615 1009 status_header( 400 );
@@ -615,8 +1009,10 @@
615 1009 status_header( 400 );
616 1010 exit;
617 1011 }
618 1012
1013 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Uploading data for chart %d with POST = %s and GET = %s', $chart_id, print_r( $_POST, true ), print_r( $_GET, true ) ), 'debug', __FILE__, __LINE__ );
1014 +
619 1015 if ( ! isset( $_POST['vz-import-time'] ) ) {
620 1016 apply_filters( 'visualizer_pro_remove_schedule', $chart_id );
621 1017 }
622 1018
@@ -628,10 +1024,23 @@
628 1024
629 1025 // delete "import from db" specific parameters.
630 1026 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
631 1027 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
1028 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS );
632 1029 }
633 1030
1031 + // delete json related data.
1032 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL );
1033 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
1034 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
1035 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS );
1036 +
1037 + // delete last error
1038 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
1039 +
1040 + // delete editor related data.
1041 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR );
1042 +
634 1043 $source = null;
635 1044 $render = new Visualizer_Render_Page_Update();
636 1045 if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
637 1046 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
@@ -637,15 +1046,25 @@
637 1046 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
638 1047 if ( isset( $_POST['vz-import-time'] ) ) {
639 1048 apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] );
640 1049 }
1050 + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
641 1051 } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
642 1052 $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
643 1053 } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
644 - $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '', $chart_id, $_POST );
1054 + $source = $this->handleCSVasString( $_POST['chart_data'], $_POST['editor-type'] );
1055 + update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
1056 + } elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
1057 + $source = $this->handleTabularData();
1058 + update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
645 1059 } else {
646 - $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
1060 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'CSV file with chart data was not uploaded for chart %d.', $chart_id ), 'error', __FILE__, __LINE__ );
1061 + $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );
1062 + update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' ) );
647 1063 }
1064 +
1065 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Uploaded data for chart %d with source %s', $chart_id, print_r( $source, true ) ), 'debug', __FILE__, __LINE__ );
1066 +
648 1067 if ( $source ) {
649 1068 if ( $source->fetch() ) {
650 1069 $content = $source->getData();
651 1070 $populate = true;
@@ -654,8 +1073,9 @@
654 1073 // if source exists, so should data. if source exists but data is blank, do not populate the chart.
655 1074 // if we populate the data even if it is empty, the chart will show "Table has no columns".
656 1075 if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) {
657 1076 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__ );
1077 + update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ) );
658 1078 $populate = false;
659 1079 }
660 1080 }
661 1081 if ( $populate ) {
@@ -664,14 +1084,30 @@
664 1084 wp_update_post( $chart->to_array() );
665 1085 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
666 1086 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
667 1087 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
1088 +
1089 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Updated post for chart %d', $chart_id ), 'debug', __FILE__, __LINE__ );
1090 +
1091 + Visualizer_Module_Utility::set_defaults( $chart, null );
1092 +
1093 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
1094 +
668 1095 $render->id = $chart->ID;
669 1096 $render->data = json_encode( $source->getRawData() );
670 1097 $render->series = json_encode( $source->getSeries() );
1098 + $render->settings = json_encode( $settings );
671 1099 } else {
672 - $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
1100 + $error = $source->get_error();
1101 + if ( empty( $error ) ) {
1102 + $error = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
1103 + }
1104 + $render->message = $error;
1105 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( '%s for chart %d.', $error, $chart_id ), 'error', __FILE__, __LINE__ );
1106 + update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, $error );
673 1107 }
1108 + } else {
1109 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unknown internal error for chart %d.', $chart_id ), 'error', __FILE__, __LINE__ );
674 1110 }
675 1111 $render->render();
676 1112 if ( ! $can_die ) {
677 1113 return;
@@ -687,27 +1123,18 @@
687 1123 * @access public
688 1124 */
689 1125 public function cloneChart() {
690 1126 $chart_id = $success = false;
691 - $nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART );
1127 + $nonce = isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], Visualizer_Plugin::ACTION_CLONE_CHART );
692 1128 $capable = current_user_can( 'edit_posts' );
693 1129 if ( $nonce && $capable ) {
694 - $chart_id = filter_input(
695 - INPUT_GET,
696 - 'chart',
697 - FILTER_VALIDATE_INT,
698 - array(
699 - 'options' => array(
700 - 'min_range' => 1,
701 - ),
702 - )
703 - );
1130 + $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
704 1131 if ( $chart_id ) {
705 1132 $chart = get_post( $chart_id );
706 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
1133 + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
707 1134 }
708 1135 }
709 - $redirect = wp_get_referer();
1136 + $redirect = remove_query_arg( 'vaction', wp_get_referer() );
710 1137 if ( $success ) {
711 1138 $new_chart_id = wp_insert_post(
712 1139 array(
713 1140 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
@@ -716,23 +1143,32 @@
716 1143 'post_status' => $chart->post_status,
717 1144 'post_content' => $chart->post_content,
718 1145 )
719 1146 );
720 - if ( $new_chart_id && ! is_wp_error( $new_chart_id ) ) {
721 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_TYPE, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ) );
722 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, get_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ) );
723 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SOURCE, get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
724 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SERIES, get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ) );
725 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SETTINGS, get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) );
1147 + if ( is_wp_error( $new_chart_id ) ) {
1148 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while cloning chart %d = %s', $chart_id, print_r( $new_chart_id, true ) ), 'error', __FILE__, __LINE__ );
1149 + } else {
1150 + $post_meta = get_post_meta( $chart_id );
1151 + foreach ( $post_meta as $key => $value ) {
1152 + if ( strpos( $key, 'visualizer-' ) !== false ) {
1153 + add_post_meta( $new_chart_id, $key, maybe_unserialize( $value[0] ) );
1154 + }
1155 + }
726 1156 $redirect = add_query_arg(
727 1157 array(
728 1158 'page' => 'visualizer',
729 1159 'type' => filter_input( INPUT_GET, 'type' ),
1160 + 'vaction' => false,
730 1161 ),
731 - admin_url( 'upload.php' )
1162 + admin_url( 'admin.php' )
732 1163 );
733 1164 }
734 1165 }
1166 +
1167 + if ( defined( 'WP_TESTS_DOMAIN' ) ) {
1168 + wp_die();
1169 + }
1170 +
735 1171 wp_redirect( $redirect );
736 1172 exit;
737 1173 }
738 1174
@@ -778,9 +1214,12 @@
778 1214 $data = $this->_getChartArray();
779 1215 $render = new Visualizer_Render_Page_Data();
780 1216 $render->chart = $this->_chart;
781 1217 $render->type = $data['type'];
782 - unset( $data['settings']['width'], $data['settings']['height'] );
1218 +
1219 + if ( $data && $data['settings'] ) {
1220 + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
1221 + }
783 1222 wp_enqueue_style( 'visualizer-frame' );
784 1223 wp_enqueue_script( 'visualizer-render' );
785 1224 wp_localize_script(
786 1225 'visualizer-render',
@@ -786,9 +1225,9 @@
786 1225 'visualizer-render',
787 1226 'visualizer',
788 1227 array(
789 1228 'l10n' => array(
790 - 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
1229 + 'invalid_source' => esc_html__( 'You have entered an invalid URL. Please provide a valid URL.', 'visualizer' ),
791 1230 'loading' => esc_html__( 'Loading...', 'visualizer' ),
792 1231 ),
793 1232 'charts' => array(
794 1233 'canvas' => $data,
@@ -794,13 +1233,16 @@
794 1233 'canvas' => $data,
795 1234 ),
796 1235 )
797 1236 );
798 - // Added by Ash/Upwork
799 - if ( VISUALIZER_PRO ) {
1237 +
1238 + do_action( 'visualizer_enqueue_scripts_and_styles', $data, $this->_chart->ID );
1239 +
1240 + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
800 1241 global $Visualizer_Pro;
801 - $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
1242 + $Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
802 1243 }
1244 +
803 1245 // Added by Ash/Upwork
804 1246 $this->_addAction( 'admin_head', 'renderFlattrScript' );
805 1247 wp_iframe( array( $render, 'render' ) );
806 1248 }
@@ -813,13 +1255,14 @@
813 1255 public function getQueryData() {
814 1256 check_ajax_referer( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION, 'security' );
815 1257
816 1258 $params = wp_parse_args( $_POST['params'] );
817 - $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1259 + $chart_id = filter_var( $params['chart_id'], FILTER_VALIDATE_INT );
1260 +
1261 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ), $chart_id, $params );
818 1262 $html = $source->fetch( true );
819 - $error = '';
820 - if ( empty( $html ) ) {
821 - $error = $source->get_error();
1263 + $error = $source->get_error();
1264 + if ( ! empty( $error ) ) {
822 1265 wp_send_json_error( array( 'msg' => $error ) );
823 1266 }
824 1267 wp_send_json_success( array( 'table' => $html ) );
825 1268 }
@@ -842,21 +1285,42 @@
842 1285 ),
843 1286 )
844 1287 );
845 1288
1289 + $hours = filter_input(
1290 + INPUT_POST,
1291 + 'refresh',
1292 + FILTER_VALIDATE_INT,
1293 + array(
1294 + 'options' => array(
1295 + 'min_range' => -1,
1296 + 'max_range' => apply_filters( 'visualizer_is_business', false ) ? PHP_INT_MAX : -1,
1297 + ),
1298 + )
1299 + );
1300 +
1301 + if ( ! is_int( $hours ) ) {
1302 + $hours = -1;
1303 + }
1304 +
846 1305 $render = new Visualizer_Render_Page_Update();
847 1306 if ( $chart_id ) {
848 1307 $params = wp_parse_args( $_POST['params'] );
849 - $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1308 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ), $chart_id, $params );
850 1309 $source->fetch( false );
851 1310 $error = $source->get_error();
852 1311 if ( empty( $error ) ) {
853 - $hours = $_POST['refresh'];
854 1312 update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, stripslashes( $params['query'] ) );
855 1313 update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
856 1314 update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
857 1315 update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, $hours );
858 1316 update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
1317 + if ( isset( $params['db_type'] ) && $params['db_type'] !== Visualizer_Plugin::WP_DB_NAME ) {
1318 + $remote_db_params = $params;
1319 + unset( $remote_db_params['query'] );
1320 + unset( $remote_db_params['chart_id'] );
1321 + update_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS, $remote_db_params );
1322 + }
859 1323
860 1324 $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
861 1325 $schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
862 1326 update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $schedules );
@@ -868,8 +1332,9 @@
868 1332 )
869 1333 );
870 1334 $render->data = json_encode( $source->getRawData() );
871 1335 $render->series = json_encode( $source->getSeries() );
1336 + $render->id = $chart_id;
872 1337 } else {
873 1338 $render->message = $error;
874 1339 }
875 1340 }
@@ -898,9 +1363,23 @@
898 1363 ),
899 1364 )
900 1365 );
901 1366
902 - $hours = $_POST['refresh'];
1367 + $hours = filter_input(
1368 + INPUT_POST,
1369 + 'refresh',
1370 + FILTER_VALIDATE_INT,
1371 + array(
1372 + 'options' => array(
1373 + 'min_range' => -1,
1374 + 'max_range' => apply_filters( 'visualizer_is_business', false ) ? PHP_INT_MAX : -1,
1375 + ),
1376 + )
1377 + );
1378 +
1379 + if ( ! $hours ) {
1380 + $hours = -1;
1381 + }
903 1382
904 1383 do_action( 'visualizer_save_filter', $chart_id, $hours );
905 1384
906 1385 if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {