Skip to content

Commit 7d6bdff

Browse files
Updates sizes for core/post-featured-image block
1 parent 714dd71 commit 7d6bdff

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

plugins/auto-sizes/hooks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ function auto_sizes_render_generator(): void {
4040
add_filter( 'the_content', 'auto_sizes_prime_attachment_caches', 9 ); // This must run before 'do_blocks', which runs at priority 9.
4141
add_filter( 'render_block_core/image', 'auto_sizes_filter_image_tag', 10, 3 );
4242
add_filter( 'render_block_core/cover', 'auto_sizes_filter_image_tag', 10, 3 );
43+
add_filter( 'render_block_core/post-featured-image', 'auto_sizes_filter_image_tag', 10, 3 );
4344
add_filter( 'get_block_type_uses_context', 'auto_sizes_filter_uses_context', 10, 2 );
4445
add_filter( 'render_block_context', 'auto_sizes_filter_render_block_context', 10, 3 );

plugins/auto-sizes/includes/improve-calculate-sizes.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,20 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
9494
* @param string $size The image size data.
9595
*/
9696
$filter = static function ( $sizes, $size ) use ( $block ) {
97-
9897
$id = isset( $block->attributes['id'] ) ? (int) $block->attributes['id'] : 0;
9998
$alignment = $block->attributes['align'] ?? '';
10099
$width = isset( $block->attributes['width'] ) ? (int) $block->attributes['width'] : 0;
101100
$max_alignment = $block->context['max_alignment'] ?? '';
102101
$container_relative_width = $block->context['container_relative_width'] ?? 1.0;
103102

103+
/*
104+
* For the post featured image block, use the post ID to get the featured image attachment ID.
105+
* See https://github.com/WordPress/wordpress-develop/blob/3f9c6fce666ed2ea0d56c21f6235c37db3d91392/src/wp-includes/blocks/post-featured-image.php#L65
106+
*/
107+
if ( 'core/post-featured-image' === $block->name && isset( $block->context['postId'] ) ) {
108+
$id = auto_sizes_get_featured_image_id_from_block( $block );
109+
}
110+
104111
/*
105112
* Update width for cover block.
106113
* See https://github.com/WordPress/gutenberg/blob/938720602082dc50a1746bd2e33faa3d3a6096d4/packages/block-library/src/cover/style.scss#L82-L87.
@@ -118,12 +125,23 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
118125
// Hook this filter early, before default filters are run.
119126
add_filter( 'wp_calculate_image_sizes', $filter, 9, 2 );
120127

128+
// Get the image ID from the block attributes or context.
129+
$id = $parsed_block['attrs']['id'] ?? 0;
130+
131+
/*
132+
* For the post featured image block, use the post ID to get the featured image attachment ID.
133+
* See https://github.com/WordPress/wordpress-develop/blob/3f9c6fce666ed2ea0d56c21f6235c37db3d91392/src/wp-includes/blocks/post-featured-image.php#L65
134+
*/
135+
if ( 'core/post-featured-image' === $block->name && isset( $block->context['postId'] ) ) {
136+
$id = auto_sizes_get_featured_image_id_from_block( $block );
137+
}
138+
121139
$sizes = wp_calculate_image_sizes(
122140
// If we don't have a size slug, assume the full size was used.
123141
$parsed_block['attrs']['sizeSlug'] ?? 'full',
124142
null,
125143
null,
126-
$parsed_block['attrs']['id'] ?? 0
144+
$id
127145
);
128146

129147
remove_filter( 'wp_calculate_image_sizes', $filter, 9 );
@@ -284,11 +302,12 @@ function auto_sizes_get_layout_width( string $alignment ): string {
284302
function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $block_type ): array {
285303
// Define block-specific context usage.
286304
$block_specific_context = array(
287-
'core/cover' => array( 'max_alignment', 'container_relative_width' ),
288-
'core/image' => array( 'max_alignment', 'container_relative_width' ),
289-
'core/group' => array( 'max_alignment' ),
290-
'core/columns' => array( 'max_alignment', 'container_relative_width' ),
291-
'core/column' => array( 'max_alignment', 'column_count' ),
305+
'core/cover' => array( 'max_alignment', 'container_relative_width' ),
306+
'core/image' => array( 'max_alignment', 'container_relative_width' ),
307+
'core/post-featured-image' => array( 'max_alignment', 'container_relative_width' ),
308+
'core/group' => array( 'max_alignment' ),
309+
'core/columns' => array( 'max_alignment', 'container_relative_width' ),
310+
'core/column' => array( 'max_alignment', 'column_count' ),
292311
);
293312

294313
if ( isset( $block_specific_context[ $block_type->name ] ) ) {
@@ -316,6 +335,7 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ?
316335
$provider_blocks = array(
317336
'core/columns',
318337
'core/group',
338+
'core/post-featured-image',
319339
);
320340

321341
if ( in_array( $block['blockName'], $provider_blocks, true ) ) {
@@ -360,3 +380,24 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ?
360380
}
361381
return $context;
362382
}
383+
384+
/**
385+
* Retrieves the featured image ID for a post featured image block.
386+
*
387+
* @since n.e.x.t
388+
*
389+
* @param WP_Block $block The block instance.
390+
* @return int The featured image ID or 0 if not found.
391+
*/
392+
function auto_sizes_get_featured_image_id_from_block( WP_Block $block ): int {
393+
$image_size = $block->attributes['sizeSlug'] ?? 'full';
394+
$post_id = $block->context['postId'];
395+
396+
$post = get_post( $post_id );
397+
398+
if ( ! ( $post instanceof WP_Post ) ) {
399+
return 0;
400+
}
401+
402+
return (int) get_post_thumbnail_id( $post );
403+
}

0 commit comments

Comments
 (0)