Skip to content

Commit 5f897cb

Browse files
Implement transient API for storing cache object #499
1 parent d37770f commit 5f897cb

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,12 @@ public function update_chart_data( $data ) {
651651

652652
wp_update_post( $chart );
653653

654+
// Clear existing chart cache.
655+
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $data['id'];
656+
if ( get_transient( $cache_key ) ) {
657+
delete_transient( $cache_key );
658+
}
659+
654660
$revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) );
655661

656662
if ( count( $revisions ) > 1 ) {

classes/Visualizer/Module/Chart.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,14 @@ public function renderChartPages() {
596596
$this->_chart = $this->handleExistingRevisions( $chart_id, $this->_chart );
597597
}
598598

599+
// Clear existing chart cache.
600+
if ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) {
601+
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
602+
if ( get_transient( $cache_key ) ) {
603+
delete_transient( $cache_key );
604+
}
605+
}
606+
599607
switch ( $tab ) {
600608
case 'settings':
601609
$this->_handleDataAndSettingsPage();

classes/Visualizer/Module/Frontend.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

classes/Visualizer/Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Visualizer_Plugin {
8989
const FILTER_UNDO_REVISIONS = 'visualizer-undo-revisions';
9090
const FILTER_HANDLE_REVISIONS = 'visualizer-handle-revisions';
9191
const FILTER_GET_CHART_DATA_AS = 'visualizer-get-chart-data-as';
92+
const FILTER_HANDLE_CACHE_EXPIRATION_TIME = 'visualizer-handle-expiration-time';
9293

9394
const CF_DB_SCHEDULE = 'visualizer-db-schedule';
9495
const CF_DB_QUERY = 'visualizer-db-query';
@@ -99,6 +100,8 @@ class Visualizer_Plugin {
99100
const PRO_TEASER_URL = 'https://themeisle.com/plugins/visualizer-charts-and-graphs/upgrade/#pricing';
100101
const PRO_TEASER_TITLE = 'Check PRO version ';
101102

103+
const CF_CHART_CACHE = 'visualizer-chart-cache';
104+
102105
/**
103106
* Name of the option for WordPress DB.
104107
*/

0 commit comments

Comments
 (0)