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 all commits
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
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@
/modules/images/dominant-color-images @pbearne @spacedmonkey
/tests/modules/images/dominant-color-images @pbearne @spacedmonkey
/tests/testdata/modules/images/dominant-color-images @pbearne @spacedmonkey

# Plugin: Auto-sizes for Lazy-Loaded Images
/plugins/auto-sizes @joemcgill
/tests/plugins/auto-sizes @joemcgill
14 changes: 7 additions & 7 deletions .github/workflows/php-test-plugins.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit Testing for Test Plugin
name: Unit Testing for Plugins

on:
push:
Expand All @@ -8,7 +8,7 @@ on:
# Only run if PHP-related files changed.
paths:
- '.github/workflows/php-test-plugins.yml'
- '**.php'
- 'plugins/**.php'
- '.wp-env.json'
- '**/package.json'
- 'package-lock.json'
Expand All @@ -24,7 +24,7 @@ on:
# Only run if PHP-related files changed.
paths:
- '.github/workflows/php-test-plugins.yml'
- '**.php'
- 'plugins/**.php'
- '.wp-env.json'
- '**/package.json'
- 'package-lock.json'
Expand Down Expand Up @@ -72,7 +72,7 @@ jobs:
# PHP that the composer.lock was created for.
- name: Composer update
run: npm run wp-env run tests-cli -- --env-cwd="wp-content/plugins/$(basename $(pwd))" composer update --no-interaction
- name: Running single site unit tests
run: npm run test-php -- -- -- --testsuite test-plugin
- name: Running multisite unit tests
run: npm run test-php-multisite -- -- -- --testsuite test-plugin
- name: Running single site unit tests for Auto-sizes for Lazy-Loaded Images Plugin
run: npm run test-php -- -- -- --testsuite auto-sizes
- name: Running multisite unit tests for Auto-sizes for Lazy-Loaded Images Plugin
run: npm run test-php-multisite -- -- -- --testsuite auto-sizes
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<rule ref="WordPress-Docs"/>
<rule ref="WordPress-Extra" />
<rule ref="WordPress.WP.I18n"/>
<config name="text_domain" value="performance-lab,default"/>
<config name="text_domain" value="performance-lab,default,auto-sizes"/>

<arg value="ps"/>
<arg name="extensions" value="php"/>
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<exclude>./tests/utils</exclude>
<exclude>./tests/plugins</exclude>
</testsuite>
<testsuite name="test-plugin">
<directory suffix=".php">./tests/plugins/test-plugin</directory>
<testsuite name="auto-sizes">
<directory suffix=".php">./tests/plugins/auto-sizes</directory>
</testsuite>
</testsuites>
<groups>
Expand Down
2 changes: 1 addition & 1 deletion plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"version": "1.0.5"
}
},
"plugins": []
"plugins": [ "auto-sizes" ]
}
Empty file removed plugins/.gitkeep
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added plugins/auto-sizes/.wordpress-org/icon-128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions plugins/auto-sizes/.wordpress-org/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions plugins/auto-sizes/auto-sizes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Plugin Name: Auto-sizes for Lazy-loaded Images
* Plugin URI: https://github.com/WordPress/performance/tree/trunk/modules/images/auto-sizes
* Description: This plugin implements the HTML spec for adding `sizes="auto"` to lazy-loaded images.
* Requires at least: 6.3
* Requires PHP: 7.0
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: auto-sizes
*
* @package auto-sizes
*/

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

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

define( 'IMAGE_AUTO_SIZES_VERSION', '1.0.0' );

require_once __DIR__ . '/hooks.php';
71 changes: 71 additions & 0 deletions plugins/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 auto-sizes
* @since 1.0.0
*/

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

/**
* Adds auto to the sizes attribute to the image, if applicable.
*
* @since 1.0.0
*
* @param array $attr Attributes for the image markup.
* @return array The filtered attributes for the image markup.
*/
function auto_sizes_update_image_attributes( $attr ) {
// 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', 'auto_sizes_update_image_attributes' );

/**
* Adds auto to the sizes attribute to the image, if applicable.
*
* @since 1.0.0
*
* @param string $html The HTML image tag markup being filtered.
* @return string The filtered HTML image tag markup.
*/
function auto_sizes_update_content_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', 'auto_sizes_update_content_img_tag' );
53 changes: 53 additions & 0 deletions plugins/auto-sizes/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== Auto-sizes for Lazy-loaded Images ===

Contributors: wordpressdotorg
Requires at least: 6.4
Tested up to: 6.4
Requires PHP: 7.0
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: performance, images, auto-sizes

Adds support for automatically calculating the sizes attribute for lazy-loaded images.

== 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.

== 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.

= Manual installation =

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

== Frequently Asked Questions ==

= Where can I submit my plugin feedback? =

Feedback is encouraged and much appreciated, especially since this plugin may contain future WordPress core features. If you have suggestions or requests for new features, you can [submit them as an issue in the WordPress Performance Team's GitHub repository](https://github.com/WordPress/performance/issues/new/choose). If you need help with troubleshooting or have a question about the plugin, please [create a new topic on our support forum](https://wordpress.org/support/plugin/auto-sizes/#new-topic-0).

= Where can I report security bugs? =

The Performance team and WordPress community take security bugs seriously. We appreciate your efforts to responsibly disclose your findings, and will make every effort to acknowledge your contributions.

To report a security issue, please visit the [WordPress HackerOne](https://hackerone.com/wordpress) program.

= How can I contribute to the plugin? =

Contributions are always welcome! Learn more about how to get involved in the [Core Performance Team Handbook](https://make.wordpress.org/performance/handbook/get-involved/).

== Changelog ==

= 1.0.0 =

* Initial release of the Auto-sizes for Lazy-loaded Images plugin as a standalone plugin. ([904](https://github.com/WordPress/performance/pull/904))
74 changes: 54 additions & 20 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,62 @@
$_test_root = '/tmp/wordpress-tests-lib';
}

// Force plugin to be active.
$GLOBALS['wp_tests_options'] = array(
'active_plugins' => array( basename( TESTS_PLUGIN_DIR ) . '/load.php' ),
);

// Add filter to ensure the plugin's admin integration and all modules are loaded for tests.
require_once $_test_root . '/includes/functions.php';
tests_add_filter(
'plugins_loaded',
static function () {
require_once TESTS_PLUGIN_DIR . '/admin/load.php';
require_once TESTS_PLUGIN_DIR . '/admin/server-timing.php';
require_once TESTS_PLUGIN_DIR . '/admin/plugins.php';
$module_files = glob( TESTS_PLUGIN_DIR . '/modules/*/*/load.php' );
if ( $module_files ) {
foreach ( $module_files as $module_file ) {
require_once $module_file;

// Check if we use the plugin's test suite. If so, disable the PL plugin and only load the requested plugin.
$plugin_name = '';
foreach ( $_SERVER['argv'] as $index => $arg ) {
if (
'--testsuite' === $arg &&
isset( $_SERVER['argv'][ $index + 1 ] ) &&
'performance-lab' !== $_SERVER['argv'][ $index + 1 ] &&
file_exists( TESTS_PLUGIN_DIR . '/plugins/' . $_SERVER['argv'][ $index + 1 ] )
) {
$plugin_name = $_SERVER['argv'][ $index + 1 ];
}
}

if ( $plugin_name ) {
$plugin_test_path = TESTS_PLUGIN_DIR . '/plugins/' . $plugin_name;
tests_add_filter(
'plugins_loaded',
static function () use ( $plugin_test_path, $plugin_name ) {
// Check if plugin has a "plugin/plugin.php" file.
if ( file_exists( $plugin_test_path . '/' . $plugin_name . '.php' ) ) {
require_once $plugin_test_path . '/' . $plugin_name . '.php';
} elseif ( file_exists( $plugin_test_path . '/load.php' ) ) {
// Check if plugin has a "plugin/load.php" file.
require_once $plugin_test_path . '/load.php';
} else {
echo "Unable to locate standalone plugin bootstrap file in $plugin_test_path.";
exit( 1 );
}
}
},
1
);
},
1
);
} else {
// Force plugin to be active.
$GLOBALS['wp_tests_options'] = array(
'active_plugins' => array( basename( TESTS_PLUGIN_DIR ) . '/load.php' ),
);

// Add filter to ensure the plugin's admin integration and all modules are loaded for tests.
tests_add_filter(
'plugins_loaded',
static function () {
require_once TESTS_PLUGIN_DIR . '/admin/load.php';
require_once TESTS_PLUGIN_DIR . '/admin/server-timing.php';
require_once TESTS_PLUGIN_DIR . '/admin/plugins.php';
$module_files = glob( TESTS_PLUGIN_DIR . '/modules/*/*/load.php' );
if ( $module_files ) {
foreach ( $module_files as $module_file ) {
require_once $module_file;
}
}
},
1
);
}

// Start up the WP testing environment.
require $_test_root . '/includes/bootstrap.php';
Loading
Loading