From cfccc141a4809bf12623e5a25532588c783a428d Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 13 Jan 2025 12:25:19 +0530 Subject: [PATCH 1/4] Add ancestor block context --- plugins/auto-sizes/hooks.php | 2 +- .../includes/improve-calculate-sizes.php | 55 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/plugins/auto-sizes/hooks.php b/plugins/auto-sizes/hooks.php index 55cbe9dec..2feab8901 100644 --- a/plugins/auto-sizes/hooks.php +++ b/plugins/auto-sizes/hooks.php @@ -39,4 +39,4 @@ function auto_sizes_render_generator(): void { add_filter( 'render_block_core/image', 'auto_sizes_filter_image_tag', 10, 3 ); add_filter( 'render_block_core/cover', 'auto_sizes_filter_image_tag', 10, 3 ); add_filter( 'get_block_type_uses_context', 'auto_sizes_filter_uses_context', 10, 2 ); -add_filter( 'render_block_context', 'auto_sizes_filter_render_block_context', 10, 2 ); +add_filter( 'render_block_context', 'auto_sizes_filter_render_block_context', 10, 3 ); diff --git a/plugins/auto-sizes/includes/improve-calculate-sizes.php b/plugins/auto-sizes/includes/improve-calculate-sizes.php index 7b57f08e3..a94c3e6f5 100644 --- a/plugins/auto-sizes/includes/improve-calculate-sizes.php +++ b/plugins/auto-sizes/includes/improve-calculate-sizes.php @@ -273,11 +273,12 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo * * @since 1.4.0 * - * @param array $context Current block context. - * @param array $block The block being rendered. + * @param array $context Current block context. + * @param array $block The block being rendered. + * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. * @return array Modified block context. */ -function auto_sizes_filter_render_block_context( array $context, array $block ): array { +function auto_sizes_filter_render_block_context( array $context, array $block, WP_Block $parent_block = null ): array { // When no max alignment is set, the maximum is assumed to be 'full'. $context['max_alignment'] = $context['max_alignment'] ?? 'full'; @@ -298,5 +299,53 @@ function auto_sizes_filter_render_block_context( array $context, array $block ): } } + static $block_width_data = array(); + + if ( null !== $parent_block ) { + $block_width_data = array(); + } + + $block_width_data = $parent_block->context['block_width_data'] ?? $block_width_data; + if ( 'core/group' === $block['blockName'] || 'core/columns' === $block['blockName'] ) { + if ( isset( $block['attrs']['align'] ) ) { + switch ( $block['attrs']['align'] ) { + case 'full': + $block_width_data[][ $block['blockName'] ] = 'full'; + break; + case 'wide': + $block_width_data[][ $block['blockName'] ] = 'wide'; // Use actual `wideWidth` here. + break; + default: + $block_width_data[][ $block['blockName'] ] = 'default'; // Use actual `contentWidth` here. + break; + } + } else { + $block_width_data[][ $block['blockName'] ] = 'default'; // Use actual `contentWidth` here. + } + } + + if ( 'core/columns' === $block['blockName'] ) { + // This is a special context key just to pass to the child 'core/column' block. + $context['column_count'] = count( $block['innerBlocks'] ); + } + + if ( 'core/column' === $block['blockName'] ) { + $found_image_block = wp_get_first_block( $block['innerBlocks'], 'core/image' ); + $found_cover_block = wp_get_first_block( $block['innerBlocks'], 'core/cover' ); + if ( count( $found_image_block ) > 0 || count( $found_cover_block ) > 0 ) { + if ( isset( $block['attrs']['width'] ) && '' !== $block['attrs']['width'] ) { + // Use specific column width. + $block_width_data[][ $block['blockName'] ] = $block['attrs']['width']; + } elseif ( isset( $parent_block->context['column_count'] ) && $parent_block->context['column_count'] ) { + // Determine the width based on equally sized columns. + $block_width_data[][ $block['blockName'] ] = '' . ( 100 / $parent_block->context['column_count'] ) . '%'; + } + } + } + + if ( $block_width_data ) { + $context['block_width_data'] = $block_width_data; + } + return $context; } From ae55c97a2da22d05fdb954f186c353e9c38d0a67 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 13 Jan 2025 12:56:24 +0530 Subject: [PATCH 2/4] Add block_width_data in to user context filter --- plugins/auto-sizes/includes/improve-calculate-sizes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/auto-sizes/includes/improve-calculate-sizes.php b/plugins/auto-sizes/includes/improve-calculate-sizes.php index a94c3e6f5..62e83d3f1 100644 --- a/plugins/auto-sizes/includes/improve-calculate-sizes.php +++ b/plugins/auto-sizes/includes/improve-calculate-sizes.php @@ -263,7 +263,7 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo if ( in_array( $block_type->name, $consumer_blocks, true ) ) { // Use array_values to reset the array keys after merging. - return array_values( array_unique( array_merge( $uses_context, array( 'max_alignment' ) ) ) ); + return array_values( array_unique( array_merge( $uses_context, array( 'max_alignment', 'block_width_data' ) ) ) ); } return $uses_context; } From f235cb56b711f9b6330297a29235960613183041 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 13 Jan 2025 14:09:01 +0530 Subject: [PATCH 3/4] Minor changes --- plugins/auto-sizes/includes/improve-calculate-sizes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/auto-sizes/includes/improve-calculate-sizes.php b/plugins/auto-sizes/includes/improve-calculate-sizes.php index 62e83d3f1..e06747c4e 100644 --- a/plugins/auto-sizes/includes/improve-calculate-sizes.php +++ b/plugins/auto-sizes/includes/improve-calculate-sizes.php @@ -278,7 +278,7 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. * @return array Modified block context. */ -function auto_sizes_filter_render_block_context( array $context, array $block, WP_Block $parent_block = null ): array { +function auto_sizes_filter_render_block_context( array $context, array $block, ?WP_Block $parent_block ): array { // When no max alignment is set, the maximum is assumed to be 'full'. $context['max_alignment'] = $context['max_alignment'] ?? 'full'; @@ -301,7 +301,7 @@ function auto_sizes_filter_render_block_context( array $context, array $block, W static $block_width_data = array(); - if ( null !== $parent_block ) { + if ( null === $parent_block ) { $block_width_data = array(); } From be6cd9f8f62d0bc0938a53fe06bfbf472be688a8 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 13 Jan 2025 14:43:39 +0530 Subject: [PATCH 4/4] Remove inline comment --- plugins/auto-sizes/includes/improve-calculate-sizes.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/auto-sizes/includes/improve-calculate-sizes.php b/plugins/auto-sizes/includes/improve-calculate-sizes.php index e06747c4e..a5922b7e1 100644 --- a/plugins/auto-sizes/includes/improve-calculate-sizes.php +++ b/plugins/auto-sizes/includes/improve-calculate-sizes.php @@ -313,14 +313,14 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ? $block_width_data[][ $block['blockName'] ] = 'full'; break; case 'wide': - $block_width_data[][ $block['blockName'] ] = 'wide'; // Use actual `wideWidth` here. + $block_width_data[][ $block['blockName'] ] = 'wide'; break; default: - $block_width_data[][ $block['blockName'] ] = 'default'; // Use actual `contentWidth` here. + $block_width_data[][ $block['blockName'] ] = 'default'; break; } } else { - $block_width_data[][ $block['blockName'] ] = 'default'; // Use actual `contentWidth` here. + $block_width_data[][ $block['blockName'] ] = 'default'; } }