@@ -277,11 +277,16 @@ public function renderChart( $atts ) {
277277 ),
278278 $ atts
279279 );
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 ) {
282284 return '' ;
283285 }
284286
287+ $ chart = $ chart_data ['chart ' ];
288+ $ type = $ chart_data ['type ' ];
289+ $ series = $ chart_data ['series ' ];
285290 // do not show the chart?
286291 if ( ! apply_filters ( 'visualizer_pro_show_chart ' , true , $ atts ['id ' ] ) ) {
287292 return '' ;
@@ -309,18 +314,16 @@ public function renderChart( $atts ) {
309314 $ attributes ['data-lazy-limit ' ] = $ atts ['lazy ' ];
310315 }
311316
312- $ type = get_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_CHART_TYPE , true );
313-
314317 $ chart = apply_filters ( 'visualizer_schedule_refresh_chart ' , $ chart , $ chart ->ID , false );
315318
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 ' ] ;
318321 if ( empty ( $ settings ['height ' ] ) ) {
319322 $ settings ['height ' ] = '400 ' ;
320323 }
321324
322325 // 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 );
324327
325328 // handle settings filter hooks
326329 $ settings = apply_filters ( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS , $ settings , $ chart ->ID , $ type );
@@ -345,7 +348,7 @@ public function renderChart( $atts ) {
345348 }
346349
347350 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 ' ] ;
349352 if ( $ chart_image ) {
350353 return '<div id=" ' . $ id . '" ' . $ this ->getHtmlAttributes ( $ attributes ) . '> ' . wp_get_attachment_image ( $ chart_image , 'full ' ) . '</div> ' ;
351354 }
@@ -544,4 +547,41 @@ private function addSchema( $id ) {
544547
545548 return '<script type="application/ld+json"> ' . $ schema . '</script> ' ;
546549 }
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 ;
586+ }
547587}
0 commit comments