PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.8
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.8
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/Frontend.php +139 -48 3.10.154.0.8 View file →
@@ -46,9 +46,9 @@
46 46 *
47 47 * @access private
48 48 * @var bool
49 49 */
50 - private $lazy_render_script = false;
50 + private $lazy_render_script = true;
51 51
52 52 /**
53 53 * Constructor.
54 54 *
@@ -86,9 +86,9 @@
86 86
87 87 /**
88 88 * Adds the async attribute to certain scripts.
89 89 */
90 - function script_loader_tag( $tag, $handle, $src ) {
90 + public function script_loader_tag( $tag, $handle, $src ) {
91 91 if ( is_admin() ) {
92 92 return $tag;
93 93 }
94 94 // Async scripts.
@@ -97,9 +97,9 @@
97 97 if ( $async === $handle ) {
98 98 $tag = str_replace( ' src', ' async src', $tag );
99 99 break;
100 100 }
101 - };
101 + }
102 102
103 103 // Async scripts.
104 104 $scripts = array( 'dom-to-image' );
105 105 foreach ( array( 'async', 'defer' ) as $attr ) {
@@ -123,9 +123,9 @@
123 123
124 124 /**
125 125 * Returns the language/locale.
126 126 */
127 - function getLanguage( $dummy, $only_language ) {
127 + public function getLanguage( $dummy, $only_language ) {
128 128 return $this->get_language();
129 129 }
130 130
131 131
@@ -131,9 +131,9 @@
131 131
132 132 /**
133 133 * Registers the endpoints
134 134 */
135 - function endpoint_register() {
135 + public function endpoint_register() {
136 136 register_rest_route(
137 137 'visualizer/v' . VISUALIZER_REST_VERSION,
138 138 '/action/(?P<chart>\d+)/(?P<type>.+)/',
139 139 array(
@@ -141,9 +141,9 @@
141 141 'methods' => array( 'POST', 'GET' ),
142 142 'args' => array(
143 143 'chart' => array(
144 144 'required' => true,
145 - 'sanitize_callback' => function( $param ) {
145 + 'sanitize_callback' => function ( $param ) {
146 146 return is_numeric( $param ) ? $param : null;
147 147 },
148 148 ),
149 149 'type' => array(
@@ -152,16 +152,27 @@
152 152 'enum' => array_merge( array( 'save', 'cancel' ), array_keys( $this->get_actions() ) ),
153 153 ),
154 154 ),
155 155 'permission_callback' => function ( WP_REST_Request $request ) {
156 - $chart_id = filter_var( sanitize_text_field( $request->get_param( 'chart' ), FILTER_VALIDATE_INT ) );
157 - if ( ! empty( $chart_id ) && in_array( $request->get_param( 'type' ), array( 'save', 'cancel' ), true ) ) {
158 - // let save and cancel go without any check as past version of pro
159 - // did not send the X-WP-Nonce
160 - // we can change this at a later date.
161 - return true;
156 + $chart_id = absint( $request->get_param( 'chart' ) );
157 + if ( ! $chart_id ) {
158 + return false;
162 159 }
163 - return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id );
160 +
161 + $chart = get_post( $chart_id );
162 + if ( ! $chart || Visualizer_Plugin::CPT_VISUALIZER !== $chart->post_type ) {
163 + return false;
164 + }
165 +
166 + if ( in_array( $request->get_param( 'type' ), array( 'save', 'cancel' ), true ) ) {
167 + return current_user_can( 'edit_post', $chart_id );
168 + }
169 +
170 + if ( 'publish' !== $chart->post_status ) {
171 + return current_user_can( 'edit_post', $chart_id );
172 + }
173 +
174 + return apply_filters( 'visualizer_pro_show_chart', true, $chart_id );
164 175 },
165 176 'callback' => array( $this, 'perform_action' ),
166 177 )
167 178 );
@@ -340,12 +351,15 @@
340 351 if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) {
341 352 return '';
342 353 }
343 354
344 - // in case revisions exist.
345 - // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
346 - if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
347 - $chart = get_post( $chart->ID );
355 + // in case revisions exist (skip D3 charts to avoid reverting AI data).
356 + $chart_library = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, true );
357 + if ( 'd3' !== $chart_library ) {
358 + $revisions = $this->undoRevisions( $chart->ID, true );
359 + if ( true === $revisions ) {
360 + $chart = get_post( $chart->ID );
361 + }
348 362 }
349 363
350 364 $id = 'visualizer-' . $atts['id'];
351 365 $defaultClass = 'visualizer-front';
@@ -382,10 +396,11 @@
382 396
383 397 $lazy_load = isset( $settings['lazy_load_chart'] ) ? $settings['lazy_load_chart'] : false;
384 398 $lazy_load = apply_filters( 'visualizer_lazy_load_chart', $lazy_load, $chart->ID );
385 399 $container_class = 'visualizer-front-container';
386 - if ( $lazy_load ) {
387 - $this->lazy_render_script = true;
400 + if ( ! $lazy_load ) {
401 + $this->lazy_render_script = false;
402 + } else {
388 403 $container_class .= ' visualizer-lazy-render';
389 404 }
390 405
391 406 // handle data filter hooks
@@ -431,9 +446,9 @@
431 446 unset( $settings['controls'] );
432 447 }
433 448
434 449 // add chart to the array
435 - $this->_charts[ $id ] = array(
450 + $chart_entry = array(
436 451 'type' => $type,
437 452 'series' => $series,
438 453 'settings' => $settings,
439 454 'data' => $data,
@@ -439,8 +454,15 @@
439 454 'data' => $data,
440 455 'library' => $library,
441 456 );
442 457
458 + // For D3 charts, include the stored code so the renderer can execute it.
459 + if ( 'd3' === $library ) {
460 + $chart_entry['code'] = get_post_meta( $chart->ID, Visualizer_Module_AIBuilder::CF_D3_CODE, true );
461 + }
462 +
463 + $this->_charts[ $id ] = $chart_entry;
464 +
443 465 $actions_div = '';
444 466 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
445 467 if ( ! empty( $actions_visible ) ) {
446 468 $actions = $this->get_actions();
@@ -474,42 +496,104 @@
474 496
475 497 $_charts = array();
476 498 $_charts_type = '';
477 499 $count = 0;
500 + static $visualizer_localized = false;
501 + static $visualizer_charts = array();
502 + static $d3_localized = false;
503 + $facade_handle = 'visualizer-render-facade';
478 504 foreach ( $this->_charts as $id => $array ) {
479 505 $_charts = $this->_charts;
480 506 $library = $array['library'];
481 507 $_charts_type = $library;
482 - if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) {
508 + $facade_deps = apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true );
509 + if ( ! wp_script_is( $facade_handle, 'registered' ) ) {
483 510 wp_register_script(
484 - "visualizer-render-$library",
511 + $facade_handle,
485 512 VISUALIZER_ABSURL . 'js/render-facade.js',
486 - apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
513 + $facade_deps,
487 514 Visualizer_Plugin::VERSION,
488 515 true
489 516 );
490 - wp_enqueue_script( "visualizer-render-$library" );
517 + wp_enqueue_script( $facade_handle );
518 + } else {
519 + // Ensure newly discovered dependencies are attached to the facade.
520 + global $wp_scripts;
521 + if ( isset( $wp_scripts->registered[ $facade_handle ] ) ) {
522 + $wp_scripts->registered[ $facade_handle ]->deps = array_unique(
523 + array_merge( $wp_scripts->registered[ $facade_handle ]->deps, $facade_deps )
524 + );
525 + }
526 + foreach ( $facade_deps as $dep_handle ) {
527 + wp_enqueue_script( $dep_handle );
528 + }
491 529 }
492 530
493 - if ( wp_script_is( "visualizer-render-$_charts_type" ) && 0 === $count ) {
494 - wp_localize_script(
495 - "visualizer-render-$_charts_type",
496 - 'visualizer',
497 - array(
498 - 'charts' => $this->_charts,
499 - 'language' => $this->get_language(),
500 - 'map_api_key' => get_option( 'visualizer-map-api-key' ),
501 - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
502 - 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
503 - 'i10n' => array(
504 - 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
505 - ),
506 - 'page_type' => 'frontend',
507 - 'is_front' => true,
508 - )
509 - );
531 + // For D3 charts, also enqueue the dedicated renderer bundle.
532 + if ( 'd3' === $library ) {
533 + $d3_renderer_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/D3Renderer/build/index.asset.php';
534 + if ( file_exists( $d3_renderer_asset ) && ! wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) {
535 + /**
536 + * Ignore missing build asset in source checkout.
537 + *
538 + * @phpstan-ignore-next-line
539 + */
540 + $d3_asset = include $d3_renderer_asset;
541 + wp_register_script(
542 + 'visualizer-d3-renderer',
543 + VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/index.js',
544 + array_merge( $d3_asset['dependencies'], array( 'jquery' ) ),
545 + $d3_asset['version'],
546 + true
547 + );
548 + wp_enqueue_script( 'visualizer-d3-renderer' );
549 + }
550 + if ( ! $d3_localized ) {
551 + wp_localize_script(
552 + 'visualizer-d3-renderer',
553 + 'vizD3Renderer',
554 + array(
555 + 'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js',
556 + )
557 + );
558 + $d3_localized = true;
559 + }
510 560 }
511 - $count++;
561 +
562 + if ( wp_script_is( $facade_handle ) && 0 === $count ) {
563 + if ( ! $visualizer_localized ) {
564 + $visualizer_charts = $this->_charts;
565 + wp_localize_script(
566 + $facade_handle,
567 + 'visualizer',
568 + array(
569 + 'charts' => $this->_charts,
570 + 'language' => $this->get_language(),
571 + 'map_api_key' => get_option( 'visualizer-map-api-key' ),
572 + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
573 + 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
574 + 'i10n' => array(
575 + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
576 + ),
577 + 'page_type' => 'frontend',
578 + 'is_front' => true,
579 + )
580 + );
581 + $visualizer_localized = true;
582 + } else {
583 + $new_charts = array_diff_key( $this->_charts, $visualizer_charts );
584 + if ( ! empty( $new_charts ) ) {
585 + wp_add_inline_script(
586 + $facade_handle,
587 + 'window.visualizer = window.visualizer || {};' .
588 + 'window.visualizer.charts = Object.assign(window.visualizer.charts || {}, ' . wp_json_encode( $new_charts ) . ');',
589 + 'before'
590 + );
591 + $visualizer_charts = array_merge( $visualizer_charts, $new_charts );
592 + }
593 + }
594 + }
595 + ++$count;
512 596 }
513 597 $prefix = 'C' . 'h' . 'a' . 'rt';
514 598 if ( $type === 'tabular' ) {
515 599 $prefix = 'T' . 'a' . 'bl' . 'e';
@@ -665,9 +749,9 @@
665 749 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
666 750 $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
667 751 $is_woocommerce_report = get_post_meta( $chart->ID, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
668 752
669 - if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
753 + if ( isset( $settings['series'] ) && is_array( $settings['series'] ) && is_array( $series ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
670 754 $diff_total_series = abs( count( $settings['series'] ) - count( $series ) );
671 755 if ( $diff_total_series ) {
672 756 foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) {
673 757 $settings['series'][] = end( $settings['series'] );
@@ -698,8 +782,9 @@
698 782 if ( $this->lazy_render_script ) {
699 783 ?>
700 784 <script type="text/javascript">
701 785 var visualizerUserInteractionEvents = [
786 + "scroll",
702 787 "mouseover",
703 788 "keydown",
704 789 "touchmove",
705 790 "touchstart"
@@ -717,16 +802,22 @@
717 802 }
718 803
719 804 function visualizerLoadScripts() {
720 805 document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) {
721 - jQuery.getScript( elem.getAttribute("data-visualizer-script") )
722 - .done( function( script, textStatus ) {
723 - elem.setAttribute("src", elem.getAttribute("data-visualizer-script"));
724 - elem.removeAttribute("data-visualizer-script");
806 + // Replace the placeholder with a real script tag using async=false:
807 + // scripts download in parallel but execute in insertion order, which
808 + // preserves the WordPress dependency order. Parallel jQuery.getScript
809 + // calls could execute render-facade.js before the chart renderer had
810 + // registered its render event listener, leaving charts blank.
811 + var script = document.createElement("script");
812 + script.src = elem.getAttribute("data-visualizer-script");
813 + script.async = false;
814 + script.onload = function() {
725 815 setTimeout( function() {
726 816 visualizerRefreshChart();
727 817 } );
728 - } );
818 + };
819 + elem.parentNode.replaceChild(script, elem);
729 820 });
730 821 }
731 822
732 823 function visualizerRefreshChart() {