PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.7.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.7.1
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 +34 -169 3.10.123.7.1 View file →
@@ -41,16 +41,8 @@
41 41 */
42 42 private $_charts = array();
43 43
44 44 /**
45 - * Lazy render script.
46 - *
47 - * @access private
48 - * @var bool
49 - */
50 - private $lazy_render_script = false;
51 -
52 - /**
53 45 * Constructor.
54 46 *
55 47 * @since 1.0.0
56 48 * @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters.
@@ -60,12 +52,10 @@
60 52 */
61 53 public function __construct( Visualizer_Plugin $plugin ) {
62 54 parent::__construct( $plugin );
63 55
64 - $this->_addAction( 'wp_print_footer_scripts', 'printFooterScripts' );
65 56 $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' );
66 57 $this->_addAction( 'load-index.php', 'enqueueScripts' );
67 - $this->_addAction( 'load-visualizer_page_visualizer-setup-wizard', 'enqueueScripts' );
68 58 $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' );
69 59 $this->_addFilter( 'visualizer_get_language', 'getLanguage' );
70 60 $this->_addShortcode( 'visualizer', 'renderChart' );
71 61
@@ -90,34 +80,16 @@
90 80 function script_loader_tag( $tag, $handle, $src ) {
91 81 if ( is_admin() ) {
92 82 return $tag;
93 83 }
94 - // Async scripts.
95 - $async_scripts = array( 'google-jsapi', 'chartjs', 'visualizer-datatables' );
96 - foreach ( $async_scripts as $async ) {
84 +
85 + $scripts = array( 'google-jsapi', 'visualizer-render-google-lib', 'visualizer-render-google' );
86 +
87 + foreach ( $scripts as $async ) {
97 88 if ( $async === $handle ) {
98 - $tag = str_replace( ' src', ' async src', $tag );
89 + $tag = str_replace( ' src', ' defer="defer" src', $tag );
99 90 break;
100 91 }
101 - };
102 -
103 - // Async scripts.
104 - $scripts = array( 'dom-to-image' );
105 - foreach ( array( 'async', 'defer' ) as $attr ) {
106 - if ( wp_scripts()->get_data( $handle, $attr ) ) {
107 - break;
108 - }
109 - if ( in_array( $handle, $async_scripts, true ) || false === strpos( $handle, 'visualizer-' ) ) {
110 - break;
111 - }
112 - if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) {
113 - $tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 );
114 - if ( $this->lazy_render_script ) {
115 - $tag = str_replace( ' src', ' data-visualizer-script', $tag );
116 - }
117 - }
118 - // Only allow async or defer, not both.
119 - break;
120 92 }
121 93 return $tag;
122 94 }
123 95
@@ -271,8 +243,9 @@
271 243 * @access public
272 244 */
273 245 public function enqueueScripts() {
274 246 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
247 + wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION );
275 248 do_action( 'visualizer_pro_frontend_load_resources' );
276 249 }
277 250
278 251 /**
@@ -305,25 +278,8 @@
305 278 ),
306 279 $atts
307 280 );
308 281
309 - $atts['id'] = (int) $atts['id'];
310 - $atts['class'] = esc_attr( $atts['class'] );
311 - $atts['lazy'] = esc_attr( $atts['lazy'] );
312 - $atts['use_image'] = esc_attr( $atts['use_image'] );
313 -
314 - global $sitepress;
315 - if ( Visualizer_Module::is_pro() && ( function_exists( 'icl_get_languages' ) && $sitepress instanceof \SitePress ) ) {
316 - global $sitepress;
317 - $locale = icl_get_current_language();
318 - $locale = strtolower( str_replace( '_', '-', $locale ) );
319 - $trid = $sitepress->get_element_trid( $atts['id'], 'post_' . Visualizer_Plugin::CPT_VISUALIZER );
320 - $translations = $sitepress->get_element_translations( $trid );
321 - if ( isset( $translations[ $locale ] ) && is_object( $translations[ $locale ] ) ) {
322 - $atts['id'] = $translations[ $locale ]->element_id;
323 - }
324 - }
325 -
326 282 $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] );
327 283 // if empty chart does not exists, then return empty string.
328 284 if ( ! $chart_data ) {
329 285 return '';
@@ -336,12 +292,8 @@
336 292 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
337 293 return '';
338 294 }
339 295
340 - if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) {
341 - return '';
342 - }
343 -
344 296 // in case revisions exist.
345 297 // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
346 298 if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
347 299 $chart = get_post( $chart->ID );
@@ -362,15 +314,12 @@
362 314 if ( ctype_digit( $atts['lazy'] ) ) {
363 315 $attributes['data-lazy-limit'] = $atts['lazy'];
364 316 }
365 317
366 - $attributes = apply_filters( 'visualizer_container_attributes', $attributes, $chart->ID );
367 -
368 318 $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false );
369 319
370 320 // Get and update settings.
371 321 $settings = $chart_data['settings'];
372 - $settings = ! empty( $settings ) ? $settings : array();
373 322 if ( empty( $settings['height'] ) ) {
374 323 $settings['height'] = '400';
375 324 }
376 325
@@ -379,16 +328,8 @@
379 328
380 329 // handle settings filter hooks
381 330 $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
382 331
383 - $lazy_load = isset( $settings['lazy_load_chart'] ) ? $settings['lazy_load_chart'] : false;
384 - $lazy_load = apply_filters( 'visualizer_lazy_load_chart', $lazy_load, $chart->ID );
385 - $container_class = 'visualizer-front-container';
386 - if ( $lazy_load ) {
387 - $this->lazy_render_script = true;
388 - $container_class .= ' visualizer-lazy-render';
389 - }
390 -
391 332 // handle data filter hooks
392 333 $data = self::get_chart_data( $chart, $type );
393 334
394 335 $css = '';
@@ -417,21 +358,8 @@
417 358 if ( isset( $settings['chart-img'] ) ) {
418 359 unset( $settings['chart-img'] );
419 360 }
420 361
421 - $enable_controls = false;
422 - if ( isset( $settings['controls'] ) && ! empty( $settings['controls']['controlType'] ) ) {
423 - $column_index = $settings['controls']['filterColumnIndex'];
424 - $column_label = $settings['controls']['filterColumnLabel'];
425 - if ( 'false' !== $column_index || 'false' !== $column_label ) {
426 - $enable_controls = true;
427 - }
428 - }
429 -
430 - if ( ! $enable_controls ) {
431 - unset( $settings['controls'] );
432 - }
433 -
434 362 // add chart to the array
435 363 $this->_charts[ $id ] = array(
436 364 'type' => $type,
437 365 'series' => $series,
@@ -473,9 +401,8 @@
473 401 $actions_div .= $css;
474 402
475 403 $_charts = array();
476 404 $_charts_type = '';
477 - $count = 0;
478 405 foreach ( $this->_charts as $id => $array ) {
479 406 $_charts = $this->_charts;
480 407 $library = $array['library'];
481 408 $_charts_type = $library;
@@ -488,38 +415,31 @@
488 415 true
489 416 );
490 417 wp_enqueue_script( "visualizer-render-$library" );
491 418 }
492 -
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 - );
510 - }
511 - $count++;
512 419 }
513 - $prefix = 'C' . 'h' . 'a' . 'rt';
514 - if ( $type === 'tabular' ) {
515 - $prefix = 'T' . 'a' . 'bl' . 'e';
420 + if ( wp_script_is( "visualizer-render-$_charts_type" ) ) {
421 + wp_localize_script(
422 + "visualizer-render-$_charts_type",
423 + 'visualizer',
424 + array(
425 + 'charts' => $_charts,
426 + 'language' => $this->get_language(),
427 + 'map_api_key' => get_option( 'visualizer-map-api-key' ),
428 + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
429 + 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
430 + 'i10n' => array(
431 + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
432 + ),
433 + 'page_type' => 'frontend',
434 + 'is_front' => true,
435 + )
436 + );
516 437 }
517 - if ( Visualizer_Module::is_pro() && $enable_controls ) {
518 - $actions_div .= '<div id="control_wrapper_' . $id . '"></div>';
519 - }
438 + wp_enqueue_style( 'visualizer-front' );
439 +
520 440 // return placeholder div
521 - return '<div class="' . $container_class . '" id="chart_wrapper_' . $id . '">' . $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ) . '</div>';
441 + return $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID );
522 442 }
523 443
524 444 /**
525 445 * Converts the array of attributes to a string of HTML attributes.
@@ -534,9 +454,9 @@
534 454 return $string;
535 455 }
536 456
537 457 foreach ( $attributes as $name => $value ) {
538 - $string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) );
458 + $string .= sprintf( '%s="%s"', esc_attr( $name ), esc_attr( $value ) );
539 459 }
540 460 return $string;
541 461 }
542 462
@@ -661,11 +581,10 @@
661 581
662 582 // Get chart by ID.
663 583 $chart = get_post( $chart_id );
664 584 if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) {
665 - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
666 - $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
667 - $is_woocommerce_report = get_post_meta( $chart->ID, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
585 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
586 + $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
668 587
669 588 if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
670 589 $diff_total_series = abs( count( $settings['series'] ) - count( $series ) );
671 590 if ( $diff_total_series ) {
@@ -674,14 +593,13 @@
674 593 }
675 594 }
676 595 }
677 596 $chart_data = array(
678 - 'chart' => $chart,
679 - 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
680 - 'settings' => $settings,
681 - 'series' => $series,
682 - 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
683 - 'is_woocommerce_report' => $is_woocommerce_report,
597 + 'chart' => $chart,
598 + 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
599 + 'settings' => $settings,
600 + 'series' => $series,
601 + 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
684 602 );
685 603
686 604 // Put the results in a transient. Expire after 12 hours.
687 605 set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) );
@@ -688,59 +606,6 @@
688 606 return $chart_data;
689 607 }
690 608
691 609 return false;
692 - }
693 -
694 - /**
695 - * Print footer script.
696 - */
697 - public function printFooterScripts() {
698 - if ( $this->lazy_render_script ) {
699 - ?>
700 - <script type="text/javascript">
701 - var visualizerUserInteractionEvents = [
702 - "mouseover",
703 - "keydown",
704 - "touchmove",
705 - "touchstart"
706 - ];
707 -
708 - visualizerUserInteractionEvents.forEach(function(event) {
709 - window.addEventListener(event, visualizerTriggerScriptLoader, { passive: true });
710 - });
711 -
712 - function visualizerTriggerScriptLoader() {
713 - visualizerLoadScripts();
714 - visualizerUserInteractionEvents.forEach(function(event) {
715 - window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true });
716 - });
717 - }
718 -
719 - function visualizerLoadScripts() {
720 - 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");
725 - setTimeout( function() {
726 - visualizerRefreshChart();
727 - } );
728 - } );
729 - });
730 - }
731 -
732 - function visualizerRefreshChart() {
733 - jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
734 - if ( jQuery( 'div.viz-facade-loaded:not(.visualizer-lazy):empty' ).length > 0 ) {
735 - visualizerUserInteractionEvents.forEach( function( event ) {
736 - window.addEventListener( event, function() {
737 - jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
738 - }, { passive: true } );
739 - } );
740 - }
741 - }
742 - </script>
743 - <?php
744 - }
745 610 }
746 611 }