| @@ -68,11 +68,32 @@ | ||
| 68 | 68 | add_filter( 'term_description', 'do_shortcode' ); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | add_action( 'rest_api_init', array( $this, 'endpoint_register' ) ); |
| 72 | + | |
| 73 | + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 ); | |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 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 | + /** | |
| 75 | 96 | * Returns the language/locale. |
| 76 | 97 | */ |
| 77 | 98 | function getLanguage( $dummy, $only_language ) { |
| 78 | 99 | return $this->get_language(); |
| @@ -189,9 +210,9 @@ | ||
| 189 | 210 | $atts |
| 190 | 211 | ); |
| 191 | 212 | |
| 192 | 213 | // if empty id or chart does not exists, then return empty string |
| 193 | - if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 214 | + if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 194 | 215 | return ''; |
| 195 | 216 | } |
| 196 | 217 | |
| 197 | 218 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| @@ -198,8 +219,9 @@ | ||
| 198 | 219 | return ''; |
| 199 | 220 | } |
| 200 | 221 | |
| 201 | 222 | // in case revisions exist. |
| 223 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 202 | 224 | if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) { |
| 203 | 225 | $chart = get_post( $chart->ID ); |
| 204 | 226 | } |
| 205 | 227 | |
| @@ -246,8 +268,13 @@ | ||
| 246 | 268 | $library = $this->load_chart_type( $chart->ID ); |
| 247 | 269 | |
| 248 | 270 | $id = $id . '-' . rand(); |
| 249 | 271 | |
| 272 | + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME ); | |
| 273 | + if ( $amp && $amp->is_amp() ) { | |
| 274 | + return '<div id="' . $id . '"' . $class . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>'; | |
| 275 | + } | |
| 276 | + | |
| 250 | 277 | // add chart to the array |
| 251 | 278 | $this->_charts[ $id ] = array( |
| 252 | 279 | 'type' => $type, |
| 253 | 280 | 'series' => $series, |
| @@ -255,33 +282,8 @@ | ||
| 255 | 282 | 'data' => $data, |
| 256 | 283 | 'library' => $library, |
| 257 | 284 | ); |
| 258 | 285 | |
| 259 | - wp_register_script( | |
| 260 | - "visualizer-render-$library", | |
| 261 | - VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 262 | - apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 263 | - Visualizer_Plugin::VERSION, | |
| 264 | - true | |
| 265 | - ); | |
| 266 | - | |
| 267 | - wp_enqueue_script( "visualizer-render-$library" ); | |
| 268 | - wp_localize_script( | |
| 269 | - "visualizer-render-$library", | |
| 270 | - 'visualizer', | |
| 271 | - array( | |
| 272 | - 'charts' => $this->_charts, | |
| 273 | - 'language' => $this->get_language(), | |
| 274 | - 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 275 | - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 276 | - 'i10n' => array( | |
| 277 | - 'copied' => __( 'Copied!', 'visualizer' ), | |
| 278 | - ), | |
| 279 | - 'page_type' => 'frontend', | |
| 280 | - ) | |
| 281 | - ); | |
| 282 | - wp_enqueue_style( 'visualizer-front' ); | |
| 283 | - | |
| 284 | 286 | $actions_div = ''; |
| 285 | 287 | $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] ); |
| 286 | 288 | if ( ! empty( $actions_visible ) ) { |
| 287 | 289 | $actions = $this->get_actions(); |
| @@ -311,8 +313,86 @@ | ||
| 311 | 313 | } |
| 312 | 314 | |
| 313 | 315 | $actions_div .= $css; |
| 314 | 316 | |
| 317 | + foreach ( $this->_charts as $id => $attributes ) { | |
| 318 | + $library = $attributes['library']; | |
| 319 | + wp_register_script( | |
| 320 | + "visualizer-render-$library", | |
| 321 | + VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 322 | + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 323 | + Visualizer_Plugin::VERSION, | |
| 324 | + true | |
| 325 | + ); | |
| 326 | + | |
| 327 | + wp_enqueue_script( "visualizer-render-$library" ); | |
| 328 | + wp_localize_script( | |
| 329 | + "visualizer-render-$library", | |
| 330 | + 'visualizer', | |
| 331 | + array( | |
| 332 | + 'charts' => $this->_charts, | |
| 333 | + 'language' => $this->get_language(), | |
| 334 | + 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 335 | + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 336 | + 'i10n' => array( | |
| 337 | + 'copied' => __( 'Copied!', 'visualizer' ), | |
| 338 | + ), | |
| 339 | + 'page_type' => 'frontend', | |
| 340 | + 'is_front' => true, | |
| 341 | + ) | |
| 342 | + ); | |
| 343 | + wp_enqueue_style( 'visualizer-front' ); | |
| 344 | + } | |
| 345 | + | |
| 315 | 346 | // return placeholder div |
| 316 | - return $actions_div . '<div id="' . $id . '"' . $class . '></div>'; | |
| 347 | + return $actions_div . '<div id="' . $id . '"' . $class . '></div>' . $this->addSchema( $chart->ID ); | |
| 348 | + } | |
| 349 | + | |
| 350 | + /** | |
| 351 | + * Adds the schema corresponding to the dataset. | |
| 352 | + * | |
| 353 | + * @since 3.3.2 | |
| 354 | + * | |
| 355 | + * @access private | |
| 356 | + */ | |
| 357 | + private function addSchema( $id ) { | |
| 358 | + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 359 | + $title = ''; | |
| 360 | + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) { | |
| 361 | + $title = $settings['title']; | |
| 362 | + if ( is_array( $title ) ) { | |
| 363 | + $title = $settings['title']['text']; | |
| 364 | + } | |
| 365 | + } | |
| 366 | + $title = apply_filters( 'visualizer_schema_name', $title, $id ); | |
| 367 | + if ( empty( $title ) ) { | |
| 368 | + return ''; | |
| 369 | + } | |
| 370 | + | |
| 371 | + $desc = ''; | |
| 372 | + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) { | |
| 373 | + $desc = $settings['description']; | |
| 374 | + } | |
| 375 | + $desc = apply_filters( 'visualizer_schema_description', $desc, $id ); | |
| 376 | + // descriptions below 50 chars are not allowed. | |
| 377 | + if ( empty( $desc ) || strlen( $desc ) < 50 ) { | |
| 378 | + return ''; | |
| 379 | + } | |
| 380 | + | |
| 381 | + $schema = apply_filters( | |
| 382 | + 'visualizer_schema', | |
| 383 | + '{ | |
| 384 | + "@context":"https://schema.org/", | |
| 385 | + "@type":"Dataset", | |
| 386 | + "name":"' . esc_html( $title ) . '", | |
| 387 | + "description":"' . esc_html( $desc ) . '" | |
| 388 | + }', | |
| 389 | + $id | |
| 390 | + ); | |
| 391 | + | |
| 392 | + if ( empty( $schema ) ) { | |
| 393 | + return ''; | |
| 394 | + } | |
| 395 | + | |
| 396 | + return '<script type="application/ld+json">' . $schema . '</script>'; | |
| 317 | 397 | } |
| 318 | 398 | } |