diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index 1b3509a65..71ec1279c 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -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
diff --git a/.github/workflows/php-test-plugins.yml b/.github/workflows/php-test-plugins.yml
index 679acf738..dfb10eb18 100644
--- a/.github/workflows/php-test-plugins.yml
+++ b/.github/workflows/php-test-plugins.yml
@@ -1,4 +1,4 @@
-name: Unit Testing for Test Plugin
+name: Unit Testing for Plugins
on:
push:
@@ -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'
@@ -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'
@@ -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
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index b5ba35f01..79b022b85 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -8,7 +8,7 @@
-
+
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 081f11cdb..37c6e656c 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -12,8 +12,8 @@
./tests/utils
./tests/plugins
-
- ./tests/plugins/test-plugin
+
+ ./tests/plugins/auto-sizes
diff --git a/plugins.json b/plugins.json
index 3e36d5c79..a839d995b 100644
--- a/plugins.json
+++ b/plugins.json
@@ -9,5 +9,5 @@
"version": "1.0.5"
}
},
- "plugins": []
+ "plugins": [ "auto-sizes" ]
}
diff --git a/plugins/.gitkeep b/plugins/.gitkeep
deleted file mode 100644
index e69de29bb..000000000
diff --git a/plugins/auto-sizes/.wordpress-org/banner-1544x500.png b/plugins/auto-sizes/.wordpress-org/banner-1544x500.png
new file mode 100644
index 000000000..df7a1a7e6
Binary files /dev/null and b/plugins/auto-sizes/.wordpress-org/banner-1544x500.png differ
diff --git a/plugins/auto-sizes/.wordpress-org/banner-772x250.png b/plugins/auto-sizes/.wordpress-org/banner-772x250.png
new file mode 100644
index 000000000..44ef279b0
Binary files /dev/null and b/plugins/auto-sizes/.wordpress-org/banner-772x250.png differ
diff --git a/plugins/auto-sizes/.wordpress-org/icon-128x128.png b/plugins/auto-sizes/.wordpress-org/icon-128x128.png
new file mode 100644
index 000000000..571e9cfa9
Binary files /dev/null and b/plugins/auto-sizes/.wordpress-org/icon-128x128.png differ
diff --git a/plugins/auto-sizes/.wordpress-org/icon-256x256.png b/plugins/auto-sizes/.wordpress-org/icon-256x256.png
new file mode 100644
index 000000000..ba7b59876
Binary files /dev/null and b/plugins/auto-sizes/.wordpress-org/icon-256x256.png differ
diff --git a/plugins/auto-sizes/.wordpress-org/icon.svg b/plugins/auto-sizes/.wordpress-org/icon.svg
new file mode 100644
index 000000000..5a4f82dbd
--- /dev/null
+++ b/plugins/auto-sizes/.wordpress-org/icon.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/plugins/auto-sizes/auto-sizes.php b/plugins/auto-sizes/auto-sizes.php
new file mode 100644
index 000000000..45db3fc04
--- /dev/null
+++ b/plugins/auto-sizes/auto-sizes.php
@@ -0,0 +1,30 @@
+ 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))
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 4fdbd1915..5d2a51234 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -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';
diff --git a/tests/plugins/auto-sizes/auto-sizes-test.php b/tests/plugins/auto-sizes/auto-sizes-test.php
new file mode 100644
index 000000000..73ba9388e
--- /dev/null
+++ b/tests/plugins/auto-sizes/auto-sizes-test.php
@@ -0,0 +1,90 @@
+attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leaves.jpg' );
+ }
+
+ /**
+ * Helper function to create image markup from a given attachment ID.
+ *
+ * @param int $attachment_id Attachment ID.
+ * @return string Image markup.
+ */
+ public function get_image_tag( $attachment_id ) {
+ return get_image_tag( $attachment_id, '', '', '', 'large' );
+ }
+
+ /**
+ * Test generated markup for an image with lazy loading gets auto-sizes.
+ *
+ * @covers ::auto_sizes_update_image_attributes
+ */
+ public function test_image_with_lazy_loading_has_auto_sizes() {
+ $this->assertStringContainsString(
+ 'sizes="auto, ',
+ wp_get_attachment_image( self::$image_id, 'large', null, array( 'loading' => 'lazy' ) )
+ );
+ }
+
+ /**
+ * Test generated markup for an image without lazy loading does not get auto-sizes.
+ *
+ * @covers ::auto_sizes_update_image_attributes
+ */
+ public function test_image_without_lazy_loading_does_not_have_auto_sizes() {
+ $this->assertStringContainsString(
+ 'sizes="(max-width: 1024px) 100vw, 1024px"',
+ wp_get_attachment_image( self::$image_id, 'large', null, array( 'loading' => '' ) )
+ );
+ }
+
+ /**
+ * Test content filtered markup with lazy loading gets auto-sizes.
+ *
+ * @covers ::auto_sizes_update_content_img_tag
+ */
+ public function test_content_image_with_lazy_loading_has_auto_sizes() {
+ // Force lazy loading attribute.
+ add_filter( 'wp_img_tag_add_loading_attr', '__return_true' );
+
+ $image_tag = $this->get_image_tag( self::$image_id );
+
+ $this->assertStringContainsString(
+ 'sizes="auto, (max-width: 1024px) 100vw, 1024px"',
+ wp_filter_content_tags( $this->get_image_tag( self::$image_id ) )
+ );
+ }
+
+ /**
+ * Test content filtered markup without lazy loading does not get auto-sizes.
+ *
+ * @covers ::auto_sizes_update_content_img_tag
+ */
+ public function test_content_image_without_lazy_loading_does_not_have_auto_sizes() {
+ // Disable lazy loading attribute.
+ add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
+
+ $this->assertStringContainsString(
+ 'sizes="(max-width: 1024px) 100vw, 1024px"',
+ wp_filter_content_tags( $this->get_image_tag( self::$image_id ) )
+ );
+ }
+}
diff --git a/tests/plugins/test-plugin/test-plugin.php b/tests/plugins/test-plugin/test-plugin.php
deleted file mode 100644
index 8b5f80e80..000000000
--- a/tests/plugins/test-plugin/test-plugin.php
+++ /dev/null
@@ -1,20 +0,0 @@
-assertTrue( true );
- }
-
- public function test_another_plugin_does_not_fatal() {
- $this->assertTrue( true );
- }
-}