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
42 changes: 42 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,46 @@ 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'] ) {
if ( wp_is_mobile() ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't rely on wp_is_mobile(), unfortunately. Page caching may not vary responses by device form factor. We'll need a solution that serves the same markup to all devices (the promise of responsive design).

// On mobile, the gallery is always 2 column.
$context['gallery_column_count'] = 2;
} elseif ( isset( $block['attrs']['columns'] ) && '' !== $block['attrs']['columns'] ) {
$context['gallery_column_count'] = $block['attrs']['columns'] ?? '';
} else {
/*
* Fallback to the inner block count if column count isn't explicitly set.
* Cap the columns at 3 to prevent layout issues, as the default gallery
* style wraps images to new lines after 3 columns.
* See https://github.com/WordPress/gutenberg/blob/46e0ceee86b66a6036c9e58568ce21bc1cf8b630/packages/block-library/src/gallery/style.scss#L167
*/
$gallery_image_block_count = count( $block['innerBlocks'] );
if ( $gallery_image_block_count <= 3 ) {
$context['gallery_column_count'] = $gallery_image_block_count;
} else {
$context['gallery_column_count'] = 3;
}
}
}

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