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

Add: Auto Sizes for Lazy-loaded Images #904

Merged
merged 42 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9a14b1f
Add: Auto Sizes for Lazy-loaded Images
joemcgill Dec 14, 2023
a5e4afa
Update module constant name
joemcgill Dec 19, 2023
a7dffc5
Add CODEOWNERS entry
joemcgill Dec 19, 2023
97980c1
Add initial unit tests
joemcgill Dec 19, 2023
cab72e1
Merge pull request #913 from WordPress/update/auto-sizes-module
joemcgill Dec 20, 2023
8e6ef6f
Update hook function names
joemcgill Jan 5, 2024
db4a5da
Add plugin readme.txt
joemcgill Jan 5, 2024
1a88b89
Fix Plugin header name.
joemcgill Jan 5, 2024
7e3c544
Update plugin description
joemcgill Jan 5, 2024
6870baf
Consistently use hyphenated 'auto-sizes'
joemcgill Jan 8, 2024
d0a19e5
Merge pull request #922 from WordPress/update/auto-sizes-plugin
joemcgill Jan 8, 2024
98af1d7
Merge branch 'trunk' into feature/auto-sizes
joemcgill Jan 10, 2024
6b3d16d
Fix PHPCS whitespace issue
joemcgill Jan 10, 2024
bd625e8
Merge pull request #931 from WordPress/update/auto-sizes-phpcs
joemcgill Jan 10, 2024
35e5fbf
Auto-sizes: Disallow direct file access
joemcgill Jan 17, 2024
3cb88e5
Merge pull request #945 from WordPress/fix/auto-sizes-direct-load
joemcgill Jan 17, 2024
161a738
Move plugin to plugins folder
mukeshpanchal27 Feb 6, 2024
1ff1004
Merge branch 'trunk' into fix/unit-test
mukeshpanchal27 Feb 6, 2024
5f174b7
Fix unit tests
mukeshpanchal27 Feb 6, 2024
2fab10a
Merge pull request #970 from WordPress/trunk
swissspidy Feb 6, 2024
a5f78b4
Merge branch 'feature/auto-sizes' into fix/unit-test
mukeshpanchal27 Feb 6, 2024
8f3c458
Merge pull request #971 from WordPress/fix/unit-test
mukeshpanchal27 Feb 6, 2024
d10052f
Merge branch 'feature/auto-sizes' into move/auto-sizes
mukeshpanchal27 Feb 6, 2024
23bcfc2
Unit test coverage setup
mukeshpanchal27 Feb 6, 2024
edba918
Add new line at end
mukeshpanchal27 Feb 6, 2024
45a8b57
Add new line at end
mukeshpanchal27 Feb 6, 2024
2daf975
Merge branch 'feature/modules-to-plugins' into move/auto-sizes
mukeshpanchal27 Feb 6, 2024
88040f2
Use single unit test file for plugins
mukeshpanchal27 Feb 7, 2024
14c4776
Adjust spacing
mukeshpanchal27 Feb 7, 2024
87f3a04
Added support for plugin/plugin.php
mukeshpanchal27 Feb 7, 2024
852775d
Remove test plugin
mukeshpanchal27 Feb 9, 2024
bc2884f
Adjust bootstrap file code
mukeshpanchal27 Feb 9, 2024
4b0974e
Add plugin into plugins.json
mukeshpanchal27 Feb 12, 2024
8cfded4
Address review feedback
mukeshpanchal27 Feb 13, 2024
2a687c7
Add plugin directory assets for Auto Sizes
westonruter Feb 14, 2024
6901573
Squoosh images from Figma
westonruter Feb 14, 2024
6fa9128
Compress icon.svg with SVGOMG
westonruter Feb 14, 2024
f0d74fb
Merge pull request #986 from WordPress/add/auto-sizes-plugin-assets
swissspidy Feb 15, 2024
7f3b13b
Address review feedback
mukeshpanchal27 Feb 15, 2024
9addb11
Merge pull request #972 from WordPress/move/auto-sizes
mukeshpanchal27 Feb 19, 2024
2d0deca
Move auto-sizes plugin assets to plugins folder
mukeshpanchal27 Feb 19, 2024
5aeb1c4
Merge pull request #999 from WordPress/move/auto-sizes-plugin-assets
mukeshpanchal27 Feb 19, 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
71 changes: 71 additions & 0 deletions modules/images/auto-sizes/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Hook callbacks used for Auto Sizes for Lazy-loaded Images.
*
* @package performance-lab
* @since n.e.x.t
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Adds auto to the sizes attribute to the image, if applicable.
*
* @since n.e.x.t
*
* @param array $attr Attributes for the image markup.
* @return array The filtered attributes for the image markup.
*/
function performance_lab_auto_sizes_attr( $attr ) {
joemcgill marked this conversation as resolved.
Show resolved Hide resolved
// Bail early if the image is not lazy-loaded.
if ( ! isset( $attr['loading'] ) || 'lazy' !== $attr['loading'] ) {
return $attr;
}

// Bail early if the image is not responsive.
if ( ! isset( $attr['sizes'] ) ) {
return $attr;
}

// Don't add 'auto' to the sizes attribute if it already exists.
if ( false !== strpos( $attr['sizes'], 'auto,' ) ) {
return $attr;
}

$attr['sizes'] = 'auto, ' . $attr['sizes'];

return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'performance_lab_auto_sizes_attr' );

/**
* Adds auto to the sizes attribute to the image, if applicable.
*
* @since n.e.x.t
*
* @param string $html The HTML image tag markup being filtered.
* @return string The filtered HTML image tag markup.
*/
function performance_lab_auto_sizes_img_tag( $html ) {
// Bail early if the image is not lazy-loaded.
if ( false === strpos( $html, 'loading="lazy"' ) ) {
return $html;
}

// Bail early if the image is not responsive.
if ( false === strpos( $html, 'sizes="' ) ) {
return $html;
}

// Don't double process content images.
if ( false !== strpos( $html, 'sizes="auto,' ) ) {
return $html;
}

$html = str_replace( 'sizes="', 'sizes="auto, ', $html );

return $html;
}
add_filter( 'wp_content_img_tag', 'performance_lab_auto_sizes_img_tag' );
18 changes: 18 additions & 0 deletions modules/images/auto-sizes/load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
joemcgill marked this conversation as resolved.
Show resolved Hide resolved
* Module Name: Auto Sizes for Lazy-loaded Images.
* Description: Implements auto sizes for lazy loaded images.
joemcgill marked this conversation as resolved.
Show resolved Hide resolved
* Experimental: Yes
*
* @package performance-lab
* @since n.e.x.t
*/

// Define the constant.
if ( defined( 'IMAGE_AUTO_SIZES' ) ) {
return;
}

define( 'IMAGE_AUTO_SIZES', 'Performance Lab ' . PERFLAB_VERSION );
joemcgill marked this conversation as resolved.
Show resolved Hide resolved

require_once __DIR__ . '/hooks.php';