Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that post thumbnail is cached in post template block. #40572

Merged
merged 4 commits into from
May 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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