PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.11.14
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.11.14
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 +511 -57 3.0.73.11.14 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,13 @@
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' );
68 + $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' );
57 69 $this->_addFilter( 'visualizer_get_language', 'getLanguage' );
58 70 $this->_addShortcode( 'visualizer', 'renderChart' );
59 71
60 72 // add do_shortocde hook for widget_text filter
@@ -67,11 +79,50 @@
67 79 add_filter( 'term_description', 'do_shortcode' );
68 80 }
69 81
70 82 add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
83 +
84 + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 );
71 85 }
72 86
73 87 /**
88 + * Adds the async attribute to certain scripts.
89 + */
90 + 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 + /**
74 125 * Returns the language/locale.
75 126 */
76 127 function getLanguage( $dummy, $only_language ) {
77 128 return $this->get_language();
@@ -85,9 +136,33 @@
85 136 register_rest_route(
86 137 'visualizer/v' . VISUALIZER_REST_VERSION,
87 138 '/action/(?P<chart>\d+)/(?P<type>.+)/',
88 139 array(
89 - '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 = 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;
162 + }
163 + return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id );
164 + },
90 165 'callback' => array( $this, 'perform_action' ),
91 166 )
92 167 );
93 168 }
@@ -97,16 +172,42 @@
97 172 *
98 173 * @access private
99 174 */
100 175 private function get_actions() {
101 - return apply_filters(
102 - 'visualizer_action_buttons', array(
103 - 'print' => __( 'Print', 'visualizer' ),
104 - 'csv' => __( 'CSV', 'visualizer' ),
105 - 'xls' => __( 'Excel', 'visualizer' ),
106 - 'copy' => __( 'Copy', 'visualizer' ),
176 + $actions = apply_filters(
177 + 'visualizer_action_buttons',
178 + array(
179 + 'print' => array(
180 + 'label' => esc_html__( 'Print', 'visualizer' ),
181 + 'title' => esc_html__( 'Print Chart', 'visualizer' ),
182 + ),
183 + 'csv' => array(
184 + 'label' => esc_html__( 'CSV', 'visualizer' ),
185 + 'title' => esc_html__( 'Download as a CSV', 'visualizer' ),
186 + ),
187 + 'xls' => array(
188 + 'label' => esc_html__( 'Excel', 'visualizer' ),
189 + 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ),
190 + ),
191 + 'copy' => array(
192 + 'label' => esc_html__( 'Copy', 'visualizer' ),
193 + 'title' => esc_html__( 'Copy data', 'visualizer' ),
194 + ),
195 + 'image' => array(
196 + 'label' => esc_html__( 'Download', 'visualizer' ),
197 + 'title' => esc_html__( 'Download as an image', 'visualizer' ),
198 + ),
107 199 )
108 200 );
201 +
202 + // backward compatibility before label and title attributes were introduced.
203 + if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) {
204 + $actions['edit'] = array(
205 + 'label' => esc_html__( 'Edit', 'visualizer' ),
206 + 'title' => esc_html__( 'Edit data', 'visualizer' ),
207 + );
208 + }
209 + return $actions;
109 210 }
110 211
111 212 /**
112 213 * The print button
@@ -137,8 +238,23 @@
137 238 break;
138 239 case 'xls':
139 240 $data = $this->_getDataAs( $chart_id, 'xls' );
140 241 break;
242 + case 'image':
243 + $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
244 + $title = 'visualizer#' . $chart_id;
245 + if ( ! empty( $settings['title'] ) ) {
246 + $title = $settings['title'];
247 + }
248 + // for ChartJS, title is an array.
249 + if ( is_array( $title ) && isset( $title['text'] ) ) {
250 + $title = $title['text'];
251 + }
252 + if ( empty( $title ) ) {
253 + $title = 'visualizer#' . $chart_id;
254 + }
255 + $data = array( 'name' => $title );
256 + break;
141 257 default:
142 258 $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
143 259 break;
144 260 }
@@ -154,13 +270,9 @@
154 270 *
155 271 * @access public
156 272 */
157 273 public function enqueueScripts() {
158 - wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
159 - wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true );
160 - wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi-old', 'jquery' ), Visualizer_Plugin::VERSION, 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 );
274 + wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
163 275 do_action( 'visualizer_pro_frontend_load_resources' );
164 276 }
165 277
166 278 /**
@@ -179,62 +291,148 @@
179 291 public function renderChart( $atts ) {
180 292 global $wp_version;
181 293 $atts = shortcode_atts(
182 294 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
188 - ), $atts
295 + // chart id
296 + 'id' => false,
297 + // chart class
298 + 'class' => false,
299 + // for lazy load
300 + // set 'yes' to use the default intersection limit (300px)
301 + // OR set a number (e.g. 700) to use 700px as the intersection limit
302 + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
303 + // Use image chart
304 + 'use_image' => 'no',
305 + ),
306 + $atts
189 307 );
190 308
191 - // if empty id or chart does not exists, then return empty string
192 - if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
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 + $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] );
327 + // if empty chart does not exists, then return empty string.
328 + if ( ! $chart_data ) {
193 329 return '';
194 330 }
195 331
332 + $chart = $chart_data['chart'];
333 + $type = $chart_data['type'];
334 + $series = $chart_data['series'];
335 + // do not show the chart?
196 336 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
197 337 return '';
198 338 }
199 339
340 + if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) {
341 + return '';
342 + }
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 );
348 + }
349 +
200 350 $id = 'visualizer-' . $atts['id'];
201 351 $defaultClass = 'visualizer-front';
202 352 $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );
203 - $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id'];
204 - $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : '';
205 353
206 - $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
354 + $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : '';
207 355
208 - // faetch and update settings
209 - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
356 + $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass );
357 + $attributes = array();
358 + if ( ! empty( $class ) ) {
359 + $attributes['class'] = trim( $class );
360 + }
361 +
362 + if ( ctype_digit( $atts['lazy'] ) ) {
363 + $attributes['data-lazy-limit'] = $atts['lazy'];
364 + }
365 +
366 + $attributes = apply_filters( 'visualizer_container_attributes', $attributes, $chart->ID );
367 +
368 + $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false );
369 +
370 + // Get and update settings.
371 + $settings = $chart_data['settings'];
372 + $settings = ! empty( $settings ) ? $settings : array();
210 373 if ( empty( $settings['height'] ) ) {
211 374 $settings['height'] = '400';
212 375 }
213 376
214 377 // handle series filter hooks
215 - $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
216 - if ( ! empty( $atts['series'] ) ) {
217 - $series = apply_filters( $atts['series'], $series, $chart->ID, $type );
218 - }
378 + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type );
379 +
219 380 // handle settings filter hooks
220 381 $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
221 - if ( ! empty( $atts['settings'] ) ) {
222 - $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
382 +
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 = false;
388 + } else {
389 + $container_class .= ' visualizer-lazy-render';
223 390 }
224 391
225 392 // handle data filter hooks
226 - $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
227 - if ( ! empty( $atts['data'] ) ) {
228 - $data = apply_filters( $atts['data'], $data, $chart->ID, $type );
393 + $data = self::get_chart_data( $chart, $type );
394 +
395 + $css = '';
396 + $arguments = $this->get_inline_custom_css( $id, $settings );
397 + if ( ! empty( $arguments ) ) {
398 + $css = $arguments[0];
399 + $settings = $arguments[1];
229 400 }
230 401
402 + $library = $this->load_chart_type( $chart->ID );
403 +
231 404 $id = $id . '-' . rand();
232 - $arguments = array( '', $id, $settings );
233 - apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) );
234 - $css = $arguments[0];
235 - $settings = $arguments[2];
236 405
406 + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME );
407 + if ( $amp && $amp->is_amp() ) {
408 + return '<div data-amp-auto-lightbox-disable id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>';
409 + }
410 +
411 + if ( 'yes' === $atts['use_image'] ) {
412 + $chart_image = $chart_data['chart_image'];
413 + if ( $chart_image ) {
414 + return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>';
415 + }
416 + }
417 + // Unset chart image base64 string.
418 + if ( isset( $settings['chart-img'] ) ) {
419 + unset( $settings['chart-img'] );
420 + }
421 +
422 + $enable_controls = false;
423 + if ( isset( $settings['controls'] ) && ! empty( $settings['controls']['controlType'] ) ) {
424 + $column_index = $settings['controls']['filterColumnIndex'];
425 + $column_label = $settings['controls']['filterColumnLabel'];
426 + if ( 'false' !== $column_index || 'false' !== $column_label ) {
427 + $enable_controls = true;
428 + }
429 + }
430 +
431 + if ( ! $enable_controls ) {
432 + unset( $settings['controls'] );
433 + }
434 +
237 435 // add chart to the array
238 436 $this->_charts[ $id ] = array(
239 437 'type' => $type,
240 438 'series' => $series,
@@ -239,25 +437,11 @@
239 437 'type' => $type,
240 438 'series' => $series,
241 439 'settings' => $settings,
242 440 'data' => $data,
441 + 'library' => $library,
243 442 );
244 443
245 - // enqueue visualizer render and update render localizations
246 - wp_enqueue_script( 'visualizer-render' );
247 - wp_localize_script(
248 - 'visualizer-render', 'visualizer', array(
249 - 'charts' => $this->_charts,
250 - 'language' => $this->get_language(),
251 - 'map_api_key' => get_option( 'visualizer-map-api-key' ),
252 - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
253 - 'i10n' => array(
254 - 'copied' => __( 'Copied!', 'visualizer' ),
255 - ),
256 - )
257 - );
258 - wp_enqueue_style( 'visualizer-front' );
259 -
260 444 $actions_div = '';
261 445 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
262 446 if ( ! empty( $actions_visible ) ) {
263 447 $actions = $this->get_actions();
@@ -269,15 +453,16 @@
269 453 $array = explode( ';', $action_type );
270 454 $key = $array[0];
271 455 $mime = end( $array );
272 456 }
273 - $label = $actions[ $key ];
274 - $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 . '" ';
457 + $label = $actions[ $key ]['label'];
458 + $title = $actions[ $key ]['title'];
459 + $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 );
275 460
276 461 if ( 'copy' === $key ) {
277 462 $copy = $this->_getDataAs( $atts['id'], 'csv' );
278 463 $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
279 - wp_enqueue_script( 'visualizer-clipboardjs' );
464 + wp_enqueue_script( 'clipboard' );
280 465 }
281 466
282 467 $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] );
283 468 $actions_div .= '>' . $label . '</a> &nbsp;';
@@ -287,8 +472,277 @@
287 472 }
288 473
289 474 $actions_div .= $css;
290 475
476 + $_charts = array();
477 + $_charts_type = '';
478 + $count = 0;
479 + foreach ( $this->_charts as $id => $array ) {
480 + $_charts = $this->_charts;
481 + $library = $array['library'];
482 + $_charts_type = $library;
483 + if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) {
484 + wp_register_script(
485 + "visualizer-render-$library",
486 + VISUALIZER_ABSURL . 'js/render-facade.js',
487 + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
488 + Visualizer_Plugin::VERSION,
489 + true
490 + );
491 + wp_enqueue_script( "visualizer-render-$library" );
492 + }
493 +
494 + if ( wp_script_is( "visualizer-render-$_charts_type" ) && 0 === $count ) {
495 + wp_localize_script(
496 + "visualizer-render-$_charts_type",
497 + 'visualizer',
498 + array(
499 + 'charts' => $this->_charts,
500 + 'language' => $this->get_language(),
501 + 'map_api_key' => get_option( 'visualizer-map-api-key' ),
502 + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
503 + 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
504 + 'i10n' => array(
505 + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
506 + ),
507 + 'page_type' => 'frontend',
508 + 'is_front' => true,
509 + )
510 + );
511 + }
512 + $count++;
513 + }
514 + $prefix = 'C' . 'h' . 'a' . 'rt';
515 + if ( $type === 'tabular' ) {
516 + $prefix = 'T' . 'a' . 'bl' . 'e';
517 + }
518 + if ( Visualizer_Module::is_pro() && $enable_controls ) {
519 + $actions_div .= '<div id="control_wrapper_' . $id . '"></div>';
520 + }
291 521 // return placeholder div
292 - return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
522 + return '<div class="' . $container_class . '" id="chart_wrapper_' . $id . '">' . $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ) . '</div>';
523 + }
524 +
525 + /**
526 + * Converts the array of attributes to a string of HTML attributes.
527 + *
528 + * @since ?
529 + *
530 + * @access private
531 + */
532 + private function getHtmlAttributes( $attributes ) {
533 + $string = '';
534 + if ( ! $attributes ) {
535 + return $string;
536 + }
537 +
538 + foreach ( $attributes as $name => $value ) {
539 + $string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) );
540 + }
541 + return $string;
542 + }
543 +
544 + /**
545 + * Adds the schema corresponding to the dataset.
546 + *
547 + * @since 3.3.2
548 + *
549 + * @access private
550 + */
551 + private function addSchema( $id ) {
552 + // should we show informative errors as HTML comments?
553 + $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id );
554 +
555 + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true );
556 + $title = '';
557 + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
558 + $title = $settings['title'];
559 + if ( is_array( $title ) ) {
560 + $title = $settings['title']['text'];
561 + }
562 + }
563 + $title = apply_filters( 'visualizer_schema_name', $title, $id );
564 + if ( empty( $title ) ) {
565 + if ( $show_errors ) {
566 + return "<!-- Not showing structured data for chart $id because title is empty -->";
567 + }
568 + return '';
569 + }
570 +
571 + $desc = '';
572 + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) {
573 + $desc = $settings['description'];
574 + }
575 + $desc = apply_filters( 'visualizer_schema_description', $desc, $id );
576 + if ( empty( $desc ) ) {
577 + if ( $show_errors ) {
578 + return "<!-- Not showing structured data for chart $id because description is empty -->";
579 + }
580 + return '';
581 + }
582 +
583 + // descriptions below 50 chars are not allowed.
584 + if ( strlen( $desc ) < 50 ) {
585 + if ( $show_errors ) {
586 + return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->";
587 + }
588 + return '';
589 + }
590 +
591 + $license = '';
592 + if ( isset( $settings['license'] ) && ! empty( $settings['license'] ) ) {
593 + $license = $settings['license'];
594 + if ( is_array( $license ) ) {
595 + $license = $settings['license']['text'];
596 + }
597 + }
598 + $license = apply_filters( 'visualizer_schema_license', $license, $id );
599 + if ( empty( $license ) ) {
600 + if ( $show_errors ) {
601 + return "<!-- Not showing structured data for chart $id because license is empty -->";
602 + }
603 + return '';
604 + }
605 +
606 + $creator = '';
607 + if ( isset( $settings['creator'] ) && ! empty( $settings['creator'] ) ) {
608 + $creator = $settings['creator'];
609 + if ( is_array( $creator ) ) {
610 + $creator = $settings['creator']['text'];
611 + }
612 + }
613 + $creator = apply_filters( 'visualizer_schema_creator', $creator, $id );
614 + if ( empty( $creator ) ) {
615 + if ( $show_errors ) {
616 + return "<!-- Not showing structured data for chart $id because creator is empty -->";
617 + }
618 + return '';
619 + }
620 +
621 + $schema = apply_filters(
622 + 'visualizer_schema',
623 + '{
624 + "@context":"https://schema.org/",
625 + "@type":"Dataset",
626 + "name":"' . esc_html( $title ) . '",
627 + "description":"' . esc_html( $desc ) . '",
628 + "license": "' . esc_html( $license ) . '",
629 + "creator": {
630 + "@type": "Person",
631 + "name": "' . esc_html( $creator ) . '"
632 + }
633 + }',
634 + $id
635 + );
636 +
637 + if ( empty( $schema ) ) {
638 + return '';
639 + }
640 +
641 + return '<script type="application/ld+json">' . $schema . '</script>';
642 + }
643 +
644 + /**
645 + * Get chart by ID.
646 + *
647 + * @param string $cache_key Cache key.
648 + * @param int $chart_id Chart ID.
649 + * @return mixed
650 + */
651 + private function getChartData( $cache_key = '', $chart_id = 0 ) {
652 + if ( ! $chart_id ) {
653 + return false;
654 + }
655 + // Create unique cache key of each chart.
656 + $cache_key .= '_' . $chart_id;
657 + // Get chart from cache.
658 + $chart = get_transient( $cache_key );
659 + if ( $chart ) {
660 + return $chart;
661 + }
662 +
663 + // Get chart by ID.
664 + $chart = get_post( $chart_id );
665 + if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) {
666 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
667 + $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
668 + $is_woocommerce_report = get_post_meta( $chart->ID, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
669 +
670 + if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
671 + $diff_total_series = abs( count( $settings['series'] ) - count( $series ) );
672 + if ( $diff_total_series ) {
673 + foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) {
674 + $settings['series'][] = end( $settings['series'] );
675 + }
676 + }
677 + }
678 + $chart_data = array(
679 + 'chart' => $chart,
680 + 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
681 + 'settings' => $settings,
682 + 'series' => $series,
683 + 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
684 + 'is_woocommerce_report' => $is_woocommerce_report,
685 + );
686 +
687 + // Put the results in a transient. Expire after 12 hours.
688 + set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) );
689 + return $chart_data;
690 + }
691 +
692 + return false;
693 + }
694 +
695 + /**
696 + * Print footer script.
697 + */
698 + public function printFooterScripts() {
699 + if ( $this->lazy_render_script ) {
700 + ?>
701 + <script type="text/javascript">
702 + var visualizerUserInteractionEvents = [
703 + "scroll",
704 + "mouseover",
705 + "keydown",
706 + "touchmove",
707 + "touchstart"
708 + ];
709 +
710 + visualizerUserInteractionEvents.forEach(function(event) {
711 + window.addEventListener(event, visualizerTriggerScriptLoader, { passive: true });
712 + });
713 +
714 + function visualizerTriggerScriptLoader() {
715 + visualizerLoadScripts();
716 + visualizerUserInteractionEvents.forEach(function(event) {
717 + window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true });
718 + });
719 + }
720 +
721 + function visualizerLoadScripts() {
722 + document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) {
723 + jQuery.getScript( elem.getAttribute("data-visualizer-script") )
724 + .done( function( script, textStatus ) {
725 + elem.setAttribute("src", elem.getAttribute("data-visualizer-script"));
726 + elem.removeAttribute("data-visualizer-script");
727 + setTimeout( function() {
728 + visualizerRefreshChart();
729 + } );
730 + } );
731 + });
732 + }
733 +
734 + function visualizerRefreshChart() {
735 + jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
736 + if ( jQuery( 'div.viz-facade-loaded:not(.visualizer-lazy):empty' ).length > 0 ) {
737 + visualizerUserInteractionEvents.forEach( function( event ) {
738 + window.addEventListener( event, function() {
739 + jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
740 + }, { passive: true } );
741 + } );
742 + }
743 + }
744 + </script>
745 + <?php
746 + }
293 747 }
294 748 }