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

Photon: Add webp support #20473

Merged
merged 12 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-webp-photon-support
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Image CDN: added support for the WEBP format
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/class.json-api-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
3 changes: 2 additions & 1 deletion projects/plugins/jetpack/class.photon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
);

/**
Expand Down
1 change: 1 addition & 0 deletions projects/plugins/jetpack/functions.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ 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' ];
const supportedImageTypes = [ 'gif', 'jpg', 'jpeg', 'png', 'webp' ];
const fileExtension = initialSrcWithoutQueryString
?.substring( initialSrcWithoutQueryString.lastIndexOf( '.' ) + 1 )
.toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/sal/class.json-api-post-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
33 changes: 33 additions & 0 deletions projects/plugins/jetpack/tests/php/test_class.jetpack_photon.php
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,39 @@ 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 ) {
$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 ) );
}

/**
* 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:enable