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

Accurate sizes: Add ancestor block context #1795

Draft
wants to merge 6 commits into
base: feature/1511-incorporate-layout-constraints-from-ancestors
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion plugins/auto-sizes/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
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 );

Check warning on line 42 in plugins/auto-sizes/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/auto-sizes/hooks.php#L42

Added line #L42 was not covered by tests
57 changes: 53 additions & 4 deletions plugins/auto-sizes/includes/improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@

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;
}
Expand All @@ -273,11 +273,12 @@
*
* @since 1.4.0
*
* @param array<string, mixed> $context Current block context.
* @param array<string, mixed> $block The block being rendered.
* @param array<string, mixed> $context Current block context.
* @param array<string, mixed> $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<string, mixed> 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 ): array {
// When no max alignment is set, the maximum is assumed to be 'full'.
$context['max_alignment'] = $context['max_alignment'] ?? 'full';

Expand All @@ -298,5 +299,53 @@
}
}

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';
break;
default:
$block_width_data[][ $block['blockName'] ] = 'default';
break;
}
} else {
$block_width_data[][ $block['blockName'] ] = 'default';

Check warning on line 323 in plugins/auto-sizes/includes/improve-calculate-sizes.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L323 was not covered by tests
}
}

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'] );

Check warning on line 329 in plugins/auto-sizes/includes/improve-calculate-sizes.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L329 was not covered by tests
}

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'] ) {

Check warning on line 336 in plugins/auto-sizes/includes/improve-calculate-sizes.php

View check run for this annotation

Codecov / codecov/patch

plugins/auto-sizes/includes/improve-calculate-sizes.php#L333-L336

Added lines #L333 - L336 were not covered by tests
// Use specific column width.
$block_width_data[][ $block['blockName'] ] = $block['attrs']['width'];
} elseif ( isset( $parent_block->context['column_count'] ) && $parent_block->context['column_count'] ) {

Check warning on line 339 in plugins/auto-sizes/includes/improve-calculate-sizes.php

View check run for this annotation

Codecov / codecov/patch

plugins/auto-sizes/includes/improve-calculate-sizes.php#L338-L339

Added lines #L338 - L339 were not covered by tests
// Determine the width based on equally sized columns.
$block_width_data[][ $block['blockName'] ] = '' . ( 100 / $parent_block->context['column_count'] ) . '%';

Check warning on line 341 in plugins/auto-sizes/includes/improve-calculate-sizes.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L341 was not covered by tests
}
}
}

if ( $block_width_data ) {
$context['block_width_data'] = $block_width_data;
}

return $context;
}
Loading