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 3 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
34 changes: 34 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,36 @@
* @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 );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$has_thumbnail = false;
foreach ( $inner_blocks_array as $block ) {
if ( 'core/post-featured-image' === $block->name ) {
$has_thumbnail = true;
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
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.
*
Expand Down Expand Up @@ -40,6 +70,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