@@ -41,6 +41,14 @@ class Visualizer_Module_Frontend extends Visualizer_Module {
4141 */
4242 private $ _charts = array ();
4343
44+ /**
45+ * Lazy render script.
46+ *
47+ * @access private
48+ * @var bool
49+ */
50+ private $ lazy_render_script = false ;
51+
4452 /**
4553 * Constructor.
4654 *
@@ -53,6 +61,7 @@ class Visualizer_Module_Frontend extends Visualizer_Module {
5361 public function __construct ( Visualizer_Plugin $ plugin ) {
5462 parent ::__construct ( $ plugin );
5563
64+ $ this ->_addAction ( 'wp_print_footer_scripts ' , 'printFooterScripts ' );
5665 $ this ->_addAction ( 'wp_enqueue_scripts ' , 'enqueueScripts ' );
5766 $ this ->_addAction ( 'load-index.php ' , 'enqueueScripts ' );
5867 $ this ->_addAction ( 'visualizer_enqueue_scripts ' , 'enqueueScripts ' );
@@ -81,14 +90,32 @@ function script_loader_tag( $tag, $handle, $src ) {
8190 if ( is_admin () ) {
8291 return $ tag ;
8392 }
84-
85- $ scripts = array ( 'google-jsapi ' , 'visualizer-render-google-lib ' , 'visualizer-render-google ' );
86-
87- foreach ( $ scripts as $ async ) {
93+ // Async scripts.
94+ $ async_scripts = array ( 'google-jsapi ' , 'chartjs ' , 'visualizer-datatables ' );
95+ foreach ( $ async_scripts as $ async ) {
8896 if ( $ async === $ handle ) {
89- $ tag = str_replace ( ' src ' , ' defer="defer" src ' , $ tag );
97+ $ tag = str_replace ( ' src ' , ' async src ' , $ tag );
98+ break ;
99+ }
100+ };
101+
102+ // Async scripts.
103+ $ scripts = array ( 'dom-to-image ' );
104+ foreach ( array ( 'async ' , 'defer ' ) as $ attr ) {
105+ if ( wp_scripts ()->get_data ( $ handle , $ attr ) ) {
106+ break ;
107+ }
108+ if ( in_array ( $ handle , $ async_scripts , true ) || false === strpos ( $ handle , 'visualizer- ' ) ) {
90109 break ;
91110 }
111+ if ( ! preg_match ( ":\s $ attr(=|>|\s): " , $ tag ) ) {
112+ $ tag = preg_replace ( ':(?=></script>): ' , " $ attr " , $ tag , 1 );
113+ if ( $ this ->lazy_render_script ) {
114+ $ tag = str_replace ( ' src ' , ' data-visualizer-script ' , $ tag );
115+ }
116+ }
117+ // Only allow async or defer, not both.
118+ break ;
92119 }
93120 return $ tag ;
94121 }
@@ -244,7 +271,6 @@ public function perform_action( WP_REST_Request $params ) {
244271 */
245272 public function enqueueScripts () {
246273 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 );
248274 do_action ( 'visualizer_pro_frontend_load_resources ' );
249275 }
250276
@@ -329,6 +355,14 @@ public function renderChart( $atts ) {
329355 // handle settings filter hooks
330356 $ settings = apply_filters ( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS , $ settings , $ chart ->ID , $ type );
331357
358+ $ lazy_load = isset ( $ settings ['lazy_load_chart ' ] ) ? $ settings ['lazy_load_chart ' ] : false ;
359+ $ lazy_load = apply_filters ( 'visualizer_lazy_load_chart ' , $ lazy_load , $ chart ->ID );
360+ $ container_class = 'visualizer-front-container ' ;
361+ if ( $ lazy_load ) {
362+ $ this ->lazy_render_script = true ;
363+ $ container_class .= ' visualizer-lazy-render ' ;
364+ }
365+
332366 // handle data filter hooks
333367 $ data = self ::get_chart_data ( $ chart , $ type );
334368
@@ -435,10 +469,9 @@ public function renderChart( $atts ) {
435469 )
436470 );
437471 }
438- wp_enqueue_style ( 'visualizer-front ' );
439472
440473 // return placeholder div
441- return $ actions_div . '<div id=" ' . $ id . '" ' . $ this ->getHtmlAttributes ( $ attributes ) . '></div> ' . $ this ->addSchema ( $ chart ->ID );
474+ return ' <div class=" ' . $ container_class . ' "> ' . $ actions_div . '<div id=" ' . $ id . '" ' . $ this ->getHtmlAttributes ( $ attributes ) . '></div> ' . $ this ->addSchema ( $ chart ->ID ) . ' </div> ' ;
442475 }
443476
444477 /**
@@ -608,4 +641,42 @@ private function getChartData( $cache_key = '', $chart_id = 0 ) {
608641
609642 return false ;
610643 }
644+
645+ /**
646+ * Print footer script.
647+ */
648+ public function printFooterScripts () {
649+ if ( $ this ->lazy_render_script ) {
650+ ?>
651+ <script type="text/javascript">
652+ var visualizerUserInteractionEvents = [
653+ "mouseover",
654+ "keydown",
655+ "touchmove",
656+ "touchstart"
657+ ];
658+
659+ visualizerUserInteractionEvents.forEach(function(event) {
660+ window.addEventListener(event, visualizerTriggerScriptLoader, { passive: true });
661+ });
662+
663+ function visualizerTriggerScriptLoader() {
664+ visualizerLoadScripts();
665+ visualizerUserInteractionEvents.forEach(function(event) {
666+ window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true });
667+ });
668+ }
669+
670+ function visualizerLoadScripts() {
671+ document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) {
672+ jQuery.getScript( elem.getAttribute("data-visualizer-script"), function() {
673+ elem.setAttribute("src", elem.getAttribute("data-visualizer-script"));
674+ elem.removeAttribute("data-visualizer-script");
675+ } );
676+ });
677+ }
678+ </script>
679+ <?php
680+ }
681+ }
611682}
0 commit comments