PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.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/Frontend.php +109 -35 3.10.34.0.0 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(
@@ -340,12 +340,15 @@
340 340 if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) {
341 341 return '';
342 342 }
343 343
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 );
344 + // in case revisions exist (skip D3 charts to avoid reverting AI data).
345 + $chart_library = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, true );
346 + if ( 'd3' !== $chart_library ) {
347 + $revisions = $this->undoRevisions( $chart->ID, true );
348 + if ( true === $revisions ) {
349 + $chart = get_post( $chart->ID );
350 + }
348 351 }
349 352
350 353 $id = 'visualizer-' . $atts['id'];
351 354 $defaultClass = 'visualizer-front';
@@ -382,10 +385,11 @@
382 385
383 386 $lazy_load = isset( $settings['lazy_load_chart'] ) ? $settings['lazy_load_chart'] : false;
384 387 $lazy_load = apply_filters( 'visualizer_lazy_load_chart', $lazy_load, $chart->ID );
385 388 $container_class = 'visualizer-front-container';
386 - if ( $lazy_load ) {
387 - $this->lazy_render_script = true;
389 + if ( ! $lazy_load ) {
390 + $this->lazy_render_script = false;
391 + } else {
388 392 $container_class .= ' visualizer-lazy-render';
389 393 }
390 394
391 395 // handle data filter hooks
@@ -431,9 +435,9 @@
431 435 unset( $settings['controls'] );
432 436 }
433 437
434 438 // add chart to the array
435 - $this->_charts[ $id ] = array(
439 + $chart_entry = array(
436 440 'type' => $type,
437 441 'series' => $series,
438 442 'settings' => $settings,
439 443 'data' => $data,
@@ -439,8 +443,15 @@
439 443 'data' => $data,
440 444 'library' => $library,
441 445 );
442 446
447 + // For D3 charts, include the stored code so the renderer can execute it.
448 + if ( 'd3' === $library ) {
449 + $chart_entry['code'] = get_post_meta( $chart->ID, Visualizer_Module_AIBuilder::CF_D3_CODE, true );
450 + }
451 +
452 + $this->_charts[ $id ] = $chart_entry;
453 +
443 454 $actions_div = '';
444 455 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
445 456 if ( ! empty( $actions_visible ) ) {
446 457 $actions = $this->get_actions();
@@ -474,42 +485,104 @@
474 485
475 486 $_charts = array();
476 487 $_charts_type = '';
477 488 $count = 0;
489 + static $visualizer_localized = false;
490 + static $visualizer_charts = array();
491 + static $d3_localized = false;
492 + $facade_handle = 'visualizer-render-facade';
478 493 foreach ( $this->_charts as $id => $array ) {
479 494 $_charts = $this->_charts;
480 495 $library = $array['library'];
481 496 $_charts_type = $library;
482 - if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) {
497 + $facade_deps = apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true );
498 + if ( ! wp_script_is( $facade_handle, 'registered' ) ) {
483 499 wp_register_script(
484 - "visualizer-render-$library",
500 + $facade_handle,
485 501 VISUALIZER_ABSURL . 'js/render-facade.js',
486 - apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
502 + $facade_deps,
487 503 Visualizer_Plugin::VERSION,
488 504 true
489 505 );
490 - wp_enqueue_script( "visualizer-render-$library" );
506 + wp_enqueue_script( $facade_handle );
507 + } else {
508 + // Ensure newly discovered dependencies are attached to the facade.
509 + global $wp_scripts;
510 + if ( isset( $wp_scripts->registered[ $facade_handle ] ) ) {
511 + $wp_scripts->registered[ $facade_handle ]->deps = array_unique(
512 + array_merge( $wp_scripts->registered[ $facade_handle ]->deps, $facade_deps )
513 + );
514 + }
515 + foreach ( $facade_deps as $dep_handle ) {
516 + wp_enqueue_script( $dep_handle );
517 + }
491 518 }
492 519
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 - );
520 + // For D3 charts, also enqueue the dedicated renderer bundle.
521 + if ( 'd3' === $library ) {
522 + $d3_renderer_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/D3Renderer/build/index.asset.php';
523 + if ( file_exists( $d3_renderer_asset ) && ! wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) {
524 + /**
525 + * Ignore missing build asset in source checkout.
526 + *
527 + * @phpstan-ignore-next-line
528 + */
529 + $d3_asset = include $d3_renderer_asset;
530 + wp_register_script(
531 + 'visualizer-d3-renderer',
532 + VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/index.js',
533 + array_merge( $d3_asset['dependencies'], array( 'jquery' ) ),
534 + $d3_asset['version'],
535 + true
536 + );
537 + wp_enqueue_script( 'visualizer-d3-renderer' );
538 + }
539 + if ( ! $d3_localized ) {
540 + wp_localize_script(
541 + 'visualizer-d3-renderer',
542 + 'vizD3Renderer',
543 + array(
544 + 'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js',
545 + )
546 + );
547 + $d3_localized = true;
548 + }
510 549 }
511 - $count++;
550 +
551 + if ( wp_script_is( $facade_handle ) && 0 === $count ) {
552 + if ( ! $visualizer_localized ) {
553 + $visualizer_charts = $this->_charts;
554 + wp_localize_script(
555 + $facade_handle,
556 + 'visualizer',
557 + array(
558 + 'charts' => $this->_charts,
559 + 'language' => $this->get_language(),
560 + 'map_api_key' => get_option( 'visualizer-map-api-key' ),
561 + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
562 + 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
563 + 'i10n' => array(
564 + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
565 + ),
566 + 'page_type' => 'frontend',
567 + 'is_front' => true,
568 + )
569 + );
570 + $visualizer_localized = true;
571 + } else {
572 + $new_charts = array_diff_key( $this->_charts, $visualizer_charts );
573 + if ( ! empty( $new_charts ) ) {
574 + wp_add_inline_script(
575 + $facade_handle,
576 + 'window.visualizer = window.visualizer || {};' .
577 + 'window.visualizer.charts = Object.assign(window.visualizer.charts || {}, ' . wp_json_encode( $new_charts ) . ');',
578 + 'before'
579 + );
580 + $visualizer_charts = array_merge( $visualizer_charts, $new_charts );
581 + }
582 + }
583 + }
584 + ++$count;
512 585 }
513 586 $prefix = 'C' . 'h' . 'a' . 'rt';
514 587 if ( $type === 'tabular' ) {
515 588 $prefix = 'T' . 'a' . 'bl' . 'e';
@@ -698,8 +771,9 @@
698 771 if ( $this->lazy_render_script ) {
699 772 ?>
700 773 <script type="text/javascript">
701 774 var visualizerUserInteractionEvents = [
775 + "scroll",
702 776 "mouseover",
703 777 "keydown",
704 778 "touchmove",
705 779 "touchstart"