From 27b625fa071dd4920fa492228f180f0a006a3ad4 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 25 Apr 2022 00:51:49 +0100 Subject: [PATCH 1/4] Ensure that post thumbnail is cached in post template block. --- packages/block-library/src/post-template/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 725c805148d82..53f2ec3e3cd48 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -39,6 +39,7 @@ function render_block_core_post_template( $attributes, $content, $block ) { if ( ! $query->have_posts() ) { return ''; } + update_post_thumbnail_cache( $query ); $classnames = ''; if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { From d38e6474745375345c1a5640a08858a2c7b4b3b9 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 6 May 2022 00:00:08 +0100 Subject: [PATCH 2/4] Check for inner blocks that use featured images. --- .../block-library/src/post-template/index.php | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 53f2ec3e3cd48..99d05dd42f5be 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -5,6 +5,35 @@ * @package WordPress */ +/** + * Loop through recursively to find blocks that use featured images. + * + * @param WP_Block_List $inner_blocks Inner block instance. + * + * @return bool + */ +function block_core_post_template_uses_feature_image( $inner_blocks ) { + $inner_blocks_array = iterator_to_array( $inner_blocks ); + foreach ( $inner_blocks_array as $block ) { + if ( 'core/post-featured-image' === $block->name ) { + $has_thumbnail = true; + break; + } + if ( 'core/cover' === $block->name && $block->attributes && isset( $block->attributes['useFeaturedImage'] ) && $block->attributes['useFeaturedImage'] ) { + $has_thumbnail = true; + break; + } + if ( $block->inner_blocks ) { + if ( block_core_post_template_uses_feature_image( $block->inner_blocks ) ) { + $has_thumbnail = true; + break; + } + } + } + + return $has_thumbnail; +} + /** * Renders the `core/post-template` block on the server. * @@ -39,7 +68,10 @@ function render_block_core_post_template( $attributes, $content, $block ) { if ( ! $query->have_posts() ) { return ''; } - update_post_thumbnail_cache( $query ); + + if ( block_core_post_template_uses_feature_image( $block->inner_blocks ) ) { + update_post_thumbnail_cache( $query ); + } $classnames = ''; if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { From 18a25c66e3e9174958d56c892dd68341a9060048 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 6 May 2022 00:10:28 +0100 Subject: [PATCH 3/4] Default value. --- packages/block-library/src/post-template/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 99d05dd42f5be..0e026535c93eb 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -14,6 +14,7 @@ */ function block_core_post_template_uses_feature_image( $inner_blocks ) { $inner_blocks_array = iterator_to_array( $inner_blocks ); + $has_thumbnail = false; foreach ( $inner_blocks_array as $block ) { if ( 'core/post-featured-image' === $block->name ) { $has_thumbnail = true; From 0a20bcb335f1ffefda4716b556b3176c25607750 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 6 May 2022 10:29:35 +0100 Subject: [PATCH 4/4] Improve logic. --- .../block-library/src/post-template/index.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 0e026535c93eb..418167e0de5ac 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -13,26 +13,19 @@ * @return bool */ function block_core_post_template_uses_feature_image( $inner_blocks ) { - $inner_blocks_array = iterator_to_array( $inner_blocks ); - $has_thumbnail = false; - foreach ( $inner_blocks_array as $block ) { + foreach ( $inner_blocks as $block ) { if ( 'core/post-featured-image' === $block->name ) { - $has_thumbnail = true; - break; + return true; } if ( 'core/cover' === $block->name && $block->attributes && isset( $block->attributes['useFeaturedImage'] ) && $block->attributes['useFeaturedImage'] ) { - $has_thumbnail = true; - break; + return true; } - if ( $block->inner_blocks ) { - if ( block_core_post_template_uses_feature_image( $block->inner_blocks ) ) { - $has_thumbnail = true; - break; - } + if ( $block->inner_blocks && block_core_post_template_uses_feature_image( $block->inner_blocks ) ) { + return true; } } - return $has_thumbnail; + return false; } /**