PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.5
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.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 +229 -51 3.0.93.4.5 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-new', 'google-jsapi-old', '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();
@@ -97,16 +119,42 @@
97 119 *
98 120 * @access private
99 121 */
100 122 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' ),
123 + $actions = apply_filters(
124 + 'visualizer_action_buttons',
125 + array(
126 + 'print' => array(
127 + 'label' => esc_html__( 'Print', 'visualizer' ),
128 + 'title' => esc_html__( 'Print Chart', 'visualizer' ),
129 + ),
130 + 'csv' => array(
131 + 'label' => esc_html__( 'CSV', 'visualizer' ),
132 + 'title' => esc_html__( 'Download as a CSV', 'visualizer' ),
133 + ),
134 + 'xls' => array(
135 + 'label' => esc_html__( 'Excel', 'visualizer' ),
136 + 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ),
137 + ),
138 + 'copy' => array(
139 + 'label' => esc_html__( 'Copy', 'visualizer' ),
140 + 'title' => esc_html__( 'Copy data', 'visualizer' ),
141 + ),
142 + 'image' => array(
143 + 'label' => esc_html__( 'Download', 'visualizer' ),
144 + 'title' => esc_html__( 'Download as an image', 'visualizer' ),
145 + ),
107 146 )
108 147 );
148 +
149 + // backward compatibility before label and title attributes were introduced.
150 + if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) {
151 + $actions['edit'] = array(
152 + 'label' => esc_html__( 'Edit', 'visualizer' ),
153 + 'title' => esc_html__( 'Edit data', 'visualizer' ),
154 + );
155 + }
156 + return $actions;
109 157 }
110 158
111 159 /**
112 160 * The print button
@@ -137,8 +185,23 @@
137 185 break;
138 186 case 'xls':
139 187 $data = $this->_getDataAs( $chart_id, 'xls' );
140 188 break;
189 + case 'image':
190 + $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
191 + $title = 'visualizer#' . $chart_id;
192 + if ( ! empty( $settings['title'] ) ) {
193 + $title = $settings['title'];
194 + }
195 + // for ChartJS, title is an array.
196 + if ( is_array( $title ) && isset( $title['text'] ) ) {
197 + $title = $title['text'];
198 + }
199 + if ( empty( $title ) ) {
200 + $title = 'visualizer#' . $chart_id;
201 + }
202 + $data = array( 'name' => $title );
203 + break;
141 204 default:
142 205 $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
143 206 break;
144 207 }
@@ -154,11 +217,9 @@
154 217 *
155 218 * @access public
156 219 */
157 220 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 );
221 + wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
161 222 wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
162 223 wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION );
163 224 do_action( 'visualizer_pro_frontend_load_resources' );
164 225 }
@@ -179,26 +240,32 @@
179 240 public function renderChart( $atts ) {
180 241 global $wp_version;
181 242 $atts = shortcode_atts(
182 243 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
244 + // chart id
245 + 'id' => false,
246 + // chart class
247 + 'class' => false,
248 + // for lazy load
249 + // set 'yes' to use the default intersection limit (300px)
250 + // OR set a number (e.g. 700) to use 700px as the intersection limit
251 + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
252 + ),
253 + $atts
189 254 );
190 255
191 256 // 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 ) {
257 + if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
193 258 return '';
194 259 }
195 260
261 + // do not show the chart?
196 262 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
197 263 return '';
198 264 }
199 265
200 266 // in case revisions exist.
267 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
201 268 if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
202 269 $chart = get_post( $chart->ID );
203 270 }
204 271
@@ -204,14 +271,26 @@
204 271
205 272 $id = 'visualizer-' . $atts['id'];
206 273 $defaultClass = 'visualizer-front';
207 274 $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 275
276 + $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : '';
277 +
278 + $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass );
279 + $attributes = array();
280 + if ( ! empty( $class ) ) {
281 + $attributes['class'] = trim( $class );
282 + }
283 +
284 + if ( ctype_digit( $atts['lazy'] ) ) {
285 + $attributes['data-lazy-limit'] = $atts['lazy'];
286 + }
287 +
211 288 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
212 289
213 - // faetch and update settings
290 + $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false );
291 +
292 + // fetch and update settings
214 293 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
215 294 if ( empty( $settings['height'] ) ) {
216 295 $settings['height'] = '400';
217 296 }
@@ -217,32 +296,31 @@
217 296 }
218 297
219 298 // handle series filter hooks
220 299 $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 - }
300 +
224 301 // handle settings filter hooks
225 302 $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 303
230 304 // 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 - }
305 + $data = self::get_chart_data( $chart, $type );
235 306
236 - $id = $id . '-' . rand();
237 - $arguments = array( '', $id, $settings );
238 307 $css = '';
239 - apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) );
308 + $arguments = $this->get_inline_custom_css( $id, $settings );
240 309 if ( ! empty( $arguments ) ) {
241 310 $css = $arguments[0];
242 - $settings = $arguments[2];
311 + $settings = $arguments[1];
243 312 }
244 313
314 + $library = $this->load_chart_type( $chart->ID );
315 +
316 + $id = $id . '-' . rand();
317 +
318 + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME );
319 + if ( $amp && $amp->is_amp() ) {
320 + return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>';
321 + }
322 +
245 323 // add chart to the array
246 324 $this->_charts[ $id ] = array(
247 325 'type' => $type,
248 326 'series' => $series,
@@ -247,25 +325,11 @@
247 325 'type' => $type,
248 326 'series' => $series,
249 327 'settings' => $settings,
250 328 'data' => $data,
329 + 'library' => $library,
251 330 );
252 331
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 332 $actions_div = '';
269 333 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
270 334 if ( ! empty( $actions_visible ) ) {
271 335 $actions = $this->get_actions();
@@ -277,10 +341,11 @@
277 341 $array = explode( ';', $action_type );
278 342 $key = $array[0];
279 343 $mime = end( $array );
280 344 }
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 . '" ';
345 + $label = $actions[ $key ]['label'];
346 + $title = $actions[ $key ]['title'];
347 + $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 348
284 349 if ( 'copy' === $key ) {
285 350 $copy = $this->_getDataAs( $atts['id'], 'csv' );
286 351 $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
@@ -295,8 +360,121 @@
295 360 }
296 361
297 362 $actions_div .= $css;
298 363
364 + foreach ( $this->_charts as $id => $array ) {
365 + $library = $array['library'];
366 + wp_register_script(
367 + "visualizer-render-$library",
368 + VISUALIZER_ABSURL . 'js/render-facade.js',
369 + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
370 + Visualizer_Plugin::VERSION,
371 + true
372 + );
373 +
374 + wp_enqueue_script( "visualizer-render-$library" );
375 + wp_localize_script(
376 + "visualizer-render-$library",
377 + 'visualizer',
378 + array(
379 + 'charts' => $this->_charts,
380 + 'language' => $this->get_language(),
381 + 'map_api_key' => get_option( 'visualizer-map-api-key' ),
382 + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
383 + 'i10n' => array(
384 + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
385 + ),
386 + 'page_type' => 'frontend',
387 + 'is_front' => true,
388 + )
389 + );
390 + wp_enqueue_style( 'visualizer-front' );
391 + }
392 +
299 393 // return placeholder div
300 - return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
394 + return $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID );
395 + }
396 +
397 + /**
398 + * Converts the array of attributes to a string of HTML attributes.
399 + *
400 + * @since ?
401 + *
402 + * @access private
403 + */
404 + private function getHtmlAttributes( $attributes ) {
405 + $string = '';
406 + if ( ! $attributes ) {
407 + return $string;
408 + }
409 +
410 + foreach ( $attributes as $name => $value ) {
411 + $string .= sprintf( '%s="%s"', esc_attr( $name ), esc_attr( $value ) );
412 + }
413 + return $string;
414 + }
415 +
416 + /**
417 + * Adds the schema corresponding to the dataset.
418 + *
419 + * @since 3.3.2
420 + *
421 + * @access private
422 + */
423 + private function addSchema( $id ) {
424 + // should we show informative errors as HTML comments?
425 + $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id );
426 +
427 + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true );
428 + $title = '';
429 + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
430 + $title = $settings['title'];
431 + if ( is_array( $title ) ) {
432 + $title = $settings['title']['text'];
433 + }
434 + }
435 + $title = apply_filters( 'visualizer_schema_name', $title, $id );
436 + if ( empty( $title ) ) {
437 + if ( $show_errors ) {
438 + return "<!-- Not showing structured data for chart $id because title is empty -->";
439 + }
440 + return '';
441 + }
442 +
443 + $desc = '';
444 + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) {
445 + $desc = $settings['description'];
446 + }
447 + $desc = apply_filters( 'visualizer_schema_description', $desc, $id );
448 + if ( empty( $desc ) ) {
449 + if ( $show_errors ) {
450 + return "<!-- Not showing structured data for chart $id because description is empty -->";
451 + }
452 + return '';
453 + }
454 +
455 + // descriptions below 50 chars are not allowed.
456 + if ( strlen( $desc ) < 50 ) {
457 + if ( $show_errors ) {
458 + return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->";
459 + }
460 + return '';
461 + }
462 +
463 + $schema = apply_filters(
464 + 'visualizer_schema',
465 + '{
466 + "@context":"https://schema.org/",
467 + "@type":"Dataset",
468 + "name":"' . esc_html( $title ) . '",
469 + "description":"' . esc_html( $desc ) . '"
470 + }',
471 + $id
472 + );
473 +
474 + if ( empty( $schema ) ) {
475 + return '';
476 + }
477 +
478 + return '<script type="application/ld+json">' . $schema . '</script>';
301 479 }
302 480 }