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