Skip to content

Commit

Permalink
Ensure that post thumbnail is cached in post template block. (#40572)
Browse files Browse the repository at this point in the history
* Ensure that post thumbnail is cached in post template block.

* Check for inner blocks that use featured images.

* Default value.

* Improve logic.
  • Loading branch information
spacedmonkey committed May 6, 2022
1 parent 24af10d commit 955f4c2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@
* @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 ) {
foreach ( $inner_blocks as $block ) {
if ( 'core/post-featured-image' === $block->name ) {
return true;
}
if ( 'core/cover' === $block->name && $block->attributes && isset( $block->attributes['useFeaturedImage'] ) && $block->attributes['useFeaturedImage'] ) {
return true;
}
if ( $block->inner_blocks && block_core_post_template_uses_feature_image( $block->inner_blocks ) ) {
return true;
}
}

return false;
}

/**
* Renders the `core/post-template` block on the server.
*
Expand Down Expand Up @@ -40,6 +63,10 @@ function render_block_core_post_template( $attributes, $content, $block ) {
return '';
}

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'] ) ) {
if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
Expand Down

0 comments on commit 955f4c2

Please sign in to comment.