Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
39a6488
Fix equal column gallery image sizes
mukeshpanchal27 Jun 12, 2026
de04562
Set column count for image without columns set
mukeshpanchal27 Jun 12, 2026
128c2bf
Update logic and inline doc
mukeshpanchal27 Jun 18, 2026
46bfea1
Set 2 columns for mobile devices
mukeshpanchal27 Jun 18, 2026
65a29f5
Add unit tests
mukeshpanchal27 Jun 19, 2026
9e2af13
Fix different column gallery image sizes
mukeshpanchal27 Jun 19, 2026
4019f63
Remove defaule for column
mukeshpanchal27 Jun 25, 2026
1c996ed
Add unit tests
mukeshpanchal27 Jun 25, 2026
a9e477c
Add more tests
mukeshpanchal27 Jun 25, 2026
c4c68db
Fix failed tests
mukeshpanchal27 Jun 25, 2026
170e226
Fix typos
mukeshpanchal27 Jun 25, 2026
5735a00
Update function signature
mukeshpanchal27 Jun 25, 2026
e5de953
Remove columns as required in unit tests
mukeshpanchal27 Jun 25, 2026
a43718d
Guard for > 0
mukeshpanchal27 Jun 26, 2026
20adc94
Address review feedbacks
mukeshpanchal27 Jun 26, 2026
03ef805
Update unit tests
mukeshpanchal27 Jun 26, 2026
cf3b240
Merge branch 'feature/2449-gallery-block' into fix/2449-equal-column
mukeshpanchal27 Jul 2, 2026
558335a
Merge branch 'fix/2449-equal-column' into fix/2449-different-columns
mukeshpanchal27 Jul 2, 2026
6280a0b
Remove dafaule from test function
mukeshpanchal27 Jul 3, 2026
c023675
Add check for isset
mukeshpanchal27 Jul 3, 2026
856846f
Update function doc block
mukeshpanchal27 Jul 3, 2026
fc56b77
Simplify logic
mukeshpanchal27 Jul 3, 2026
68cc81a
Correct inline comment format
mukeshpanchal27 Jul 3, 2026
41a9f78
Simplify logic
mukeshpanchal27 Jul 3, 2026
6a36dc8
Add check for instanceof WP_Block
mukeshpanchal27 Jul 3, 2026
8f02df1
Merge branch 'fix/2449-equal-column' into fix/2449-different-columns
mukeshpanchal27 Jul 3, 2026
ca242c9
Merge branch 'fix/2449-different-columns' of github.com:WordPress/per…
mukeshpanchal27 Jul 3, 2026
4cc381b
Address review feedbacks
mukeshpanchal27 Jul 3, 2026
8d589dd
Merge branch 'fix/2449-equal-column' into fix/2449-different-columns
mukeshpanchal27 Jul 3, 2026
4777b82
Simplify logic
mukeshpanchal27 Jul 3, 2026
cbec66b
Simplify logic
mukeshpanchal27 Jul 3, 2026
a7f0b48
Remove wp_is_mobile check
mukeshpanchal27 Jul 3, 2026
6990e4c
Merge branch 'fix/2449-equal-column' into fix/2449-different-columns
mukeshpanchal27 Jul 3, 2026
74c84f3
Apply suggestions from code review
mukeshpanchal27 Jul 10, 2026
8813370
Merge pull request #2543 from WordPress/fix/2449-different-columns
mukeshpanchal27 Jul 10, 2026
9bd12cc
Add check for WP_Block instance
mukeshpanchal27 Jul 10, 2026
c5ac72f
Remove explicit word from tests
mukeshpanchal27 Jul 10, 2026
358069a
Add wide align unit tests
mukeshpanchal27 Jul 11, 2026
9f3baf4
Use multiline comment
mukeshpanchal27 Jul 11, 2026
8d04b07
Use custom context instead galleryId
mukeshpanchal27 Jul 13, 2026
7e22de1
Address review feedbacks
mukeshpanchal27 Jul 13, 2026
2af7005
Update inline comment
mukeshpanchal27 Jul 13, 2026
a4f79e2
Merge pull request #2577 from WordPress/add/wide-align-unit-tests
mukeshpanchal27 Jul 18, 2026
5e1ba8b
Add full align gallery support and unit tests
mukeshpanchal27 Jul 20, 2026
37e15bb
Add test case
mukeshpanchal27 Jul 21, 2026
714535c
Update inline comment
mukeshpanchal27 Jul 21, 2026
003cf9b
Merge pull request #2587 from WordPress/add/full-align-unit-tests
mukeshpanchal27 Jul 29, 2026
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
83 changes: 82 additions & 1 deletion plugins/auto-sizes/includes/improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
$size = array( 420, 420 );
}

/*
* When a Gallery block has a wide or full alignment, child Image blocks inherit that
* alignment unless the child block explicitly sets its own alignment.
*/
$is_parent_gallery_aligned = (bool) ( $block->context['is_parent_aligned'] ?? false );
if ( $is_parent_gallery_aligned && '' === $alignment ) {
$alignment = $max_alignment;

/*
* A full alignment normally spans the whole viewport, but inside a narrower
* container (e.g. a column) the gallery is constrained to that container instead.
* The resulting width cannot be expressed reliably here, so keep the default sizes.
*/
if ( 1.0 !== (float) $container_relative_width && 'full' === $alignment ) {
return $sizes;
}
}

$better_sizes = auto_sizes_calculate_better_sizes( $id, $size, $alignment, $width, $max_alignment, $container_relative_width );

// If better sizes can't be calculated, use the default sizes.
Expand Down Expand Up @@ -311,11 +329,12 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo
// 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/image' => array( 'max_alignment', 'container_relative_width', 'is_parent_aligned' ),
'core/post-featured-image' => array( 'max_alignment', 'container_relative_width' ),
'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', 'gallery_total_images' ),
);

if ( isset( $block_specific_context[ $block_type->name ] ) ) {
Expand Down Expand Up @@ -344,6 +363,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 +375,67 @@ 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'] ) {
$gallery_image_block_count = count( $block['innerBlocks'] );
// Store the total number of inner images in the gallery context for child blocks to access.
$context['gallery_total_images'] = $gallery_image_block_count;

if ( isset( $block['attrs']['columns'] ) && is_numeric( $block['attrs']['columns'] ) && (int) $block['attrs']['columns'] > 0 ) {
$context['gallery_column_count'] = (int) $block['attrs']['columns'];
} elseif ( $gallery_image_block_count <= 3 ) {
/*
* 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
*/
$context['gallery_column_count'] = $gallery_image_block_count;
} else {
$context['gallery_column_count'] = 3;
}

// If the gallery is wide or full aligned, treat child Image blocks as aligned too.
$gallery_alignment = $block['attrs']['align'] ?? '';
$context['is_parent_aligned'] = in_array( $gallery_alignment, array( 'wide', 'full' ), true );
}

// Special handling for images inside galleries, as they have a different layout calculation that depends on the number of columns.
if ( 'core/image' === $block['blockName'] && $parent_block instanceof WP_Block && 'core/gallery' === $parent_block->parsed_block['blockName'] ) {
$columns = $parent_block->context['gallery_column_count'] ?? 0;
$total_images = $parent_block->context['gallery_total_images'] ?? 0;

$current_width = 1.0;

if ( $total_images > 0 && $columns > 0 ) {
// Sequential rendering tracker.
static $last_parent_gallery = null;
static $image_index = 0;

// If the parent block object changes, reset the index for the new gallery.
if ( $last_parent_gallery !== $parent_block ) {
$last_parent_gallery = $parent_block;
$image_index = 0;
}

$remainder = $total_images % $columns;
$full_rows_images_count = $total_images - $remainder;

if ( $remainder > 0 && $image_index >= $full_rows_images_count ) {
// Image falls in the last, incomplete row. Distribute evenly among leftovers.
$current_width = 1.0 / $remainder;
} else {
// Image falls within the perfect full grid rows.
$current_width = 1.0 / $columns;
}

// Move to the next image index for the next iteration.
++$image_index;
}

// Multiply with parent's width if available.
$context['container_relative_width'] = ( $parent_block->context['container_relative_width'] ?? 1 ) * $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
Loading