| @@ -276,13 +276,18 @@ | ||
| 276 | 276 | 'use_image' => 'no', |
| 277 | 277 | ), |
| 278 | 278 | $atts |
| 279 | 279 | ); |
| 280 | - // if empty id or chart does not exists, then return empty string | |
| 281 | - if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 280 | + | |
| 281 | + $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] ); | |
| 282 | + // if empty chart does not exists, then return empty string. | |
| 283 | + if ( ! $chart_data ) { | |
| 282 | 284 | return ''; |
| 283 | 285 | } |
| 284 | 286 | |
| 287 | + $chart = $chart_data['chart']; | |
| 288 | + $type = $chart_data['type']; | |
| 289 | + $series = $chart_data['series']; | |
| 285 | 290 | // do not show the chart? |
| 286 | 291 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| 287 | 292 | return ''; |
| 288 | 293 | } |
| @@ -308,20 +313,18 @@ | ||
| 308 | 313 | if ( ctype_digit( $atts['lazy'] ) ) { |
| 309 | 314 | $attributes['data-lazy-limit'] = $atts['lazy']; |
| 310 | 315 | } |
| 311 | 316 | |
| 312 | - $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 313 | - | |
| 314 | 317 | $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false ); |
| 315 | 318 | |
| 316 | - // fetch and update settings | |
| 317 | - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 319 | + // Get and update settings. | |
| 320 | + $settings = $chart_data['settings']; | |
| 318 | 321 | if ( empty( $settings['height'] ) ) { |
| 319 | 322 | $settings['height'] = '400'; |
| 320 | 323 | } |
| 321 | 324 | |
| 322 | 325 | // handle series filter hooks |
| 323 | - $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); | |
| 326 | + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type ); | |
| 324 | 327 | |
| 325 | 328 | // handle settings filter hooks |
| 326 | 329 | $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); |
| 327 | 330 | |
| @@ -344,9 +347,9 @@ | ||
| 344 | 347 | return '<div data-amp-auto-lightbox-disable id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>'; |
| 345 | 348 | } |
| 346 | 349 | |
| 347 | 350 | if ( 'yes' === $atts['use_image'] ) { |
| 348 | - $chart_image = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ); | |
| 351 | + $chart_image = $chart_data['chart_image']; | |
| 349 | 352 | if ( $chart_image ) { |
| 350 | 353 | return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>'; |
| 351 | 354 | } |
| 352 | 355 | } |
| @@ -542,6 +545,43 @@ | ||
| 542 | 545 | return ''; |
| 543 | 546 | } |
| 544 | 547 | |
| 545 | 548 | return '<script type="application/ld+json">' . $schema . '</script>'; |
| 549 | + } | |
| 550 | + | |
| 551 | + /** | |
| 552 | + * Get chart by ID. | |
| 553 | + * | |
| 554 | + * @param string $cache_key Cache key. | |
| 555 | + * @param int $chart_id Chart ID. | |
| 556 | + * @return mixed | |
| 557 | + */ | |
| 558 | + private function getChartData( $cache_key = '', $chart_id = 0 ) { | |
| 559 | + if ( ! $chart_id ) { | |
| 560 | + return false; | |
| 561 | + } | |
| 562 | + // Create unique cache key of each chart. | |
| 563 | + $cache_key .= '_' . $chart_id; | |
| 564 | + // Get chart from cache. | |
| 565 | + $chart = get_transient( $cache_key ); | |
| 566 | + if ( $chart ) { | |
| 567 | + return $chart; | |
| 568 | + } | |
| 569 | + | |
| 570 | + // Get chart by ID. | |
| 571 | + $chart = get_post( $chart_id ); | |
| 572 | + if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) { | |
| 573 | + $chart_data = array( | |
| 574 | + 'chart' => $chart, | |
| 575 | + 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ), | |
| 576 | + 'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ), | |
| 577 | + 'series' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), | |
| 578 | + 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ), | |
| 579 | + ); | |
| 580 | + // Put the results in a transient. Expire after 12 hours. | |
| 581 | + set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) ); | |
| 582 | + return $chart_data; | |
| 583 | + } | |
| 584 | + | |
| 585 | + return false; | |
| 546 | 586 | } |
| 547 | 587 | } |