From cff49a408c256886702bce86b5289b47e2a830a5 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 28 Jul 2021 14:56:49 -0500 Subject: [PATCH 1/6] Add failing unit test --- .../tests/php/test_class.jetpack_photon.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php b/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php index 8a6b75dc4af14..8468a1f097a8e 100644 --- a/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php +++ b/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php @@ -1444,6 +1444,57 @@ public function test_photon_strip_image_dimensions_maybe_strips_resized_string() wp_delete_attachment( $id ); } + + /** + * Tests Photon's HTML parsing based on file type. + * + * @param string $url URL being validated. + * @param bool $expected If is valid Photon-able URL. + * + * @author kraftbj + * @covers Jetpack_Photon::validate_image_url + * @dataProvider get_test_photon_validate_image_url_file_types_data_provider + * @since 10.0.0 + */ + public function test_photon_validate_image_url_file_types( $url, $expected ) { + $this->assertEquals( $expected, Testable_Jetpack_Photon::validate_image_url( $url ) ); + } + + /** + * Possible values for test_photon_validate_image_url_file_types. + */ + public function get_test_photon_validate_image_url_file_types_data_provider() { + return array( + 'gif' => array( 'http://example.com/example-150x150.gif', true ), + 'jpg' => array( 'http://example.com/example-150x150.jpg', true ), + 'jpeg' => array( 'http://example.com/example-150x150.jpeg', true ), + 'png' => array( 'http://example.com/example-150x150.png', true ), + 'webp' => array( 'http://example.com/example-150x150.webp', true ), + 'invalid' => array( 'http://example.com/example-150x150.invalid', false ), + + ); + } +} + +// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound +// phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found + +/** + * Class Testable_Jetpack_Photon + * + * Extending the Photon class to make protected methods testable. + */ +class Testable_Jetpack_Photon extends Jetpack_Photon { + /** + * Making the validate_image_url function accessible. + * + * @param string $url URL to be validated. + * + * @return bool If the URL is Photonable. + */ + public static function validate_image_url( $url ) { + return parent::validate_image_url( $url ); + } } // phpcs:enable From 5f48e85492d1fb510f5fa92dad4e33d61f97b06c Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 28 Jul 2021 14:58:58 -0500 Subject: [PATCH 2/6] Add webp support to Photon --- projects/plugins/jetpack/changelog/add-webp-photon-support | 4 ++++ projects/plugins/jetpack/class.photon.php | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 projects/plugins/jetpack/changelog/add-webp-photon-support diff --git a/projects/plugins/jetpack/changelog/add-webp-photon-support b/projects/plugins/jetpack/changelog/add-webp-photon-support new file mode 100644 index 0000000000000..a33d369a708fb --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-webp-photon-support @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Image CDN: added support for the WEBP format diff --git a/projects/plugins/jetpack/class.photon.php b/projects/plugins/jetpack/class.photon.php index 8cd28eb2c40cf..0c05562850b0c 100644 --- a/projects/plugins/jetpack/class.photon.php +++ b/projects/plugins/jetpack/class.photon.php @@ -21,13 +21,14 @@ class Jetpack_Photon { /** * Allowed extensions. * - * @var string[] Allowed extensions must match https://code.trac.wordpress.org/browser/photon/index.php#L31 + * @var string[] Allowed extensions must match https://code.trac.wordpress.org/browser/photon/index.php#L41 */ protected static $extensions = array( 'gif', 'jpg', 'jpeg', 'png', + 'webp', // Jetpack assumes Photon_OpenCV backend class is being used on the server. See link in docblock. ); /** From 65a9cc2b392c4163a99f5b233e33a337de98a325 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 28 Jul 2021 15:04:10 -0500 Subject: [PATCH 3/6] Update Instant Search to support webp via Photon --- .../modules/search/instant-search/lib/hooks/use-photon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js b/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js index a4679e1292f45..2c04680343147 100644 --- a/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js +++ b/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js @@ -34,7 +34,7 @@ export function usePhoton( initialSrc, width, height, isPhotonEnabled = true ) { // Photon only supports GIF, JPG and PNG // @see https://developer.wordpress.com/docs/photon/ - const supportedImageTypes = [ 'gif', 'jpg', 'jpeg', 'png' ]; + const supportedImageTypes = [ 'gif', 'jpg', 'jpeg', 'png', 'webp' ]; const fileExtension = initialSrcWithoutQueryString ?.substring( initialSrcWithoutQueryString.lastIndexOf( '.' ) + 1 ) .toLowerCase(); From 24cc3ccb5d0609de879980fc6a214024a7212526 Mon Sep 17 00:00:00 2001 From: Chris R Date: Thu, 29 Jul 2021 09:22:51 +1200 Subject: [PATCH 4/6] Instant Search: update comment to include WebP --- .../modules/search/instant-search/lib/hooks/use-photon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js b/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js index 2c04680343147..6cb37a6d09b77 100644 --- a/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js +++ b/projects/plugins/jetpack/modules/search/instant-search/lib/hooks/use-photon.js @@ -32,7 +32,7 @@ export function usePhoton( initialSrc, width, height, isPhotonEnabled = true ) { const [ src, setSrc ] = useState( null ); const initialSrcWithoutQueryString = stripQueryString( initialSrc ); - // Photon only supports GIF, JPG and PNG + // Photon only supports GIF, JPG, PNG and WebP images // @see https://developer.wordpress.com/docs/photon/ const supportedImageTypes = [ 'gif', 'jpg', 'jpeg', 'png', 'webp' ]; const fileExtension = initialSrcWithoutQueryString From 744c14bfb64c7544ab00a7830132d0a426933726 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Mon, 2 Aug 2021 09:14:10 -0500 Subject: [PATCH 5/6] Update testing method to use reflection --- .../tests/php/test_class.jetpack_photon.php | 26 +++---------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php b/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php index 8468a1f097a8e..d6045324c70a2 100644 --- a/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php +++ b/projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php @@ -1457,7 +1457,10 @@ public function test_photon_strip_image_dimensions_maybe_strips_resized_string() * @since 10.0.0 */ public function test_photon_validate_image_url_file_types( $url, $expected ) { - $this->assertEquals( $expected, Testable_Jetpack_Photon::validate_image_url( $url ) ); + $testable = new ReflectionClass( Jetpack_Photon::class ); + $testable_validate_image_url = $testable->getMethod( 'validate_image_url' ); + $testable_validate_image_url->setAccessible( true ); + $this->assertEquals( $expected, $testable_validate_image_url->invoke( null, $url ) ); } /** @@ -1476,25 +1479,4 @@ public function get_test_photon_validate_image_url_file_types_data_provider() { } } -// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound -// phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found - -/** - * Class Testable_Jetpack_Photon - * - * Extending the Photon class to make protected methods testable. - */ -class Testable_Jetpack_Photon extends Jetpack_Photon { - /** - * Making the validate_image_url function accessible. - * - * @param string $url URL to be validated. - * - * @return bool If the URL is Photonable. - */ - public static function validate_image_url( $url ) { - return parent::validate_image_url( $url ); - } -} - // phpcs:enable From e1c47bcc992c95caec2e78e04b4c789f69de4366 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 25 Aug 2021 14:55:13 -0500 Subject: [PATCH 6/6] Add wepb support to additional places --- .../_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php | 2 +- projects/plugins/jetpack/class.json-api-endpoints.php | 2 +- projects/plugins/jetpack/functions.global.php | 1 + .../jetpack/modules/videopress/js/videopress-plupload.js | 2 +- projects/plugins/jetpack/sal/class.json-api-post-base.php | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/projects/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php b/projects/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php index ea407a917109d..10ae6ca822250 100644 --- a/projects/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php +++ b/projects/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php @@ -221,7 +221,7 @@ public static function get_benefits() { // Number of images in the library if Photon is active. if ( Jetpack::is_module_active( 'photon' ) ) { $photon_count = array_reduce( - get_object_vars( wp_count_attachments( array( 'image/jpeg', 'image/png', 'image/gif', 'image/bmp' ) ) ), + get_object_vars( wp_count_attachments( array( 'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp' ) ) ), function ( $i, $j ) { return $i + $j; } diff --git a/projects/plugins/jetpack/class.json-api-endpoints.php b/projects/plugins/jetpack/class.json-api-endpoints.php index 4c6a9bf7f9a94..97057c804d28d 100644 --- a/projects/plugins/jetpack/class.json-api-endpoints.php +++ b/projects/plugins/jetpack/class.json-api-endpoints.php @@ -1367,7 +1367,7 @@ function get_media_item_v1_1( $media_id, $media_item = null, $file = null ) { 'thumbnails' => array(), ); - if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) { + if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif', 'webp' ), true ) ) { $metadata = wp_get_attachment_metadata( $media_item->ID ); if ( isset( $metadata['height'], $metadata['width'] ) ) { $response['height'] = $metadata['height']; diff --git a/projects/plugins/jetpack/functions.global.php b/projects/plugins/jetpack/functions.global.php index cc119b452d658..0072035a0c170 100644 --- a/projects/plugins/jetpack/functions.global.php +++ b/projects/plugins/jetpack/functions.global.php @@ -384,6 +384,7 @@ function jetpack_is_file_supported_for_sideloading( $file ) { 'image/jpeg', 'image/gif', 'image/bmp', + 'image/webp', 'video/quicktime', 'video/mp4', 'video/mpeg', diff --git a/projects/plugins/jetpack/modules/videopress/js/videopress-plupload.js b/projects/plugins/jetpack/modules/videopress/js/videopress-plupload.js index 7f6cf16ec4535..28854f29df07e 100644 --- a/projects/plugins/jetpack/modules/videopress/js/videopress-plupload.js +++ b/projects/plugins/jetpack/modules/videopress/js/videopress-plupload.js @@ -244,7 +244,7 @@ window.wp = window.wp || {}; ); // Handle early mime type scanning for images. - image = /(?:jpe?g|png|gif)$/i.exec( file.name ); + image = /(?:jpe?g|png|gif|webp)$/i.exec( file.name ); // For images set the model's type and subtype attributes. if ( image ) { diff --git a/projects/plugins/jetpack/sal/class.json-api-post-base.php b/projects/plugins/jetpack/sal/class.json-api-post-base.php index 4531ead924c2b..b7841bc9de313 100644 --- a/projects/plugins/jetpack/sal/class.json-api-post-base.php +++ b/projects/plugins/jetpack/sal/class.json-api-post-base.php @@ -580,7 +580,7 @@ private function get_media_item_v1_1( $media_id ) { 'thumbnails' => array() ); - if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) { + if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif', 'webp' ), true ) ) { $metadata = wp_get_attachment_metadata( $media_item->ID ); if ( isset( $metadata['height'], $metadata['width'] ) ) { $response['height'] = $metadata['height'];