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 +584 -63 3.1.04.0.8 View file →
@@ -41,8 +41,16 @@
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 = true;
51 +
52 + /**
45 53 * Constructor.
46 54 *
47 55 * @since 1.0.0
48 56 * @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters.
@@ -52,9 +60,12 @@
52 60 */
53 61 public function __construct( Visualizer_Plugin $plugin ) {
54 62 parent::__construct( $plugin );
55 63
64 + $this->_addAction( 'wp_print_footer_scripts', 'printFooterScripts' );
56 65 $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' );
66 + $this->_addAction( 'load-index.php', 'enqueueScripts' );
67 + $this->_addAction( 'load-visualizer_page_visualizer-setup-wizard', 'enqueueScripts' );
57 68 $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' );
58 69 $this->_addFilter( 'visualizer_get_language', 'getLanguage' );
59 70 $this->_addShortcode( 'visualizer', 'renderChart' );
60 71
@@ -68,14 +79,53 @@
68 79 add_filter( 'term_description', 'do_shortcode' );
69 80 }
70 81
71 82 add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
83 +
84 + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 );
72 85 }
73 86
74 87 /**
88 + * Adds the async attribute to certain scripts.
89 + */
90 + public function script_loader_tag( $tag, $handle, $src ) {
91 + if ( is_admin() ) {
92 + return $tag;
93 + }
94 + // Async scripts.
95 + $async_scripts = array( 'google-jsapi', 'chartjs', 'visualizer-datatables' );
96 + foreach ( $async_scripts as $async ) {
97 + if ( $async === $handle ) {
98 + $tag = str_replace( ' src', ' async src', $tag );
99 + break;
100 + }
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 + }
121 + return $tag;
122 + }
123 +
124 + /**
75 125 * Returns the language/locale.
76 126 */
77 - function getLanguage( $dummy, $only_language ) {
127 + public function getLanguage( $dummy, $only_language ) {
78 128 return $this->get_language();
79 129 }
80 130
81 131
@@ -81,14 +131,49 @@
81 131
82 132 /**
83 133 * Registers the endpoints
84 134 */
85 - function endpoint_register() {
135 + public function endpoint_register() {
86 136 register_rest_route(
87 137 'visualizer/v' . VISUALIZER_REST_VERSION,
88 138 '/action/(?P<chart>\d+)/(?P<type>.+)/',
89 139 array(
90 - 'methods' => array( 'GET', 'POST' ),
140 + // POST is required for save/cancel, GET for all others
141 + 'methods' => array( 'POST', 'GET' ),
142 + 'args' => array(
143 + 'chart' => array(
144 + 'required' => true,
145 + 'sanitize_callback' => function ( $param ) {
146 + return is_numeric( $param ) ? $param : null;
147 + },
148 + ),
149 + 'type' => array(
150 + 'required' => true,
151 + 'type' => 'string',
152 + 'enum' => array_merge( array( 'save', 'cancel' ), array_keys( $this->get_actions() ) ),
153 + ),
154 + ),
155 + 'permission_callback' => function ( WP_REST_Request $request ) {
156 + $chart_id = absint( $request->get_param( 'chart' ) );
157 + if ( ! $chart_id ) {
158 + return false;
159 + }
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 );
175 + },
91 176 'callback' => array( $this, 'perform_action' ),
92 177 )
93 178 );
94 179 }
@@ -98,17 +183,42 @@
98 183 *
99 184 * @access private
100 185 */
101 186 private function get_actions() {
102 - return apply_filters(
187 + $actions = apply_filters(
103 188 'visualizer_action_buttons',
104 189 array(
105 - 'print' => __( 'Print', 'visualizer' ),
106 - 'csv' => __( 'CSV', 'visualizer' ),
107 - 'xls' => __( 'Excel', 'visualizer' ),
108 - 'copy' => __( 'Copy', 'visualizer' ),
190 + 'print' => array(
191 + 'label' => esc_html__( 'Print', 'visualizer' ),
192 + 'title' => esc_html__( 'Print Chart', 'visualizer' ),
193 + ),
194 + 'csv' => array(
195 + 'label' => esc_html__( 'CSV', 'visualizer' ),
196 + 'title' => esc_html__( 'Download as a CSV', 'visualizer' ),
197 + ),
198 + 'xls' => array(
199 + 'label' => esc_html__( 'Excel', 'visualizer' ),
200 + 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ),
201 + ),
202 + 'copy' => array(
203 + 'label' => esc_html__( 'Copy', 'visualizer' ),
204 + 'title' => esc_html__( 'Copy data', 'visualizer' ),
205 + ),
206 + 'image' => array(
207 + 'label' => esc_html__( 'Download', 'visualizer' ),
208 + 'title' => esc_html__( 'Download as an image', 'visualizer' ),
209 + ),
109 210 )
110 211 );
212 +
213 + // backward compatibility before label and title attributes were introduced.
214 + if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) {
215 + $actions['edit'] = array(
216 + 'label' => esc_html__( 'Edit', 'visualizer' ),
217 + 'title' => esc_html__( 'Edit data', 'visualizer' ),
218 + );
219 + }
220 + return $actions;
111 221 }
112 222
113 223 /**
114 224 * The print button
@@ -139,8 +249,23 @@
139 249 break;
140 250 case 'xls':
141 251 $data = $this->_getDataAs( $chart_id, 'xls' );
142 252 break;
253 + case 'image':
254 + $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
255 + $title = 'visualizer#' . $chart_id;
256 + if ( ! empty( $settings['title'] ) ) {
257 + $title = $settings['title'];
258 + }
259 + // for ChartJS, title is an array.
260 + if ( is_array( $title ) && isset( $title['text'] ) ) {
261 + $title = $title['text'];
262 + }
263 + if ( empty( $title ) ) {
264 + $title = 'visualizer#' . $chart_id;
265 + }
266 + $data = array( 'name' => $title );
267 + break;
143 268 default:
144 269 $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
145 270 break;
146 271 }
@@ -157,10 +282,8 @@
157 282 * @access public
158 283 */
159 284 public function enqueueScripts() {
160 285 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
161 - wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
162 - wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION );
163 286 do_action( 'visualizer_pro_frontend_load_resources' );
164 287 }
165 288
166 289 /**
@@ -179,63 +302,110 @@
179 302 public function renderChart( $atts ) {
180 303 global $wp_version;
181 304 $atts = shortcode_atts(
182 305 array(
183 - 'id' => false, // chart id
184 - 'class' => false, // chart class
185 - 'series' => false, // series filter hook
186 - 'data' => false, // data filter hook
187 - 'settings' => false, // data filter hook
306 + // chart id
307 + 'id' => false,
308 + // chart class
309 + 'class' => false,
310 + // for lazy load
311 + // set 'yes' to use the default intersection limit (300px)
312 + // OR set a number (e.g. 700) to use 700px as the intersection limit
313 + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
314 + // Use image chart
315 + 'use_image' => 'no',
188 316 ),
189 317 $atts
190 318 );
191 319
192 - // if empty id or chart does not exists, then return empty string
193 - if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
320 + $atts['id'] = (int) $atts['id'];
321 + $atts['class'] = esc_attr( $atts['class'] );
322 + $atts['lazy'] = esc_attr( $atts['lazy'] );
323 + $atts['use_image'] = esc_attr( $atts['use_image'] );
324 +
325 + global $sitepress;
326 + if ( Visualizer_Module::is_pro() && ( function_exists( 'icl_get_languages' ) && $sitepress instanceof \SitePress ) ) {
327 + global $sitepress;
328 + $locale = icl_get_current_language();
329 + $locale = strtolower( str_replace( '_', '-', $locale ) );
330 + $trid = $sitepress->get_element_trid( $atts['id'], 'post_' . Visualizer_Plugin::CPT_VISUALIZER );
331 + $translations = $sitepress->get_element_translations( $trid );
332 + if ( isset( $translations[ $locale ] ) && is_object( $translations[ $locale ] ) ) {
333 + $atts['id'] = $translations[ $locale ]->element_id;
334 + }
335 + }
336 +
337 + $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] );
338 + // if empty chart does not exists, then return empty string.
339 + if ( ! $chart_data ) {
194 340 return '';
195 341 }
196 342
343 + $chart = $chart_data['chart'];
344 + $type = $chart_data['type'];
345 + $series = $chart_data['series'];
346 + // do not show the chart?
197 347 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
198 348 return '';
199 349 }
200 350
201 - // in case revisions exist.
202 - if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
203 - $chart = get_post( $chart->ID );
351 + if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) {
352 + return '';
204 353 }
205 354
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 + }
362 + }
363 +
206 364 $id = 'visualizer-' . $atts['id'];
207 365 $defaultClass = 'visualizer-front';
208 366 $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );
209 - $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id'];
210 - $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : '';
211 367
212 - $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
368 + $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : '';
213 369
370 + $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass );
371 + $attributes = array();
372 + if ( ! empty( $class ) ) {
373 + $attributes['class'] = trim( $class );
374 + }
375 +
376 + if ( ctype_digit( $atts['lazy'] ) ) {
377 + $attributes['data-lazy-limit'] = $atts['lazy'];
378 + }
379 +
380 + $attributes = apply_filters( 'visualizer_container_attributes', $attributes, $chart->ID );
381 +
214 382 $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false );
215 383
216 - // fetch and update settings
217 - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
384 + // Get and update settings.
385 + $settings = $chart_data['settings'];
386 + $settings = ! empty( $settings ) ? $settings : array();
218 387 if ( empty( $settings['height'] ) ) {
219 388 $settings['height'] = '400';
220 389 }
221 390
222 391 // handle series filter hooks
223 - $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
224 - if ( ! empty( $atts['series'] ) ) {
225 - $series = apply_filters( $atts['series'], $series, $chart->ID, $type );
226 - }
392 + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type );
393 +
227 394 // handle settings filter hooks
228 395 $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
229 - if ( ! empty( $atts['settings'] ) ) {
230 - $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
396 +
397 + $lazy_load = isset( $settings['lazy_load_chart'] ) ? $settings['lazy_load_chart'] : false;
398 + $lazy_load = apply_filters( 'visualizer_lazy_load_chart', $lazy_load, $chart->ID );
399 + $container_class = 'visualizer-front-container';
400 + if ( ! $lazy_load ) {
401 + $this->lazy_render_script = false;
402 + } else {
403 + $container_class .= ' visualizer-lazy-render';
231 404 }
232 405
233 406 // handle data filter hooks
234 - $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
235 - if ( ! empty( $atts['data'] ) ) {
236 - $data = apply_filters( $atts['data'], $data, $chart->ID, $type );
237 - }
407 + $data = self::get_chart_data( $chart, $type );
238 408
239 409 $css = '';
240 410 $arguments = $this->get_inline_custom_css( $id, $settings );
241 411 if ( ! empty( $arguments ) ) {
@@ -244,10 +414,41 @@
244 414 }
245 415
246 416 $library = $this->load_chart_type( $chart->ID );
247 417
418 + $id = $id . '-' . rand();
419 +
420 + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME );
421 + if ( $amp && $amp->is_amp() ) {
422 + return '<div data-amp-auto-lightbox-disable id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>';
423 + }
424 +
425 + if ( 'yes' === $atts['use_image'] ) {
426 + $chart_image = $chart_data['chart_image'];
427 + if ( $chart_image ) {
428 + return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>';
429 + }
430 + }
431 + // Unset chart image base64 string.
432 + if ( isset( $settings['chart-img'] ) ) {
433 + unset( $settings['chart-img'] );
434 + }
435 +
436 + $enable_controls = false;
437 + if ( isset( $settings['controls'] ) && ! empty( $settings['controls']['controlType'] ) ) {
438 + $column_index = $settings['controls']['filterColumnIndex'];
439 + $column_label = $settings['controls']['filterColumnLabel'];
440 + if ( 'false' !== $column_index || 'false' !== $column_label ) {
441 + $enable_controls = true;
442 + }
443 + }
444 +
445 + if ( ! $enable_controls ) {
446 + unset( $settings['controls'] );
447 + }
448 +
248 449 // add chart to the array
249 - $this->_charts[ $id ] = array(
450 + $chart_entry = array(
250 451 'type' => $type,
251 452 'series' => $series,
252 453 'settings' => $settings,
253 454 'data' => $data,
@@ -253,32 +454,14 @@
253 454 'data' => $data,
254 455 'library' => $library,
255 456 );
256 457
257 - wp_register_script(
258 - "visualizer-render-$library",
259 - VISUALIZER_ABSURL . 'js/render-facade.js',
260 - apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
261 - Visualizer_Plugin::VERSION,
262 - true
263 - );
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 + }
264 462
265 - wp_enqueue_script( "visualizer-render-$library" );
266 - wp_localize_script(
267 - "visualizer-render-$library",
268 - 'visualizer',
269 - array(
270 - 'charts' => $this->_charts,
271 - 'language' => $this->get_language(),
272 - 'map_api_key' => get_option( 'visualizer-map-api-key' ),
273 - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
274 - 'i10n' => array(
275 - 'copied' => __( 'Copied!', 'visualizer' ),
276 - ),
277 - 'page_type' => 'frontend',
278 - )
279 - );
280 - wp_enqueue_style( 'visualizer-front' );
463 + $this->_charts[ $id ] = $chart_entry;
281 464
282 465 $actions_div = '';
283 466 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
284 467 if ( ! empty( $actions_visible ) ) {
@@ -291,15 +474,16 @@
291 474 $array = explode( ';', $action_type );
292 475 $key = $array[0];
293 476 $mime = end( $array );
294 477 }
295 - $label = $actions[ $key ];
296 - $actions_div .= '<a href="#" class="visualizer-action visualizer-action-' . $key . '" data-visualizer-type="' . $key . '" data-visualizer-chart-id="' . $atts['id'] . '" data-visualizer-mime="' . $mime . '" title="' . $label . '" ';
478 + $label = $actions[ $key ]['label'];
479 + $title = $actions[ $key ]['title'];
480 + $actions_div .= sprintf( '<a href="#" class="visualizer-action visualizer-action-%s" data-visualizer-type="%s" data-visualizer-chart-id="%s" data-visualizer-container-id="%s" data-visualizer-mime="%s" title="%s"', $key, $key, $atts['id'], $id, $mime, $title );
297 481
298 482 if ( 'copy' === $key ) {
299 483 $copy = $this->_getDataAs( $atts['id'], 'csv' );
300 484 $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
301 - wp_enqueue_script( 'visualizer-clipboardjs' );
485 + wp_enqueue_script( 'clipboard' );
302 486 }
303 487
304 488 $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] );
305 489 $actions_div .= '>' . $label . '</a> &nbsp;';
@@ -309,8 +493,345 @@
309 493 }
310 494
311 495 $actions_div .= $css;
312 496
497 + $_charts = array();
498 + $_charts_type = '';
499 + $count = 0;
500 + static $visualizer_localized = false;
501 + static $visualizer_charts = array();
502 + static $d3_localized = false;
503 + $facade_handle = 'visualizer-render-facade';
504 + foreach ( $this->_charts as $id => $array ) {
505 + $_charts = $this->_charts;
506 + $library = $array['library'];
507 + $_charts_type = $library;
508 + $facade_deps = apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true );
509 + if ( ! wp_script_is( $facade_handle, 'registered' ) ) {
510 + wp_register_script(
511 + $facade_handle,
512 + VISUALIZER_ABSURL . 'js/render-facade.js',
513 + $facade_deps,
514 + Visualizer_Plugin::VERSION,
515 + true
516 + );
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 + }
529 + }
530 +
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 + }
560 + }
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;
596 + }
597 + $prefix = 'C' . 'h' . 'a' . 'rt';
598 + if ( $type === 'tabular' ) {
599 + $prefix = 'T' . 'a' . 'bl' . 'e';
600 + }
601 + if ( Visualizer_Module::is_pro() && $enable_controls ) {
602 + $actions_div .= '<div id="control_wrapper_' . $id . '"></div>';
603 + }
313 604 // return placeholder div
314 - return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
605 + return '<div class="' . $container_class . '" id="chart_wrapper_' . $id . '">' . $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ) . '</div>';
606 + }
607 +
608 + /**
609 + * Converts the array of attributes to a string of HTML attributes.
610 + *
611 + * @since ?
612 + *
613 + * @access private
614 + */
615 + private function getHtmlAttributes( $attributes ) {
616 + $string = '';
617 + if ( ! $attributes ) {
618 + return $string;
619 + }
620 +
621 + foreach ( $attributes as $name => $value ) {
622 + $string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) );
623 + }
624 + return $string;
625 + }
626 +
627 + /**
628 + * Adds the schema corresponding to the dataset.
629 + *
630 + * @since 3.3.2
631 + *
632 + * @access private
633 + */
634 + private function addSchema( $id ) {
635 + // should we show informative errors as HTML comments?
636 + $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id );
637 +
638 + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true );
639 + $title = '';
640 + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
641 + $title = $settings['title'];
642 + if ( is_array( $title ) ) {
643 + $title = $settings['title']['text'];
644 + }
645 + }
646 + $title = apply_filters( 'visualizer_schema_name', $title, $id );
647 + if ( empty( $title ) ) {
648 + if ( $show_errors ) {
649 + return "<!-- Not showing structured data for chart $id because title is empty -->";
650 + }
651 + return '';
652 + }
653 +
654 + $desc = '';
655 + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) {
656 + $desc = $settings['description'];
657 + }
658 + $desc = apply_filters( 'visualizer_schema_description', $desc, $id );
659 + if ( empty( $desc ) ) {
660 + if ( $show_errors ) {
661 + return "<!-- Not showing structured data for chart $id because description is empty -->";
662 + }
663 + return '';
664 + }
665 +
666 + // descriptions below 50 chars are not allowed.
667 + if ( strlen( $desc ) < 50 ) {
668 + if ( $show_errors ) {
669 + return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->";
670 + }
671 + return '';
672 + }
673 +
674 + $license = '';
675 + if ( isset( $settings['license'] ) && ! empty( $settings['license'] ) ) {
676 + $license = $settings['license'];
677 + if ( is_array( $license ) ) {
678 + $license = $settings['license']['text'];
679 + }
680 + }
681 + $license = apply_filters( 'visualizer_schema_license', $license, $id );
682 + if ( empty( $license ) ) {
683 + if ( $show_errors ) {
684 + return "<!-- Not showing structured data for chart $id because license is empty -->";
685 + }
686 + return '';
687 + }
688 +
689 + $creator = '';
690 + if ( isset( $settings['creator'] ) && ! empty( $settings['creator'] ) ) {
691 + $creator = $settings['creator'];
692 + if ( is_array( $creator ) ) {
693 + $creator = $settings['creator']['text'];
694 + }
695 + }
696 + $creator = apply_filters( 'visualizer_schema_creator', $creator, $id );
697 + if ( empty( $creator ) ) {
698 + if ( $show_errors ) {
699 + return "<!-- Not showing structured data for chart $id because creator is empty -->";
700 + }
701 + return '';
702 + }
703 +
704 + $schema = apply_filters(
705 + 'visualizer_schema',
706 + '{
707 + "@context":"https://schema.org/",
708 + "@type":"Dataset",
709 + "name":"' . esc_html( $title ) . '",
710 + "description":"' . esc_html( $desc ) . '",
711 + "license": "' . esc_html( $license ) . '",
712 + "creator": {
713 + "@type": "Person",
714 + "name": "' . esc_html( $creator ) . '"
715 + }
716 + }',
717 + $id
718 + );
719 +
720 + if ( empty( $schema ) ) {
721 + return '';
722 + }
723 +
724 + return '<script type="application/ld+json">' . $schema . '</script>';
725 + }
726 +
727 + /**
728 + * Get chart by ID.
729 + *
730 + * @param string $cache_key Cache key.
731 + * @param int $chart_id Chart ID.
732 + * @return mixed
733 + */
734 + private function getChartData( $cache_key = '', $chart_id = 0 ) {
735 + if ( ! $chart_id ) {
736 + return false;
737 + }
738 + // Create unique cache key of each chart.
739 + $cache_key .= '_' . $chart_id;
740 + // Get chart from cache.
741 + $chart = get_transient( $cache_key );
742 + if ( $chart ) {
743 + return $chart;
744 + }
745 +
746 + // Get chart by ID.
747 + $chart = get_post( $chart_id );
748 + if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) {
749 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
750 + $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
751 + $is_woocommerce_report = get_post_meta( $chart->ID, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
752 +
753 + if ( isset( $settings['series'] ) && is_array( $settings['series'] ) && is_array( $series ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
754 + $diff_total_series = abs( count( $settings['series'] ) - count( $series ) );
755 + if ( $diff_total_series ) {
756 + foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) {
757 + $settings['series'][] = end( $settings['series'] );
758 + }
759 + }
760 + }
761 + $chart_data = array(
762 + 'chart' => $chart,
763 + 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
764 + 'settings' => $settings,
765 + 'series' => $series,
766 + 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
767 + 'is_woocommerce_report' => $is_woocommerce_report,
768 + );
769 +
770 + // Put the results in a transient. Expire after 12 hours.
771 + set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) );
772 + return $chart_data;
773 + }
774 +
775 + return false;
776 + }
777 +
778 + /**
779 + * Print footer script.
780 + */
781 + public function printFooterScripts() {
782 + if ( $this->lazy_render_script ) {
783 + ?>
784 + <script type="text/javascript">
785 + var visualizerUserInteractionEvents = [
786 + "scroll",
787 + "mouseover",
788 + "keydown",
789 + "touchmove",
790 + "touchstart"
791 + ];
792 +
793 + visualizerUserInteractionEvents.forEach(function(event) {
794 + window.addEventListener(event, visualizerTriggerScriptLoader, { passive: true });
795 + });
796 +
797 + function visualizerTriggerScriptLoader() {
798 + visualizerLoadScripts();
799 + visualizerUserInteractionEvents.forEach(function(event) {
800 + window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true });
801 + });
802 + }
803 +
804 + function visualizerLoadScripts() {
805 + document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) {
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() {
815 + setTimeout( function() {
816 + visualizerRefreshChart();
817 + } );
818 + };
819 + elem.parentNode.replaceChild(script, elem);
820 + });
821 + }
822 +
823 + function visualizerRefreshChart() {
824 + jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
825 + if ( jQuery( 'div.viz-facade-loaded:not(.visualizer-lazy):empty' ).length > 0 ) {
826 + visualizerUserInteractionEvents.forEach( function( event ) {
827 + window.addEventListener( event, function() {
828 + jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
829 + }, { passive: true } );
830 + } );
831 + }
832 + }
833 + </script>
834 + <?php
835 + }
315 836 }
316 837 }