| @@ -343,7 +343,56 @@ | ||
| 343 | 343 | wp_enqueue_style( 'visualizer-front' ); |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | // return placeholder div |
| 347 | - 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>'; | |
| 348 | 397 | } |
| 349 | 398 | } |