Skip to content
Draft
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
36 changes: 36 additions & 0 deletions plugins/auto-sizes/includes/improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo
'core/group' => array( 'max_alignment' ),
'core/columns' => array( 'max_alignment', 'column_count', 'container_relative_width' ),
'core/column' => array( 'max_alignment' ),
'core/gallery' => array( 'max_alignment', 'gallery_column_count', 'container_relative_width' ),
);

if ( isset( $block_specific_context[ $block_type->name ] ) ) {
Expand Down Expand Up @@ -344,6 +345,7 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ?
'core/columns',
'core/group',
'core/post-featured-image',
'core/gallery',
);

if ( in_array( $block['blockName'], $provider_blocks, true ) ) {
Expand All @@ -355,6 +357,40 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ?
$context['max_alignment'] = $constraints[ $context['max_alignment'] ] > $constraints[ $alignment ] ? $context['max_alignment'] : $alignment;
}

if ( 'core/gallery' === $block['blockName'] ) {
// Get column count, if explicitly set.
if ( isset( $block['attrs']['columns'] ) && '' !== $block['attrs']['columns'] ) {
$context['gallery_column_count'] = $block['attrs']['columns'] ?? '';
}

/*
* If column count is not explicitly set, use the number of inner blocks as a fallback,
* but only for up to 3 images to avoid incorrect context for larger galleries.
*/
$gallery_image_block_count = count( $block['innerBlocks'] );
if ( $gallery_image_block_count <= 3 ) {
$context['gallery_column_count'] = $gallery_image_block_count;
}
}

if ( 'core/image' === $block['blockName'] ) {
$current_width = 1.0;
if ( isset( $parent_block->context['gallery_column_count'] ) && $parent_block->context['gallery_column_count'] ) {
// Default to equally divided width if not explicitly set.
$current_width = $current_width / $parent_block->context['gallery_column_count'];
}

// Multiply with parent's width if available.
if (
isset( $parent_block->context['container_relative_width'] ) &&
( $current_width > 0.0 || $current_width < 1.0 )
) {
$context['container_relative_width'] = $parent_block->context['container_relative_width'] * $current_width;
} else {
$context['container_relative_width'] = $current_width;
}
}

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