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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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 .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"core": null,
"core": "WordPress/WordPress#master",
"plugins": [
"./plugins/optimization-detective",
"./plugins/auto-sizes",
Expand Down
2 changes: 1 addition & 1 deletion plugins/auto-sizes/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,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 44 in plugins/auto-sizes/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/auto-sizes/hooks.php#L44

Added line #L44 was not covered by tests
91 changes: 62 additions & 29 deletions plugins/auto-sizes/includes/improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* @since 1.4.0
*/

/*
* Map alignment values to a weighting value so they can be compared.
* Note that 'left' and 'right' alignments are only constrained by max alignment.
*/
const AUTO_SIZES_CONSTRAINTS = array(
'full' => 0,
'wide' => 1,
'left' => 2,
'right' => 2,
'default' => 3,
'center' => 3,
);

Check warning on line 20 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#L13-L20

Added lines #L13 - L20 were not covered by tests

/**
* Primes attachment into the cache with a single database query.
*
Expand Down Expand Up @@ -172,18 +185,8 @@
// Normalize default alignment values.
$align = '' !== $align ? $align : 'default';

/*
* Map alignment values to a weighting value so they can be compared.
* Note that 'left' and 'right' alignments are only constrained by max alignment.
*/
$constraints = array(
'full' => 0,
'wide' => 1,
'left' => 2,
'right' => 2,
'default' => 3,
'center' => 3,
);
// Use the defined constant for constraints.
$constraints = AUTO_SIZES_CONSTRAINTS;

$alignment = $constraints[ $align ] > $constraints[ $max_alignment ] ? $align : $max_alignment;

Expand Down Expand Up @@ -255,15 +258,18 @@
* @return string[] The filtered context keys used by the block type.
*/
function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $block_type ): array {
// The list of blocks that can consume outer layout context.
$consumer_blocks = array(
'core/cover',
'core/image',
// Define block-specific context usage.
$block_specific_context = array(
'core/cover' => array( 'max_alignment', 'container_relative_width' ),
'core/image' => array( 'max_alignment', 'container_relative_width' ),
'core/group' => array( 'max_alignment' ),
'core/columns' => array( 'max_alignment', 'container_relative_width' ),
'core/column' => array( 'max_alignment', 'column_count' ),
);

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' ) ) ) );
if ( isset( $block_specific_context[ $block_type->name ] ) ) {
// Use array_values to reset array keys after merging.
return array_values( array_unique( array_merge( $uses_context, $block_specific_context[ $block_type->name ] ) ) );
}
return $uses_context;
}
Expand All @@ -273,11 +279,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 @@ -288,15 +295,41 @@
);

if ( in_array( $block['blockName'], $provider_blocks, true ) ) {
$alignment = $block['attrs']['align'] ?? '';
// Normalize default alignment values.
$alignment = isset( $block['attrs']['align'] ) && '' !== $block['attrs']['align'] ? $block['attrs']['align'] : 'default';
// Use the defined constant for constraints.
$constraints = AUTO_SIZES_CONSTRAINTS;

// If the container block doesn't have alignment, it's assumed to be 'default'.
if ( '' === $alignment ) {
$context['max_alignment'] = 'default';
} elseif ( 'wide' === $alignment ) {
$context['max_alignment'] = 'wide';
}
$context['max_alignment'] = $constraints[ $context['max_alignment'] ] > $constraints[ $alignment ] ? $context['max_alignment'] : $alignment;
}

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 308 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#L308

Added line #L308 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 ) {

Check warning on line 314 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#L312-L314

Added lines #L312 - L314 were not covered by tests
// Get column width, if explicitly set.
if ( isset( $block['attrs']['width'] ) && '' !== $block['attrs']['width'] ) {
$current_width = floatval( rtrim( $block['attrs']['width'], '%' ) ) / 100;
} elseif ( isset( $parent_block->context['column_count'] ) && $parent_block->context['column_count'] ) {

Check warning on line 318 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#L316-L318

Added lines #L316 - L318 were not covered by tests
// Default to equally divided width if not explicitly set.
$current_width = 1.0 / $parent_block->context['column_count'];

Check warning on line 320 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#L320

Added line #L320 was not covered by tests
} else {
// Full width fallback.
$current_width = 1.0;

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
}

// Multiply with parent's width if available.
if ( isset( $parent_block->context['container_relative_width'] ) ) {
$context['container_relative_width'] = $parent_block->context['container_relative_width'] * $current_width;

Check warning on line 328 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#L327-L328

Added lines #L327 - L328 were not covered by tests
} else {
$context['container_relative_width'] = $current_width;

Check warning on line 330 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#L330

Added line #L330 was not covered by tests
}
}
}
return $context;
}