Skip to content
57 changes: 55 additions & 2 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )
return $metadata;
}

$sizes_with_mime_type_support = webp_uploads_get_image_sizes_additional_mime_type_support();

foreach ( $metadata['sizes'] as $size_name => $properties ) {
Comment thread
felixarntz marked this conversation as resolved.
// This image size is not defined or not an array.
if ( ! is_array( $properties ) ) {
// Do nothing if this image size is not an array or is not allowed to have additional mime types.
if ( ! is_array( $properties ) || empty( $sizes_with_mime_type_support[ $size_name ] ) ) {
continue;
}

Expand Down Expand Up @@ -637,3 +639,54 @@ function webp_uploads_update_featured_image( $html, $post_id, $attachment_id ) {
return webp_uploads_img_tag_update_mime_type( $html, 'post_thumbnail_html', $attachment_id );
}
add_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image', 10, 3 );

/**
* Returns an array of image size names that have secondary mime type output enabled. Core sizes and
* core theme sizes are enabled by default.
*
* Developers can control the generation of additional mime images for all sizes using the
* webp_uploads_image_sizes_with_additional_mime_type_support filter.
*
* @since n.e.x.t
*
* @return array An array of image sizes that can have additional mime types.
*/
function webp_uploads_get_image_sizes_additional_mime_type_support() {
$additional_sizes = wp_get_additional_image_sizes();
$allowed_sizes = array(
Comment thread
felixarntz marked this conversation as resolved.
'thumbnail' => true,
'medium' => true,
'medium_large' => true,
'large' => true,
'post-thumbnail' => true,
// 2x medium_large size.
'1536x1536' => true,
// 2x large size.
'2048x2048' => true,
// Twentyeleven theme.
'large-feature' => true,
'small-feature' => true,
// Twentyfourteen theme.
'twentyfourteen-full-width' => true,
// Twentyseventeen theme.
'twentyseventeen-featured-image' => true,
'twentyseventeen-thumbnail-avatar' => true,
// Twentytwenty theme.
'twentytwenty-fullscreen' => true,
);
Comment thread
felixarntz marked this conversation as resolved.

foreach ( $additional_sizes as $size => $size_details ) {
$allowed_sizes[ $size ] = ! empty( $size_details['provide_additional_mime_types'] );
}

/**
* Filters whether additional mime types are allowed for image sizes.
*
* @since n.e.x.t
*
* @param array $allowed_sizes A map of image size names and whether they are allowed to have additional mime types.
*/
$allowed_sizes = apply_filters( 'webp_uploads_image_sizes_with_additional_mime_type_support', $allowed_sizes );

return $allowed_sizes;
}
42 changes: 42 additions & 0 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,14 @@ function () {
}
);

add_filter(
'webp_uploads_image_sizes_with_additional_mime_type_support',
function( $sizes ) {
$sizes['1536x1536'] = '1536x1536';
return $sizes;
}
);
Comment thread
felixarntz marked this conversation as resolved.
Outdated

$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/paint.jpeg' );
$metadata = wp_get_attachment_metadata( $attachment_id );

Expand Down Expand Up @@ -602,6 +610,14 @@ public function it_should_use_smaller_jpg_image_when_smaller_size_preferred() {
* @test
*/
public function it_should_replace_the_featured_image_to_web_p_when_requesting_the_featured_image() {
add_filter(
'webp_uploads_image_sizes_with_additional_mime_type_support',
function( $sizes ) {
$sizes['1536x1536'] = '1536x1536';
return $sizes;
}
);
Comment thread
felixarntz marked this conversation as resolved.
Outdated

$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/paint.jpeg' );
$post_id = $this->factory()->post->create();
set_post_thumbnail( $post_id, $attachment_id );
Expand Down Expand Up @@ -635,4 +651,30 @@ function() {
$tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$this->assertNotSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}

/**
* Tests whether additional mime types generated only for allowed image sizes or not.
*
* @test
*/
public function it_should_create_mime_types_for_allowed_sizes_only() {
Comment thread
felixarntz marked this conversation as resolved.
Outdated
add_filter(
'webp_uploads_image_sizes_with_additional_mime_type_support',
function( $sizes ) {
$sizes['allowed_size_400x300'] = 'allowed_size_400x300';
Comment thread
felixarntz marked this conversation as resolved.
Outdated
return $sizes;
}
);

add_image_size( 'allowed_size_400x300', 400, 300, true );
add_image_size( 'not_allowed_size_200x150', 200, 150, true );
Comment thread
felixarntz marked this conversation as resolved.

$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' );

Comment thread
eugene-manuilov marked this conversation as resolved.
remove_image_size( 'allowed_size_400x300' );
remove_image_size( 'not_allowed_size_200x150' );

$this->assertImageHasSizeSource( $attachment_id, 'allowed_size_400x300', 'image/webp' );
$this->assertImageNotHasSizeSource( $attachment_id, 'not_allowed_size_200x150', 'image/webp' );
}
}
3 changes: 2 additions & 1 deletion tests/utils/Constraint/ImageHasSizeSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected function matches( $attachment_id ): bool {
! isset( $metadata['sizes'][ $this->size ]['sources'] ) ||
! is_array( $metadata['sizes'][ $this->size ]['sources'] )
) {
return false;
// Don't fail if we intentionally check that the image doesn't have a mime type for the size.
return $this->is_not ? true : false;
}

return $this->verify_sources( $metadata['sizes'][ $this->size ]['sources'] );
Expand Down