| @@ -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', '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(); |
| @@ -86,9 +107,33 @@ | ||
| 86 | 107 | register_rest_route( |
| 87 | 108 | 'visualizer/v' . VISUALIZER_REST_VERSION, |
| 88 | 109 | '/action/(?P<chart>\d+)/(?P<type>.+)/', |
| 89 | 110 | array( |
| 90 | - 'methods' => array( 'GET', 'POST' ), | |
| 111 | + // POST is required for save/cancel, GET for all others | |
| 112 | + 'methods' => array( 'POST', 'GET' ), | |
| 113 | + 'args' => array( | |
| 114 | + 'chart' => array( | |
| 115 | + 'required' => true, | |
| 116 | + 'sanitize_callback' => function( $param ) { | |
| 117 | + return is_numeric( $param ) ? $param : null; | |
| 118 | + }, | |
| 119 | + ), | |
| 120 | + 'type' => array( | |
| 121 | + 'required' => true, | |
| 122 | + 'type' => 'string', | |
| 123 | + 'enum' => array_merge( array( 'save', 'cancel' ), array_keys( $this->get_actions() ) ), | |
| 124 | + ), | |
| 125 | + ), | |
| 126 | + 'permission_callback' => function ( WP_REST_Request $request ) { | |
| 127 | + $chart_id = filter_var( sanitize_text_field( $request->get_param( 'chart' ), FILTER_VALIDATE_INT ) ); | |
| 128 | + if ( ! empty( $chart_id ) && in_array( $request->get_param( 'type' ), array( 'save', 'cancel' ), true ) ) { | |
| 129 | + // let save and cancel go without any check as past version of pro | |
| 130 | + // did not send the X-WP-Nonce | |
| 131 | + // we can change this at a later date. | |
| 132 | + return true; | |
| 133 | + } | |
| 134 | + return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id ); | |
| 135 | + }, | |
| 91 | 136 | 'callback' => array( $this, 'perform_action' ), |
| 92 | 137 | ) |
| 93 | 138 | ); |
| 94 | 139 | } |
| @@ -98,17 +143,42 @@ | ||
| 98 | 143 | * |
| 99 | 144 | * @access private |
| 100 | 145 | */ |
| 101 | 146 | private function get_actions() { |
| 102 | - return apply_filters( | |
| 147 | + $actions = apply_filters( | |
| 103 | 148 | 'visualizer_action_buttons', |
| 104 | 149 | array( |
| 105 | - 'print' => __( 'Print', 'visualizer' ), | |
| 106 | - 'csv' => __( 'CSV', 'visualizer' ), | |
| 107 | - 'xls' => __( 'Excel', 'visualizer' ), | |
| 108 | - 'copy' => __( 'Copy', 'visualizer' ), | |
| 150 | + 'print' => array( | |
| 151 | + 'label' => esc_html__( 'Print', 'visualizer' ), | |
| 152 | + 'title' => esc_html__( 'Print Chart', 'visualizer' ), | |
| 153 | + ), | |
| 154 | + 'csv' => array( | |
| 155 | + 'label' => esc_html__( 'CSV', 'visualizer' ), | |
| 156 | + 'title' => esc_html__( 'Download as a CSV', 'visualizer' ), | |
| 157 | + ), | |
| 158 | + 'xls' => array( | |
| 159 | + 'label' => esc_html__( 'Excel', 'visualizer' ), | |
| 160 | + 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ), | |
| 161 | + ), | |
| 162 | + 'copy' => array( | |
| 163 | + 'label' => esc_html__( 'Copy', 'visualizer' ), | |
| 164 | + 'title' => esc_html__( 'Copy data', 'visualizer' ), | |
| 165 | + ), | |
| 166 | + 'image' => array( | |
| 167 | + 'label' => esc_html__( 'Download', 'visualizer' ), | |
| 168 | + 'title' => esc_html__( 'Download as an image', 'visualizer' ), | |
| 169 | + ), | |
| 109 | 170 | ) |
| 110 | 171 | ); |
| 172 | + | |
| 173 | + // backward compatibility before label and title attributes were introduced. | |
| 174 | + if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) { | |
| 175 | + $actions['edit'] = array( | |
| 176 | + 'label' => esc_html__( 'Edit', 'visualizer' ), | |
| 177 | + 'title' => esc_html__( 'Edit data', 'visualizer' ), | |
| 178 | + ); | |
| 179 | + } | |
| 180 | + return $actions; | |
| 111 | 181 | } |
| 112 | 182 | |
| 113 | 183 | /** |
| 114 | 184 | * The print button |
| @@ -139,8 +209,23 @@ | ||
| 139 | 209 | break; |
| 140 | 210 | case 'xls': |
| 141 | 211 | $data = $this->_getDataAs( $chart_id, 'xls' ); |
| 142 | 212 | break; |
| 213 | + case 'image': | |
| 214 | + $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 215 | + $title = 'visualizer#' . $chart_id; | |
| 216 | + if ( ! empty( $settings['title'] ) ) { | |
| 217 | + $title = $settings['title']; | |
| 218 | + } | |
| 219 | + // for ChartJS, title is an array. | |
| 220 | + if ( is_array( $title ) && isset( $title['text'] ) ) { | |
| 221 | + $title = $title['text']; | |
| 222 | + } | |
| 223 | + if ( empty( $title ) ) { | |
| 224 | + $title = 'visualizer#' . $chart_id; | |
| 225 | + } | |
| 226 | + $data = array( 'name' => $title ); | |
| 227 | + break; | |
| 143 | 228 | default: |
| 144 | 229 | $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this ); |
| 145 | 230 | break; |
| 146 | 231 | } |
| @@ -157,9 +242,8 @@ | ||
| 157 | 242 | * @access public |
| 158 | 243 | */ |
| 159 | 244 | public function enqueueScripts() { |
| 160 | 245 | wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true ); |
| 161 | - wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); | |
| 162 | 246 | wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION ); |
| 163 | 247 | do_action( 'visualizer_pro_frontend_load_resources' ); |
| 164 | 248 | } |
| 165 | 249 | |
| @@ -179,27 +263,33 @@ | ||
| 179 | 263 | public function renderChart( $atts ) { |
| 180 | 264 | global $wp_version; |
| 181 | 265 | $atts = shortcode_atts( |
| 182 | 266 | 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 | |
| 267 | + // chart id | |
| 268 | + 'id' => false, | |
| 269 | + // chart class | |
| 270 | + 'class' => false, | |
| 271 | + // for lazy load | |
| 272 | + // set 'yes' to use the default intersection limit (300px) | |
| 273 | + // OR set a number (e.g. 700) to use 700px as the intersection limit | |
| 274 | + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 275 | + // Use image chart | |
| 276 | + 'use_image' => 'no', | |
| 188 | 277 | ), |
| 189 | 278 | $atts |
| 190 | 279 | ); |
| 191 | - | |
| 192 | 280 | // 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 ) { | |
| 281 | + if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 194 | 282 | return ''; |
| 195 | 283 | } |
| 196 | 284 | |
| 285 | + // do not show the chart? | |
| 197 | 286 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| 198 | 287 | return ''; |
| 199 | 288 | } |
| 200 | 289 | |
| 201 | 290 | // in case revisions exist. |
| 291 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 202 | 292 | if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) { |
| 203 | 293 | $chart = get_post( $chart->ID ); |
| 204 | 294 | } |
| 205 | 295 | |
| @@ -205,11 +295,21 @@ | ||
| 205 | 295 | |
| 206 | 296 | $id = 'visualizer-' . $atts['id']; |
| 207 | 297 | $defaultClass = 'visualizer-front'; |
| 208 | 298 | $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); |
| 209 | - $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id']; | |
| 210 | - $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : ''; | |
| 211 | 299 | |
| 300 | + $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : ''; | |
| 301 | + | |
| 302 | + $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass ); | |
| 303 | + $attributes = array(); | |
| 304 | + if ( ! empty( $class ) ) { | |
| 305 | + $attributes['class'] = trim( $class ); | |
| 306 | + } | |
| 307 | + | |
| 308 | + if ( ctype_digit( $atts['lazy'] ) ) { | |
| 309 | + $attributes['data-lazy-limit'] = $atts['lazy']; | |
| 310 | + } | |
| 311 | + | |
| 212 | 312 | $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 213 | 313 | |
| 214 | 314 | $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false ); |
| 215 | 315 | |
| @@ -220,22 +320,14 @@ | ||
| 220 | 320 | } |
| 221 | 321 | |
| 222 | 322 | // handle series filter hooks |
| 223 | 323 | $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); |
| 224 | - if ( ! empty( $atts['series'] ) ) { | |
| 225 | - $series = apply_filters( $atts['series'], $series, $chart->ID, $type ); | |
| 226 | - } | |
| 324 | + | |
| 227 | 325 | // handle settings filter hooks |
| 228 | 326 | $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); |
| 229 | - if ( ! empty( $atts['settings'] ) ) { | |
| 230 | - $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); | |
| 231 | - } | |
| 232 | 327 | |
| 233 | 328 | // handle data filter hooks |
| 234 | - $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type ); | |
| 235 | - if ( ! empty( $atts['data'] ) ) { | |
| 236 | - $data = apply_filters( $atts['data'], $data, $chart->ID, $type ); | |
| 237 | - } | |
| 329 | + $data = self::get_chart_data( $chart, $type ); | |
| 238 | 330 | |
| 239 | 331 | $css = ''; |
| 240 | 332 | $arguments = $this->get_inline_custom_css( $id, $settings ); |
| 241 | 333 | if ( ! empty( $arguments ) ) { |
| @@ -244,8 +336,22 @@ | ||
| 244 | 336 | } |
| 245 | 337 | |
| 246 | 338 | $library = $this->load_chart_type( $chart->ID ); |
| 247 | 339 | |
| 340 | + $id = $id . '-' . rand(); | |
| 341 | + | |
| 342 | + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME ); | |
| 343 | + if ( $amp && $amp->is_amp() ) { | |
| 344 | + return '<div data-amp-auto-lightbox-disable id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>'; | |
| 345 | + } | |
| 346 | + | |
| 347 | + if ( 'yes' === $atts['use_image'] ) { | |
| 348 | + $chart_image = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ); | |
| 349 | + if ( $chart_image ) { | |
| 350 | + return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>'; | |
| 351 | + } | |
| 352 | + } | |
| 353 | + | |
| 248 | 354 | // add chart to the array |
| 249 | 355 | $this->_charts[ $id ] = array( |
| 250 | 356 | 'type' => $type, |
| 251 | 357 | 'series' => $series, |
| @@ -253,33 +359,8 @@ | ||
| 253 | 359 | 'data' => $data, |
| 254 | 360 | 'library' => $library, |
| 255 | 361 | ); |
| 256 | 362 | |
| 257 | - wp_register_script( | |
| 258 | - "visualizer-render-$library", | |
| 259 | - VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 260 | - apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 261 | - Visualizer_Plugin::VERSION, | |
| 262 | - true | |
| 263 | - ); | |
| 264 | - | |
| 265 | - wp_enqueue_script( "visualizer-render-$library" ); | |
| 266 | - wp_localize_script( | |
| 267 | - "visualizer-render-$library", | |
| 268 | - 'visualizer', | |
| 269 | - array( | |
| 270 | - 'charts' => $this->_charts, | |
| 271 | - 'language' => $this->get_language(), | |
| 272 | - 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 273 | - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 274 | - 'i10n' => array( | |
| 275 | - 'copied' => __( 'Copied!', 'visualizer' ), | |
| 276 | - ), | |
| 277 | - 'page_type' => 'frontend', | |
| 278 | - ) | |
| 279 | - ); | |
| 280 | - wp_enqueue_style( 'visualizer-front' ); | |
| 281 | - | |
| 282 | 363 | $actions_div = ''; |
| 283 | 364 | $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] ); |
| 284 | 365 | if ( ! empty( $actions_visible ) ) { |
| 285 | 366 | $actions = $this->get_actions(); |
| @@ -291,15 +372,16 @@ | ||
| 291 | 372 | $array = explode( ';', $action_type ); |
| 292 | 373 | $key = $array[0]; |
| 293 | 374 | $mime = end( $array ); |
| 294 | 375 | } |
| 295 | - $label = $actions[ $key ]; | |
| 296 | - $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 . '" '; | |
| 376 | + $label = $actions[ $key ]['label']; | |
| 377 | + $title = $actions[ $key ]['title']; | |
| 378 | + $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 ); | |
| 297 | 379 | |
| 298 | 380 | if ( 'copy' === $key ) { |
| 299 | 381 | $copy = $this->_getDataAs( $atts['id'], 'csv' ); |
| 300 | 382 | $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"'; |
| 301 | - wp_enqueue_script( 'visualizer-clipboardjs' ); | |
| 383 | + wp_enqueue_script( 'clipboard' ); | |
| 302 | 384 | } |
| 303 | 385 | |
| 304 | 386 | $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] ); |
| 305 | 387 | $actions_div .= '>' . $label . '</a> '; |
| @@ -309,8 +391,157 @@ | ||
| 309 | 391 | } |
| 310 | 392 | |
| 311 | 393 | $actions_div .= $css; |
| 312 | 394 | |
| 395 | + foreach ( $this->_charts as $id => $array ) { | |
| 396 | + $library = $array['library']; | |
| 397 | + wp_register_script( | |
| 398 | + "visualizer-render-$library", | |
| 399 | + VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 400 | + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 401 | + Visualizer_Plugin::VERSION, | |
| 402 | + true | |
| 403 | + ); | |
| 404 | + | |
| 405 | + wp_enqueue_script( "visualizer-render-$library" ); | |
| 406 | + wp_localize_script( | |
| 407 | + "visualizer-render-$library", | |
| 408 | + 'visualizer', | |
| 409 | + array( | |
| 410 | + 'charts' => $this->_charts, | |
| 411 | + 'language' => $this->get_language(), | |
| 412 | + 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 413 | + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 414 | + 'wp_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 415 | + 'i10n' => array( | |
| 416 | + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ), | |
| 417 | + ), | |
| 418 | + 'page_type' => 'frontend', | |
| 419 | + 'is_front' => true, | |
| 420 | + ) | |
| 421 | + ); | |
| 422 | + wp_enqueue_style( 'visualizer-front' ); | |
| 423 | + } | |
| 424 | + | |
| 313 | 425 | // return placeholder div |
| 314 | - return $actions_div . '<div id="' . $id . '"' . $class . '></div>'; | |
| 426 | + return $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ); | |
| 427 | + } | |
| 428 | + | |
| 429 | + /** | |
| 430 | + * Converts the array of attributes to a string of HTML attributes. | |
| 431 | + * | |
| 432 | + * @since ? | |
| 433 | + * | |
| 434 | + * @access private | |
| 435 | + */ | |
| 436 | + private function getHtmlAttributes( $attributes ) { | |
| 437 | + $string = ''; | |
| 438 | + if ( ! $attributes ) { | |
| 439 | + return $string; | |
| 440 | + } | |
| 441 | + | |
| 442 | + foreach ( $attributes as $name => $value ) { | |
| 443 | + $string .= sprintf( '%s="%s"', esc_attr( $name ), esc_attr( $value ) ); | |
| 444 | + } | |
| 445 | + return $string; | |
| 446 | + } | |
| 447 | + | |
| 448 | + /** | |
| 449 | + * Adds the schema corresponding to the dataset. | |
| 450 | + * | |
| 451 | + * @since 3.3.2 | |
| 452 | + * | |
| 453 | + * @access private | |
| 454 | + */ | |
| 455 | + private function addSchema( $id ) { | |
| 456 | + // should we show informative errors as HTML comments? | |
| 457 | + $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id ); | |
| 458 | + | |
| 459 | + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 460 | + $title = ''; | |
| 461 | + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) { | |
| 462 | + $title = $settings['title']; | |
| 463 | + if ( is_array( $title ) ) { | |
| 464 | + $title = $settings['title']['text']; | |
| 465 | + } | |
| 466 | + } | |
| 467 | + $title = apply_filters( 'visualizer_schema_name', $title, $id ); | |
| 468 | + if ( empty( $title ) ) { | |
| 469 | + if ( $show_errors ) { | |
| 470 | + return "<!-- Not showing structured data for chart $id because title is empty -->"; | |
| 471 | + } | |
| 472 | + return ''; | |
| 473 | + } | |
| 474 | + | |
| 475 | + $desc = ''; | |
| 476 | + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) { | |
| 477 | + $desc = $settings['description']; | |
| 478 | + } | |
| 479 | + $desc = apply_filters( 'visualizer_schema_description', $desc, $id ); | |
| 480 | + if ( empty( $desc ) ) { | |
| 481 | + if ( $show_errors ) { | |
| 482 | + return "<!-- Not showing structured data for chart $id because description is empty -->"; | |
| 483 | + } | |
| 484 | + return ''; | |
| 485 | + } | |
| 486 | + | |
| 487 | + // descriptions below 50 chars are not allowed. | |
| 488 | + if ( strlen( $desc ) < 50 ) { | |
| 489 | + if ( $show_errors ) { | |
| 490 | + return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->"; | |
| 491 | + } | |
| 492 | + return ''; | |
| 493 | + } | |
| 494 | + | |
| 495 | + $license = ''; | |
| 496 | + if ( isset( $settings['license'] ) && ! empty( $settings['license'] ) ) { | |
| 497 | + $license = $settings['license']; | |
| 498 | + if ( is_array( $license ) ) { | |
| 499 | + $license = $settings['license']['text']; | |
| 500 | + } | |
| 501 | + } | |
| 502 | + $license = apply_filters( 'visualizer_schema_license', $license, $id ); | |
| 503 | + if ( empty( $license ) ) { | |
| 504 | + if ( $show_errors ) { | |
| 505 | + return "<!-- Not showing structured data for chart $id because license is empty -->"; | |
| 506 | + } | |
| 507 | + return ''; | |
| 508 | + } | |
| 509 | + | |
| 510 | + $creator = ''; | |
| 511 | + if ( isset( $settings['creator'] ) && ! empty( $settings['creator'] ) ) { | |
| 512 | + $creator = $settings['creator']; | |
| 513 | + if ( is_array( $creator ) ) { | |
| 514 | + $creator = $settings['creator']['text']; | |
| 515 | + } | |
| 516 | + } | |
| 517 | + $creator = apply_filters( 'visualizer_schema_creator', $creator, $id ); | |
| 518 | + if ( empty( $creator ) ) { | |
| 519 | + if ( $show_errors ) { | |
| 520 | + return "<!-- Not showing structured data for chart $id because creator is empty -->"; | |
| 521 | + } | |
| 522 | + return ''; | |
| 523 | + } | |
| 524 | + | |
| 525 | + $schema = apply_filters( | |
| 526 | + 'visualizer_schema', | |
| 527 | + '{ | |
| 528 | + "@context":"https://schema.org/", | |
| 529 | + "@type":"Dataset", | |
| 530 | + "name":"' . esc_html( $title ) . '", | |
| 531 | + "description":"' . esc_html( $desc ) . '", | |
| 532 | + "license": "' . esc_html( $license ) . '", | |
| 533 | + "creator": { | |
| 534 | + "@type": "Person", | |
| 535 | + "name": "' . esc_html( $creator ) . '" | |
| 536 | + } | |
| 537 | + }', | |
| 538 | + $id | |
| 539 | + ); | |
| 540 | + | |
| 541 | + if ( empty( $schema ) ) { | |
| 542 | + return ''; | |
| 543 | + } | |
| 544 | + | |
| 545 | + return '<script type="application/ld+json">' . $schema . '</script>'; | |
| 315 | 546 | } |
| 316 | 547 | } |