PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.8.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.8.0
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
visualizer / classes / Visualizer / Module / Frontend.php

Frontend.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.8.0, at classes/Visualizer/Module/Frontend.php

722 lines 23.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // +----------------------------------------------------------------------+
4 // | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
5 // +----------------------------------------------------------------------+
6 // | This program is free software; you can redistribute it and/or modify |
7 // | it under the terms of the GNU General Public License, version 2, as |
8 // | published by the Free Software Foundation. |
9 // | |
10 // | This program is distributed in the hope that it will be useful, |
11 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 // | GNU General Public License for more details. |
14 // | |
15 // | You should have received a copy of the GNU General Public License |
16 // | along with this program; if not, write to the Free Software |
17 // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
18 // | MA 02110-1301 USA |
19 // +----------------------------------------------------------------------+
20 // | Author: Eugene Manuilov <eugene@manuilov.org> |
21 // +----------------------------------------------------------------------+
22 /**
23 * Frontend module class.
24 *
25 * @category Visualizer
26 * @package Module
27 *
28 * @since 1.0.0
29 */
30 class Visualizer_Module_Frontend extends Visualizer_Module {
31
32 const NAME = __CLASS__;
33
34 /**
35 * The array of charts to render.
36 *
37 * @since 1.0.0
38 *
39 * @access private
40 * @var array
41 */
42 private $_charts = array();
43
44 /**
45 * Lazy render script.
46 *
47 * @access private
48 * @var bool
49 */
50 private $lazy_render_script = false;
51
52 /**
53 * Constructor.
54 *
55 * @since 1.0.0
56 * @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters.
57 *
58 * @access public
59 * @param Visualizer_Plugin $plugin The instance of the plugin.
60 */
61 public function __construct( Visualizer_Plugin $plugin ) {
62 parent::__construct( $plugin );
63
64 $this->_addAction( 'wp_print_footer_scripts', 'printFooterScripts' );
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' );
69 $this->_addShortcode( 'visualizer', 'renderChart' );
70
71 // add do_shortocde hook for widget_text filter
72 if ( ! has_filter( 'widget_text', 'do_shortcode' ) ) {
73 add_filter( 'widget_text', 'do_shortcode' );
74 }
75
76 // add do_shortcode hook for term_description filter
77 if ( ! has_filter( 'term_description', 'do_shortcode' ) ) {
78 add_filter( 'term_description', 'do_shortcode' );
79 }
80
81 add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
82
83 $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 );
84 }
85
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 /**
132 * Registers the endpoints
133 */
134 function endpoint_register() {
135 register_rest_route(
136 'visualizer/v' . VISUALIZER_REST_VERSION,
137 '/action/(?P<chart>\d+)/(?P<type>.+)/',
138 array(
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 },
164 'callback' => array( $this, 'perform_action' ),
165 )
166 );
167 }
168
169 /**
170 * All possible actions and their labels
171 *
172 * @access private
173 */
174 private function get_actions() {
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 ),
198 )
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;
209 }
210
211 /**
212 * The print button
213 *
214 * @since 1.0.0
215 *
216 * @access public
217 */
218 public function perform_action( WP_REST_Request $params ) {
219 $chart_id = filter_var( sanitize_text_field( $params['chart'] ), FILTER_VALIDATE_INT );
220 $type = sanitize_text_field( $params['type'] );
221 $data = null;
222
223 if ( ! $chart_id ) {
224 return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid chart ID', 'visualizer' ) ) ) );
225 }
226
227 if ( ! $type ) {
228 return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid action', 'visualizer' ) ) ) );
229 }
230
231 switch ( $type ) {
232 case 'print':
233 $data = $this->_getDataAs( $chart_id, 'print' );
234 break;
235 case 'csv':
236 $data = $this->_getDataAs( $chart_id, 'csv' );
237 break;
238 case 'xls':
239 $data = $this->_getDataAs( $chart_id, 'xls' );
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;
256 default:
257 $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
258 break;
259 }
260
261 return new WP_REST_Response( array( 'data' => $data ) );
262 }
263
264 /**
265 * Registers scripts for charts rendering.
266 *
267 * @since 1.0.0
268 * @uses wp_register_script To register javascript file.
269 *
270 * @access public
271 */
272 public function enqueueScripts() {
273 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
274 do_action( 'visualizer_pro_frontend_load_resources' );
275 }
276
277 /**
278 * Returns placeholder for chart according to visualizer shortcode attributes.
279 *
280 * @since 1.0.0
281 * @uses shortcode_atts() To parse income shortocdes.
282 * @uses apply_filters() To filter chart's data and series arrays.
283 * @uses get_post_meta() To fetch chart's meta information.
284 * @uses wp_enqueue_script() To enqueue charts render script.
285 * @uses wp_localize_script() To add chart data to the page inline script.
286 *
287 * @access public
288 * @param array $atts The array of shortcode attributes.
289 */
290 public function renderChart( $atts ) {
291 global $wp_version;
292 $atts = shortcode_atts(
293 array(
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
306 );
307
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 ) {
311 return '';
312 }
313
314 $chart = $chart_data['chart'];
315 $type = $chart_data['type'];
316 $series = $chart_data['series'];
317 // do not show the chart?
318 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
319 return '';
320 }
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
328 $id = 'visualizer-' . $atts['id'];
329 $defaultClass = 'visualizer-front';
330 $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );
331
332 $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : '';
333
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'];
350 if ( empty( $settings['height'] ) ) {
351 $settings['height'] = '400';
352 }
353
354 // handle series filter hooks
355 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type );
356
357 // handle settings filter hooks
358 $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_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';
366 }
367
368 // handle data filter hooks
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];
376 }
377
378 $library = $this->load_chart_type( $chart->ID );
379
380 $id = $id . '-' . rand();
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
398 $enable_controls = false;
399 if ( isset( $settings['controls'] ) && ! empty( $settings['controls']['controlType'] ) ) {
400 $column_index = $settings['controls']['filterColumnIndex'];
401 $column_label = $settings['controls']['filterColumnLabel'];
402 if ( 'false' !== $column_index || 'false' !== $column_label ) {
403 $enable_controls = true;
404 }
405 }
406
407 if ( ! $enable_controls ) {
408 unset( $settings['controls'] );
409 }
410
411 // add chart to the array
412 $this->_charts[ $id ] = array(
413 'type' => $type,
414 'series' => $series,
415 'settings' => $settings,
416 'data' => $data,
417 'library' => $library,
418 );
419
420 $actions_div = '';
421 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
422 if ( ! empty( $actions_visible ) ) {
423 $actions = $this->get_actions();
424 $actions_div = '<div class="visualizer-actions">';
425 foreach ( $actions_visible as $action_type ) {
426 $key = $action_type;
427 $mime = '';
428 if ( strpos( $action_type, ';' ) !== false ) {
429 $array = explode( ';', $action_type );
430 $key = $array[0];
431 $mime = end( $array );
432 }
433 $label = $actions[ $key ]['label'];
434 $title = $actions[ $key ]['title'];
435 $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 );
436
437 if ( 'copy' === $key ) {
438 $copy = $this->_getDataAs( $atts['id'], 'csv' );
439 $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
440 wp_enqueue_script( 'clipboard' );
441 }
442
443 $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] );
444 $actions_div .= '>' . $label . '</a> &nbsp;';
445 }
446
447 $actions_div .= '</div>';
448 }
449
450 $actions_div .= $css;
451
452 $_charts = array();
453 $_charts_type = '';
454 $count = 0;
455 foreach ( $this->_charts as $id => $array ) {
456 $_charts = $this->_charts;
457 $library = $array['library'];
458 $_charts_type = $library;
459 if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) {
460 wp_register_script(
461 "visualizer-render-$library",
462 VISUALIZER_ABSURL . 'js/render-facade.js',
463 apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
464 Visualizer_Plugin::VERSION,
465 true
466 );
467 wp_enqueue_script( "visualizer-render-$library" );
468 }
469
470 if ( wp_script_is( "visualizer-render-$_charts_type" ) && 0 === $count ) {
471 wp_localize_script(
472 "visualizer-render-$_charts_type",
473 'visualizer',
474 array(
475 'charts' => $this->_charts,
476 'language' => $this->get_language(),
477 'map_api_key' => get_option( 'visualizer-map-api-key' ),
478 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
479 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
480 'i10n' => array(
481 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
482 ),
483 'page_type' => 'frontend',
484 'is_front' => true,
485 )
486 );
487 }
488 $count++;
489 }
490 $prefix = 'C' . 'h' . 'a' . 'rt';
491 if ( $type === 'tabular' ) {
492 $prefix = 'T' . 'a' . 'bl' . 'e';
493 }
494 if ( Visualizer_Module::is_pro() && $enable_controls ) {
495 $actions_div .= '<div id="control_wrapper_' . $id . '"></div>';
496 }
497 // return placeholder div
498 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>';
499 }
500
501 /**
502 * Converts the array of attributes to a string of HTML attributes.
503 *
504 * @since ?
505 *
506 * @access private
507 */
508 private function getHtmlAttributes( $attributes ) {
509 $string = '';
510 if ( ! $attributes ) {
511 return $string;
512 }
513
514 foreach ( $attributes as $name => $value ) {
515 $string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) );
516 }
517 return $string;
518 }
519
520 /**
521 * Adds the schema corresponding to the dataset.
522 *
523 * @since 3.3.2
524 *
525 * @access private
526 */
527 private function addSchema( $id ) {
528 // should we show informative errors as HTML comments?
529 $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id );
530
531 $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true );
532 $title = '';
533 if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
534 $title = $settings['title'];
535 if ( is_array( $title ) ) {
536 $title = $settings['title']['text'];
537 }
538 }
539 $title = apply_filters( 'visualizer_schema_name', $title, $id );
540 if ( empty( $title ) ) {
541 if ( $show_errors ) {
542 return "<!-- Not showing structured data for chart $id because title is empty -->";
543 }
544 return '';
545 }
546
547 $desc = '';
548 if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) {
549 $desc = $settings['description'];
550 }
551 $desc = apply_filters( 'visualizer_schema_description', $desc, $id );
552 if ( empty( $desc ) ) {
553 if ( $show_errors ) {
554 return "<!-- Not showing structured data for chart $id because description is empty -->";
555 }
556 return '';
557 }
558
559 // descriptions below 50 chars are not allowed.
560 if ( strlen( $desc ) < 50 ) {
561 if ( $show_errors ) {
562 return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->";
563 }
564 return '';
565 }
566
567 $license = '';
568 if ( isset( $settings['license'] ) && ! empty( $settings['license'] ) ) {
569 $license = $settings['license'];
570 if ( is_array( $license ) ) {
571 $license = $settings['license']['text'];
572 }
573 }
574 $license = apply_filters( 'visualizer_schema_license', $license, $id );
575 if ( empty( $license ) ) {
576 if ( $show_errors ) {
577 return "<!-- Not showing structured data for chart $id because license is empty -->";
578 }
579 return '';
580 }
581
582 $creator = '';
583 if ( isset( $settings['creator'] ) && ! empty( $settings['creator'] ) ) {
584 $creator = $settings['creator'];
585 if ( is_array( $creator ) ) {
586 $creator = $settings['creator']['text'];
587 }
588 }
589 $creator = apply_filters( 'visualizer_schema_creator', $creator, $id );
590 if ( empty( $creator ) ) {
591 if ( $show_errors ) {
592 return "<!-- Not showing structured data for chart $id because creator is empty -->";
593 }
594 return '';
595 }
596
597 $schema = apply_filters(
598 'visualizer_schema',
599 '{
600 "@context":"https://schema.org/",
601 "@type":"Dataset",
602 "name":"' . esc_html( $title ) . '",
603 "description":"' . esc_html( $desc ) . '",
604 "license": "' . esc_html( $license ) . '",
605 "creator": {
606 "@type": "Person",
607 "name": "' . esc_html( $creator ) . '"
608 }
609 }',
610 $id
611 );
612
613 if ( empty( $schema ) ) {
614 return '';
615 }
616
617 return '<script type="application/ld+json">' . $schema . '</script>';
618 }
619
620 /**
621 * Get chart by ID.
622 *
623 * @param string $cache_key Cache key.
624 * @param int $chart_id Chart ID.
625 * @return mixed
626 */
627 private function getChartData( $cache_key = '', $chart_id = 0 ) {
628 if ( ! $chart_id ) {
629 return false;
630 }
631 // Create unique cache key of each chart.
632 $cache_key .= '_' . $chart_id;
633 // Get chart from cache.
634 $chart = get_transient( $cache_key );
635 if ( $chart ) {
636 return $chart;
637 }
638
639 // Get chart by ID.
640 $chart = get_post( $chart_id );
641 if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) {
642 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
643 $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
644
645 if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
646 $diff_total_series = abs( count( $settings['series'] ) - count( $series ) );
647 if ( $diff_total_series ) {
648 foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) {
649 $settings['series'][] = end( $settings['series'] );
650 }
651 }
652 }
653 $chart_data = array(
654 'chart' => $chart,
655 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
656 'settings' => $settings,
657 'series' => $series,
658 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
659 );
660
661 // Put the results in a transient. Expire after 12 hours.
662 set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) );
663 return $chart_data;
664 }
665
666 return false;
667 }
668
669 /**
670 * Print footer script.
671 */
672 public function printFooterScripts() {
673 if ( $this->lazy_render_script ) {
674 ?>
675 <script type="text/javascript">
676 var visualizerUserInteractionEvents = [
677 "mouseover",
678 "keydown",
679 "touchmove",
680 "touchstart"
681 ];
682
683 visualizerUserInteractionEvents.forEach(function(event) {
684 window.addEventListener(event, visualizerTriggerScriptLoader, { passive: true });
685 });
686
687 function visualizerTriggerScriptLoader() {
688 visualizerLoadScripts();
689 visualizerUserInteractionEvents.forEach(function(event) {
690 window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true });
691 });
692 }
693
694 function visualizerLoadScripts() {
695 document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) {
696 jQuery.getScript( elem.getAttribute("data-visualizer-script") )
697 .done( function( script, textStatus ) {
698 elem.setAttribute("src", elem.getAttribute("data-visualizer-script"));
699 elem.removeAttribute("data-visualizer-script");
700 setTimeout( function() {
701 visualizerRefreshChart();
702 } );
703 } );
704 });
705 }
706
707 function visualizerRefreshChart() {
708 jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
709 if ( jQuery( 'div.viz-facade-loaded:not(.visualizer-lazy):empty' ).length > 0 ) {
710 visualizerUserInteractionEvents.forEach( function( event ) {
711 window.addEventListener( event, function() {
712 jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize();
713 }, { passive: true } );
714 } );
715 }
716 }
717 </script>
718 <?php
719 }
720 }
721 }
722