Skip to content

Commit

Permalink
Merge pull request #157 from globeandmail/fix/cache-issue-sit-2704
Browse files Browse the repository at this point in the history
Add cache to the curated posts retrieved
  • Loading branch information
Rahmon authored Dec 22, 2021
2 parents ef0ce83 + e0f3329 commit 70014db
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions includes/blocks/site-automation-block/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ function render_block_callback( $attributes, $content, $block ) {
$page_name = sanitize_title( $attributes['pageName'] );
$widget_name = sanitize_title( $attributes['widgetName'] );

// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_posts_get_posts
$curated_posts = get_posts(
[
'sophi_curated_page' => $page_name,
'sophi_curated_widget' => $widget_name,
'suppress_filters' => false,
]
);
$curated_posts_transient_key = 'sophi_curated_posts_' . $page_name . '_' . $widget_name;

if ( empty( $curated_posts ) ) {
return '';
$curated_posts = get_transient( $curated_posts_transient_key );

if ( false === $curated_posts ) {
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_posts_get_posts
$curated_posts = get_posts(
[
'sophi_curated_page' => $page_name,
'sophi_curated_widget' => $widget_name,
]
);

if ( empty( $curated_posts ) ) {
return '';
}

set_transient( $curated_posts_transient_key, $curated_posts, 5 * MINUTE_IN_SECONDS );
}

ob_start();
Expand Down

0 comments on commit 70014db

Please sign in to comment.