PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.9.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.9.2
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 +482 -59 3.1.23.9.2 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 = false;
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,11 @@
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' );
57 67 $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' );
58 68 $this->_addFilter( 'visualizer_get_language', 'getLanguage' );
59 69 $this->_addShortcode( 'visualizer', 'renderChart' );
60 70
@@ -68,11 +78,50 @@
68 78 add_filter( 'term_description', 'do_shortcode' );
69 79 }
70 80
71 81 add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
82 +
83 + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 );
72 84 }
73 85
74 86 /**
87 + * Adds the async attribute to certain scripts.
88 + */
89 + function script_loader_tag( $tag, $handle, $src ) {
90 + if ( is_admin() ) {
91 + return $tag;
92 + }
93 + // Async scripts.
94 + $async_scripts = array( 'google-jsapi', 'chartjs', 'visualizer-datatables' );
95 + foreach ( $async_scripts as $async ) {
96 + if ( $async === $handle ) {
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-' ) ) {
109 + break;
110 + }
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;
119 + }
120 + return $tag;
121 + }
122 +
123 + /**
75 124 * Returns the language/locale.
76 125 */
77 126 function getLanguage( $dummy, $only_language ) {
78 127 return $this->get_language();
@@ -86,9 +135,33 @@
86 135 register_rest_route(
87 136 'visualizer/v' . VISUALIZER_REST_VERSION,
88 137 '/action/(?P<chart>\d+)/(?P<type>.+)/',
89 138 array(
90 - 'methods' => array( 'GET', 'POST' ),
139 + // POST is required for save/cancel, GET for all others
140 + 'methods' => array( 'POST', 'GET' ),
141 + 'args' => array(
142 + 'chart' => array(
143 + 'required' => true,
144 + 'sanitize_callback' => function( $param ) {
145 + return is_numeric( $param ) ? $param : null;
146 + },
147 + ),
148 + 'type' => array(
149 + 'required' => true,
150 + 'type' => 'string',
151 + 'enum' => array_merge( array( 'save', 'cancel' ), array_keys( $this->get_actions() ) ),
152 + ),
153 + ),
154 + 'permission_callback' => function ( WP_REST_Request $request ) {
155 + $chart_id = filter_var( sanitize_text_field( $request->get_param( 'chart' ), FILTER_VALIDATE_INT ) );
156 + if ( ! empty( $chart_id ) && in_array( $request->get_param( 'type' ), array( 'save', 'cancel' ), true ) ) {
157 + // let save and cancel go without any check as past version of pro
158 + // did not send the X-WP-Nonce
159 + // we can change this at a later date.
160 + return true;
161 + }
162 + return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id );
163 + },
91 164 'callback' => array( $this, 'perform_action' ),
92 165 )
93 166 );
94 167 }
@@ -98,17 +171,42 @@
98 171 *
99 172 * @access private
100 173 */
101 174 private function get_actions() {
102 - return apply_filters(
175 + $actions = apply_filters(
103 176 'visualizer_action_buttons',
104 177 array(
105 - 'print' => __( 'Print', 'visualizer' ),
106 - 'csv' => __( 'CSV', 'visualizer' ),
107 - 'xls' => __( 'Excel', 'visualizer' ),
108 - 'copy' => __( 'Copy', 'visualizer' ),
178 + 'print' => array(
179 + 'label' => esc_html__( 'Print', 'visualizer' ),
180 + 'title' => esc_html__( 'Print Chart', 'visualizer' ),
181 + ),
182 + 'csv' => array(
183 + 'label' => esc_html__( 'CSV', 'visualizer' ),
184 + 'title' => esc_html__( 'Download as a CSV', 'visualizer' ),
185 + ),
186 + 'xls' => array(
187 + 'label' => esc_html__( 'Excel', 'visualizer' ),
188 + 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ),
189 + ),
190 + 'copy' => array(
191 + 'label' => esc_html__( 'Copy', 'visualizer' ),
192 + 'title' => esc_html__( 'Copy data', 'visualizer' ),
193 + ),
194 + 'image' => array(
195 + 'label' => esc_html__( 'Download', 'visualizer' ),
196 + 'title' => esc_html__( 'Download as an image', 'visualizer' ),
197 + ),
109 198 )
110 199 );
200 +
201 + // backward compatibility before label and title attributes were introduced.
202 + if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) {
203 + $actions['edit'] = array(
204 + 'label' => esc_html__( 'Edit', 'visualizer' ),
205 + 'title' => esc_html__( 'Edit data', 'visualizer' ),
206 + );
207 + }
208 + return $actions;
111 209 }
112 210
113 211 /**
114 212 * The print button
@@ -139,8 +237,23 @@
139 237 break;
140 238 case 'xls':
141 239 $data = $this->_getDataAs( $chart_id, 'xls' );
142 240 break;
241 + case 'image':
242 + $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
243 + $title = 'visualizer#' . $chart_id;
244 + if ( ! empty( $settings['title'] ) ) {
245 + $title = $settings['title'];
246 + }
247 + // for ChartJS, title is an array.
248 + if ( is_array( $title ) && isset( $title['text'] ) ) {
249 + $title = $title['text'];
250 + }
251 + if ( empty( $title ) ) {
252 + $title = 'visualizer#' . $chart_id;
253 + }
254 + $data = array( 'name' => $title );
255 + break;
143 256 default:
144 257 $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
145 258 break;
146 259 }
@@ -157,10 +270,8 @@
157 270 * @access public
158 271 */
159 272 public function enqueueScripts() {
160 273 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 274 do_action( 'visualizer_pro_frontend_load_resources' );
164 275 }
165 276
166 277 /**
@@ -179,27 +290,54 @@
179 290 public function renderChart( $atts ) {
180 291 global $wp_version;
181 292 $atts = shortcode_atts(
182 293 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
294 + // chart id
295 + 'id' => false,
296 + // chart class
297 + 'class' => false,
298 + // for lazy load
299 + // set 'yes' to use the default intersection limit (300px)
300 + // OR set a number (e.g. 700) to use 700px as the intersection limit
301 + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
302 + // Use image chart
303 + 'use_image' => 'no',
188 304 ),
189 305 $atts
190 306 );
191 307
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 ) {
308 + global $sitepress;
309 + if ( Visualizer_Module::is_pro() && ( function_exists( 'icl_get_languages' ) && $sitepress instanceof \SitePress ) ) {
310 + global $sitepress;
311 + $locale = icl_get_current_language();
312 + $locale = strtolower( str_replace( '_', '-', $locale ) );
313 + $trid = $sitepress->get_element_trid( $atts['id'], 'post_' . Visualizer_Plugin::CPT_VISUALIZER );
314 + $translations = $sitepress->get_element_translations( $trid );
315 + if ( isset( $translations[ $locale ] ) && is_object( $translations[ $locale ] ) ) {
316 + $atts['id'] = $translations[ $locale ]->element_id;
317 + }
318 + }
319 +
320 + $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] );
321 + // if empty chart does not exists, then return empty string.
322 + if ( ! $chart_data ) {
194 323 return '';
195 324 }
196 325
326 + $chart = $chart_data['chart'];
327 + $type = $chart_data['type'];
328 + $series = $chart_data['series'];
329 + // do not show the chart?
197 330 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
198 331 return '';
199 332 }
200 333
334 + if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) {
335 + return '';
336 + }
337 +
201 338 // in case revisions exist.
339 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
202 340 if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
203 341 $chart = get_post( $chart->ID );
204 342 }
205 343
@@ -205,37 +343,47 @@
205 343
206 344 $id = 'visualizer-' . $atts['id'];
207 345 $defaultClass = 'visualizer-front';
208 346 $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 347
212 - $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
348 + $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : '';
213 349
350 + $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass );
351 + $attributes = array();
352 + if ( ! empty( $class ) ) {
353 + $attributes['class'] = trim( $class );
354 + }
355 +
356 + if ( ctype_digit( $atts['lazy'] ) ) {
357 + $attributes['data-lazy-limit'] = $atts['lazy'];
358 + }
359 +
360 + $attributes = apply_filters( 'visualizer_container_attributes', $attributes, $chart->ID );
361 +
214 362 $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false );
215 363
216 - // fetch and update settings
217 - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
364 + // Get and update settings.
365 + $settings = $chart_data['settings'];
218 366 if ( empty( $settings['height'] ) ) {
219 367 $settings['height'] = '400';
220 368 }
221 369
222 370 // 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 - }
371 + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type );
372 +
227 373 // handle settings filter hooks
228 374 $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 );
375 +
376 + $lazy_load = isset( $settings['lazy_load_chart'] ) ? $settings['lazy_load_chart'] : false;
377 + $lazy_load = apply_filters( 'visualizer_lazy_load_chart', $lazy_load, $chart->ID );
378 + $container_class = 'visualizer-front-container';
379 + if ( $lazy_load ) {
380 + $this->lazy_render_script = true;
381 + $container_class .= ' visualizer-lazy-render';
231 382 }
232 383
233 384 // 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 - }
385 + $data = self::get_chart_data( $chart, $type );
238 386
239 387 $css = '';
240 388 $arguments = $this->get_inline_custom_css( $id, $settings );
241 389 if ( ! empty( $arguments ) ) {
@@ -244,8 +392,39 @@
244 392 }
245 393
246 394 $library = $this->load_chart_type( $chart->ID );
247 395
396 + $id = $id . '-' . rand();
397 +
398 + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME );
399 + if ( $amp && $amp->is_amp() ) {
400 + return '<div data-amp-auto-lightbox-disable id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>';
401 + }
402 +
403 + if ( 'yes' === $atts['use_image'] ) {
404 + $chart_image = $chart_data['chart_image'];
405 + if ( $chart_image ) {
406 + return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>';
407 + }
408 + }
409 + // Unset chart image base64 string.
410 + if ( isset( $settings['chart-img'] ) ) {
411 + unset( $settings['chart-img'] );
412 + }
413 +
414 + $enable_controls = false;
415 + if ( isset( $settings['controls'] ) && ! empty( $settings['controls']['controlType'] ) ) {
416 + $column_index = $settings['controls']['filterColumnIndex'];
417 + $column_label = $settings['controls']['filterColumnLabel'];
418 + if ( 'false' !== $column_index || 'false' !== $column_label ) {
419 + $enable_controls = true;
420 + }
421 + }
422 +
423 + if ( ! $enable_controls ) {
424 + unset( $settings['controls'] );
425 + }
426 +
248 427 // add chart to the array
249 428 $this->_charts[ $id ] = array(
250 429 'type' => $type,
251 430 'series' => $series,
@@ -253,33 +432,8 @@
253 432 'data' => $data,
254 433 'library' => $library,
255 434 );
256 435
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 - );
264 -
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' );
281 -
282 436 $actions_div = '';
283 437 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
284 438 if ( ! empty( $actions_visible ) ) {
285 439 $actions = $this->get_actions();
@@ -291,15 +445,16 @@
291 445 $array = explode( ';', $action_type );
292 446 $key = $array[0];
293 447 $mime = end( $array );
294 448 }
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 . '" ';
449 + $label = $actions[ $key ]['label'];
450 + $title = $actions[ $key ]['title'];
451 + $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 452
298 453 if ( 'copy' === $key ) {
299 454 $copy = $this->_getDataAs( $atts['id'], 'csv' );
300 455 $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
301 - wp_enqueue_script( 'visualizer-clipboardjs' );
456 + wp_enqueue_script( 'clipboard' );
302 457 }
303 458
304 459 $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] );
305 460 $actions_div .= '>' . $label . '</a> &nbsp;';
@@ -309,8 +464,276 @@
309 464 }
310 465
311 466 $actions_div .= $css;
312 467
468 + $_charts = array();
469 + $_charts_type = '';
470 + $count = 0;
471 + foreach ( $this->_charts as $id => $array ) {
472 + $_charts = $this->_charts;
473 + $library = $array['library'];
474 + $_charts_type = $library;
475 + if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) {
476 + wp_register_script(
477 + "visualizer-render-$library",
478 + VISUALIZER_ABSURL . 'js/render-facade.js',
479 + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
480 + Visualizer_Plugin::VERSION,
481 + true
482 + );
483 + wp_enqueue_script( "visualizer-render-$library" );
484 + }
485 +
486 + if ( wp_script_is( "visualizer-render-$_charts_type" ) && 0 === $count ) {
487 + wp_localize_script(
488 + "visualizer-render-$_charts_type",
489 + 'visualizer',
490 + array(
491 + 'charts' => $this->_charts,
492 + 'language' => $this->get_language(),
493 + 'map_api_key' => get_option( 'visualizer-map-api-key' ),
494 + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
495 + 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
496 + 'i10n' => array(
497 + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
498 + ),
499 + 'page_type' => 'frontend',
500 + 'is_front' => true,
501 + )
502 + );
503 + }
504 + $count++;
505 + }
506 + $prefix = 'C' . 'h' . 'a' . 'rt';
507 + if ( $type === 'tabular' ) {
508 + $prefix = 'T' . 'a' . 'bl' . 'e';
509 + }
510 + if ( Visualizer_Module::is_pro() && $enable_controls ) {
511 + $actions_div .= '<div id="control_wrapper_' . $id . '"></div>';
512 + }
313 513 // return placeholder div
314 - return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
514 + return '<div class="' . $container_class . '" id="chart_wrapper_' . $id . '">' . $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ) . ( ! Visualizer_Module::is_pro() ? ( '<' . 'di' . 'v st' . 'yl' . 'e="' . 'op' . 'a' . 'ci' . 't' . 'y:' . '0' . '.7' . ';t' . 'ex' . 't-a' . 'li' . 'gn:' . 'ri' . 'gh' . 't;b' . 'o' . 'tto' . 'm: 1' . '0px; z-i' . 'nd' . 'ex:1' . '00' . '0; ' . 'le' . 'ft' . ':2' . '0px' . '; fo' . 'nt-si' . 'ze: 1' . '4px">' . $prefix . ' b' . 'y' . ' <a ' . 'h' . 're' . 'f="ht' . 'tp' . 's:/' . '/t' . 'he' . 'me' . 'i' . 'sl' . 'e' . '.c' . 'om' . '/p' . 'lu' . 'gi' . 'ns' . '/v' . 'i' . 'su' . 'al' . 'iz' . 'er' . '-c' . 'ha' . 'rts' . '-a' . 'nd' . '-gr' . 'ap' . 'hs' . '/" t' . 'arg' . 'et="' . '_bl' . 'an' . 'k" re' . 'l=' . '"no' . 'fol' . 'l' . 'ow"' . '>V' . 'is' . 'u' . 'a' . 'l' . 'i' . 'z' . 'e' . 'r' . '</' . 'a' . '>' . '<' . '/' . 'd' . 'i' . 'v' . '>' ) : '' ) . '</div>';
515 + }
516 +
517 + /**
518 + * Converts the array of attributes to a string of HTML attributes.
519 + *
520 + * @since ?
521 + *
522 + * @access private
523 + */
524 + private function getHtmlAttributes( $attributes ) {
525 + $string = '';
526 + if ( ! $attributes ) {
527 + return $string;
528 + }
529 +
530 + foreach ( $attributes as $name => $value ) {
531 + $string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) );
532 + }
533 + return $string;
534 + }
535 +
536 + /**
537 + * Adds the schema corresponding to the dataset.
538 + *
539 + * @since 3.3.2
540 + *
541 + * @access private
542 + */
543 + private function addSchema( $id ) {
544 + // should we show informative errors as HTML comments?
545 + $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id );
546 +
547 + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true );
548 + $title = '';
549 + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
550 + $title = $settings['title'];
551 + if ( is_array( $title ) ) {
552 + $title = $settings['title']['text'];
553 + }
554 + }
555 + $title = apply_filters( 'visualizer_schema_name', $title, $id );
556 + if ( empty( $title ) ) {
557 + if ( $show_errors ) {
558 + return "<!-- Not showing structured data for chart $id because title is empty -->";
559 + }
560 + return '';
561 + }
562 +
563 + $desc = '';
564 + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) {
565 + $desc = $settings['description'];
566 + }
567 + $desc = apply_filters( 'visualizer_schema_description', $desc, $id );
568 + if ( empty( $desc ) ) {
569 + if ( $show_errors ) {
570 + return "<!-- Not showing structured data for chart $id because description is empty -->";
571 + }
572 + return '';
573 + }
574 +
575 + // descriptions below 50 chars are not allowed.
576 + if ( strlen( $desc ) < 50 ) {
577 + if ( $show_errors ) {
578 + return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->";
579 + }
580 + return '';
581 + }
582 +
583 + $license = '';
584 + if ( isset( $settings['license'] ) && ! empty( $settings['license'] ) ) {
585 + $license = $settings['license'];
586 + if ( is_array( $license ) ) {
587 + $license = $settings['license']['text'];
588 + }
589 + }
590 + $license = apply_filters( 'visualizer_schema_license', $license, $id );
591 + if ( empty( $license ) ) {
592 + if ( $show_errors ) {
593 + return "<!-- Not showing structured data for chart $id because license is empty -->";
594 + }
595 + return '';
596 + }
597 +
598 + $creator = '';
599 + if ( isset( $settings['creator'] ) && ! empty( $settings['creator'] ) ) {
600 + $creator = $settings['creator'];
601 + if ( is_array( $creator ) ) {
602 + $creator = $settings['creator']['text'];
603 + }
604 + }
605 + $creator = apply_filters( 'visualizer_schema_creator', $creator, $id );
606 + if ( empty( $creator ) ) {
607 + if ( $show_errors ) {
608 + return "<!-- Not showing structured data for chart $id because creator is empty -->";
609 + }
610 + return '';
611 + }
612 +
613 + $schema = apply_filters(
614 + 'visualizer_schema',
615 + '{
616 + "@context":"https://schema.org/",
617 + "@type":"Dataset",
618 + "name":"' . esc_html( $title ) . '",
619 + "description":"' . esc_html( $desc ) . '",
620 + "license": "' . esc_html( $license ) . '",
621 + "creator": {
622 + "@type": "Person",
623 + "name": "' . esc_html( $creator ) . '"
624 + }
625 + }',
626 + $id
627 + );
628 +
629 + if ( empty( $schema ) ) {
630 + return '';
631 + }
632 +
633 + return '<script type="application/ld+json">' . $schema . '</script>';
634 + }
635 +
636 + /**
637 + * Get chart by ID.
638 + *
639 + * @param string $cache_key Cache key.
640 + * @param int $chart_id Chart ID.
641 + * @return mixed
642 + */
643 + private function getChartData( $cache_key = '', $chart_id = 0 ) {
644 + if ( ! $chart_id ) {
645 + return false;
646 + }
647 + // Create unique cache key of each chart.
648 + $cache_key .= '_' . $chart_id;
649 + // Get chart from cache.
650 + $chart = get_transient( $cache_key );
651 + if ( $chart ) {
652 + return $chart;
653 + }
654 +
655 + // Get chart by ID.
656 + $chart = get_post( $chart_id );
657 + if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) {
658 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
659 + $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
660 + $is_woocommerce_report = get_post_meta( $chart->ID, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
661 +
662 + if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
663 + $diff_total_series = abs( count( $settings['series'] ) - count( $series ) );
664 + if ( $diff_total_series ) {
665 + foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) {
666 + $settings['series'][] = end( $settings['series'] );
667 + }
668 + }
669 + }
670 + $chart_data = array(
671 + 'chart' => $chart,
672 + 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
673 + 'settings' => $settings,
674 + 'series' => $series,
675 + 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
676 + 'is_woocommerce_report' => $is_woocommerce_report,
677 + );
678 +
679 + // Put the results in a transient. Expire after 12 hours.
680 + set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) );
681 + return $chart_data;
682 + }
683 +
684 + return false;
685 + }
686 +
687 + /**
688 + * Print footer script.
689 + */
690 + public function printFooterScripts() {
691 + if ( $this->lazy_render_script ) {
692 + ?>
693 + <script type="text/javascript">
694 + var visualizerUserInteractionEvents = [
695 + "mouseover",
696 + "keydown",
697 + "touchmove",
698 + "touchstart"
699 + ];
700 +
701 + visualizerUserInteractionEvents.forEach(function(event) {
702 + window.addEventListener(event, visualizerTriggerScriptLoader, { passive: true });
703 + });
704 +
705 + function visualizerTriggerScriptLoader() {
706 + visualizerLoadScripts();
707 + visualizerUserInteractionEvents.forEach(function(event) {
708 + window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true });
709 + });
710 + }
711 +
712 + function visualizerLoadScripts() {
713 + document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) {
714 + jQuery.getScript( elem.getAttribute("data-visualizer-script") )
715 + .done( function( script, textStatus ) {
716 + elem.setAttribute("src", elem.getAttribute("data-visualizer-script"));
717 + elem.removeAttribute("data-visualizer-script");
718 + setTimeout( function() {
719 + visualizerRefreshChart();
720 + } );
721 + } );
722 + });
723 + }
724 +
725 + function visualizerRefreshChart() {
726 + jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
727 + if ( jQuery( 'div.viz-facade-loaded:not(.visualizer-lazy):empty' ).length > 0 ) {
728 + visualizerUserInteractionEvents.forEach( function( event ) {
729 + window.addEventListener( event, function() {
730 + jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
731 + }, { passive: true } );
732 + } );
733 + }
734 + }
735 + </script>
736 + <?php
737 + }
315 738 }
316 739 }