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