PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.0
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 +613 -73 3.1.23.4.0 View file →
@@ -60,11 +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 +
70 + $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' );
71 +
72 + $this->_addFilter( 'visualizer_get_sidebar', 'getSidebar', 10, 2 );
73 +
64 74 }
65 75
66 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 + /**
67 278 * Fetches charts from database.
68 279 *
69 280 * This method is also called from the media pop-up (classic editor: create a post and add chart from insert content).
70 281 *
@@ -93,9 +304,9 @@
93 304 // 'filter' is from the modal from the add media button.
94 305 $filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING );
95 306 }
96 307
97 - if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
308 + if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
98 309 $query_args['meta_query'] = array(
99 310 array(
100 311 'key' => Visualizer_Plugin::CF_CHART_TYPE,
101 312 'value' => $filter,
@@ -162,8 +373,10 @@
162 373 $css = $arguments[0];
163 374 $settings = $arguments[1];
164 375 }
165 376
377 + $date_formats = Visualizer_Source::get_date_formats_if_exists( $series, $data );
378 +
166 379 return array(
167 380 'type' => $type,
168 381 'series' => $series,
169 382 'settings' => $settings,
@@ -169,8 +382,9 @@
169 382 'settings' => $settings,
170 383 'data' => $data,
171 384 'library' => $library,
172 385 'css' => $css,
386 + 'date_formats' => $date_formats,
173 387 );
174 388 }
175 389
176 390 /**
@@ -197,9 +411,9 @@
197 411 *
198 412 * @access public
199 413 */
200 414 public function deleteChart() {
201 - $is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
415 + $is_post = $_SERVER['REQUEST_METHOD'] === 'POST';
202 416 $input_method = $is_post ? INPUT_POST : INPUT_GET;
203 417 $chart_id = $success = false;
204 418 $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) );
205 419 $capable = current_user_can( 'delete_posts' );
@@ -215,9 +429,9 @@
215 429 )
216 430 );
217 431 if ( $chart_id ) {
218 432 $chart = get_post( $chart_id );
219 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
433 + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
220 434 }
221 435 }
222 436 if ( $success ) {
223 437 wp_delete_post( $chart_id, true );
@@ -228,13 +442,41 @@
228 442 'success' => $success,
229 443 )
230 444 );
231 445 }
232 - wp_redirect( wp_get_referer() );
446 + wp_redirect( remove_query_arg( 'vaction', wp_get_referer() ) );
233 447 exit;
234 448 }
235 449
236 450 /**
451 + * Delete charts that are still in auto-draft mode.
452 + */
453 + private function deleteOldCharts() {
454 + $query = new WP_Query(
455 + array(
456 + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
457 + 'post_status' => 'auto-draft',
458 + 'fields' => 'ids',
459 + 'update_post_meta_cache' => false,
460 + 'update_post_term_cache' => false,
461 + 'posts_per_page' => 50,
462 + 'date_query' => array(
463 + array(
464 + 'before' => 'today',
465 + ),
466 + ),
467 + )
468 + );
469 +
470 + if ( $query->have_posts() ) {
471 + $ids = array();
472 + while ( $query->have_posts() ) {
473 + wp_delete_post( $query->next_post(), true );
474 + }
475 + }
476 + }
477 +
478 + /**
237 479 * Renders appropriate page for chart builder. Creates new auto draft chart
238 480 * if no chart has been specified.
239 481 *
240 482 * @since 1.0.0
@@ -244,9 +486,10 @@
244 486 public function renderChartPages() {
245 487 defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
246 488 // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
247 489 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
248 - 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 ) {
491 + $this->deleteOldCharts();
249 492 $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
250 493 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
251 494 $source->fetch();
252 495 $chart_id = wp_insert_post(
@@ -262,8 +505,9 @@
262 505 add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type );
263 506 add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
264 507 add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
265 508 add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
509 + add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
266 510 add_post_meta(
267 511 $chart_id,
268 512 Visualizer_Plugin::CF_SETTINGS,
269 513 array(
@@ -275,16 +519,23 @@
275 519 wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
276 520 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
277 521 }
278 522
279 - $this->load_chart_type( $chart_id );
523 + $lib = $this->load_chart_type( $chart_id );
280 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 +
281 532 // enqueue and register scripts and styles
282 533 wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
283 534 wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
284 535
285 536 wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION );
286 - 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 );
287 538 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
288 539 wp_register_script(
289 540 'visualizer-render',
290 541 VISUALIZER_ABSURL . 'js/render-facade.js',
@@ -295,19 +546,23 @@
295 546 wp_register_script(
296 547 'visualizer-preview',
297 548 VISUALIZER_ABSURL . 'js/preview.js',
298 549 array(
299 - 'wp-color-picker',
550 + $color_picker_dep,
300 551 'visualizer-render',
301 552 ),
302 553 Visualizer_Plugin::VERSION,
303 554 true
304 555 );
305 - // added by Ash/Upwork
306 - 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' ) ) {
307 561 global $Visualizer_Pro;
308 562 $Visualizer_Pro->_addScriptsAndStyles();
309 563 }
564 +
310 565 // dispatch pages
311 566 $this->_chart = get_post( $chart_id );
312 567 $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer';
313 568
@@ -344,25 +599,26 @@
344 599
345 600 /**
346 601 * Load code editor assets.
347 602 */
348 - private function loadCodeEditorAssets() {
603 + private function loadCodeEditorAssets( $chart_id ) {
349 604 global $wp_version;
350 605
351 - 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() ) {
352 616 return;
353 617 }
354 618
355 - $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 );
356 620
357 - // data tables assets.
358 - wp_register_script( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
359 - wp_register_style( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css', array(), Visualizer_Plugin::VERSION );
360 - wp_register_style( 'visualizer-datatables-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION );
361 -
362 - wp_enqueue_script( 'visualizer-datatables' );
363 - wp_enqueue_style( 'visualizer-datatables-ui' );
364 -
365 621 if ( version_compare( $wp_version, '4.9.0', '<' ) ) {
366 622 // code mirror assets.
367 623 wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
368 624 wp_register_script( 'visualizer-codemirror-placeholder', '//codemirror.net/addon/display/placeholder.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
@@ -402,10 +658,10 @@
402 658 private function _handleDataAndSettingsPage() {
403 659 if ( isset( $_POST['map_api_key'] ) ) {
404 660 update_option( 'visualizer-map-api-key', $_POST['map_api_key'] );
405 661 }
406 - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
407 - 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' ) {
408 664 $this->_chart->post_status = 'publish';
409 665
410 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.
411 667 // we do not want any difference in data so disable revisions temporarily.
@@ -414,8 +670,24 @@
414 670 }
415 671 // save meta data only when it is NOT being canceled.
416 672 if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) {
417 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 );
418 690 }
419 691 $render = new Visualizer_Render_Page_Send();
420 692 $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID );
421 693 wp_iframe( array( $render, 'render' ) );
@@ -423,9 +695,9 @@
423 695 return;
424 696 }
425 697 $data = $this->_getChartArray();
426 698 $sidebar = '';
427 - $sidebar_class = 'Visualizer_Render_Sidebar_Type_' . ucfirst( $data['type'] );
699 + $sidebar_class = $this->load_chart_class_name( $this->_chart->ID );
428 700 if ( class_exists( $sidebar_class, true ) ) {
429 701 $sidebar = new $sidebar_class( $data['settings'] );
430 702 $sidebar->__series = $data['series'];
431 703 $sidebar->__data = $data['data'];
@@ -430,14 +702,14 @@
430 702 $sidebar->__series = $data['series'];
431 703 $sidebar->__data = $data['data'];
432 704 } else {
433 705 $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data );
434 - if ( $sidebar != '' ) {
706 + if ( $sidebar !== '' ) {
435 707 $sidebar->__series = $data['series'];
436 708 $sidebar->__data = $data['data'];
437 709 }
438 710 }
439 - unset( $data['settings']['width'], $data['settings']['height'] );
711 + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
440 712 wp_enqueue_style( 'wp-color-picker' );
441 713 wp_enqueue_style( 'visualizer-frame' );
442 714 wp_enqueue_script( 'visualizer-preview' );
443 715 wp_enqueue_script( 'visualizer-chosen' );
@@ -442,17 +714,36 @@
442 714 wp_enqueue_script( 'visualizer-preview' );
443 715 wp_enqueue_script( 'visualizer-chosen' );
444 716 wp_enqueue_script( 'visualizer-render' );
445 717
446 - $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 + }
447 734
735 + $table_col_mapping = $this->loadCodeEditorAssets( $this->_chart->ID );
736 +
448 737 wp_localize_script(
449 738 'visualizer-render',
450 739 'visualizer',
451 740 array(
452 741 'l10n' => array(
453 - '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' ),
454 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' ),
455 746 ),
456 747 'charts' => array(
457 748 'canvas' => $data,
458 749 'id' => $this->_chart->ID,
@@ -463,19 +754,28 @@
463 754 'url' => admin_url( 'admin-ajax.php' ),
464 755 'nonces' => array(
465 756 'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ),
466 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 ),
467 761 ),
468 762 'actions' => array(
469 763 'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA,
470 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,
471 768 ),
472 769 ),
473 770 'db_query' => array(
474 771 'tables' => $table_col_mapping,
475 772 ),
476 - 'is_pro' => VISUALIZER_PRO,
773 + 'is_pro' => Visualizer_Module::is_pro(),
477 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,
478 778 )
479 779 );
480 780
481 781 $render = new Visualizer_Render_Page_Data();
@@ -483,21 +783,25 @@
483 783 $render->type = $data['type'];
484 784 $render->custom_css = $data['css'];
485 785 $render->sidebar = $sidebar;
486 786 if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) {
487 - $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
488 788 ? esc_html__( 'Save Chart', 'visualizer' )
489 789 : esc_html__( 'Create Chart', 'visualizer' );
490 - if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) {
790 + if ( filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) {
491 791 $render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
492 792 }
493 793 } else {
494 794 $render->button = esc_attr__( 'Insert Chart', 'visualizer' );
495 795 }
496 - 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' ) ) {
497 800 global $Visualizer_Pro;
498 - $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
801 + $Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
499 802 }
803 +
500 804 $this->_addAction( 'admin_head', 'renderFlattrScript' );
501 805 wp_iframe( array( $render, 'render' ) );
502 806 }
503 807
@@ -509,13 +813,21 @@
509 813 * @access private
510 814 */
511 815 private function _handleTypesPage() {
512 816 // process post request
513 - 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' ) ) ) {
514 818 $type = filter_input( INPUT_POST, 'type' );
515 - 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 +
516 827 // save new chart type
517 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 );
518 830 // if the chart has default data, update it with appropriate default data for new type
519 831 if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) {
520 832 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' );
521 833 $source->fetch();
@@ -522,8 +834,11 @@
522 834 $this->_chart->post_content = $source->getData();
523 835 wp_update_post( $this->_chart->to_array() );
524 836 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
525 837 }
838 +
839 + Visualizer_Module_Utility::set_defaults( $this->_chart );
840 +
526 841 // redirect to next tab
527 842 // changed by Ash/Upwork
528 843 wp_redirect( add_query_arg( 'tab', 'settings' ) );
529 844
@@ -531,9 +846,9 @@
531 846 }
532 847 }
533 848 $render = new Visualizer_Render_Page_Types();
534 849 $render->type = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
535 - $render->types = Visualizer_Module_Admin::_getChartTypesLocalized();
850 + $render->types = Visualizer_Module_Admin::_getChartTypesLocalized( false, false, false, 'types' );
536 851 $render->chart = $this->_chart;
537 852 wp_enqueue_style( 'visualizer-frame' );
538 853 wp_enqueue_script( 'visualizer-frame' );
539 854 wp_iframe( array( $render, 'render' ) );
@@ -549,10 +864,119 @@
549 864 */
550 865 public function renderFlattrScript() {
551 866 echo '';
552 867 }
553 - // changed by Ash/Upwork
868 +
554 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 'excel':
902 + // data coming in from the excel editor.
903 + $source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
904 + break;
905 + }
906 + return $source;
907 + }
908 +
909 + /**
910 + * Parses the data uploaded as an HTML table.
911 + *
912 + * @since 3.2.0
913 + *
914 + * @access private
915 + */
916 + private function handleTabularData() {
917 + $csv = array();
918 + // the datatable mentions the headers twice, so lets remove the duplicates.
919 + $headers = array_unique( array_filter( $_POST['header'] ) );
920 + $types = $_POST['type'];
921 +
922 + // capture all the indexes that correspond to excluded columns.
923 + $exclude = array();
924 + $index = 0;
925 + foreach ( $types as $type ) {
926 + if ( empty( $type ) ) {
927 + $exclude[] = $index;
928 + }
929 + $index++;
930 + }
931 +
932 + // when N headers are being renamed, the number of headers increases by N
933 + // because of the way datatable duplicates header information
934 + // so unset the headers that have been renamed.
935 + if ( count( $headers ) !== count( $types ) ) {
936 + $to = count( $headers );
937 + for ( $i = count( $types ); $i < $to; $i++ ) {
938 + unset( $headers[ $i + 1 ] );
939 + }
940 + }
941 +
942 + $columns = array();
943 + for ( $i = 0; $i < count( $headers ); $i++ ) {
944 + if ( ! isset( $_POST[ 'data' . $i ] ) ) {
945 + continue;
946 + }
947 + $columns[ $i ] = $_POST[ 'data' . $i ];
948 + }
949 +
950 + $csv[] = $headers;
951 + $csv[] = $types;
952 + for ( $j = 0; $j < count( $columns[0] ); $j++ ) {
953 + $row = array();
954 + for ( $i = 0; $i < count( $headers ); $i++ ) {
955 + $row[] = sanitize_text_field( $columns[ $i ][ $j ] );
956 + }
957 + $csv[] = $row;
958 + }
959 +
960 + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
961 + $handle = fopen( $tmpfile, 'w' );
962 +
963 + if ( $csv ) {
964 + $index = 0;
965 + foreach ( $csv as $row ) {
966 + // remove all the cells corresponding to the excluded headers.
967 + foreach ( $exclude as $j ) {
968 + unset( $row[ $j ] );
969 + }
970 + fputcsv( $handle, $row );
971 + }
972 + }
973 + $source = new Visualizer_Source_Csv( $tmpfile );
974 + fclose( $handle );
975 + return $source;
976 + }
977 +
978 + /**
555 979 * Parses uploaded CSV file and saves new data for the chart.
556 980 *
557 981 * @since 1.0.0
558 982 *
@@ -574,9 +998,9 @@
574 998
575 999 // check chart, if chart exists
576 1000 // do not use filter_input as it does not work for phpunit test cases, use filter_var instead
577 1001 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
578 - if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
1002 + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
579 1003 if ( ! $can_die ) {
580 1004 return;
581 1005 }
582 1006 status_header( 400 );
@@ -582,8 +1006,10 @@
582 1006 status_header( 400 );
583 1007 exit;
584 1008 }
585 1009
1010 + 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__ );
1011 +
586 1012 if ( ! isset( $_POST['vz-import-time'] ) ) {
587 1013 apply_filters( 'visualizer_pro_remove_schedule', $chart_id );
588 1014 }
589 1015
@@ -588,15 +1014,30 @@
588 1014 }
589 1015
590 1016 if ( ! isset( $_POST['chart_data_src'] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $_POST['chart_data_src'] ) {
591 1017 // delete the filters in case this chart is being uploaded from other data sources
592 - delete_post_meta( $chart_id, 'visualizer-filter-config' );
1018 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG );
1019 + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_FILTER_CONFIG );
1020 + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_DB_QUERY );
593 1021
594 1022 // delete "import from db" specific parameters.
595 1023 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
596 1024 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
1025 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS );
597 1026 }
598 1027
1028 + // delete json related data.
1029 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL );
1030 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
1031 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
1032 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS );
1033 +
1034 + // delete last error
1035 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
1036 +
1037 + // delete editor related data.
1038 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR );
1039 +
599 1040 $source = null;
600 1041 $render = new Visualizer_Render_Page_Update();
601 1042 if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
602 1043 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
@@ -602,15 +1043,25 @@
602 1043 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
603 1044 if ( isset( $_POST['vz-import-time'] ) ) {
604 1045 apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] );
605 1046 }
1047 + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
606 1048 } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
607 1049 $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
608 1050 } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
609 - $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' );
1051 + $source = $this->handleCSVasString( $_POST['chart_data'], $_POST['editor-type'] );
1052 + update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
1053 + } elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
1054 + $source = $this->handleTabularData();
1055 + update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
610 1056 } else {
611 - $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
1057 + 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__ );
1058 + $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );
1059 + update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' ) );
612 1060 }
1061 +
1062 + 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__ );
1063 +
613 1064 if ( $source ) {
614 1065 if ( $source->fetch() ) {
615 1066 $content = $source->getData();
616 1067 $populate = true;
@@ -619,8 +1070,9 @@
619 1070 // if source exists, so should data. if source exists but data is blank, do not populate the chart.
620 1071 // if we populate the data even if it is empty, the chart will show "Table has no columns".
621 1072 if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) {
622 1073 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__ );
1074 + update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ) );
623 1075 $populate = false;
624 1076 }
625 1077 }
626 1078 if ( $populate ) {
@@ -629,14 +1081,30 @@
629 1081 wp_update_post( $chart->to_array() );
630 1082 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
631 1083 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
632 1084 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
1085 +
1086 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Updated post for chart %d', $chart_id ), 'debug', __FILE__, __LINE__ );
1087 +
1088 + Visualizer_Module_Utility::set_defaults( $chart, null );
1089 +
1090 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
1091 +
633 1092 $render->id = $chart->ID;
634 1093 $render->data = json_encode( $source->getRawData() );
635 1094 $render->series = json_encode( $source->getSeries() );
1095 + $render->settings = json_encode( $settings );
636 1096 } else {
637 - $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
1097 + $error = $source->get_error();
1098 + if ( empty( $error ) ) {
1099 + $error = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
1100 + }
1101 + $render->message = $error;
1102 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( '%s for chart %d.', $error, $chart_id ), 'error', __FILE__, __LINE__ );
1103 + update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, $error );
638 1104 }
1105 + } else {
1106 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unknown internal error for chart %d.', $chart_id ), 'error', __FILE__, __LINE__ );
639 1107 }
640 1108 $render->render();
641 1109 if ( ! $can_die ) {
642 1110 return;
@@ -652,27 +1120,18 @@
652 1120 * @access public
653 1121 */
654 1122 public function cloneChart() {
655 1123 $chart_id = $success = false;
656 - $nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART );
1124 + $nonce = isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], Visualizer_Plugin::ACTION_CLONE_CHART );
657 1125 $capable = current_user_can( 'edit_posts' );
658 1126 if ( $nonce && $capable ) {
659 - $chart_id = filter_input(
660 - INPUT_GET,
661 - 'chart',
662 - FILTER_VALIDATE_INT,
663 - array(
664 - 'options' => array(
665 - 'min_range' => 1,
666 - ),
667 - )
668 - );
1127 + $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
669 1128 if ( $chart_id ) {
670 1129 $chart = get_post( $chart_id );
671 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
1130 + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
672 1131 }
673 1132 }
674 - $redirect = wp_get_referer();
1133 + $redirect = remove_query_arg( 'vaction', wp_get_referer() );
675 1134 if ( $success ) {
676 1135 $new_chart_id = wp_insert_post(
677 1136 array(
678 1137 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
@@ -681,23 +1140,32 @@
681 1140 'post_status' => $chart->post_status,
682 1141 'post_content' => $chart->post_content,
683 1142 )
684 1143 );
685 - if ( $new_chart_id && ! is_wp_error( $new_chart_id ) ) {
686 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_TYPE, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ) );
687 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, get_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ) );
688 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SOURCE, get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
689 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SERIES, get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ) );
690 - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SETTINGS, get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) );
1144 + if ( is_wp_error( $new_chart_id ) ) {
1145 + 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__ );
1146 + } else {
1147 + $post_meta = get_post_meta( $chart_id );
1148 + foreach ( $post_meta as $key => $value ) {
1149 + if ( strpos( $key, 'visualizer-' ) !== false ) {
1150 + add_post_meta( $new_chart_id, $key, maybe_unserialize( $value[0] ) );
1151 + }
1152 + }
691 1153 $redirect = add_query_arg(
692 1154 array(
693 1155 'page' => 'visualizer',
694 1156 'type' => filter_input( INPUT_GET, 'type' ),
1157 + 'vaction' => false,
695 1158 ),
696 - admin_url( 'upload.php' )
1159 + admin_url( 'admin.php' )
697 1160 );
698 1161 }
699 1162 }
1163 +
1164 + if ( defined( 'WP_TESTS_DOMAIN' ) ) {
1165 + wp_die();
1166 + }
1167 +
700 1168 wp_redirect( $redirect );
701 1169 exit;
702 1170 }
703 1171
@@ -743,9 +1211,12 @@
743 1211 $data = $this->_getChartArray();
744 1212 $render = new Visualizer_Render_Page_Data();
745 1213 $render->chart = $this->_chart;
746 1214 $render->type = $data['type'];
747 - unset( $data['settings']['width'], $data['settings']['height'] );
1215 +
1216 + if ( $data && $data['settings'] ) {
1217 + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
1218 + }
748 1219 wp_enqueue_style( 'visualizer-frame' );
749 1220 wp_enqueue_script( 'visualizer-render' );
750 1221 wp_localize_script(
751 1222 'visualizer-render',
@@ -751,9 +1222,9 @@
751 1222 'visualizer-render',
752 1223 'visualizer',
753 1224 array(
754 1225 'l10n' => array(
755 - 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
1226 + 'invalid_source' => esc_html__( 'You have entered an invalid URL. Please provide a valid URL.', 'visualizer' ),
756 1227 'loading' => esc_html__( 'Loading...', 'visualizer' ),
757 1228 ),
758 1229 'charts' => array(
759 1230 'canvas' => $data,
@@ -759,13 +1230,16 @@
759 1230 'canvas' => $data,
760 1231 ),
761 1232 )
762 1233 );
763 - // Added by Ash/Upwork
764 - if ( VISUALIZER_PRO ) {
1234 +
1235 + do_action( 'visualizer_enqueue_scripts_and_styles', $data, $this->_chart->ID );
1236 +
1237 + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
765 1238 global $Visualizer_Pro;
766 - $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
1239 + $Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
767 1240 }
1241 +
768 1242 // Added by Ash/Upwork
769 1243 $this->_addAction( 'admin_head', 'renderFlattrScript' );
770 1244 wp_iframe( array( $render, 'render' ) );
771 1245 }
@@ -778,13 +1252,14 @@
778 1252 public function getQueryData() {
779 1253 check_ajax_referer( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION, 'security' );
780 1254
781 1255 $params = wp_parse_args( $_POST['params'] );
782 - $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1256 + $chart_id = filter_var( $params['chart_id'], FILTER_VALIDATE_INT );
1257 +
1258 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ), $chart_id, $params );
783 1259 $html = $source->fetch( true );
784 - $error = '';
785 - if ( empty( $html ) ) {
786 - $error = $source->get_error();
1260 + $error = $source->get_error();
1261 + if ( ! empty( $error ) ) {
787 1262 wp_send_json_error( array( 'msg' => $error ) );
788 1263 }
789 1264 wp_send_json_success( array( 'table' => $html ) );
790 1265 }
@@ -807,21 +1282,42 @@
807 1282 ),
808 1283 )
809 1284 );
810 1285
1286 + $hours = filter_input(
1287 + INPUT_POST,
1288 + 'refresh',
1289 + FILTER_VALIDATE_INT,
1290 + array(
1291 + 'options' => array(
1292 + 'min_range' => -1,
1293 + 'max_range' => apply_filters( 'visualizer_is_business', false ) ? PHP_INT_MAX : -1,
1294 + ),
1295 + )
1296 + );
1297 +
1298 + if ( ! is_int( $hours ) ) {
1299 + $hours = -1;
1300 + }
1301 +
811 1302 $render = new Visualizer_Render_Page_Update();
812 1303 if ( $chart_id ) {
813 1304 $params = wp_parse_args( $_POST['params'] );
814 - $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1305 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ), $chart_id, $params );
815 1306 $source->fetch( false );
816 1307 $error = $source->get_error();
817 1308 if ( empty( $error ) ) {
818 - $hours = $_POST['refresh'];
819 1309 update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, stripslashes( $params['query'] ) );
820 1310 update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
821 1311 update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
822 1312 update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, $hours );
823 1313 update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
1314 + if ( isset( $params['db_type'] ) && $params['db_type'] !== Visualizer_Plugin::WP_DB_NAME ) {
1315 + $remote_db_params = $params;
1316 + unset( $remote_db_params['query'] );
1317 + unset( $remote_db_params['chart_id'] );
1318 + update_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS, $remote_db_params );
1319 + }
824 1320
825 1321 $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
826 1322 $schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
827 1323 update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $schedules );
@@ -833,13 +1329,57 @@
833 1329 )
834 1330 );
835 1331 $render->data = json_encode( $source->getRawData() );
836 1332 $render->series = json_encode( $source->getSeries() );
1333 + $render->id = $chart_id;
837 1334 } else {
838 1335 $render->message = $error;
839 1336 }
840 1337 }
841 1338 $render->render();
1339 + if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
1340 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
1341 + }
1342 + }
1343 +
1344 +
1345 + /**
1346 + * Saves the filter query and the schedule.
1347 + *
1348 + * @access public
1349 + */
1350 + public function saveFilter() {
1351 + check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION, 'security' );
1352 +
1353 + $chart_id = filter_input(
1354 + INPUT_GET,
1355 + 'chart',
1356 + FILTER_VALIDATE_INT,
1357 + array(
1358 + 'options' => array(
1359 + 'min_range' => 1,
1360 + ),
1361 + )
1362 + );
1363 +
1364 + $hours = filter_input(
1365 + INPUT_POST,
1366 + 'refresh',
1367 + FILTER_VALIDATE_INT,
1368 + array(
1369 + 'options' => array(
1370 + 'min_range' => -1,
1371 + 'max_range' => apply_filters( 'visualizer_is_business', false ) ? PHP_INT_MAX : -1,
1372 + ),
1373 + )
1374 + );
1375 +
1376 + if ( ! $hours ) {
1377 + $hours = -1;
1378 + }
1379 +
1380 + do_action( 'visualizer_save_filter', $chart_id, $hours );
1381 +
842 1382 if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
843 1383 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
844 1384 }
845 1385 }