PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.3.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.3.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 +380 -42 3.1.33.3.0 View file →
@@ -60,12 +60,159 @@
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 +
65 72 }
66 73
67 74 /**
75 + * Sets the schedule for how JSON-endpoint charts should be updated.
76 + *
77 + * @since ?
78 + *
79 + * @access public
80 + */
81 + public function setJsonSchedule() {
82 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION, 'security' );
83 +
84 + $chart_id = filter_input(
85 + INPUT_POST,
86 + 'chart',
87 + FILTER_VALIDATE_INT,
88 + array(
89 + 'options' => array(
90 + 'min_range' => 1,
91 + ),
92 + )
93 + );
94 +
95 + if ( ! $chart_id ) {
96 + wp_send_json_error();
97 + }
98 +
99 + $time = filter_input(
100 + INPUT_POST,
101 + 'time',
102 + FILTER_VALIDATE_INT,
103 + array(
104 + 'options' => array(
105 + 'min_range' => -1,
106 + ),
107 + )
108 + );
109 +
110 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE );
111 +
112 + if ( -1 < $time ) {
113 + add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time );
114 + }
115 + wp_send_json_success();
116 + }
117 +
118 + /**
119 + * Get the root elements for JSON-endpoint.
120 + *
121 + * @since ?
122 + *
123 + * @access public
124 + */
125 + public function getJsonRoots() {
126 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION, 'security' );
127 +
128 + $params = wp_parse_args( $_POST['params'] );
129 +
130 + $source = new Visualizer_Source_Json( $params );
131 +
132 + $roots = $source->fetchRoots();
133 + if ( empty( $roots ) ) {
134 + wp_send_json_error( array( 'msg' => $source->get_error() ) );
135 + }
136 +
137 + wp_send_json_success( array( 'url' => $params['url'], 'roots' => $roots ) );
138 + }
139 +
140 + /**
141 + * Get the data for the JSON-endpoint corresponding to the chosen root.
142 + *
143 + * @since ?
144 + *
145 + * @access public
146 + */
147 + public function getJsonData() {
148 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION, 'security' );
149 +
150 + $params = wp_parse_args( $_POST['params'] );
151 +
152 + $chart_id = $params['chart'];
153 +
154 + if ( empty( $chart_id ) ) {
155 + wp_die();
156 + }
157 +
158 + $source = new Visualizer_Source_Json( $params );
159 +
160 + $data = $source->parse();
161 + if ( empty( $data ) ) {
162 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
163 + }
164 +
165 + $data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false );
166 + wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) );
167 + }
168 +
169 + /**
170 + * Updates the database with the correct post parameters for JSON-endpoint charts.
171 + *
172 + * @since ?
173 + *
174 + * @access public
175 + */
176 + public function setJsonData() {
177 + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION, 'security' );
178 +
179 + $params = $_POST;
180 + $chart_id = $_GET['chart'];
181 +
182 + if ( empty( $chart_id ) ) {
183 + wp_die();
184 + }
185 +
186 + $chart = get_post( $chart_id );
187 +
188 + $source = new Visualizer_Source_Json( $params );
189 + $source->fetch();
190 +
191 + $content = $source->getData();
192 + $chart->post_content = $content;
193 + wp_update_post( $chart->to_array() );
194 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
195 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
196 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
197 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
198 + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
199 + delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING );
200 + if ( ! empty( $params['paging'] ) ) {
201 + add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] );
202 + }
203 +
204 + $render = new Visualizer_Render_Page_Update();
205 + $render->id = $chart->ID;
206 + $render->data = json_encode( $source->getRawData() );
207 + $render->series = json_encode( $source->getSeries() );
208 + $render->render();
209 +
210 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
211 + }
212 +
213 +
214 + /**
68 215 * Fetches charts from database.
69 216 *
70 217 * This method is also called from the media pop-up (classic editor: create a post and add chart from insert content).
71 218 *
@@ -94,9 +241,9 @@
94 241 // 'filter' is from the modal from the add media button.
95 242 $filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING );
96 243 }
97 244
98 - if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
245 + if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
99 246 $query_args['meta_query'] = array(
100 247 array(
101 248 'key' => Visualizer_Plugin::CF_CHART_TYPE,
102 249 'value' => $filter,
@@ -201,9 +348,9 @@
201 348 *
202 349 * @access public
203 350 */
204 351 public function deleteChart() {
205 - $is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
352 + $is_post = $_SERVER['REQUEST_METHOD'] === 'POST';
206 353 $input_method = $is_post ? INPUT_POST : INPUT_GET;
207 354 $chart_id = $success = false;
208 355 $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) );
209 356 $capable = current_user_can( 'delete_posts' );
@@ -219,9 +366,9 @@
219 366 )
220 367 );
221 368 if ( $chart_id ) {
222 369 $chart = get_post( $chart_id );
223 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
370 + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
224 371 }
225 372 }
226 373 if ( $success ) {
227 374 wp_delete_post( $chart_id, true );
@@ -232,9 +379,9 @@
232 379 'success' => $success,
233 380 )
234 381 );
235 382 }
236 - wp_redirect( wp_get_referer() );
383 + wp_redirect( remove_query_arg( 'vaction', wp_get_referer() ) );
237 384 exit;
238 385 }
239 386
240 387 /**
@@ -276,9 +423,9 @@
276 423 public function renderChartPages() {
277 424 defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
278 425 // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
279 426 $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 ) {
427 + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
281 428 $this->deleteOldCharts();
282 429 $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
283 430 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
284 431 $source->fetch();
@@ -308,16 +455,23 @@
308 455 wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
309 456 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
310 457 }
311 458
312 - $this->load_chart_type( $chart_id );
459 + $lib = $this->load_chart_type( $chart_id );
313 460
461 + // the alpha color picker (RGBA) is not supported by google.
462 + $color_picker_dep = 'wp-color-picker';
463 + if ( in_array( $lib, array( 'chartjs', 'datatables' ), true ) && ! wp_script_is( 'wp-color-picker-alpha', 'registered' ) ) {
464 + wp_register_script( 'wp-color-picker-alpha', VISUALIZER_ABSURL . 'js/lib/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), Visualizer_Plugin::VERSION );
465 + $color_picker_dep = 'wp-color-picker-alpha';
466 + }
467 +
314 468 // enqueue and register scripts and styles
315 469 wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
316 470 wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
317 471
318 472 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 );
473 + wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen', 'jquery-ui-accordion' ), Visualizer_Plugin::VERSION, true );
320 474 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
321 475 wp_register_script(
322 476 'visualizer-render',
323 477 VISUALIZER_ABSURL . 'js/render-facade.js',
@@ -328,19 +482,23 @@
328 482 wp_register_script(
329 483 'visualizer-preview',
330 484 VISUALIZER_ABSURL . 'js/preview.js',
331 485 array(
332 - 'wp-color-picker',
486 + $color_picker_dep,
333 487 'visualizer-render',
334 488 ),
335 489 Visualizer_Plugin::VERSION,
336 490 true
337 491 );
338 - // added by Ash/Upwork
339 - if ( VISUALIZER_PRO ) {
492 + wp_register_script( 'visualizer-editor-simple', VISUALIZER_ABSURL . 'js/simple-editor.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
493 +
494 + do_action( 'visualizer_add_scripts' );
495 +
496 + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
340 497 global $Visualizer_Pro;
341 498 $Visualizer_Pro->_addScriptsAndStyles();
342 499 }
500 +
343 501 // dispatch pages
344 502 $this->_chart = get_post( $chart_id );
345 503 $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer';
346 504
@@ -380,22 +538,23 @@
380 538 */
381 539 private function loadCodeEditorAssets() {
382 540 global $wp_version;
383 541
384 - if ( ! VISUALIZER_PRO ) {
542 + $wp_scripts = wp_scripts();
543 +
544 + // data tables assets.
545 + wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
546 + wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION );
547 + 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 );
548 + wp_enqueue_script( 'visualizer-datatables' );
549 + wp_enqueue_style( 'visualizer-jquery-ui' );
550 +
551 + if ( ! Visualizer_Module::is_pro() ) {
385 552 return;
386 553 }
387 554
388 555 $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping();
389 556
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 557 if ( version_compare( $wp_version, '4.9.0', '<' ) ) {
399 558 // code mirror assets.
400 559 wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
401 560 wp_register_script( 'visualizer-codemirror-placeholder', '//codemirror.net/addon/display/placeholder.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
@@ -435,10 +594,10 @@
435 594 private function _handleDataAndSettingsPage() {
436 595 if ( isset( $_POST['map_api_key'] ) ) {
437 596 update_option( 'visualizer-map-api-key', $_POST['map_api_key'] );
438 597 }
439 - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
440 - if ( $this->_chart->post_status == 'auto-draft' ) {
598 + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
599 + if ( $this->_chart->post_status === 'auto-draft' ) {
441 600 $this->_chart->post_status = 'publish';
442 601
443 602 // 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 603 // we do not want any difference in data so disable revisions temporarily.
@@ -447,8 +606,24 @@
447 606 }
448 607 // save meta data only when it is NOT being canceled.
449 608 if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) {
450 609 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST );
610 +
611 + // we will keep a parameter called 'internal_title' that will be set to the given title or, if empty, the chart ID
612 + // this will help in searching with the chart id.
613 + $settings = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
614 + $title = null;
615 + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
616 + $title = $settings['title'];
617 + if ( is_array( $title ) ) {
618 + $title = $settings['title']['text'];
619 + }
620 + }
621 + if ( empty( $title ) ) {
622 + $title = $this->_chart->ID;
623 + }
624 + $settings['internal_title'] = $title;
625 + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings );
451 626 }
452 627 $render = new Visualizer_Render_Page_Send();
453 628 $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID );
454 629 wp_iframe( array( $render, 'render' ) );
@@ -456,9 +631,9 @@
456 631 return;
457 632 }
458 633 $data = $this->_getChartArray();
459 634 $sidebar = '';
460 - $sidebar_class = 'Visualizer_Render_Sidebar_Type_' . ucfirst( $data['type'] );
635 + $sidebar_class = $this->load_chart_class_name( $this->_chart->ID );
461 636 if ( class_exists( $sidebar_class, true ) ) {
462 637 $sidebar = new $sidebar_class( $data['settings'] );
463 638 $sidebar->__series = $data['series'];
464 639 $sidebar->__data = $data['data'];
@@ -463,14 +638,14 @@
463 638 $sidebar->__series = $data['series'];
464 639 $sidebar->__data = $data['data'];
465 640 } else {
466 641 $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data );
467 - if ( $sidebar != '' ) {
642 + if ( $sidebar !== '' ) {
468 643 $sidebar->__series = $data['series'];
469 644 $sidebar->__data = $data['data'];
470 645 }
471 646 }
472 - unset( $data['settings']['width'], $data['settings']['height'] );
647 + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
473 648 wp_enqueue_style( 'wp-color-picker' );
474 649 wp_enqueue_style( 'visualizer-frame' );
475 650 wp_enqueue_script( 'visualizer-preview' );
476 651 wp_enqueue_script( 'visualizer-chosen' );
@@ -475,8 +650,25 @@
475 650 wp_enqueue_script( 'visualizer-preview' );
476 651 wp_enqueue_script( 'visualizer-chosen' );
477 652 wp_enqueue_script( 'visualizer-render' );
478 653
654 + if ( ! Visualizer_Module::is_pro() ) {
655 + wp_enqueue_script( 'visualizer-editor-simple' );
656 + wp_localize_script(
657 + 'visualizer-editor-simple',
658 + 'visualizer1',
659 + array(
660 + 'ajax' => array(
661 + 'url' => admin_url( 'admin-ajax.php' ),
662 + 'nonces' => array(
663 + ),
664 + 'actions' => array(
665 + ),
666 + ),
667 + )
668 + );
669 + }
670 +
479 671 $table_col_mapping = $this->loadCodeEditorAssets();
480 672
481 673 wp_localize_script(
482 674 'visualizer-render',
@@ -484,8 +676,9 @@
484 676 array(
485 677 'l10n' => array(
486 678 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
487 679 'loading' => esc_html__( 'Loading...', 'visualizer' ),
680 + 'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
488 681 ),
489 682 'charts' => array(
490 683 'canvas' => $data,
491 684 'id' => $this->_chart->ID,
@@ -496,19 +689,28 @@
496 689 'url' => admin_url( 'admin-ajax.php' ),
497 690 'nonces' => array(
498 691 'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ),
499 692 'db_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ),
693 + 'json_get_roots' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION ),
694 + 'json_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ),
695 + 'json_set_schedule' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ),
500 696 ),
501 697 'actions' => array(
502 698 'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA,
503 699 'db_get_data' => Visualizer_Plugin::ACTION_FETCH_DB_DATA,
700 + 'json_get_roots' => Visualizer_Plugin::ACTION_JSON_GET_ROOTS,
701 + 'json_get_data' => Visualizer_Plugin::ACTION_JSON_GET_DATA,
702 + 'json_set_schedule' => Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE,
504 703 ),
505 704 ),
506 705 'db_query' => array(
507 706 'tables' => $table_col_mapping,
508 707 ),
509 - 'is_pro' => VISUALIZER_PRO,
708 + 'is_pro' => Visualizer_Module::is_pro(),
510 709 'page_type' => 'chart',
710 + 'json_tag_separator' => Visualizer_Source_Json::TAG_SEPARATOR,
711 + 'json_tag_separator_view' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW,
712 + 'is_front' => false,
511 713 )
512 714 );
513 715
514 716 $render = new Visualizer_Render_Page_Data();
@@ -516,21 +718,25 @@
516 718 $render->type = $data['type'];
517 719 $render->custom_css = $data['css'];
518 720 $render->sidebar = $sidebar;
519 721 if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) {
520 - $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART
722 + $render->button = filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART
521 723 ? esc_html__( 'Save Chart', 'visualizer' )
522 724 : esc_html__( 'Create Chart', 'visualizer' );
523 - if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) {
725 + if ( filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) {
524 726 $render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
525 727 }
526 728 } else {
527 729 $render->button = esc_attr__( 'Insert Chart', 'visualizer' );
528 730 }
529 - if ( VISUALIZER_PRO ) {
731 +
732 + do_action( 'visualizer_enqueue_scripts_and_styles', $data );
733 +
734 + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
530 735 global $Visualizer_Pro;
531 736 $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
532 737 }
738 +
533 739 $this->_addAction( 'admin_head', 'renderFlattrScript' );
534 740 wp_iframe( array( $render, 'render' ) );
535 741 }
536 742
@@ -542,13 +748,21 @@
542 748 * @access private
543 749 */
544 750 private function _handleTypesPage() {
545 751 // process post request
546 - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) {
752 + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) {
547 753 $type = filter_input( INPUT_POST, 'type' );
548 - if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) {
754 + $library = filter_input( INPUT_POST, 'chart-library' );
755 + if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
756 + if ( empty( $library ) ) {
757 + // library cannot be empty.
758 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Chart library empty while creating the chart! Aborting...', 'error', __FILE__, __LINE__ );
759 + return;
760 + }
761 +
549 762 // save new chart type
550 763 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type );
764 + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, $library );
551 765 // if the chart has default data, update it with appropriate default data for new type
552 766 if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) {
553 767 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' );
554 768 $source->fetch();
@@ -555,8 +769,11 @@
555 769 $this->_chart->post_content = $source->getData();
556 770 wp_update_post( $this->_chart->to_array() );
557 771 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
558 772 }
773 +
774 + Visualizer_Module_Utility::set_defaults( $this->_chart );
775 +
559 776 // redirect to next tab
560 777 // changed by Ash/Upwork
561 778 wp_redirect( add_query_arg( 'tab', 'settings' ) );
562 779
@@ -564,9 +781,9 @@
564 781 }
565 782 }
566 783 $render = new Visualizer_Render_Page_Types();
567 784 $render->type = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
568 - $render->types = Visualizer_Module_Admin::_getChartTypesLocalized();
785 + $render->types = Visualizer_Module_Admin::_getChartTypesLocalized( false, false, false, 'types' );
569 786 $render->chart = $this->_chart;
570 787 wp_enqueue_style( 'visualizer-frame' );
571 788 wp_enqueue_script( 'visualizer-frame' );
572 789 wp_iframe( array( $render, 'render' ) );
@@ -582,10 +799,110 @@
582 799 */
583 800 public function renderFlattrScript() {
584 801 echo '';
585 802 }
586 - // changed by Ash/Upwork
803 +
587 804 /**
805 + * Processes the CSV that is sent in the request as a string.
806 + *
807 + * @since 3.2.0
808 + */
809 + private function handleCSVasString( $data ) {
810 + $source = null;
811 +
812 + // @issue https://github.com/Codeinwp/visualizer/issues/412
813 + if ( has_filter( 'visualizer_pro_handle_chart_data' ) ) {
814 + $source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
815 + } else {
816 + // data coming in from the text editor.
817 + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
818 + $handle = fopen( $tmpfile, 'w' );
819 + $values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) );
820 + if ( $values ) {
821 + foreach ( $values as $row ) {
822 + if ( empty( $row ) ) {
823 + continue;
824 + }
825 + $columns = explode( ',', $row );
826 + fputcsv( $handle, $columns );
827 + }
828 + }
829 + $source = new Visualizer_Source_Csv( $tmpfile );
830 + fclose( $handle );
831 + }
832 + return $source;
833 + }
834 +
835 + /**
836 + * Parses the data uploaded as an HTML table.
837 + *
838 + * @since 3.2.0
839 + *
840 + * @access private
841 + */
842 + private function handleTabularData() {
843 + $csv = array();
844 + // the datatable mentions the headers twice, so lets remove the duplicates.
845 + $headers = array_unique( array_filter( $_POST['header'] ) );
846 + $types = $_POST['type'];
847 +
848 + // capture all the indexes that correspond to excluded columns.
849 + $exclude = array();
850 + $index = 0;
851 + foreach ( $types as $type ) {
852 + if ( empty( $type ) ) {
853 + $exclude[] = $index;
854 + }
855 + $index++;
856 + }
857 +
858 + // when N headers are being renamed, the number of headers increases by N
859 + // because of the way datatable duplicates header information
860 + // so unset the headers that have been renamed.
861 + if ( count( $headers ) !== count( $types ) ) {
862 + $to = count( $headers );
863 + for ( $i = count( $types ); $i < $to; $i++ ) {
864 + unset( $headers[ $i + 1 ] );
865 + }
866 + }
867 +
868 + $columns = array();
869 + for ( $i = 0; $i < count( $headers ); $i++ ) {
870 + if ( ! isset( $_POST[ 'data' . $i ] ) ) {
871 + continue;
872 + }
873 + $columns[ $i ] = $_POST[ 'data' . $i ];
874 + }
875 +
876 + $csv[] = $headers;
877 + $csv[] = $types;
878 + for ( $j = 0; $j < count( $columns[0] ); $j++ ) {
879 + $row = array();
880 + for ( $i = 0; $i < count( $headers ); $i++ ) {
881 + $row[] = $columns[ $i ][ $j ];
882 + }
883 + $csv[] = $row;
884 + }
885 +
886 + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
887 + $handle = fopen( $tmpfile, 'w' );
888 +
889 + if ( $csv ) {
890 + $index = 0;
891 + foreach ( $csv as $row ) {
892 + // remove all the cells corresponding to the excluded headers.
893 + foreach ( $exclude as $j ) {
894 + unset( $row[ $j ] );
895 + }
896 + fputcsv( $handle, $row );
897 + }
898 + }
899 + $source = new Visualizer_Source_Csv( $tmpfile );
900 + fclose( $handle );
901 + return $source;
902 + }
903 +
904 + /**
588 905 * Parses uploaded CSV file and saves new data for the chart.
589 906 *
590 907 * @since 1.0.0
591 908 *
@@ -607,9 +924,9 @@
607 924
608 925 // check chart, if chart exists
609 926 // do not use filter_input as it does not work for phpunit test cases, use filter_var instead
610 927 $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 ) {
928 + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
612 929 if ( ! $can_die ) {
613 930 return;
614 931 }
615 932 status_header( 400 );
@@ -630,8 +947,13 @@
630 947 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
631 948 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
632 949 }
633 950
951 + // delete json related data.
952 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL );
953 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
954 + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
955 +
634 956 $source = null;
635 957 $render = new Visualizer_Render_Page_Update();
636 958 if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
637 959 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
@@ -637,14 +959,17 @@
637 959 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
638 960 if ( isset( $_POST['vz-import-time'] ) ) {
639 961 apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] );
640 962 }
963 + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
641 964 } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
642 965 $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
643 966 } 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 );
967 + $source = $this->handleCSVasString( $_POST['chart_data'] );
968 + } elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
969 + $source = $this->handleTabularData();
645 970 } else {
646 - $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
971 + $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );
647 972 }
648 973 if ( $source ) {
649 974 if ( $source->fetch() ) {
650 975 $content = $source->getData();
@@ -664,13 +989,22 @@
664 989 wp_update_post( $chart->to_array() );
665 990 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
666 991 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
667 992 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
993 +
994 + Visualizer_Module_Utility::set_defaults( $chart, null );
995 +
996 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
997 +
668 998 $render->id = $chart->ID;
669 999 $render->data = json_encode( $source->getRawData() );
670 1000 $render->series = json_encode( $source->getSeries() );
1001 + $render->settings = json_encode( $settings );
671 1002 } else {
672 - $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
1003 + $render->message = $source->get_error();
1004 + if ( empty( $render->message ) ) {
1005 + $render->message = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
1006 + }
673 1007 }
674 1008 }
675 1009 $render->render();
676 1010 if ( ! $can_die ) {
@@ -702,12 +1036,12 @@
702 1036 )
703 1037 );
704 1038 if ( $chart_id ) {
705 1039 $chart = get_post( $chart_id );
706 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
1040 + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
707 1041 }
708 1042 }
709 - $redirect = wp_get_referer();
1043 + $redirect = remove_query_arg( 'vaction', wp_get_referer() );
710 1044 if ( $success ) {
711 1045 $new_chart_id = wp_insert_post(
712 1046 array(
713 1047 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
@@ -726,10 +1060,11 @@
726 1060 $redirect = add_query_arg(
727 1061 array(
728 1062 'page' => 'visualizer',
729 1063 'type' => filter_input( INPUT_GET, 'type' ),
1064 + 'vaction' => false,
730 1065 ),
731 - admin_url( 'upload.php' )
1066 + admin_url( 'admin.php' )
732 1067 );
733 1068 }
734 1069 }
735 1070 wp_redirect( $redirect );
@@ -778,9 +1113,9 @@
778 1113 $data = $this->_getChartArray();
779 1114 $render = new Visualizer_Render_Page_Data();
780 1115 $render->chart = $this->_chart;
781 1116 $render->type = $data['type'];
782 - unset( $data['settings']['width'], $data['settings']['height'] );
1117 + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
783 1118 wp_enqueue_style( 'visualizer-frame' );
784 1119 wp_enqueue_script( 'visualizer-render' );
785 1120 wp_localize_script(
786 1121 'visualizer-render',
@@ -794,13 +1129,16 @@
794 1129 'canvas' => $data,
795 1130 ),
796 1131 )
797 1132 );
798 - // Added by Ash/Upwork
799 - if ( VISUALIZER_PRO ) {
1133 +
1134 + do_action( 'visualizer_enqueue_scripts_and_styles', $data );
1135 +
1136 + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
800 1137 global $Visualizer_Pro;
801 1138 $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
802 1139 }
1140 +
803 1141 // Added by Ash/Upwork
804 1142 $this->_addAction( 'admin_head', 'renderFlattrScript' );
805 1143 wp_iframe( array( $render, 'render' ) );
806 1144 }