PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.6
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.6
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 +249 -54 3.0.83.4.6 View file →
@@ -53,8 +53,9 @@
53 53 public function __construct( Visualizer_Plugin $plugin ) {
54 54 parent::__construct( $plugin );
55 55
56 56 $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' );
57 + $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' );
57 58 $this->_addFilter( 'visualizer_get_language', 'getLanguage' );
58 59 $this->_addShortcode( 'visualizer', 'renderChart' );
59 60
60 61 // add do_shortocde hook for widget_text filter
@@ -67,11 +68,32 @@
67 68 add_filter( 'term_description', 'do_shortcode' );
68 69 }
69 70
70 71 add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
72 +
73 + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 );
71 74 }
72 75
73 76 /**
77 + * Adds the async attribute to certain scripts.
78 + */
79 + function script_loader_tag( $tag, $handle, $src ) {
80 + if ( is_admin() ) {
81 + return $tag;
82 + }
83 +
84 + $scripts = array( 'google-jsapi', 'visualizer-render-google-lib', 'visualizer-render-google' );
85 +
86 + foreach ( $scripts as $async ) {
87 + if ( $async === $handle ) {
88 + $tag = str_replace( ' src', ' defer="defer" src', $tag );
89 + break;
90 + }
91 + }
92 + return $tag;
93 + }
94 +
95 + /**
74 96 * Returns the language/locale.
75 97 */
76 98 function getLanguage( $dummy, $only_language ) {
77 99 return $this->get_language();
@@ -85,9 +107,26 @@
85 107 register_rest_route(
86 108 'visualizer/v' . VISUALIZER_REST_VERSION,
87 109 '/action/(?P<chart>\d+)/(?P<type>.+)/',
88 110 array(
89 - 'methods' => array( 'GET', 'POST' ),
111 + 'methods' => 'GET',
112 + 'args' => array(
113 + 'chart' => array(
114 + 'required' => true,
115 + 'sanitize_callback' => function( $param ) {
116 + return is_numeric( $param ) ? $param : null;
117 + },
118 + ),
119 + 'type' => array(
120 + 'required' => true,
121 + 'type' => 'string',
122 + 'enum' => array_keys( $this->get_actions() ),
123 + ),
124 + ),
125 + 'permission_callback' => function ( WP_REST_Request $request ) {
126 + $chart_id = filter_var( sanitize_text_field( $request->get_param( 'chart' ), FILTER_VALIDATE_INT ) );
127 + return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id );
128 + },
90 129 'callback' => array( $this, 'perform_action' ),
91 130 )
92 131 );
93 132 }
@@ -97,16 +136,42 @@
97 136 *
98 137 * @access private
99 138 */
100 139 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' ),
140 + $actions = apply_filters(
141 + 'visualizer_action_buttons',
142 + array(
143 + 'print' => array(
144 + 'label' => esc_html__( 'Print', 'visualizer' ),
145 + 'title' => esc_html__( 'Print Chart', 'visualizer' ),
146 + ),
147 + 'csv' => array(
148 + 'label' => esc_html__( 'CSV', 'visualizer' ),
149 + 'title' => esc_html__( 'Download as a CSV', 'visualizer' ),
150 + ),
151 + 'xls' => array(
152 + 'label' => esc_html__( 'Excel', 'visualizer' ),
153 + 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ),
154 + ),
155 + 'copy' => array(
156 + 'label' => esc_html__( 'Copy', 'visualizer' ),
157 + 'title' => esc_html__( 'Copy data', 'visualizer' ),
158 + ),
159 + 'image' => array(
160 + 'label' => esc_html__( 'Download', 'visualizer' ),
161 + 'title' => esc_html__( 'Download as an image', 'visualizer' ),
162 + ),
107 163 )
108 164 );
165 +
166 + // backward compatibility before label and title attributes were introduced.
167 + if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) {
168 + $actions['edit'] = array(
169 + 'label' => esc_html__( 'Edit', 'visualizer' ),
170 + 'title' => esc_html__( 'Edit data', 'visualizer' ),
171 + );
172 + }
173 + return $actions;
109 174 }
110 175
111 176 /**
112 177 * The print button
@@ -137,8 +202,23 @@
137 202 break;
138 203 case 'xls':
139 204 $data = $this->_getDataAs( $chart_id, 'xls' );
140 205 break;
206 + case 'image':
207 + $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
208 + $title = 'visualizer#' . $chart_id;
209 + if ( ! empty( $settings['title'] ) ) {
210 + $title = $settings['title'];
211 + }
212 + // for ChartJS, title is an array.
213 + if ( is_array( $title ) && isset( $title['text'] ) ) {
214 + $title = $title['text'];
215 + }
216 + if ( empty( $title ) ) {
217 + $title = 'visualizer#' . $chart_id;
218 + }
219 + $data = array( 'name' => $title );
220 + break;
141 221 default:
142 222 $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
143 223 break;
144 224 }
@@ -154,12 +234,9 @@
154 234 *
155 235 * @access public
156 236 */
157 237 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 );
238 + wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
162 239 wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION );
163 240 do_action( 'visualizer_pro_frontend_load_resources' );
164 241 }
165 242
@@ -179,26 +256,32 @@
179 256 public function renderChart( $atts ) {
180 257 global $wp_version;
181 258 $atts = shortcode_atts(
182 259 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
260 + // chart id
261 + 'id' => false,
262 + // chart class
263 + 'class' => false,
264 + // for lazy load
265 + // set 'yes' to use the default intersection limit (300px)
266 + // OR set a number (e.g. 700) to use 700px as the intersection limit
267 + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
268 + ),
269 + $atts
189 270 );
190 271
191 272 // 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 ) {
273 + if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
193 274 return '';
194 275 }
195 276
277 + // do not show the chart?
196 278 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
197 279 return '';
198 280 }
199 281
200 282 // in case revisions exist.
283 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
201 284 if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
202 285 $chart = get_post( $chart->ID );
203 286 }
204 287
@@ -204,14 +287,26 @@
204 287
205 288 $id = 'visualizer-' . $atts['id'];
206 289 $defaultClass = 'visualizer-front';
207 290 $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 291
292 + $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : '';
293 +
294 + $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass );
295 + $attributes = array();
296 + if ( ! empty( $class ) ) {
297 + $attributes['class'] = trim( $class );
298 + }
299 +
300 + if ( ctype_digit( $atts['lazy'] ) ) {
301 + $attributes['data-lazy-limit'] = $atts['lazy'];
302 + }
303 +
211 304 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
212 305
213 - // faetch and update settings
306 + $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false );
307 +
308 + // fetch and update settings
214 309 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
215 310 if ( empty( $settings['height'] ) ) {
216 311 $settings['height'] = '400';
217 312 }
@@ -217,32 +312,31 @@
217 312 }
218 313
219 314 // handle series filter hooks
220 315 $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 - }
316 +
224 317 // handle settings filter hooks
225 318 $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 );
228 - }
229 319
230 320 // 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 - }
321 + $data = self::get_chart_data( $chart, $type );
235 322
236 - $id = $id . '-' . rand();
237 - $arguments = array( '', $id, $settings );
238 323 $css = '';
239 - apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) );
324 + $arguments = $this->get_inline_custom_css( $id, $settings );
240 325 if ( ! empty( $arguments ) ) {
241 326 $css = $arguments[0];
242 - $settings = $arguments[2];
327 + $settings = $arguments[1];
243 328 }
244 329
330 + $library = $this->load_chart_type( $chart->ID );
331 +
332 + $id = $id . '-' . rand();
333 +
334 + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME );
335 + if ( $amp && $amp->is_amp() ) {
336 + return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>';
337 + }
338 +
245 339 // add chart to the array
246 340 $this->_charts[ $id ] = array(
247 341 'type' => $type,
248 342 'series' => $series,
@@ -247,25 +341,11 @@
247 341 'type' => $type,
248 342 'series' => $series,
249 343 'settings' => $settings,
250 344 'data' => $data,
345 + 'library' => $library,
251 346 );
252 347
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 348 $actions_div = '';
269 349 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
270 350 if ( ! empty( $actions_visible ) ) {
271 351 $actions = $this->get_actions();
@@ -277,15 +357,16 @@
277 357 $array = explode( ';', $action_type );
278 358 $key = $array[0];
279 359 $mime = end( $array );
280 360 }
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 . '" ';
361 + $label = $actions[ $key ]['label'];
362 + $title = $actions[ $key ]['title'];
363 + $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 364
284 365 if ( 'copy' === $key ) {
285 366 $copy = $this->_getDataAs( $atts['id'], 'csv' );
286 367 $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
287 - wp_enqueue_script( 'visualizer-clipboardjs' );
368 + wp_enqueue_script( 'clipboard' );
288 369 }
289 370
290 371 $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] );
291 372 $actions_div .= '>' . $label . '</a> &nbsp;';
@@ -295,8 +376,122 @@
295 376 }
296 377
297 378 $actions_div .= $css;
298 379
380 + foreach ( $this->_charts as $id => $array ) {
381 + $library = $array['library'];
382 + wp_register_script(
383 + "visualizer-render-$library",
384 + VISUALIZER_ABSURL . 'js/render-facade.js',
385 + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
386 + Visualizer_Plugin::VERSION,
387 + true
388 + );
389 +
390 + wp_enqueue_script( "visualizer-render-$library" );
391 + wp_localize_script(
392 + "visualizer-render-$library",
393 + 'visualizer',
394 + array(
395 + 'charts' => $this->_charts,
396 + 'language' => $this->get_language(),
397 + 'map_api_key' => get_option( 'visualizer-map-api-key' ),
398 + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
399 + 'wp_nonce' => wp_create_nonce( 'wp_rest' ),
400 + 'i10n' => array(
401 + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
402 + ),
403 + 'page_type' => 'frontend',
404 + 'is_front' => true,
405 + )
406 + );
407 + wp_enqueue_style( 'visualizer-front' );
408 + }
409 +
299 410 // return placeholder div
300 - return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
411 + return $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID );
412 + }
413 +
414 + /**
415 + * Converts the array of attributes to a string of HTML attributes.
416 + *
417 + * @since ?
418 + *
419 + * @access private
420 + */
421 + private function getHtmlAttributes( $attributes ) {
422 + $string = '';
423 + if ( ! $attributes ) {
424 + return $string;
425 + }
426 +
427 + foreach ( $attributes as $name => $value ) {
428 + $string .= sprintf( '%s="%s"', esc_attr( $name ), esc_attr( $value ) );
429 + }
430 + return $string;
431 + }
432 +
433 + /**
434 + * Adds the schema corresponding to the dataset.
435 + *
436 + * @since 3.3.2
437 + *
438 + * @access private
439 + */
440 + private function addSchema( $id ) {
441 + // should we show informative errors as HTML comments?
442 + $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id );
443 +
444 + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true );
445 + $title = '';
446 + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
447 + $title = $settings['title'];
448 + if ( is_array( $title ) ) {
449 + $title = $settings['title']['text'];
450 + }
451 + }
452 + $title = apply_filters( 'visualizer_schema_name', $title, $id );
453 + if ( empty( $title ) ) {
454 + if ( $show_errors ) {
455 + return "<!-- Not showing structured data for chart $id because title is empty -->";
456 + }
457 + return '';
458 + }
459 +
460 + $desc = '';
461 + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) {
462 + $desc = $settings['description'];
463 + }
464 + $desc = apply_filters( 'visualizer_schema_description', $desc, $id );
465 + if ( empty( $desc ) ) {
466 + if ( $show_errors ) {
467 + return "<!-- Not showing structured data for chart $id because description is empty -->";
468 + }
469 + return '';
470 + }
471 +
472 + // descriptions below 50 chars are not allowed.
473 + if ( strlen( $desc ) < 50 ) {
474 + if ( $show_errors ) {
475 + return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->";
476 + }
477 + return '';
478 + }
479 +
480 + $schema = apply_filters(
481 + 'visualizer_schema',
482 + '{
483 + "@context":"https://schema.org/",
484 + "@type":"Dataset",
485 + "name":"' . esc_html( $title ) . '",
486 + "description":"' . esc_html( $desc ) . '"
487 + }',
488 + $id
489 + );
490 +
491 + if ( empty( $schema ) ) {
492 + return '';
493 + }
494 +
495 + return '<script type="application/ld+json">' . $schema . '</script>';
301 496 }
302 497 }