@@ -94,13 +94,20 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
94
94
* @param string $size The image size data.
95
95
*/
96
96
$ filter = static function ( $ sizes , $ size ) use ( $ block ) {
97
-
98
97
$ id = isset ( $ block ->attributes ['id ' ] ) ? (int ) $ block ->attributes ['id ' ] : 0 ;
99
98
$ alignment = $ block ->attributes ['align ' ] ?? '' ;
100
99
$ width = isset ( $ block ->attributes ['width ' ] ) ? (int ) $ block ->attributes ['width ' ] : 0 ;
101
100
$ max_alignment = $ block ->context ['max_alignment ' ] ?? '' ;
102
101
$ container_relative_width = $ block ->context ['container_relative_width ' ] ?? 1.0 ;
103
102
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_attachment_id ( $ block ->context ['postId ' ] );
109
+ }
110
+
104
111
/*
105
112
* Update width for cover block.
106
113
* 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
118
125
// Hook this filter early, before default filters are run.
119
126
add_filter ( 'wp_calculate_image_sizes ' , $ filter , 9 , 2 );
120
127
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_attachment_id ( $ block ->context ['postId ' ] );
137
+ }
138
+
121
139
$ sizes = wp_calculate_image_sizes (
122
140
// If we don't have a size slug, assume the full size was used.
123
141
$ parsed_block ['attrs ' ]['sizeSlug ' ] ?? 'full ' ,
124
142
null ,
125
143
null ,
126
- $ parsed_block [ ' attrs ' ][ ' id ' ] ?? 0
144
+ $ id
127
145
);
128
146
129
147
remove_filter ( 'wp_calculate_image_sizes ' , $ filter , 9 );
@@ -284,11 +302,12 @@ function auto_sizes_get_layout_width( string $alignment ): string {
284
302
function auto_sizes_filter_uses_context ( array $ uses_context , WP_Block_Type $ block_type ): array {
285
303
// Define block-specific context usage.
286
304
$ 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 ' ),
292
311
);
293
312
294
313
if ( isset ( $ block_specific_context [ $ block_type ->name ] ) ) {
@@ -316,6 +335,7 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ?
316
335
$ provider_blocks = array (
317
336
'core/columns ' ,
318
337
'core/group ' ,
338
+ 'core/post-featured-image ' ,
319
339
);
320
340
321
341
if ( in_array ( $ block ['blockName ' ], $ provider_blocks , true ) ) {
@@ -360,3 +380,19 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ?
360
380
}
361
381
return $ context ;
362
382
}
383
+
384
+ /**
385
+ * Retrieves the featured image attachment ID for a given post ID.
386
+ *
387
+ * @since n.e.x.t
388
+ *
389
+ * @param int $post_id The post ID.
390
+ * @return int The featured image attachment ID or 0 if not found.
391
+ */
392
+ function auto_sizes_get_featured_image_attachment_id ( int $ post_id ): int {
393
+ if ( 0 === $ post_id ) {
394
+ return 0 ;
395
+ }
396
+
397
+ return (int ) get_post_thumbnail_id ( $ post_id );
398
+ }
0 commit comments