| @@ -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( 'load-index.php', 'enqueueScripts' ); | |
| 57 | 58 | $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' ); |
| 58 | 59 | $this->_addFilter( 'visualizer_get_language', 'getLanguage' ); |
| 59 | 60 | $this->_addShortcode( 'visualizer', 'renderChart' ); |
| 60 | 61 | |
| @@ -68,11 +69,32 @@ | ||
| 68 | 69 | add_filter( 'term_description', 'do_shortcode' ); |
| 69 | 70 | } |
| 70 | 71 | |
| 71 | 72 | add_action( 'rest_api_init', array( $this, 'endpoint_register' ) ); |
| 73 | + | |
| 74 | + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 ); | |
| 72 | 75 | } |
| 73 | 76 | |
| 74 | 77 | /** |
| 78 | + * Adds the async attribute to certain scripts. | |
| 79 | + */ | |
| 80 | + function script_loader_tag( $tag, $handle, $src ) { | |
| 81 | + if ( is_admin() ) { | |
| 82 | + return $tag; | |
| 83 | + } | |
| 84 | + | |
| 85 | + $scripts = array( 'google-jsapi', 'visualizer-render-google-lib', 'visualizer-render-google' ); | |
| 86 | + | |
| 87 | + foreach ( $scripts as $async ) { | |
| 88 | + if ( $async === $handle ) { | |
| 89 | + $tag = str_replace( ' src', ' defer="defer" src', $tag ); | |
| 90 | + break; | |
| 91 | + } | |
| 92 | + } | |
| 93 | + return $tag; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 75 | 97 | * Returns the language/locale. |
| 76 | 98 | */ |
| 77 | 99 | function getLanguage( $dummy, $only_language ) { |
| 78 | 100 | return $this->get_language(); |
| @@ -86,9 +108,33 @@ | ||
| 86 | 108 | register_rest_route( |
| 87 | 109 | 'visualizer/v' . VISUALIZER_REST_VERSION, |
| 88 | 110 | '/action/(?P<chart>\d+)/(?P<type>.+)/', |
| 89 | 111 | array( |
| 90 | - 'methods' => array( 'GET', 'POST' ), | |
| 112 | + // POST is required for save/cancel, GET for all others | |
| 113 | + 'methods' => array( 'POST', 'GET' ), | |
| 114 | + 'args' => array( | |
| 115 | + 'chart' => array( | |
| 116 | + 'required' => true, | |
| 117 | + 'sanitize_callback' => function( $param ) { | |
| 118 | + return is_numeric( $param ) ? $param : null; | |
| 119 | + }, | |
| 120 | + ), | |
| 121 | + 'type' => array( | |
| 122 | + 'required' => true, | |
| 123 | + 'type' => 'string', | |
| 124 | + 'enum' => array_merge( array( 'save', 'cancel' ), array_keys( $this->get_actions() ) ), | |
| 125 | + ), | |
| 126 | + ), | |
| 127 | + 'permission_callback' => function ( WP_REST_Request $request ) { | |
| 128 | + $chart_id = filter_var( sanitize_text_field( $request->get_param( 'chart' ), FILTER_VALIDATE_INT ) ); | |
| 129 | + if ( ! empty( $chart_id ) && in_array( $request->get_param( 'type' ), array( 'save', 'cancel' ), true ) ) { | |
| 130 | + // let save and cancel go without any check as past version of pro | |
| 131 | + // did not send the X-WP-Nonce | |
| 132 | + // we can change this at a later date. | |
| 133 | + return true; | |
| 134 | + } | |
| 135 | + return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id ); | |
| 136 | + }, | |
| 91 | 137 | 'callback' => array( $this, 'perform_action' ), |
| 92 | 138 | ) |
| 93 | 139 | ); |
| 94 | 140 | } |
| @@ -98,17 +144,42 @@ | ||
| 98 | 144 | * |
| 99 | 145 | * @access private |
| 100 | 146 | */ |
| 101 | 147 | private function get_actions() { |
| 102 | - return apply_filters( | |
| 148 | + $actions = apply_filters( | |
| 103 | 149 | 'visualizer_action_buttons', |
| 104 | 150 | array( |
| 105 | - 'print' => __( 'Print', 'visualizer' ), | |
| 106 | - 'csv' => __( 'CSV', 'visualizer' ), | |
| 107 | - 'xls' => __( 'Excel', 'visualizer' ), | |
| 108 | - 'copy' => __( 'Copy', 'visualizer' ), | |
| 151 | + 'print' => array( | |
| 152 | + 'label' => esc_html__( 'Print', 'visualizer' ), | |
| 153 | + 'title' => esc_html__( 'Print Chart', 'visualizer' ), | |
| 154 | + ), | |
| 155 | + 'csv' => array( | |
| 156 | + 'label' => esc_html__( 'CSV', 'visualizer' ), | |
| 157 | + 'title' => esc_html__( 'Download as a CSV', 'visualizer' ), | |
| 158 | + ), | |
| 159 | + 'xls' => array( | |
| 160 | + 'label' => esc_html__( 'Excel', 'visualizer' ), | |
| 161 | + 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ), | |
| 162 | + ), | |
| 163 | + 'copy' => array( | |
| 164 | + 'label' => esc_html__( 'Copy', 'visualizer' ), | |
| 165 | + 'title' => esc_html__( 'Copy data', 'visualizer' ), | |
| 166 | + ), | |
| 167 | + 'image' => array( | |
| 168 | + 'label' => esc_html__( 'Download', 'visualizer' ), | |
| 169 | + 'title' => esc_html__( 'Download as an image', 'visualizer' ), | |
| 170 | + ), | |
| 109 | 171 | ) |
| 110 | 172 | ); |
| 173 | + | |
| 174 | + // backward compatibility before label and title attributes were introduced. | |
| 175 | + if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) { | |
| 176 | + $actions['edit'] = array( | |
| 177 | + 'label' => esc_html__( 'Edit', 'visualizer' ), | |
| 178 | + 'title' => esc_html__( 'Edit data', 'visualizer' ), | |
| 179 | + ); | |
| 180 | + } | |
| 181 | + return $actions; | |
| 111 | 182 | } |
| 112 | 183 | |
| 113 | 184 | /** |
| 114 | 185 | * The print button |
| @@ -139,8 +210,23 @@ | ||
| 139 | 210 | break; |
| 140 | 211 | case 'xls': |
| 141 | 212 | $data = $this->_getDataAs( $chart_id, 'xls' ); |
| 142 | 213 | break; |
| 214 | + case 'image': | |
| 215 | + $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 216 | + $title = 'visualizer#' . $chart_id; | |
| 217 | + if ( ! empty( $settings['title'] ) ) { | |
| 218 | + $title = $settings['title']; | |
| 219 | + } | |
| 220 | + // for ChartJS, title is an array. | |
| 221 | + if ( is_array( $title ) && isset( $title['text'] ) ) { | |
| 222 | + $title = $title['text']; | |
| 223 | + } | |
| 224 | + if ( empty( $title ) ) { | |
| 225 | + $title = 'visualizer#' . $chart_id; | |
| 226 | + } | |
| 227 | + $data = array( 'name' => $title ); | |
| 228 | + break; | |
| 143 | 229 | default: |
| 144 | 230 | $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this ); |
| 145 | 231 | break; |
| 146 | 232 | } |
| @@ -157,9 +243,8 @@ | ||
| 157 | 243 | * @access public |
| 158 | 244 | */ |
| 159 | 245 | public function enqueueScripts() { |
| 160 | 246 | 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 | 247 | wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION ); |
| 163 | 248 | do_action( 'visualizer_pro_frontend_load_resources' ); |
| 164 | 249 | } |
| 165 | 250 | |
| @@ -179,27 +264,38 @@ | ||
| 179 | 264 | public function renderChart( $atts ) { |
| 180 | 265 | global $wp_version; |
| 181 | 266 | $atts = shortcode_atts( |
| 182 | 267 | 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 | |
| 268 | + // chart id | |
| 269 | + 'id' => false, | |
| 270 | + // chart class | |
| 271 | + 'class' => false, | |
| 272 | + // for lazy load | |
| 273 | + // set 'yes' to use the default intersection limit (300px) | |
| 274 | + // OR set a number (e.g. 700) to use 700px as the intersection limit | |
| 275 | + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 276 | + // Use image chart | |
| 277 | + 'use_image' => 'no', | |
| 188 | 278 | ), |
| 189 | 279 | $atts |
| 190 | 280 | ); |
| 191 | 281 | |
| 192 | - // 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 ) { | |
| 282 | + $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] ); | |
| 283 | + // if empty chart does not exists, then return empty string. | |
| 284 | + if ( ! $chart_data ) { | |
| 194 | 285 | return ''; |
| 195 | 286 | } |
| 196 | 287 | |
| 288 | + $chart = $chart_data['chart']; | |
| 289 | + $type = $chart_data['type']; | |
| 290 | + $series = $chart_data['series']; | |
| 291 | + // do not show the chart? | |
| 197 | 292 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| 198 | 293 | return ''; |
| 199 | 294 | } |
| 200 | 295 | |
| 201 | 296 | // in case revisions exist. |
| 297 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 202 | 298 | if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) { |
| 203 | 299 | $chart = get_post( $chart->ID ); |
| 204 | 300 | } |
| 205 | 301 | |
| @@ -205,37 +301,37 @@ | ||
| 205 | 301 | |
| 206 | 302 | $id = 'visualizer-' . $atts['id']; |
| 207 | 303 | $defaultClass = 'visualizer-front'; |
| 208 | 304 | $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 | 305 | |
| 212 | - $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 306 | + $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : ''; | |
| 213 | 307 | |
| 308 | + $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass ); | |
| 309 | + $attributes = array(); | |
| 310 | + if ( ! empty( $class ) ) { | |
| 311 | + $attributes['class'] = trim( $class ); | |
| 312 | + } | |
| 313 | + | |
| 314 | + if ( ctype_digit( $atts['lazy'] ) ) { | |
| 315 | + $attributes['data-lazy-limit'] = $atts['lazy']; | |
| 316 | + } | |
| 317 | + | |
| 214 | 318 | $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false ); |
| 215 | 319 | |
| 216 | - // fetch and update settings | |
| 217 | - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 320 | + // Get and update settings. | |
| 321 | + $settings = $chart_data['settings']; | |
| 218 | 322 | if ( empty( $settings['height'] ) ) { |
| 219 | 323 | $settings['height'] = '400'; |
| 220 | 324 | } |
| 221 | 325 | |
| 222 | 326 | // handle series filter hooks |
| 223 | - $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 | - } | |
| 327 | + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type ); | |
| 328 | + | |
| 227 | 329 | // handle settings filter hooks |
| 228 | 330 | $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 | 331 | |
| 233 | 332 | // 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 | - } | |
| 333 | + $data = self::get_chart_data( $chart, $type ); | |
| 238 | 334 | |
| 239 | 335 | $css = ''; |
| 240 | 336 | $arguments = $this->get_inline_custom_css( $id, $settings ); |
| 241 | 337 | if ( ! empty( $arguments ) ) { |
| @@ -246,8 +342,24 @@ | ||
| 246 | 342 | $library = $this->load_chart_type( $chart->ID ); |
| 247 | 343 | |
| 248 | 344 | $id = $id . '-' . rand(); |
| 249 | 345 | |
| 346 | + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME ); | |
| 347 | + if ( $amp && $amp->is_amp() ) { | |
| 348 | + return '<div data-amp-auto-lightbox-disable id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>'; | |
| 349 | + } | |
| 350 | + | |
| 351 | + if ( 'yes' === $atts['use_image'] ) { | |
| 352 | + $chart_image = $chart_data['chart_image']; | |
| 353 | + if ( $chart_image ) { | |
| 354 | + return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>'; | |
| 355 | + } | |
| 356 | + } | |
| 357 | + // Unset chart image base64 string. | |
| 358 | + if ( isset( $settings['chart-img'] ) ) { | |
| 359 | + unset( $settings['chart-img'] ); | |
| 360 | + } | |
| 361 | + | |
| 250 | 362 | // add chart to the array |
| 251 | 363 | $this->_charts[ $id ] = array( |
| 252 | 364 | 'type' => $type, |
| 253 | 365 | 'series' => $series, |
| @@ -255,33 +367,8 @@ | ||
| 255 | 367 | 'data' => $data, |
| 256 | 368 | 'library' => $library, |
| 257 | 369 | ); |
| 258 | 370 | |
| 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 | 371 | $actions_div = ''; |
| 285 | 372 | $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] ); |
| 286 | 373 | if ( ! empty( $actions_visible ) ) { |
| 287 | 374 | $actions = $this->get_actions(); |
| @@ -293,15 +380,16 @@ | ||
| 293 | 380 | $array = explode( ';', $action_type ); |
| 294 | 381 | $key = $array[0]; |
| 295 | 382 | $mime = end( $array ); |
| 296 | 383 | } |
| 297 | - $label = $actions[ $key ]; | |
| 298 | - $actions_div .= '<a href="#" class="visualizer-action visualizer-action-' . $key . '" data-visualizer-type="' . $key . '" data-visualizer-chart-id="' . $atts['id'] . '" data-visualizer-container-id="' . $id . '" data-visualizer-mime="' . $mime . '" title="' . $label . '" '; | |
| 384 | + $label = $actions[ $key ]['label']; | |
| 385 | + $title = $actions[ $key ]['title']; | |
| 386 | + $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 ); | |
| 299 | 387 | |
| 300 | 388 | if ( 'copy' === $key ) { |
| 301 | 389 | $copy = $this->_getDataAs( $atts['id'], 'csv' ); |
| 302 | 390 | $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"'; |
| 303 | - wp_enqueue_script( 'visualizer-clipboardjs' ); | |
| 391 | + wp_enqueue_script( 'clipboard' ); | |
| 304 | 392 | } |
| 305 | 393 | |
| 306 | 394 | $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] ); |
| 307 | 395 | $actions_div .= '>' . $label . '</a> '; |
| @@ -311,8 +399,213 @@ | ||
| 311 | 399 | } |
| 312 | 400 | |
| 313 | 401 | $actions_div .= $css; |
| 314 | 402 | |
| 403 | + $_charts = array(); | |
| 404 | + $_charts_type = ''; | |
| 405 | + foreach ( $this->_charts as $id => $array ) { | |
| 406 | + $_charts = $this->_charts; | |
| 407 | + $library = $array['library']; | |
| 408 | + $_charts_type = $library; | |
| 409 | + if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) { | |
| 410 | + wp_register_script( | |
| 411 | + "visualizer-render-$library", | |
| 412 | + VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 413 | + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 414 | + Visualizer_Plugin::VERSION, | |
| 415 | + true | |
| 416 | + ); | |
| 417 | + wp_enqueue_script( "visualizer-render-$library" ); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + if ( wp_script_is( "visualizer-render-$_charts_type" ) ) { | |
| 421 | + wp_localize_script( | |
| 422 | + "visualizer-render-$_charts_type", | |
| 423 | + 'visualizer', | |
| 424 | + array( | |
| 425 | + 'charts' => $_charts, | |
| 426 | + 'language' => $this->get_language(), | |
| 427 | + 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 428 | + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 429 | + 'wp_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 430 | + 'i10n' => array( | |
| 431 | + 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ), | |
| 432 | + ), | |
| 433 | + 'page_type' => 'frontend', | |
| 434 | + 'is_front' => true, | |
| 435 | + ) | |
| 436 | + ); | |
| 437 | + } | |
| 438 | + wp_enqueue_style( 'visualizer-front' ); | |
| 439 | + | |
| 315 | 440 | // return placeholder div |
| 316 | - return $actions_div . '<div id="' . $id . '"' . $class . '></div>'; | |
| 441 | + return $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ); | |
| 442 | + } | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Converts the array of attributes to a string of HTML attributes. | |
| 446 | + * | |
| 447 | + * @since ? | |
| 448 | + * | |
| 449 | + * @access private | |
| 450 | + */ | |
| 451 | + private function getHtmlAttributes( $attributes ) { | |
| 452 | + $string = ''; | |
| 453 | + if ( ! $attributes ) { | |
| 454 | + return $string; | |
| 455 | + } | |
| 456 | + | |
| 457 | + foreach ( $attributes as $name => $value ) { | |
| 458 | + $string .= sprintf( '%s="%s"', esc_attr( $name ), esc_attr( $value ) ); | |
| 459 | + } | |
| 460 | + return $string; | |
| 461 | + } | |
| 462 | + | |
| 463 | + /** | |
| 464 | + * Adds the schema corresponding to the dataset. | |
| 465 | + * | |
| 466 | + * @since 3.3.2 | |
| 467 | + * | |
| 468 | + * @access private | |
| 469 | + */ | |
| 470 | + private function addSchema( $id ) { | |
| 471 | + // should we show informative errors as HTML comments? | |
| 472 | + $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id ); | |
| 473 | + | |
| 474 | + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 475 | + $title = ''; | |
| 476 | + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) { | |
| 477 | + $title = $settings['title']; | |
| 478 | + if ( is_array( $title ) ) { | |
| 479 | + $title = $settings['title']['text']; | |
| 480 | + } | |
| 481 | + } | |
| 482 | + $title = apply_filters( 'visualizer_schema_name', $title, $id ); | |
| 483 | + if ( empty( $title ) ) { | |
| 484 | + if ( $show_errors ) { | |
| 485 | + return "<!-- Not showing structured data for chart $id because title is empty -->"; | |
| 486 | + } | |
| 487 | + return ''; | |
| 488 | + } | |
| 489 | + | |
| 490 | + $desc = ''; | |
| 491 | + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) { | |
| 492 | + $desc = $settings['description']; | |
| 493 | + } | |
| 494 | + $desc = apply_filters( 'visualizer_schema_description', $desc, $id ); | |
| 495 | + if ( empty( $desc ) ) { | |
| 496 | + if ( $show_errors ) { | |
| 497 | + return "<!-- Not showing structured data for chart $id because description is empty -->"; | |
| 498 | + } | |
| 499 | + return ''; | |
| 500 | + } | |
| 501 | + | |
| 502 | + // descriptions below 50 chars are not allowed. | |
| 503 | + if ( strlen( $desc ) < 50 ) { | |
| 504 | + if ( $show_errors ) { | |
| 505 | + return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->"; | |
| 506 | + } | |
| 507 | + return ''; | |
| 508 | + } | |
| 509 | + | |
| 510 | + $license = ''; | |
| 511 | + if ( isset( $settings['license'] ) && ! empty( $settings['license'] ) ) { | |
| 512 | + $license = $settings['license']; | |
| 513 | + if ( is_array( $license ) ) { | |
| 514 | + $license = $settings['license']['text']; | |
| 515 | + } | |
| 516 | + } | |
| 517 | + $license = apply_filters( 'visualizer_schema_license', $license, $id ); | |
| 518 | + if ( empty( $license ) ) { | |
| 519 | + if ( $show_errors ) { | |
| 520 | + return "<!-- Not showing structured data for chart $id because license is empty -->"; | |
| 521 | + } | |
| 522 | + return ''; | |
| 523 | + } | |
| 524 | + | |
| 525 | + $creator = ''; | |
| 526 | + if ( isset( $settings['creator'] ) && ! empty( $settings['creator'] ) ) { | |
| 527 | + $creator = $settings['creator']; | |
| 528 | + if ( is_array( $creator ) ) { | |
| 529 | + $creator = $settings['creator']['text']; | |
| 530 | + } | |
| 531 | + } | |
| 532 | + $creator = apply_filters( 'visualizer_schema_creator', $creator, $id ); | |
| 533 | + if ( empty( $creator ) ) { | |
| 534 | + if ( $show_errors ) { | |
| 535 | + return "<!-- Not showing structured data for chart $id because creator is empty -->"; | |
| 536 | + } | |
| 537 | + return ''; | |
| 538 | + } | |
| 539 | + | |
| 540 | + $schema = apply_filters( | |
| 541 | + 'visualizer_schema', | |
| 542 | + '{ | |
| 543 | + "@context":"https://schema.org/", | |
| 544 | + "@type":"Dataset", | |
| 545 | + "name":"' . esc_html( $title ) . '", | |
| 546 | + "description":"' . esc_html( $desc ) . '", | |
| 547 | + "license": "' . esc_html( $license ) . '", | |
| 548 | + "creator": { | |
| 549 | + "@type": "Person", | |
| 550 | + "name": "' . esc_html( $creator ) . '" | |
| 551 | + } | |
| 552 | + }', | |
| 553 | + $id | |
| 554 | + ); | |
| 555 | + | |
| 556 | + if ( empty( $schema ) ) { | |
| 557 | + return ''; | |
| 558 | + } | |
| 559 | + | |
| 560 | + return '<script type="application/ld+json">' . $schema . '</script>'; | |
| 561 | + } | |
| 562 | + | |
| 563 | + /** | |
| 564 | + * Get chart by ID. | |
| 565 | + * | |
| 566 | + * @param string $cache_key Cache key. | |
| 567 | + * @param int $chart_id Chart ID. | |
| 568 | + * @return mixed | |
| 569 | + */ | |
| 570 | + private function getChartData( $cache_key = '', $chart_id = 0 ) { | |
| 571 | + if ( ! $chart_id ) { | |
| 572 | + return false; | |
| 573 | + } | |
| 574 | + // Create unique cache key of each chart. | |
| 575 | + $cache_key .= '_' . $chart_id; | |
| 576 | + // Get chart from cache. | |
| 577 | + $chart = get_transient( $cache_key ); | |
| 578 | + if ( $chart ) { | |
| 579 | + return $chart; | |
| 580 | + } | |
| 581 | + | |
| 582 | + // Get chart by ID. | |
| 583 | + $chart = get_post( $chart_id ); | |
| 584 | + if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) { | |
| 585 | + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 586 | + $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ); | |
| 587 | + | |
| 588 | + if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) { | |
| 589 | + $diff_total_series = abs( count( $settings['series'] ) - count( $series ) ); | |
| 590 | + if ( $diff_total_series ) { | |
| 591 | + foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) { | |
| 592 | + $settings['series'][] = end( $settings['series'] ); | |
| 593 | + } | |
| 594 | + } | |
| 595 | + } | |
| 596 | + $chart_data = array( | |
| 597 | + 'chart' => $chart, | |
| 598 | + 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ), | |
| 599 | + 'settings' => $settings, | |
| 600 | + 'series' => $series, | |
| 601 | + 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ), | |
| 602 | + ); | |
| 603 | + | |
| 604 | + // Put the results in a transient. Expire after 12 hours. | |
| 605 | + set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) ); | |
| 606 | + return $chart_data; | |
| 607 | + } | |
| 608 | + | |
| 609 | + return false; | |
| 317 | 610 | } |
| 318 | 611 | } |