PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.3.4
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.3.4
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 3.10.4 All 148 releases
← All changes | classes/Visualizer/Module/Chart.php +129 -621 4.0.43.3.4 View file →
@@ -68,38 +68,11 @@
68 68 $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE, 'setJsonSchedule' );
69 69
70 70 $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' );
71 71
72 - $this->_addFilter( 'visualizer_get_sidebar', 'getSidebar', 10, 2 );
73 72 }
74 73
75 74 /**
76 - * Generates the HTML of the sidebar for the chart.
77 - *
78 - * @since ?
79 - *
80 - * @access public
81 - */
82 - public function getSidebar( $sidebar, $chart_id ) {
83 - $chart = get_post( $chart_id );
84 - $data = $this->_getChartArray( $chart );
85 - $sidebar = '';
86 - $sidebar_class = $this->load_chart_class_name( $chart_id );
87 - if ( class_exists( $sidebar_class, true ) ) {
88 - $sidebar = new $sidebar_class( $data['settings'] );
89 - $sidebar->__series = $data['series'];
90 - $sidebar->__data = $data['data'];
91 - } else {
92 - $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data );
93 - if ( $sidebar !== '' ) {
94 - $sidebar->__series = $data['series'];
95 - $sidebar->__data = $data['data'];
96 - }
97 - }
98 - return str_replace( "'", '"', $sidebar->__toString() );
99 - }
100 -
101 - /**
102 75 * Sets the schedule for how JSON-endpoint charts should be updated.
103 76 *
104 77 * @since ?
105 78 *
@@ -133,30 +106,12 @@
133 106 ),
134 107 )
135 108 );
136 109
137 - if ( Visualizer_Module::is_pro() ) {
138 - $is_woocommerce_report = filter_input(
139 - INPUT_POST,
140 - 'is_woocommerce_report',
141 - FILTER_VALIDATE_BOOLEAN
142 - );
143 -
144 - if ( $is_woocommerce_report ) {
145 - update_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
146 - } else {
147 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE );
148 - }
149 - }
150 -
151 110 delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE );
152 111
153 112 if ( -1 < $time ) {
154 113 add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time );
155 - // Update schedules.
156 - $schedules = get_option( Visualizer_Plugin::CF_JSON_SCHEDULE, array() );
157 - $schedules[ $chart_id ] = time() + $time * HOUR_IN_SECONDS;
158 - update_option( Visualizer_Plugin::CF_JSON_SCHEDULE, $schedules );
159 114 }
160 115 wp_send_json_success();
161 116 }
162 117
@@ -200,11 +155,10 @@
200 155 wp_die();
201 156 }
202 157
203 158 $source = new Visualizer_Source_Json( $params );
204 - $source->fetch();
205 - $data = $source->getRawData();
206 159
160 + $data = $source->parse();
207 161 if ( empty( $data ) ) {
208 162 wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
209 163 }
210 164
@@ -231,12 +185,11 @@
231 185
232 186 $chart = get_post( $chart_id );
233 187
234 188 $source = new Visualizer_Source_Json( $params );
235 - update_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true );
236 - $source->fetchFromEditableTable();
189 + $source->fetch();
237 190
238 - $content = $source->getData( get_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) );
191 + $content = $source->getData();
239 192 $chart->post_content = $content;
240 193 wp_update_post( $chart->to_array() );
241 194 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
242 195 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
@@ -242,62 +195,20 @@
242 195 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
243 196 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
244 197 update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
245 198 update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
246 -
247 - delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS );
248 - $headers = array( 'method' => $params['method'] );
249 - if ( ! empty( $params['auth'] ) ) {
250 - $headers['auth'] = $params['auth'];
251 - } elseif ( ! empty( $params['username'] ) && ! empty( $params['password'] ) ) {
252 - $headers['auth'] = array( 'username' => $params['username'], 'password' => $params['password'] );
253 - }
254 -
255 - add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers );
256 -
257 199 delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING );
258 200 if ( ! empty( $params['paging'] ) ) {
259 201 add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] );
260 202 }
261 203
262 - if ( Visualizer_Module::is_pro() ) {
263 - if ( ! empty( $params['vz_woo_source'] ) ) {
264 - update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_WOOCOMMERCE_SOURCE, $params['vz_woo_source'] );
265 - } else {
266 - delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_WOOCOMMERCE_SOURCE );
267 - }
268 - }
269 -
270 - $time = filter_input(
271 - INPUT_POST,
272 - 'time',
273 - FILTER_VALIDATE_INT,
274 - array(
275 - 'options' => array(
276 - 'min_range' => -1,
277 - ),
278 - )
279 - );
280 -
281 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE );
282 -
283 - if ( -1 < $time ) {
284 - add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time );
285 - }
286 -
287 - // delete other source specific parameters.
288 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
289 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
290 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_URL );
291 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_SCHEDULE );
292 -
293 204 $render = new Visualizer_Render_Page_Update();
294 205 $render->id = $chart->ID;
295 - $render->data = json_encode( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) );
206 + $render->data = json_encode( $source->getRawData() );
296 207 $render->series = json_encode( $source->getSeries() );
297 208 $render->render();
298 209
299 - ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
210 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
300 211 }
301 212
302 213
303 214 /**
@@ -373,19 +284,19 @@
373 284 * @since 1.0.0
374 285 *
375 286 * @access private
376 287 *
377 - * @param WP_Post|null $chart The chart object.
288 + * @param WP_Post $chart The chart object.
378 289 *
379 290 * @return array The array of chart data.
380 291 */
381 - private function _getChartArray( $chart = null ) {
292 + private function _getChartArray( WP_Post $chart = null ) {
382 293 if ( is_null( $chart ) ) {
383 294 $chart = $this->_chart;
384 295 }
385 296 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
386 297 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
387 - $data = self::get_chart_data( $chart, $type );
298 + $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
388 299 $library = $this->load_chart_type( $chart->ID );
389 300
390 301 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
391 302 $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
@@ -401,13 +312,8 @@
401 312 }
402 313
403 314 $date_formats = Visualizer_Source::get_date_formats_if_exists( $series, $data );
404 315
405 - $code = '';
406 - if ( 'd3' === $library ) {
407 - $code = get_post_meta( $chart->ID, Visualizer_Module_AIBuilder::CF_D3_CODE, true );
408 - }
409 -
410 316 return array(
411 317 'type' => $type,
412 318 'series' => $series,
413 319 'settings' => $settings,
@@ -412,9 +318,8 @@
412 318 'series' => $series,
413 319 'settings' => $settings,
414 320 'data' => $data,
415 321 'library' => $library,
416 - 'code' => $code,
417 322 'css' => $css,
418 323 'date_formats' => $date_formats,
419 324 );
420 325 }
@@ -431,9 +336,9 @@
431 336 public static function _sendResponse( $results ) {
432 337 header( 'Content-type: application/json' );
433 338 nocache_headers();
434 339 echo json_encode( $results );
435 - ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
340 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
436 341 }
437 342
438 343 /**
439 344 * Deletes a chart from database.
@@ -465,22 +370,9 @@
465 370 $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
466 371 }
467 372 }
468 373 if ( $success ) {
469 - global $sitepress;
470 - if ( Visualizer_Module::is_pro() && ( function_exists( 'icl_get_languages' ) && $sitepress instanceof \SitePress ) ) {
471 - $trid = $sitepress->get_element_trid( $chart_id, 'post_' . Visualizer_Plugin::CPT_VISUALIZER );
472 - $translations = $sitepress->get_element_translations( $trid );
473 - if ( ! empty( $translations ) ) {
474 - foreach ( $translations as $translated_post ) {
475 - wp_delete_post( $translated_post->element_id, true );
476 - }
477 - } else {
478 - wp_delete_post( $chart_id, true );
479 - }
480 - } else {
481 - wp_delete_post( $chart_id, true );
482 - }
374 + wp_delete_post( $chart_id, true );
483 375 }
484 376 if ( $is_post ) {
485 377 self::_sendResponse(
486 378 array(
@@ -528,108 +420,44 @@
528 420 *
529 421 * @access public
530 422 */
531 423 public function renderChartPages() {
532 - if ( ! current_user_can( 'edit_posts' ) ) {
533 - wp_die( __( 'You do not have permission to access this page.', 'visualizer' ) );
534 - }
535 -
536 424 defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
537 - if ( ! defined( 'ET_BUILDER_PRODUCT_VERSION' ) && function_exists( 'et_get_theme_version' ) ) {
538 - define( 'ET_BUILDER_PRODUCT_VERSION', et_get_theme_version() );
539 - }
540 - // Set current screen for the render chart.
541 - set_current_screen( 'visualizer_render_chart' );
542 425 // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
543 426 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
544 - if ( ! empty( $_POST ) ) {
545 - $_POST = map_deep( $_POST, 'wp_strip_all_tags' );
546 - }
547 - $chart = $chart_id ? get_post( $chart_id ) : null;
548 - if ( ! $chart_id || ! $chart || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
549 - if ( empty( $_GET['lang'] ) || empty( $_GET['parent_chart_id'] ) ) {
550 - $this->deleteOldCharts();
551 - $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
552 - $chart_status = Visualizer_Module_Admin::checkChartStatus( $default_type );
553 - if ( ! $chart_status ) {
554 - $default_type = 'line';
555 - }
556 - $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
557 - $source->fetch();
558 - $chart_id = wp_insert_post(
427 + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
428 + $this->deleteOldCharts();
429 + $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
430 + $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
431 + $source->fetch();
432 + $chart_id = wp_insert_post(
433 + array(
434 + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
435 + 'post_title' => 'Visualization',
436 + 'post_author' => get_current_user_id(),
437 + 'post_status' => 'auto-draft',
438 + 'post_content' => $source->getData(),
439 + )
440 + );
441 + if ( $chart_id && ! is_wp_error( $chart_id ) ) {
442 + add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type );
443 + add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
444 + add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
445 + add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
446 + add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
447 + add_post_meta(
448 + $chart_id,
449 + Visualizer_Plugin::CF_SETTINGS,
559 450 array(
560 - 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
561 - 'post_title' => 'Visualization',
562 - 'post_author' => get_current_user_id(),
563 - 'post_status' => 'auto-draft',
564 - 'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ),
451 + 'focusTarget' => 'datum',
565 452 )
566 453 );
567 - if ( $chart_id && ! is_wp_error( $chart_id ) ) {
568 - add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type );
569 - add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
570 - add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
571 - add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
572 - add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
573 - add_post_meta(
574 - $chart_id,
575 - Visualizer_Plugin::CF_SETTINGS,
576 - array(
577 - 'focusTarget' => 'datum',
578 - )
579 - );
580 -
581 - do_action( 'visualizer_pro_new_chart_defaults', $chart_id );
582 - }
583 - } else {
584 - $parent_chart_id = filter_var( $_GET['parent_chart_id'], FILTER_VALIDATE_INT );
585 - $success = false;
586 - if ( $parent_chart_id ) {
587 - $parent_chart = get_post( $parent_chart_id );
588 - $success = $parent_chart && $parent_chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
589 - }
590 - if ( $success ) {
591 - $new_chart_id = wp_insert_post(
592 - array(
593 - 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
594 - 'post_title' => 'Visualization',
595 - 'post_author' => get_current_user_id(),
596 - 'post_status' => $parent_chart->post_status,
597 - 'post_content' => $parent_chart->post_content,
598 - )
599 - );
600 -
601 - if ( is_wp_error( $new_chart_id ) ) {
602 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while cloning chart %d = %s', $parent_chart_id, print_r( $new_chart_id, true ) ), 'error', __FILE__, __LINE__ );
603 - } else {
604 - $post_meta = get_post_meta( $parent_chart_id );
605 - $chart_id = $new_chart_id;
606 - foreach ( $post_meta as $key => $value ) {
607 - if ( strpos( $key, 'visualizer-' ) !== false ) {
608 - add_post_meta( $new_chart_id, $key, maybe_unserialize( $value[0] ) );
609 - }
610 - }
611 - }
612 - }
613 454 do_action( 'visualizer_pro_new_chart_defaults', $chart_id );
614 455 }
615 - wp_redirect( esc_url_raw( add_query_arg( 'chart', (int) $chart_id ) ) );
616 -
617 - if ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) {
618 - wp_die();
619 - }
620 - exit();
456 + wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
457 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
621 458 }
622 459
623 - $_POST['save_chart_image'] = isset( $_POST['save_chart_image'] ) && 'yes' === $_POST['save_chart_image'] ? true : false;
624 - $_POST['lazy_load_chart'] = isset( $_POST['lazy_load_chart'] ) && 'yes' === $_POST['lazy_load_chart'] ? true : false;
625 -
626 - if ( isset( $_POST['chart-img'] ) && ! empty( $_POST['chart-img'] ) ) {
627 - $attachment_id = $this->save_chart_image( $_POST['chart-img'], $chart_id, $_POST['save_chart_image'] );
628 - if ( $attachment_id ) {
629 - update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id );
630 - }
631 - }
632 460 $lib = $this->load_chart_type( $chart_id );
633 461
634 462 // the alpha color picker (RGBA) is not supported by google.
635 463 $color_picker_dep = 'wp-color-picker';
@@ -642,9 +470,9 @@
642 470 wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
643 471 wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
644 472
645 473 wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION );
646 - wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen', 'jquery-ui-accordion', 'jquery-ui-tabs' ), Visualizer_Plugin::VERSION, true );
474 + wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen', 'jquery-ui-accordion' ), Visualizer_Plugin::VERSION, true );
647 475 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
648 476 wp_register_script(
649 477 'visualizer-render',
650 478 VISUALIZER_ABSURL . 'js/render-facade.js',
@@ -683,9 +511,8 @@
683 511 if ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) {
684 512 // if the cancel button is clicked.
685 513 $this->undoRevisions( $chart_id, true );
686 514 } elseif ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) {
687 - $this->handlePermissions();
688 515 // if the save button is clicked.
689 516 $this->undoRevisions( $chart_id, false );
690 517 } else {
691 518 // if the edit button is clicked.
@@ -691,16 +518,8 @@
691 518 // if the edit button is clicked.
692 519 $this->_chart = $this->handleExistingRevisions( $chart_id, $this->_chart );
693 520 }
694 521
695 - // Clear existing chart cache.
696 - if ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) {
697 - $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
698 - if ( get_transient( $cache_key ) ) {
699 - delete_transient( $cache_key );
700 - }
701 - }
702 -
703 522 switch ( $tab ) {
704 523 case 'settings':
705 524 $this->_handleDataAndSettingsPage();
706 525 break;
@@ -708,18 +527,18 @@
708 527 case 'visualizer': // fall through.
709 528 $this->_handleTypesPage();
710 529 break;
711 530 default:
712 - // this should never happen.
531 + do_action( 'visualizer_pro_handle_tab', $tab, $this->_chart );
713 532 break;
714 533 }
715 - ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
534 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
716 535 }
717 536
718 537 /**
719 538 * Load code editor assets.
720 539 */
721 - private function loadCodeEditorAssets( $chart_id ) {
540 + private function loadCodeEditorAssets() {
722 541 global $wp_version;
723 542
724 543 $wp_scripts = wp_scripts();
725 544
@@ -725,9 +544,9 @@
725 544
726 545 // data tables assets.
727 546 wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
728 547 wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION );
729 - wp_register_style( 'visualizer-jquery-ui', sprintf( '//code.jquery.com/ui/%s/themes/smoothness/jquery-ui.css', $wp_scripts->registered['jquery-ui-core']->ver ), array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION );
548 + 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 );
730 549 wp_enqueue_script( 'visualizer-datatables' );
731 550 wp_enqueue_style( 'visualizer-jquery-ui' );
732 551
733 552 if ( ! Visualizer_Module::is_pro() ) {
@@ -733,9 +552,9 @@
733 552 if ( ! Visualizer_Module::is_pro() ) {
734 553 return;
735 554 }
736 555
737 - $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( $chart_id );
556 + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping();
738 557
739 558 if ( version_compare( $wp_version, '4.9.0', '<' ) ) {
740 559 // code mirror assets.
741 560 wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
@@ -743,9 +562,9 @@
743 562 wp_register_script( 'visualizer-codemirror-matchbrackets', '//codemirror.net/addon/edit/matchbrackets.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
744 563 wp_register_script( 'visualizer-codemirror-closebrackets', '//codemirror.net/addon/edit/closebrackets.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
745 564 wp_register_script( 'visualizer-codemirror-sql', '//codemirror.net/mode/sql/sql.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
746 565 wp_register_script( 'visualizer-codemirror-sql-hint', '//codemirror.net/addon/hint/sql-hint.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
747 - wp_register_script( 'visualizer-codemirror-hint', '//codemirror.net/addon/hint/show-hint.js', array( 'visualizer-codemirror-sql', 'visualizer-codemirror-sql-hint', 'visualizer-codemirror-placeholder', 'visualizer-codemirror-matchbrackets', 'visualizer-codemirror-closebrackets' ), Visualizer_Plugin::VERSION );
566 + wp_register_script( 'visualizer-codemirror-hint', '//codemirror.net/addon/hint/show-hint.js', array( 'visualizer-codemirror-sql', 'visualizer-codemirror-sql-hint', 'visualizer-codemirror-placeholder', 'visualizer-codemirror-matchbrackets', 'visualizer-codemirror-closebrackets' ), Visualizer_Plugin::VERSION );
748 567 wp_register_style( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.css', array(), Visualizer_Plugin::VERSION );
749 568 wp_register_style( 'visualizer-codemirror-hint', '//codemirror.net/addon/hint/show-hint.css', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION );
750 569
751 570 wp_enqueue_script( 'visualizer-codemirror-hint' );
@@ -759,9 +578,9 @@
759 578 'lineWrapping' => true,
760 579 'dragDrop' => false,
761 580 'matchBrackets' => true,
762 581 'autoCloseBrackets' => true,
763 - 'extraKeys' => array( 'Shift-Space' => 'autocomplete' ),
582 + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ),
764 583 'hintOptions' => array( 'tables' => $table_col_mapping ),
765 584 ),
766 585 )
767 586 );
@@ -770,25 +589,8 @@
770 589 return $table_col_mapping;
771 590 }
772 591
773 592 /**
774 - * Handle permissions from the new conslidated settings sidebar.
775 - */
776 - private function handlePermissions() {
777 - if ( ! Visualizer_Module::is_pro() ) {
778 - return;
779 - }
780 -
781 - // we will not support old free and new pro.
782 - // handling new free and old pro.
783 - if ( has_action( 'visualizer_pro_handle_tab' ) ) {
784 - do_action( 'visualizer_pro_handle_tab', 'permissions', $this->_chart );
785 - } else {
786 - do_action( 'visualizer_handle_permissions', $this->_chart );
787 - }
788 - }
789 -
790 - /**
791 593 * Handle data and settings page
792 594 */
793 595 private function _handleDataAndSettingsPage() {
794 596 if ( isset( $_POST['map_api_key'] ) ) {
@@ -793,30 +595,20 @@
793 595 private function _handleDataAndSettingsPage() {
794 596 if ( isset( $_POST['map_api_key'] ) ) {
795 597 update_option( 'visualizer-map-api-key', $_POST['map_api_key'] );
796 598 }
797 -
798 599 if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
799 - $is_canceled = isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] );
800 - $is_newly_created = $this->_chart->post_status === 'auto-draft';
801 -
802 - if ( $is_newly_created && ! $is_canceled ) {
600 + if ( $this->_chart->post_status === 'auto-draft' ) {
803 601 $this->_chart->post_status = 'publish';
804 602
805 603 // 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.
806 604 // we do not want any difference in data so disable revisions temporarily.
807 - $this->disableRevisionsTemporarily();
808 -
605 + add_filter( 'wp_revisions_to_keep', '__return_false' );
809 606 wp_update_post( $this->_chart->to_array() );
810 607 }
811 608 // save meta data only when it is NOT being canceled.
812 - if ( ! $is_canceled ) {
813 - $post_settings = $_POST;
814 - $existing = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
815 - if ( isset( $existing['colors'] ) && is_array( $existing['colors'] ) && ! isset( $post_settings['colors'] ) ) {
816 - $post_settings['colors'] = $existing['colors'];
817 - }
818 - update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $post_settings );
609 + if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) {
610 + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST );
819 611
820 612 // we will keep a parameter called 'internal_title' that will be set to the given title or, if empty, the chart ID
821 613 // this will help in searching with the chart id.
822 614 $settings = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
@@ -830,19 +622,14 @@
830 622 if ( empty( $title ) ) {
831 623 $title = $this->_chart->ID;
832 624 }
833 625 $settings['internal_title'] = $title;
834 - $settings_label = isset( $settings['pieResidueSliceLabel'] ) ? $settings['pieResidueSliceLabel'] : '';
835 - if ( empty( $settings_label ) ) {
836 - $settings['pieResidueSliceLabel'] = esc_html__( 'Other', 'visualizer' );
837 - } else {
838 - $settings['pieResidueSliceLabel'] = $settings_label;
839 - }
840 626 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings );
841 627 }
842 628 $render = new Visualizer_Render_Page_Send();
843 629 $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID );
844 630 wp_iframe( array( $render, 'render' ) );
631 +
845 632 return;
846 633 }
847 634 $data = $this->_getChartArray();
848 635 $sidebar = '';
@@ -863,11 +650,10 @@
863 650 wp_enqueue_style( 'visualizer-frame' );
864 651 wp_enqueue_script( 'visualizer-preview' );
865 652 wp_enqueue_script( 'visualizer-chosen' );
866 653 wp_enqueue_script( 'visualizer-render' );
867 - wp_enqueue_code_editor( array( 'type' => 'application/json' ) );
868 654
869 - if ( Visualizer_Module::can_show_feature( 'simple-editor' ) ) {
655 + if ( ! Visualizer_Module::is_pro() ) {
870 656 wp_enqueue_script( 'visualizer-editor-simple' );
871 657 wp_localize_script(
872 658 'visualizer-editor-simple',
873 659 'visualizer1',
@@ -873,16 +659,18 @@
873 659 'visualizer1',
874 660 array(
875 661 'ajax' => array(
876 662 'url' => admin_url( 'admin-ajax.php' ),
877 - 'nonces' => array(),
878 - 'actions' => array(),
663 + 'nonces' => array(
664 + ),
665 + 'actions' => array(
666 + ),
879 667 ),
880 668 )
881 669 );
882 670 }
883 671
884 - $table_col_mapping = $this->loadCodeEditorAssets( $this->_chart->ID );
672 + $table_col_mapping = $this->loadCodeEditorAssets();
885 673
886 674 wp_localize_script(
887 675 'visualizer-render',
888 676 'visualizer',
@@ -887,15 +675,11 @@
887 675 'visualizer-render',
888 676 'visualizer',
889 677 array(
890 678 'l10n' => array(
891 - 'invalid_source' => esc_html__( 'The URL you entered is invalid. Please enter a valid URL.', 'visualizer' ),
892 - 'loading' => esc_html__( 'Loading...', 'visualizer' ),
893 - 'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
894 - 'select_columns' => esc_html__( 'Please select a few columns to include in the chart.', 'visualizer' ),
895 - 'save_settings' => __( 'You have modified the chart\'s settings. To modify the source/data again, you must save this chart and reopen it for editing. If you continue without saving the chart, you may lose your changes.', 'visualizer' ),
896 - 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
897 - 'invalid_format' => esc_html__( 'This format pattern is not supported in the series settings field. Use the Manual Configuration option instead.', 'visualizer' ),
679 + 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
680 + 'loading' => esc_html__( 'Loading...', 'visualizer' ),
681 + 'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
898 682 ),
899 683 'charts' => array(
900 684 'canvas' => $data,
901 685 'id' => $this->_chart->ID,
@@ -926,9 +710,8 @@
926 710 'page_type' => 'chart',
927 711 'json_tag_separator' => Visualizer_Source_Json::TAG_SEPARATOR,
928 712 'json_tag_separator_view' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW,
929 713 'is_front' => false,
930 - 'rest_base' => get_rest_url( null, 'wc/v3/reports/' ),
931 714 )
932 715 );
933 716
934 717 $render = new Visualizer_Render_Page_Data();
@@ -939,19 +722,20 @@
939 722 if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) {
940 723 $render->button = filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART
941 724 ? esc_html__( 'Save Chart', 'visualizer' )
942 725 : esc_html__( 'Create Chart', 'visualizer' );
943 -
944 - $render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
726 + if ( filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) {
727 + $render->cancel_button = esc_html__( 'Cancel', 'visualizer' );
728 + }
945 729 } else {
946 730 $render->button = esc_attr__( 'Insert Chart', 'visualizer' );
947 731 }
948 732
949 - do_action( 'visualizer_enqueue_scripts_and_styles', $data, $this->_chart->ID );
733 + do_action( 'visualizer_enqueue_scripts_and_styles', $data );
950 734
951 735 if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
952 736 global $Visualizer_Pro;
953 - $Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
737 + $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
954 738 }
955 739
956 740 $this->_addAction( 'admin_head', 'renderFlattrScript' );
957 741 wp_iframe( array( $render, 'render' ) );
@@ -965,12 +749,12 @@
965 749 * @access private
966 750 */
967 751 private function _handleTypesPage() {
968 752 // process post request
969 - if ( $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ), 'visualizer-upload-data' ) ) {
753 + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) {
970 754 $type = filter_input( INPUT_POST, 'type' );
971 755 $library = filter_input( INPUT_POST, 'chart-library' );
972 - if ( Visualizer_Module_Admin::checkChartStatus( $type ) ) {
756 + if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
973 757 if ( empty( $library ) ) {
974 758 // library cannot be empty.
975 759 do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Chart library empty while creating the chart! Aborting...', 'error', __FILE__, __LINE__ );
976 760 return;
@@ -982,9 +766,9 @@
982 766 // if the chart has default data, update it with appropriate default data for new type
983 767 if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) {
984 768 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' );
985 769 $source->fetch();
986 - $this->_chart->post_content = $source->getData( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) );
770 + $this->_chart->post_content = $source->getData();
987 771 wp_update_post( $this->_chart->to_array() );
988 772 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
989 773 }
990 774
@@ -991,9 +775,9 @@
991 775 Visualizer_Module_Utility::set_defaults( $this->_chart );
992 776
993 777 // redirect to next tab
994 778 // changed by Ash/Upwork
995 - wp_redirect( esc_url_raw( add_query_arg( 'tab', 'settings' ) ) );
779 + wp_redirect( add_query_arg( 'tab', 'settings' ) );
996 780
997 781 return;
998 782 }
999 783 }
@@ -1022,123 +806,30 @@
1022 806 * Processes the CSV that is sent in the request as a string.
1023 807 *
1024 808 * @since 3.2.0
1025 809 */
1026 - /**
1027 - * Determines whether a remote URL serves an XLSX file.
1028 - *
1029 - * Used as a fallback when the URL path has no recognisable file extension
1030 - * (e.g. SharePoint, signed S3 URLs, or "download?id=…" endpoints).
1031 - *
1032 - * Uses wp_safe_remote_get() to block requests to private/loopback addresses,
1033 - * and streams the response to a temp file so no body data is held in memory
1034 - * regardless of whether the server honours the Range header.
1035 - *
1036 - * The check relies on the ZIP magic number (PK\x03\x04) that every XLSX
1037 - * file begins with, making it immune to misleading Content-Type headers
1038 - * such as application/octet-stream. Content-Type is used as a last-resort
1039 - * fallback only when the temp file is empty (e.g. a HEAD-only server).
1040 - *
1041 - * @access private
1042 - * @param string $url The remote URL to probe.
1043 - * @return bool TRUE if the file appears to be XLSX, FALSE otherwise.
1044 - */
1045 - private static function _url_is_xlsx( $url ) {
1046 - $tmpfile = wp_tempnam( 'visualizer_xlsx_probe' );
1047 - if ( ! $tmpfile ) {
1048 - return false;
1049 - }
1050 -
1051 - $response = wp_safe_remote_get(
1052 - $url,
1053 - array(
1054 - 'timeout' => 10,
1055 - 'user-agent' => 'WordPress/' . get_bloginfo( 'version' ),
1056 - 'headers' => array( 'Range' => 'bytes=0-3' ),
1057 - 'stream' => true,
1058 - 'filename' => $tmpfile,
1059 - )
1060 - );
1061 -
1062 - if ( is_wp_error( $response ) ) {
1063 - @unlink( $tmpfile ); // phpcs:ignore WordPress.PHP.NoSilencedErrors
1064 - return false;
1065 - }
1066 -
1067 - $magic = '';
1068 - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
1069 - $fh = @fopen( $tmpfile, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors
1070 - if ( $fh ) {
1071 - $magic = fread( $fh, 4 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread
1072 - fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
1073 - }
1074 - @unlink( $tmpfile ); // phpcs:ignore WordPress.PHP.NoSilencedErrors
1075 -
1076 - if ( strlen( $magic ) >= 4 ) {
1077 - // XLSX (and all ZIP-based Office formats) start with PK\x03\x04.
1078 - return $magic === "PK\x03\x04";
1079 - }
1080 -
1081 - // Last resort: server returned an empty body (e.g. ignored Range and
1082 - // returned only headers). Check Content-Type from the same response.
1083 - // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
1084 - return false !== strpos(
1085 - wp_remote_retrieve_header( $response, 'content-type' ),
1086 - 'spreadsheetml'
1087 - );
1088 - }
1089 -
1090 - /**
1091 - * Parses a raw CSV string or editor payload and returns a source object.
1092 - *
1093 - * @access private
1094 - * @param string $data The raw CSV data string.
1095 - * @param string $editor_type The editor type ('text' or 'tabular').
1096 - * @return Visualizer_Source|null The populated source object, or null on failure.
1097 - */
1098 - private function handleCSVasString( $data, $editor_type ) {
810 + private function handleCSVasString( $data ) {
1099 811 $source = null;
1100 812
1101 - switch ( $editor_type ) {
1102 - case 'text':
1103 - // data coming in from the text editor.
1104 - $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
1105 - $handle = fopen( $tmpfile, 'w' );
1106 - $values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) );
1107 - if ( $values ) {
1108 - foreach ( $values as $row ) {
1109 - if ( empty( $row ) ) {
1110 - continue;
1111 - }
1112 - $row = explode( ',', $row );
1113 - $row = array_map(
1114 - function ( $r ) {
1115 - return '' === $r ? ' ' : $r;
1116 - },
1117 - $row
1118 - );
1119 - $row = implode( ',', $row );
1120 - // don't use fpucsv here because we need to just dump the data
1121 - // minus the empty rows
1122 - // because fputcsv needs to tokenize
1123 - // we can standardize the CSV enclosure here and replace all ' with "
1124 - // we can assume that if there are an even number of ' they should be changed to "
1125 - // but that will screw up words like fo'c'sle
1126 - // so here let's just assume ' will NOT be used for enclosures
1127 - fwrite( $handle, $row );
1128 - fwrite( $handle, PHP_EOL );
813 + // @issue https://github.com/Codeinwp/visualizer/issues/412
814 + if ( has_filter( 'visualizer_pro_handle_chart_data' ) ) {
815 + $source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
816 + } else {
817 + // data coming in from the text editor.
818 + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
819 + $handle = fopen( $tmpfile, 'w' );
820 + $values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) );
821 + if ( $values ) {
822 + foreach ( $values as $row ) {
823 + if ( empty( $row ) ) {
824 + continue;
1129 825 }
826 + $columns = explode( ',', $row );
827 + fputcsv( $handle, $columns );
1130 828 }
1131 - $source = new Visualizer_Source_Csv( $tmpfile );
1132 - fclose( $handle );
1133 - break;
1134 - case 'table':
1135 - // Import from Chart
1136 - // fall-through.
1137 - case 'excel':
1138 - // data coming in from the excel editor.
1139 - $source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
1140 - break;
829 + }
830 + $source = new Visualizer_Source_Csv( $tmpfile );
831 + fclose( $handle );
1141 832 }
1142 833 return $source;
1143 834 }
1144 835
@@ -1161,9 +852,9 @@
1161 852 foreach ( $types as $type ) {
1162 853 if ( empty( $type ) ) {
1163 854 $exclude[] = $index;
1164 855 }
1165 - ++$index;
856 + $index++;
1166 857 }
1167 858
1168 859 // when N headers are being renamed, the number of headers increases by N
1169 860 // because of the way datatable duplicates header information
@@ -1187,9 +878,9 @@
1187 878 $csv[] = $types;
1188 879 for ( $j = 0; $j < count( $columns[0] ); $j++ ) {
1189 880 $row = array();
1190 881 for ( $i = 0; $i < count( $headers ); $i++ ) {
1191 - $row[] = sanitize_text_field( $columns[ $i ][ $j ] );
882 + $row[] = $columns[ $i ][ $j ];
1192 883 }
1193 884 $csv[] = $row;
1194 885 }
1195 886
@@ -1223,13 +914,9 @@
1223 914 // otherwise, assume this is a normal web request.
1224 915 $can_die = ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE );
1225 916
1226 917 // validate nonce
1227 - if (
1228 - ! isset( $_GET['nonce'] ) ||
1229 - ! wp_verify_nonce( $_GET['nonce'], 'visualizer-upload-data' ) ||
1230 - ! current_user_can( 'edit_posts' )
1231 - ) {
918 + if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'] ) ) {
1232 919 if ( ! $can_die ) {
1233 920 return;
1234 921 }
1235 922 status_header( 403 );
@@ -1238,15 +925,9 @@
1238 925
1239 926 // check chart, if chart exists
1240 927 // do not use filter_input as it does not work for phpunit test cases, use filter_var instead
1241 928 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
1242 - $chart = $chart_id ? get_post( $chart_id ) : null;
1243 - if (
1244 - ! $chart_id ||
1245 - ! $chart ||
1246 - $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ||
1247 - ! current_user_can( 'edit_post', $chart_id )
1248 - ) {
929 + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
1249 930 if ( ! $can_die ) {
1250 931 return;
1251 932 }
1252 933 status_header( 400 );
@@ -1252,10 +933,8 @@
1252 933 status_header( 400 );
1253 934 exit;
1254 935 }
1255 936
1256 - 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__ );
1257 -
1258 937 if ( ! isset( $_POST['vz-import-time'] ) ) {
1259 938 apply_filters( 'visualizer_pro_remove_schedule', $chart_id );
1260 939 }
1261 940
@@ -1267,9 +946,8 @@
1267 946
1268 947 // delete "import from db" specific parameters.
1269 948 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
1270 949 delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
1271 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS );
1272 950 }
1273 951
1274 952 // delete json related data.
1275 953 delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL );
@@ -1274,60 +952,29 @@
1274 952 // delete json related data.
1275 953 delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL );
1276 954 delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
1277 955 delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
1278 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS );
1279 956
1280 - // delete last error
1281 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
1282 -
1283 - // delete editor related data.
1284 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR );
1285 -
1286 - // delete this so that a JSON import can be later edited manually without a problem.
1287 - delete_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE );
1288 -
1289 957 $source = null;
1290 958 $render = new Visualizer_Render_Page_Update();
1291 -
1292 - $remote_data = false;
1293 - if ( isset( $_POST['remote_data'] ) && function_exists( 'wp_http_validate_url' ) ) {
1294 - $remote_data = wp_http_validate_url( $_POST['remote_data'] );
1295 - }
1296 - if ( false !== $remote_data ) {
1297 - $remote_ext = strtolower( pathinfo( parse_url( $remote_data, PHP_URL_PATH ), PATHINFO_EXTENSION ) );
1298 - if ( 'xlsx' === $remote_ext || ( 'csv' !== $remote_ext && self::_url_is_xlsx( $remote_data ) ) ) {
1299 - $source = new Visualizer_Source_Xlsx_Remote( $remote_data );
1300 - } else {
1301 - $source = new Visualizer_Source_Csv_Remote( $remote_data );
1302 - }
959 + if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
960 + $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
1303 961 if ( isset( $_POST['vz-import-time'] ) ) {
1304 - apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $remote_data, $_POST['vz-import-time'] );
962 + apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] );
1305 963 }
1306 - } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] === 0 ) {
1307 - $local_ext = strtolower( pathinfo( isset( $_FILES['local_data']['name'] ) ? $_FILES['local_data']['name'] : '', PATHINFO_EXTENSION ) );
1308 - if ( 'xlsx' === $local_ext ) {
1309 - $source = new Visualizer_Source_Xlsx( $_FILES['local_data']['tmp_name'] );
1310 - } else {
1311 - $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
1312 - }
964 + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
965 + } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
966 + $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
1313 967 } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
1314 - $source = $this->handleCSVasString( $_POST['chart_data'], $_POST['editor-type'] );
1315 - update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
968 + $source = $this->handleCSVasString( $_POST['chart_data'] );
1316 969 } elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
1317 970 $source = $this->handleTabularData();
1318 - update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
1319 971 } else {
1320 - 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__ );
1321 - $render->message = esc_html__( 'No CSV file was received. Select a file and try uploading again.', 'visualizer' );
1322 - update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, esc_html__( 'No CSV file was received. Select a file and try uploading again.', 'visualizer' ) );
972 + $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );
1323 973 }
1324 -
1325 - 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__ );
1326 -
1327 974 if ( $source ) {
1328 975 if ( $source->fetch() ) {
1329 - $content = $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) );
976 + $content = $source->getData();
1330 977 $populate = true;
1331 978 if ( is_string( $content ) && is_array( unserialize( $content ) ) ) {
1332 979 $json = unserialize( $content );
1333 980 // if source exists, so should data. if source exists but data is blank, do not populate the chart.
@@ -1333,9 +980,8 @@
1333 980 // if source exists, so should data. if source exists but data is blank, do not populate the chart.
1334 981 // if we populate the data even if it is empty, the chart will show "Table has no columns".
1335 982 if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) {
1336 983 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__ );
1337 - update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ) );
1338 984 $populate = false;
1339 985 }
1340 986 }
1341 987 if ( $populate ) {
@@ -1341,49 +987,32 @@
1341 987 if ( $populate ) {
1342 988 $chart->post_content = $content;
1343 989 }
1344 990 wp_update_post( $chart->to_array() );
1345 -
1346 991 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
1347 992 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
1348 993 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
1349 994
1350 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Updated post for chart %d', $chart_id ), 'debug', __FILE__, __LINE__ );
1351 -
1352 995 Visualizer_Module_Utility::set_defaults( $chart, null );
1353 996
1354 997 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
1355 - if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $source->getSeries() ) > 1 ) ) {
1356 - $diff_total_series = abs( count( $settings['series'] ) - count( $source->getSeries() ) );
1357 - if ( $diff_total_series ) {
1358 - foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) {
1359 - $settings['series'][] = end( $settings['series'] );
1360 - }
1361 - update_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings );
1362 - }
1363 - }
1364 998
1365 999 $render->id = $chart->ID;
1366 - $render->data = json_encode( $source->getRawData( get_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) );
1000 + $render->data = json_encode( $source->getRawData() );
1367 1001 $render->series = json_encode( $source->getSeries() );
1368 1002 $render->settings = json_encode( $settings );
1369 1003 } else {
1370 - $error = $source->get_error();
1371 - if ( empty( $error ) ) {
1372 - $error = esc_html__( 'The CSV file couldn\'t be read. Check that it\'s properly formatted and try again.', 'visualizer' );
1004 + $render->message = $source->get_error();
1005 + if ( empty( $render->message ) ) {
1006 + $render->message = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
1373 1007 }
1374 - $render->message = $error;
1375 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( '%s for chart %d.', $error, $chart_id ), 'error', __FILE__, __LINE__ );
1376 - update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, $error );
1377 1008 }
1378 - } else {
1379 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unknown internal error for chart %d.', $chart_id ), 'error', __FILE__, __LINE__ );
1380 1009 }
1381 1010 $render->render();
1382 1011 if ( ! $can_die ) {
1383 1012 return;
1384 1013 }
1385 - ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
1014 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
1386 1015 }
1387 1016
1388 1017 /**
1389 1018 * Clones the chart.
@@ -1422,26 +1051,23 @@
1422 1051 if ( strpos( $key, 'visualizer-' ) !== false ) {
1423 1052 add_post_meta( $new_chart_id, $key, maybe_unserialize( $value[0] ) );
1424 1053 }
1425 1054 }
1426 - $redirect = esc_url(
1427 - add_query_arg(
1428 - array(
1429 - 'page' => 'visualizer',
1430 - 'type' => filter_input( INPUT_GET, 'type' ),
1431 - 'vaction' => false,
1432 - ),
1433 - admin_url( 'admin.php' )
1055 + $redirect = add_query_arg(
1056 + array(
1057 + 'page' => 'visualizer',
1058 + 'type' => filter_input( INPUT_GET, 'type' ),
1059 + 'vaction' => false,
1434 1060 ),
1435 - null,
1436 - 'db'
1061 + admin_url( 'admin.php' )
1437 1062 );
1438 1063 }
1439 1064 }
1440 1065
1441 - if ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) {
1066 + if ( defined( 'WP_TESTS_DOMAIN' ) ) {
1442 1067 wp_die();
1443 1068 }
1069 +
1444 1070 wp_redirect( $redirect );
1445 1071 exit;
1446 1072 }
1447 1073
@@ -1472,9 +1098,9 @@
1472 1098 }
1473 1099 }
1474 1100 }
1475 1101
1476 - ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
1102 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
1477 1103 }
1478 1104
1479 1105 /**
1480 1106 * Handles chart data page.
@@ -1487,12 +1113,9 @@
1487 1113 $data = $this->_getChartArray();
1488 1114 $render = new Visualizer_Render_Page_Data();
1489 1115 $render->chart = $this->_chart;
1490 1116 $render->type = $data['type'];
1491 -
1492 - if ( $data && $data['settings'] ) {
1493 - unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
1494 - }
1117 + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] );
1495 1118 wp_enqueue_style( 'visualizer-frame' );
1496 1119 wp_enqueue_script( 'visualizer-render' );
1497 1120 wp_localize_script(
1498 1121 'visualizer-render',
@@ -1498,11 +1121,10 @@
1498 1121 'visualizer-render',
1499 1122 'visualizer',
1500 1123 array(
1501 1124 'l10n' => array(
1502 - 'invalid_source' => esc_html__( 'The URL you entered is invalid. Please enter a valid URL.', 'visualizer' ),
1503 - 'loading' => esc_html__( 'Loading...', 'visualizer' ),
1504 - 'invalid_format' => esc_html__( 'This format pattern is not supported in the series settings field. To display percentages, use the Manual Configuration option instead.', 'visualizer' ),
1125 + 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
1126 + 'loading' => esc_html__( 'Loading...', 'visualizer' ),
1505 1127 ),
1506 1128 'charts' => array(
1507 1129 'canvas' => $data,
1508 1130 ),
@@ -1508,13 +1130,13 @@
1508 1130 ),
1509 1131 )
1510 1132 );
1511 1133
1512 - do_action( 'visualizer_enqueue_scripts_and_styles', $data, $this->_chart->ID );
1134 + do_action( 'visualizer_enqueue_scripts_and_styles', $data );
1513 1135
1514 1136 if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
1515 1137 global $Visualizer_Pro;
1516 - $Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
1138 + $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
1517 1139 }
1518 1140
1519 1141 // Added by Ash/Upwork
1520 1142 $this->_addAction( 'admin_head', 'renderFlattrScript' );
@@ -1528,27 +1150,14 @@
1528 1150 */
1529 1151 public function getQueryData() {
1530 1152 check_ajax_referer( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION, 'security' );
1531 1153
1532 - if ( ! current_user_can( 'administrator' ) ) {
1533 - wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
1534 - }
1535 - if ( ! is_super_admin() ) {
1536 - wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
1537 - }
1538 -
1539 - if ( ! Visualizer_Module::is_pro() ) {
1540 - wp_send_json_error( array( 'msg' => __( 'Feature is not available.', 'visualizer' ) ) );
1541 - }
1542 -
1543 1154 $params = wp_parse_args( $_POST['params'] );
1544 - $chart_id = filter_var( $params['chart_id'], FILTER_VALIDATE_INT );
1545 - $query = trim( $params['query'], ';' );
1546 -
1547 - $source = new Visualizer_Source_Query( stripslashes( $query ), $chart_id, $params );
1155 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1548 1156 $html = $source->fetch( true );
1549 - $error = $source->get_error();
1550 - if ( ! empty( $error ) ) {
1157 + $error = '';
1158 + if ( empty( $html ) ) {
1159 + $error = $source->get_error();
1551 1160 wp_send_json_error( array( 'msg' => $error ) );
1552 1161 }
1553 1162 wp_send_json_success( array( 'table' => $html ) );
1554 1163 }
@@ -1560,19 +1169,8 @@
1560 1169 */
1561 1170 public function saveQuery() {
1562 1171 check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION, 'security' );
1563 1172
1564 - if ( ! current_user_can( 'administrator' ) ) {
1565 - wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
1566 - }
1567 - if ( ! is_super_admin() ) {
1568 - wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
1569 - }
1570 -
1571 - if ( ! Visualizer_Module::is_pro() ) {
1572 - wp_send_json_error( array( 'msg' => __( 'Feature is not available.', 'visualizer' ) ) );
1573 - }
1574 -
1575 1173 $chart_id = filter_input(
1576 1174 INPUT_GET,
1577 1175 'chart',
1578 1176 FILTER_VALIDATE_INT,
@@ -1582,43 +1180,21 @@
1582 1180 ),
1583 1181 )
1584 1182 );
1585 1183
1586 - $hours = filter_input(
1587 - INPUT_POST,
1588 - 'refresh',
1589 - FILTER_VALIDATE_FLOAT,
1590 - array(
1591 - 'options' => array(
1592 - 'min_range' => -1,
1593 - 'max_range' => apply_filters( 'visualizer_is_business', false ) ? PHP_INT_MAX : -1,
1594 - ),
1595 - )
1596 - );
1597 -
1598 - if ( ! is_numeric( $hours ) ) {
1599 - $hours = -1;
1600 - }
1601 -
1602 1184 $render = new Visualizer_Render_Page_Update();
1603 1185 if ( $chart_id ) {
1604 1186 $params = wp_parse_args( $_POST['params'] );
1605 - $query = trim( $params['query'], ';' );
1606 - $source = new Visualizer_Source_Query( stripslashes( $query ), $chart_id, $params );
1187 + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1607 1188 $source->fetch( false );
1608 1189 $error = $source->get_error();
1609 1190 if ( empty( $error ) ) {
1610 - update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, stripslashes( $query ) );
1191 + $hours = $_POST['refresh'];
1192 + update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, stripslashes( $params['query'] ) );
1611 1193 update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
1612 1194 update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
1613 1195 update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, $hours );
1614 1196 update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
1615 - if ( isset( $params['db_type'] ) && $params['db_type'] !== Visualizer_Plugin::WP_DB_NAME ) {
1616 - $remote_db_params = $params;
1617 - unset( $remote_db_params['query'] );
1618 - unset( $remote_db_params['chart_id'] );
1619 - update_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS, $remote_db_params );
1620 - }
1621 1197
1622 1198 $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
1623 1199 $schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
1624 1200 update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $schedules );
@@ -1625,14 +1201,13 @@
1625 1201
1626 1202 wp_update_post(
1627 1203 array(
1628 1204 'ID' => $chart_id,
1629 - 'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ),
1205 + 'post_content' => $source->getData(),
1630 1206 )
1631 1207 );
1632 - $render->data = json_encode( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) );
1208 + $render->data = json_encode( $source->getRawData() );
1633 1209 $render->series = json_encode( $source->getSeries() );
1634 - $render->id = $chart_id;
1635 1210 } else {
1636 1211 $render->message = $error;
1637 1212 }
1638 1213 }
@@ -1637,9 +1212,9 @@
1637 1212 }
1638 1213 }
1639 1214 $render->render();
1640 1215 if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
1641 - ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
1216 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
1642 1217 }
1643 1218 }
1644 1219
1645 1220
@@ -1661,80 +1236,13 @@
1661 1236 ),
1662 1237 )
1663 1238 );
1664 1239
1665 - $hours = filter_input(
1666 - INPUT_POST,
1667 - 'refresh',
1668 - FILTER_VALIDATE_FLOAT,
1669 - array(
1670 - 'options' => array(
1671 - 'min_range' => -1,
1672 - 'max_range' => apply_filters( 'visualizer_is_business', false ) ? PHP_INT_MAX : -1,
1673 - ),
1674 - )
1675 - );
1240 + $hours = $_POST['refresh'];
1676 1241
1677 - if ( ! is_numeric( $hours ) ) {
1678 - $hours = -1;
1679 - }
1680 -
1681 1242 do_action( 'visualizer_save_filter', $chart_id, $hours );
1682 1243
1683 1244 if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
1684 - ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
1245 + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
1685 1246 }
1686 - }
1687 -
1688 - /**
1689 - * Save chart image.
1690 - *
1691 - * @param string $base64_img Chart image.
1692 - * @param int $chart_id Chart ID.
1693 - * @param bool $save_attachment Save attachment.
1694 - * @return attachment ID
1695 - */
1696 - public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) {
1697 - // Delete old chart image.
1698 - $old_attachment_id = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true );
1699 - if ( $old_attachment_id ) {
1700 - wp_delete_attachment( $old_attachment_id, true );
1701 - }
1702 -
1703 - if ( ! $save_attachment ) {
1704 - return 0;
1705 - }
1706 -
1707 - // Upload dir.
1708 - $upload_dir = wp_upload_dir();
1709 - $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
1710 -
1711 - $img = str_replace( 'data:image/png;base64,', '', $base64_img );
1712 - $img = str_replace( ' ', '+', $img );
1713 - $decoded = base64_decode( $img );
1714 - $filename = 'visualization-' . $chart_id . '.png';
1715 - $file_type = 'image/png';
1716 - $hashed_filename = $filename;
1717 -
1718 - // Save the image in the uploads directory.
1719 - require_once ABSPATH . '/wp-admin/includes/file.php';
1720 - \WP_Filesystem();
1721 - global $wp_filesystem;
1722 - if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base' ) ) {
1723 - $creds = request_filesystem_credentials( site_url() );
1724 - wp_filesystem( $creds );
1725 - }
1726 - $upload_file = $wp_filesystem->put_contents( $upload_path . $hashed_filename, $decoded );
1727 -
1728 - // Insert new chart image.
1729 - $attachment = array(
1730 - 'post_mime_type' => $file_type,
1731 - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $hashed_filename ) ),
1732 - 'post_content' => '',
1733 - 'post_status' => 'inherit',
1734 - 'guid' => $upload_dir['url'] . '/' . basename( $hashed_filename ),
1735 - );
1736 -
1737 - $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $hashed_filename );
1738 - return $attach_id;
1739 1247 }
1740 1248 }