Skip to content

Merge initial accurate sizes work into trunk #1329

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

Merged
merged 53 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
ebcf7c7
Initial implementation of improved image sizes algorithm
mukeshpanchal27 May 27, 2024
2f497ec
Remove empty lines
mukeshpanchal27 May 27, 2024
6d653b2
Use correct sizes for small images
mukeshpanchal27 May 28, 2024
bab71b6
Address review feedback
mukeshpanchal27 May 29, 2024
1151b98
Merge pull request #1268 from WordPress/trunk
swissspidy Jun 3, 2024
ee021aa
Merge branch 'feature/more-accurate-sizes-attribute' into fix/1187-ac…
mukeshpanchal27 Jun 3, 2024
237f2e8
Add unit tests
mukeshpanchal27 Jun 3, 2024
b267e1c
Address review feedback
mukeshpanchal27 Jun 4, 2024
a0e4e26
Update unit tests
mukeshpanchal27 Jun 4, 2024
e2672f4
Move variable into condition
mukeshpanchal27 Jun 4, 2024
e48f1e6
Fix merge conflict
mukeshpanchal27 Jun 5, 2024
d866bef
Cleanup
mukeshpanchal27 Jun 5, 2024
5f1664e
Merge pull request #1250 from WordPress/fix/1187-accurate-sizes-image…
mukeshpanchal27 Jun 5, 2024
d3b5dc4
Merge branch 'feature/more-accurate-sizes-attribute' into fix/small-i…
mukeshpanchal27 Jun 5, 2024
868225f
Add image size tests coverage
mukeshpanchal27 Jun 5, 2024
a6732f4
Correct image urls
mukeshpanchal27 Jun 5, 2024
00769a3
Update docblock & use str_ends_with
mukeshpanchal27 Jun 10, 2024
2656a0d
Update code that account for resized image width
mukeshpanchal27 Jun 10, 2024
b9b75b3
Update image size classes
mukeshpanchal27 Jun 10, 2024
0ad91f6
Fix sizes for left right alignment
mukeshpanchal27 Jun 10, 2024
7cbb7ae
Fix sizes for center alignment
mukeshpanchal27 Jun 12, 2024
fa9f39f
Exclude full and wide alignment from small size codebase
mukeshpanchal27 Jun 12, 2024
b254fc5
Fix spell
mukeshpanchal27 Jun 12, 2024
93fa153
Revise the approach
mukeshpanchal27 Jun 17, 2024
4e56425
Remove multiple parsing
mukeshpanchal27 Jun 24, 2024
711c500
Fix phpstan issue
mukeshpanchal27 Jun 25, 2024
b5a6ded
Fix unit tests
mukeshpanchal27 Jun 25, 2024
1f10468
Load filter prior to auto sizes filter
mukeshpanchal27 Jun 25, 2024
b722402
Address review feedbacks
mukeshpanchal27 Jun 25, 2024
3235b9b
Address unit tests feedback
mukeshpanchal27 Jun 25, 2024
6bf672f
Update file name for consistency
mukeshpanchal27 Jun 25, 2024
41e118f
Fix unit tests
mukeshpanchal27 Jun 25, 2024
fe1442e
Clean-up
mukeshpanchal27 Jun 25, 2024
486939b
Merge branch 'trunk' of https://github.com/WordPress/performance into…
westonruter Jun 25, 2024
0a6a563
Merge remote-tracking branch 'origin/trunk' into feature/more-accurat…
westonruter Jun 25, 2024
0a15a38
Merge branch 'feature/more-accurate-sizes-attribute' into fix/small-i…
westonruter Jun 25, 2024
b7117cf
Revert changes to composer.lock file
joemcgill Jun 25, 2024
0247988
Properly revert composer.lock file
joemcgill Jun 25, 2024
9e4ee2b
Merge pull request #1252 from WordPress/fix/small-image-sizes
joemcgill Jun 25, 2024
16d876d
Resolve merge conflict
mukeshpanchal27 Jul 1, 2024
892b0dc
Fix unit test juggling
mukeshpanchal27 Jul 1, 2024
6b0f4fc
Address review feedback
mukeshpanchal27 Jul 2, 2024
4a59047
Remove tear_down
mukeshpanchal27 Jul 3, 2024
a305629
Merge pull request #1290 from WordPress/add/left-right-alignment
mukeshpanchal27 Jul 3, 2024
54e592d
Update plugin name
mukeshpanchal27 Jul 9, 2024
7316439
Update remaining name references
westonruter Jul 9, 2024
7224ccb
Merge branch 'trunk' into fix/1330-plugin-name
joemcgill Jul 9, 2024
b307a53
Merge pull request #1335 from WordPress/fix/1330-plugin-name
joemcgill Jul 9, 2024
7611ee7
Initial readme description updates
joemcgill Jul 9, 2024
e39dca4
Apply suggestions from code review
mukeshpanchal27 Jul 10, 2024
9cd21fb
Minor update
mukeshpanchal27 Jul 10, 2024
e6f5645
Merge pull request #1339 from WordPress/update/plugin-description-1.1
mukeshpanchal27 Jul 10, 2024
4d8f0bd
Merge branch 'trunk' into feature/more-accurate-sizes-attribute
joemcgill Jul 11, 2024
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
6 changes: 3 additions & 3 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
/tests/includes/site-health/audit-enqueued-assets @manuelRod
/tests/testdata/modules/site-health/audit-enqueued-assets @manuelRod

# Plugin: Auto-sizes for Lazy-Loaded Images
/plugins/auto-sizes @joemcgill
/tests/plugins/auto-sizes @joemcgill
# Plugin: Enhanced Responsive Images
/plugins/auto-sizes @joemcgill @mukeshpanchal27
/tests/plugins/auto-sizes @joemcgill @mukeshpanchal27

# Plugin: Embed Optimizer
/plugins/embed-optimizer @adamsilverstein @westonruter
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ parameters:
# See <https://github.com/php-stubs/wordpress-stubs/issues/100>.
- vendor/phpstan/php-8-stubs/stubs/ext/standard/str_contains.php
- vendor/phpstan/php-8-stubs/stubs/ext/standard/str_starts_with.php
- vendor/phpstan/php-8-stubs/stubs/ext/standard/str_ends_with.php
stubFiles:
- tools/phpstan/filtered-functions.stub
dynamicConstantNames:
Expand Down
2 changes: 1 addition & 1 deletion plugins/auto-sizes/auto-sizes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Plugin Name: Auto-sizes for Lazy-loaded Images
* Plugin Name: Enhanced Responsive Images
* Plugin URI: https://github.com/WordPress/performance/tree/trunk/plugins/auto-sizes
* Description: Instructs browsers to automatically choose the right image size for lazy-loaded images.
* Requires at least: 6.5
Expand Down
128 changes: 127 additions & 1 deletion plugins/auto-sizes/hooks.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Hook callbacks used for Auto-sizes for Lazy-loaded Images.
* Hook callbacks used for Enhanced Responsive Images.
*
* @package auto-sizes
* @since 1.0.0
Expand Down Expand Up @@ -90,3 +90,129 @@ function auto_sizes_render_generator(): void {
echo '<meta name="generator" content="auto-sizes ' . esc_attr( IMAGE_AUTO_SIZES_VERSION ) . '">' . "\n";
}
add_action( 'wp_head', 'auto_sizes_render_generator' );

/**
* Gets the smaller image size if the layout width is bigger.
*
* It will return the smaller image size and return "px" if the layout width
* is something else, e.g. min(640px, 90vw) or 90vw.
*
* @since n.e.x.t
*
* @param string $layout_width The layout width.
* @param int $image_width The image width.
* @return string The proper width after some calculations.
*/
function auto_sizes_get_width( string $layout_width, int $image_width ): string {
if ( str_ends_with( $layout_width, 'px' ) ) {
return $image_width > (int) $layout_width ? $layout_width : $image_width . 'px';
}
return $image_width . 'px';
}

/**
* Filter the sizes attribute for images to improve the default calculation.
*
* @since n.e.x.t
*
* @param string $content The block content about to be rendered.
* @param array<string, mixed> $parsed_block The parsed block.
* @return string The updated block content.
*/
function auto_sizes_filter_image_tag( string $content, array $parsed_block ): string {
$processor = new WP_HTML_Tag_Processor( $content );
$has_image = $processor->next_tag( array( 'tag_name' => 'img' ) );

// Only update the markup if an image is found.
if ( $has_image ) {
$processor->set_attribute( 'data-needs-sizes-update', true );
$align = $parsed_block['attrs']['align'] ?? '';
if ( $align ) {
$processor->set_attribute( 'data-align', $align );
}

// Resize image width.
$resize_image_width = $parsed_block['attrs']['width'] ?? '';
if ( $resize_image_width ) {
$processor->set_attribute( 'data-resize-width', $resize_image_width );
}

$content = $processor->get_updated_html();
}
return $content;
}
add_filter( 'render_block_core/image', 'auto_sizes_filter_image_tag', 10, 2 );
add_filter( 'render_block_core/cover', 'auto_sizes_filter_image_tag', 10, 2 );

/**
* Filter the sizes attribute for images to improve the default calculation.
*
* @since n.e.x.t
*
* @param string $content The block content about to be rendered.
* @return string The updated block content.
*/
function auto_sizes_improve_image_sizes_attributes( string $content ): string {
$processor = new WP_HTML_Tag_Processor( $content );
if ( ! $processor->next_tag( array( 'tag_name' => 'img' ) ) ) {
return $content;
}

// Skips second time parsing if already processed.
if ( null === $processor->get_attribute( 'data-needs-sizes-update' ) ) {
return $content;
}

$align = $processor->get_attribute( 'data-align' );

// Retrieve width from the image tag itself.
$image_width = $processor->get_attribute( 'width' );
if ( ! $image_width && ! in_array( $align, array( 'full', 'wide' ), true ) ) {
return $content;
}

$layout = wp_get_global_settings( array( 'layout' ) );

$sizes = null;
// Handle different alignment use cases.
switch ( $align ) {
case 'full':
$sizes = '100vw';
break;

case 'wide':
if ( array_key_exists( 'wideSize', $layout ) ) {
$sizes = sprintf( '(max-width: %1$s) 100vw, %1$s', $layout['wideSize'] );
}
break;

case 'left':
case 'right':
case 'center':
// Resize image width.
$image_width = $processor->get_attribute( 'data-resize-width' ) ?? $image_width;
$sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $image_width );
break;

default:
if ( array_key_exists( 'contentSize', $layout ) ) {
// Resize image width.
$image_width = $processor->get_attribute( 'data-resize-width' ) ?? $image_width;
$width = auto_sizes_get_width( $layout['contentSize'], (int) $image_width );
$sizes = sprintf( '(max-width: %1$s) 100vw, %1$s', $width );
}
break;
}

if ( $sizes ) {
$processor->set_attribute( 'sizes', $sizes );
}

$processor->remove_attribute( 'data-needs-sizes-update' );
$processor->remove_attribute( 'data-align' );
$processor->remove_attribute( 'data-resize-width' );

return $processor->get_updated_html();
}
// Run filter prior to auto sizes "auto_sizes_update_content_img_tag" filter.
add_filter( 'wp_content_img_tag', 'auto_sizes_improve_image_sizes_attributes', 9 );
2 changes: 1 addition & 1 deletion plugins/auto-sizes/phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset name="WPP-AutoSizes">
<description>WordPress Coding Standards for Auto-sizes for Lazy-loaded Images Plugin</description>
<description>WordPress Coding Standards for Enhanced Responsive Images Plugin</description>

<rule ref="../../tools/phpcs/phpcs.ruleset.xml"/>

Expand Down
20 changes: 12 additions & 8 deletions plugins/auto-sizes/readme.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
=== Auto-sizes for Lazy-loaded Images ===
=== Enhanced Responsive Images ===

Contributors: wordpressdotorg
Tested up to: 6.6
Expand All @@ -7,26 +7,30 @@ License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: performance, images, auto-sizes

Instructs browsers to automatically choose the right image size for lazy-loaded images.
Improvements for responsive images in WordPress.

== Description ==

This plugin implements the HTML spec for adding `sizes="auto"` to lazy-loaded images.
For background, see: https://github.com/whatwg/html/issues/4654.
This plugin implements experimental enhancements for the responsive images functionality in WordPress. Currently, this includes:

1. Improvements to the accuracy of the `sizes` attribute by using available layout information in the theme.
2. Implementation of the new HTML spec for adding `sizes="auto"` to lazy-loaded images. See the HTML spec issue [Add "auto sizes" for lazy-loaded images](https://github.com/whatwg/html/issues/4654).

There are currently **no settings** and no user interface for this plugin since it is designed to work without any configuration.

== Installation ==

= Installation from within WordPress =

1. Visit **Plugins > Add New**.
2. Search for **Auto-sizes for Lazy-loaded Images**.
3. Install and activate the **Auto-sizes for Lazy-loaded Images** plugin.
2. Search for **Enhanced Responsive Images**.
3. Install and activate the **Enhanced Responsive Images** plugin.

= Manual installation =

1. Upload the entire `auto-sizes` folder to the `/wp-content/plugins/` directory.
1. Upload the entire plugin folder to the `/wp-content/plugins/` directory.
2. Visit **Plugins**.
3. Activate the **Auto-sizes for Lazy-loaded Images** plugin.
3. Activate the **Enhanced Responsive Images** plugin.

== Frequently Asked Questions ==

Expand Down
2 changes: 1 addition & 1 deletion plugins/auto-sizes/tests/test-auto-sizes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Tests for the Auto-sizes for Lazy-loaded Images plugin.
* Tests for the Enhanced Responsive Images plugin.
*
* @package auto-sizes
* @group auto-sizes
Expand Down
Loading
Loading