Media: enable generating multiple mime types for image uploads; specifically WebP versions for JPEG images by default.

This changeset adds the capability for core media uploads to generate sub sized images in more than a single mime type. The output formats for each mime type can be controlled through a filter. WebP is used as an additional output format for JPEG images by default to improve front end performance.

When generating additional mime types, only images which are smaller than the respective original are retained. By default, additional mime type images are only generated for the built-in core image sizes and any custom sizes that have opted in.

Image meta is updated with a new 'sources' array containing file details for each mime type. Each image size in the 'sizes' array also gets a new 'sources' array that contains the image file details for each mime type.

This change also increases image upload retries to accommodate additional image sizes. It also adds a `$mime_type` parameter to the `wp_get_missing_image_subsizes` function and filter.

This change adds three new filters to enable full control of secondary mime image generation and output:

* A new filter `wp_image_sizes_with_additional_mime_type_support` that filters the sizes that support secondary mime type output. Developers can use this to control the output of additional mime type sub-sized images on a per size basis.
* A new filter `wp_upload_image_mime_transforms` that filters the output mime types for a given input mime type. Developers can use this to control generation of additional mime types for a given input mime type or even override the original mime type.
* A new filter `wp_content_image_mimes` which controls image mime type output selection and order for frontend content. Developers can use this to control the mime type output preference order for content images. Content images inserted from the media library will use the available image versions based on the order from this filter.

Thanks to the many contributors who helped develop, test and give feedback on this feature.

A haiku to summarize:

Upload a JPEG
Images of all sizes
Output as WebPs

Props flixos90, MatthiasReinholz, studiolxv, markhowellsmead, eatingrules, pbiron, mukesh27, joegrainger, mehulkaklotar, tweetythierry, akshitsethi, peterwilsoncc, eugenemanuilov, mitogh, shetheliving, clarkeemily, codekraft, mikeschroder, clorith, kasparsd, spacedmonkey, trevorpfromsandee, jb510, scofennellgmailcom, seedsca, cagsmith, karinclimber, dainemawer, baxbridge, grapplerulrich, sobatkras, chynnabenton, tonylocalword, barneydavey, kwillmorth, garymatthews919, olliejones, imarkinteractive, jeffpaul, feastdesignco, webbeetle, masteradhoc.

See #55443.



git-svn-id: https://develop.svn.wordpress.org/trunk@53751 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein
2022-07-21 18:01:01 +00:00
parent 0a9e88bafc
commit 9bbf3985e3
12 changed files with 1791 additions and 128 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 344 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

+404 -2
View File
@@ -131,6 +131,7 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase {
$this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
// Removing PNG to WEBP conversion on save. Quality setting should reset to the default.
$editor->reset_output_mime_type();
remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
$editor->save();
$this->assertSame( 82, $editor->get_quality(), 'After removing image conversion quality setting should reset to the default of 82.' );
@@ -154,6 +155,7 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase {
$this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality shoild be 42.' );
// After removing the conversion the quality setting should reset to the filtered value for the original image type, JPEG.
$editor->reset_output_mime_type();
remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
$editor->save();
$this->assertSame(
@@ -226,10 +228,10 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase {
$this->assertSame( trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) );
// Test with a suffix only.
$this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
$this->assertSame( 'canola-100x50-jpg.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
// Combo!
$this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
$this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new-jpg.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
// Test with a stream destination.
$this->assertSame( 'file://testing/path/canola-100x50.jpg', $editor->generate_filename( null, 'file://testing/path' ) );
@@ -362,4 +364,404 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase {
);
}
/**
* Test creating the original image mime type when the image is uploaded.
*
* @ticket 55443
*
* @dataProvider provider_image_with_default_behaviors_during_upload
*/
public function it_should_create_the_original_image_mime_type_when_the_image_is_uploaded( $file_location, $expected_mime, $targeted_mime ) {
$attachment_id = $this->factory->attachment->create_upload_object( $file_location );
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertIsArray( $metadata );
foreach ( $metadata['sizes'] as $size_name => $properties ) {
$this->assertArrayHasKey( 'sources', $properties );
$this->assertIsArray( $properties['sources'] );
$this->assertArrayHasKey( $expected_mime, $properties['sources'] );
$this->assertArrayHasKey( 'filesize', $properties['sources'][ $expected_mime ] );
$this->assertArrayHasKey( 'file', $properties['sources'][ $expected_mime ] );
$this->assertArrayHasKey( $targeted_mime, $properties['sources'] );
$this->assertArrayHasKey( 'filesize', $properties['sources'][ $targeted_mime ] );
$this->assertArrayHasKey( 'file', $properties['sources'][ $targeted_mime ] );
}
}
/**
* Data provider for it_should_create_the_original_image_mime_type_when_the_image_is_uploaded.
*/
public function provider_image_with_default_behaviors_during_upload() {
yield 'JPEG image' => array(
DIR_TESTDATA . '/images/test-image.jpg',
'image/jpeg',
'image/webp',
);
yield 'WebP image' => array(
DIR_TESTDATA . '/images/webp-lossy.webp',
'image/webp',
'image/jpeg',
);
}
/**
* Test Do not create the sources property if no transform is provided.
*
* @ticket 55443
*/
public function it_should_not_create_the_sources_property_if_no_transform_is_provided() {
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertIsArray( $metadata );
foreach ( $metadata['sizes'] as $size_name => $properties ) {
$this->assertArrayNotHasKey( 'sources', $properties );
}
}
/**
* Test creating the sources property when no transform is available.
*
* @ticket 55443
*/
public function it_should_create_the_sources_property_when_no_transform_is_available() {
add_filter(
'wp_upload_image_mime_transforms',
function () {
return array( 'image/jpeg' => array() );
}
);
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertIsArray( $metadata );
foreach ( $metadata['sizes'] as $size_name => $properties ) {
$this->assertArrayHasKey( 'sources', $properties );
$this->assertIsArray( $properties['sources'] );
$this->assertArrayHasKey( 'image/jpeg', $properties['sources'] );
$this->assertArrayHasKey( 'filesize', $properties['sources']['image/jpeg'] );
$this->assertArrayHasKey( 'file', $properties['sources']['image/jpeg'] );
$this->assertArrayNotHasKey( 'image/webp', $properties['sources'] );
}
}
/**
* Test not creating the sources property if the mime is not specified on the transforms images.
*
* @ticket 55443
*/
public function it_should_not_create_the_sources_property_if_the_mime_is_not_specified_on_the_transforms_images() {
add_filter(
'wp_upload_image_mime_transforms',
function () {
return array( 'image/jpeg' => array() );
}
);
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/webp-lossy.webp'
);
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertIsArray( $metadata );
foreach ( $metadata['sizes'] as $size_name => $properties ) {
$this->assertArrayNotHasKey( 'sources', $properties );
}
}
/**
* Test creating a WebP version with all the required properties.
*
* @ticket 55443
*/
public function it_should_create_a_webp_version_with_all_the_required_properties() {
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertArrayHasKey( 'sources', $metadata['sizes']['thumbnail'] );
$this->assertArrayHasKey( 'image/jpeg', $metadata['sizes']['thumbnail']['sources'] );
$this->assertArrayHasKey( 'filesize', $metadata['sizes']['thumbnail']['sources']['image/jpeg'] );
$this->assertArrayHasKey( 'file', $metadata['sizes']['thumbnail']['sources']['image/jpeg'] );
$this->assertArrayHasKey( 'image/webp', $metadata['sizes']['thumbnail']['sources'] );
$this->assertArrayHasKey( 'filesize', $metadata['sizes']['thumbnail']['sources']['image/webp'] );
$this->assertArrayHasKey( 'file', $metadata['sizes']['thumbnail']['sources']['image/webp'] );
$this->assertStringEndsNotWith( '.jpeg', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] );
$this->assertStringEndsWith( '.webp', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] );
}
/**
* Test removing `scaled` suffix from the generated filename.
*
* @ticket 55443
*/
public function it_should_remove_scaled_suffix_from_the_generated_filename() {
// The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created.
add_filter(
'big_image_size_threshold',
function () {
return 850;
}
);
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertStringEndsWith( '-scaled.jpg', get_attached_file( $attachment_id ) );
$this->assertArrayHasKey( 'image/webp', $metadata['sizes']['medium']['sources'] );
$this->assertStringEndsNotWith( '-scaled.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
$this->assertStringEndsWith( '-300x200.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
}
/**
* Test removing the generated webp images when the attachment is deleted.
*
* @ticket 55443
*/
public function it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted() {
// Make sure no editor is available.
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$file = get_attached_file( $attachment_id, true );
$dirname = pathinfo( $file, PATHINFO_DIRNAME );
$this->assertIsString( $file );
$this->assertFileExists( $file );
$metadata = wp_get_attachment_metadata( $attachment_id );
$sizes = array( 'thumbnail', 'medium' );
foreach ( $sizes as $size_name ) {
$this->assertArrayHasKey( 'image/webp', $metadata['sizes'][ $size_name ]['sources'] );
$this->assertArrayHasKey( 'file', $metadata['sizes'][ $size_name ]['sources']['image/webp'] );
$this->assertFileExists(
path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] )
);
}
wp_delete_attachment( $attachment_id );
foreach ( $sizes as $size_name ) {
$this->assertFileDoesNotExist(
path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] )
);
}
}
/**
* Test removing the attached WebP version if the attachment is force deleted but empty trash day is not defined.
*
* @ticket 55443
*/
public function it_should_remove_the_attached_webp_version_if_the_attachment_is_force_deleted_but_empty_trash_day_is_not_defined() {
// Make sure no editor is available.
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$file = get_attached_file( $attachment_id, true );
$dirname = pathinfo( $file, PATHINFO_DIRNAME );
$this->assertIsString( $file );
$this->assertFileExists( $file );
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertFileExists(
path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
);
wp_delete_attachment( $attachment_id, true );
$this->assertFileDoesNotExist(
path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
);
}
/**
* Test removing the WebP version of the image if the image is force deleted and empty trash days is set to zero.
*
* @ticket 55443
*/
public function it_should_remove_the_webp_version_of_the_image_if_the_image_is_force_deleted_and_empty_trash_days_is_set_to_zero() {
// Make sure no editor is available.
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$file = get_attached_file( $attachment_id, true );
$dirname = pathinfo( $file, PATHINFO_DIRNAME );
$this->assertIsString( $file );
$this->assertFileExists( $file );
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertFileExists(
path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
);
define( 'EMPTY_TRASH_DAYS', 0 );
wp_delete_attachment( $attachment_id, true );
$this->assertFileDoesNotExist(
path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
);
}
/**
* Test avoiding the change of URLs of images that are not part of the media library.
*
* @ticket 55443
*/
public function it_should_avoid_the_change_of_urls_of_images_that_are_not_part_of_the_media_library() {
$paragraph = '<p>Donec accumsan, sapien et <img src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>';
$this->assertSame( $paragraph, webp_uploads_update_image_references( $paragraph ) );
}
/**
* Test avoiding replacing not existing attachment IDs.
*
* @ticket 55443
*/
public function it_should_avoid_replacing_not_existing_attachment_i_ds() {
$paragraph = '<p>Donec accumsan, sapien et <img class="wp-image-0" src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>';
$this->assertSame( $paragraph, webp_uploads_update_image_references( $paragraph ) );
}
/**
* Test preventing replacing a WebP image.
*
* @ticket 55443
*/
public function it_should_test_preventing_replacing_a_webp_image() {
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/webp-lossy.webp'
);
$tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}
/**
* Test preventing replacing a jpg image if the image does not have the target class name.
*
* @ticket 55443
*/
public function it_should_test_preventing_replacing_a_jpg_image_if_the_image_does_not_have_the_target_class_name() {
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$tag = wp_get_attachment_image( $attachment_id, 'medium' );
$this->assertSame( $tag, webp_uploads_update_image_references( $tag ) );
}
/**
* Test replacing the references to a JPG image to a WebP version.
*
* @dataProvider provider_replace_images_with_different_extensions
*
* @ticket 55443
*/
public function it_should_replace_the_references_to_a_jpg_image_to_a_webp_version( $image_path ) {
$attachment_id = $this->factory->attachment->create_upload_object( $image_path );
$tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$expected_tag = $tag;
$metadata = wp_get_attachment_metadata( $attachment_id );
foreach ( $metadata['sizes'] as $size => $properties ) {
$expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag );
}
$this->assertNotEmpty( $expected_tag );
$this->assertNotSame( $tag, $expected_tag );
$this->assertSame( $expected_tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}
public function provider_replace_images_with_different_extensions() {
yield 'An image with a .jpg extension' => array( DIR_TESTDATA . '/images/test-image.jpg' );
yield 'An image with a .jpeg extension' => array( DIR_TESTDATA . '/images/test-image.jpeg' );
}
/**
* Test the full image size from the original mime type.
*
* @ticket 55443
*/
public function it_should_contain_the_full_image_size_from_the_original_mime() {
$attachment_id = $this->factory->attachment->create_upload_object(
DIR_TESTDATA . '/images/test-image.jpg'
);
$tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$expected = array(
'ext' => 'jpg',
'type' => 'image/jpeg',
);
$this->assertSame( $expected, wp_check_filetype( get_attached_file( $attachment_id ) ) );
$this->assertContains( wp_basename( get_attached_file( $attachment_id ) ), webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}
/**
* Test preventing replacing an image with no available sources.
*
* @ticket 55443
*/
public function it_should_prevent_replacing_an_image_with_no_available_sources() {
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
$attachment_id = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.jpg' );
$tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}
/**
* Test preventing update not supported images with no available sources.
*
* @dataProvider provider_it_should_prevent_update_not_supported_images_with_no_available_sources
*
* @ticket 55443
*/
public function it_should_prevent_update_not_supported_images_with_no_available_sources( $image_path ) {
$attachment_id = $this->factory->attachment->create_upload_object( $image_path );
$this->assertIsNumeric( $attachment_id );
$tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}
/**
* Data provider for it_should_prevent_update_not_supported_images_with_no_available_sources.
*/
public function provider_it_should_prevent_update_not_supported_images_with_no_available_sources() {
yield 'PNG image' => array( DIR_TESTDATA . '/images/test-image.png' );
yield 'GIFT image' => array( DIR_TESTDATA . '/images/test-image.gif' );
}
}
+483
View File
@@ -639,6 +639,9 @@ class Tests_Image_Functions extends WP_UnitTestCase {
$this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
}
// Use legacy JPEG output.
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
$orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
$test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
copy( $orig_file, $test_file );
@@ -677,6 +680,12 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'height' => 300,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
'sources' => array(
'image/jpeg' => array(
'file' => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
),
),
),
'large' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
@@ -684,6 +693,12 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'height' => 1024,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
'sources' => array(
'image/jpeg' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
),
),
),
'thumbnail' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
@@ -691,6 +706,12 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'height' => 150,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
'sources' => array(
'image/jpeg' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
),
),
),
),
'filesize' => wp_filesize( $test_file ),
@@ -702,6 +723,7 @@ class Tests_Image_Functions extends WP_UnitTestCase {
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}
remove_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
}
/**
@@ -716,6 +738,9 @@ class Tests_Image_Functions extends WP_UnitTestCase {
update_option( 'medium_crop', 1 );
// Use legacy JPEG output.
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
$orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
$test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
copy( $orig_file, $test_file );
@@ -754,6 +779,12 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'height' => 300,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
'sources' => array(
'image/jpeg' => array(
'file' => 'wordpress-gsoc-flyer-pdf-300x300.jpg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
),
),
),
'large' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
@@ -761,6 +792,13 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'height' => 1024,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
'sources' => array(
'image/jpeg' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
),
),
),
'thumbnail' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
@@ -768,6 +806,12 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'height' => 150,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
'sources' => array(
'image/jpeg' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
),
),
),
),
'filesize' => wp_filesize( $test_file ),
@@ -779,6 +823,8 @@ class Tests_Image_Functions extends WP_UnitTestCase {
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}
remove_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
}
/**
@@ -789,6 +835,9 @@ class Tests_Image_Functions extends WP_UnitTestCase {
$this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
}
// Use legacy JPEG output.
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
$orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
$test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
copy( $orig_file, $test_file );
@@ -821,6 +870,12 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'height' => 100,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
'sources' => array(
'image/jpeg' => array(
'file' => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
),
),
);
// Different environments produce slightly different filesize results.
@@ -836,6 +891,7 @@ class Tests_Image_Functions extends WP_UnitTestCase {
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}
remove_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
}
public function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) {
@@ -1026,4 +1082,431 @@ class Tests_Image_Functions extends WP_UnitTestCase {
),
);
}
/**
* @ticket 55443
*/
public function test_wp_upload_image_mime_transforms_generates_webp_and_jpeg_for_both_by_default() {
$result = wp_upload_image_mime_transforms( 42 );
$this->assertArrayHasKey( 'image/jpeg', $result );
$this->assertArrayHasKey( 'image/webp', $result );
$this->assertSameSets( array( 'image/jpeg', 'image/webp' ), $result['image/jpeg'] );
$this->assertSameSets( array( 'image/jpeg', 'image/webp' ), $result['image/webp'] );
}
/**
* @ticket 55443
*/
public function test_wp_upload_image_mime_transforms_filter_always_use_webp_instead_of_jpeg() {
add_filter(
'wp_upload_image_mime_transforms',
function( $transforms ) {
// Ensure JPG only results in WebP files.
$transforms['image/jpeg'] = array( 'image/webp' );
// Unset WebP since it does not need any transformation in that case.
unset( $transforms['image/webp'] );
return $transforms;
}
);
$result = wp_upload_image_mime_transforms( 42 );
$this->assertArrayHasKey( 'image/jpeg', $result );
$this->assertArrayNotHasKey( 'image/webp', $result );
$this->assertSameSets( array( 'image/webp' ), $result['image/jpeg'] );
}
/**
* @ticket 55443
*/
public function test_wp_upload_image_mime_transforms_filter_receives_parameters() {
$attachment_id = null;
add_filter(
'wp_upload_image_mime_transforms',
function( $transforms, $param1 ) use ( &$attachment_id ) {
$attachment_id = $param1;
return $transforms;
},
10,
2
);
wp_upload_image_mime_transforms( 23 );
$this->assertSame( 23, $attachment_id );
}
/**
* @ticket 55443
*/
public function test_wp_upload_image_mime_transforms_filter_with_empty_array() {
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
$result = wp_upload_image_mime_transforms( 42 );
$this->assertSame( array(), $result );
}
/**
* @ticket 55443
*/
public function test_wp_upload_image_mime_transforms_filter_with_invalid_usage() {
$default = wp_upload_image_mime_transforms( 42 );
add_filter( 'wp_upload_image_mime_transforms', '__return_false' );
$result = wp_upload_image_mime_transforms( 42 );
$this->assertSame( $default, $result );
}
/**
* @ticket 55443
*/
public function test__wp_get_primary_and_additional_mime_types_default() {
$jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';
list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );
$this->assertSame( 'image/jpeg', $primary_mime_type );
// WebP may not be supported by the server, in which case it will be stripped from the results.
if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->assertSame( array( 'image/webp' ), $additional_mime_types );
} else {
$this->assertSame( array(), $additional_mime_types );
}
}
/**
* @ticket 55443
*/
public function test__wp_get_primary_and_additional_mime_types_prefer_original_mime() {
$jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';
// Set 'image/jpeg' only as secondary output MIME type.
// Still, because it is the original, it should be chosen as primary over 'image/webp'.
add_filter(
'wp_upload_image_mime_transforms',
function( $transforms ) {
$transforms['image/jpeg'] = array( 'image/webp', 'image/jpeg' );
return $transforms;
}
);
list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );
$this->assertSame( 'image/jpeg', $primary_mime_type );
// WebP may not be supported by the server, in which case it will be stripped from the results.
if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->assertSame( array( 'image/webp' ), $additional_mime_types );
} else {
$this->assertSame( array(), $additional_mime_types );
}
}
/**
* @ticket 55443
*/
public function test__wp_get_primary_and_additional_mime_types_use_original_mime_when_no_transformation_rules() {
$jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';
// Strip all transformation rules.
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );
$this->assertSame( 'image/jpeg', $primary_mime_type );
$this->assertSame( array(), $additional_mime_types );
}
/**
* @ticket 55443
*/
public function test__wp_get_primary_and_additional_mime_types_different_output_mime() {
$jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';
// Set 'image/webp' as the only output MIME type.
// In that case, JPEG is not generated at all, so WebP becomes the primary MIME type.
add_filter(
'wp_upload_image_mime_transforms',
function( $transforms ) {
$transforms['image/jpeg'] = array( 'image/webp' );
return $transforms;
}
);
list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );
// WebP may not be supported by the server, in which case it will fall back to the original MIME type.
if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->assertSame( 'image/webp', $primary_mime_type );
} else {
$this->assertSame( 'image/jpeg', $primary_mime_type );
}
$this->assertSame( array(), $additional_mime_types );
}
/**
* @ticket 55443
*/
public function test__wp_get_primary_and_additional_mime_types_different_output_mimes() {
$jpeg_file = DIR_TESTDATA . '/images/test-image-large.jpg';
// Set 'image/webp' and 'image/avif' as output MIME types.
// In that case, JPEG is not generated at all, with WebP being the primary MIME type and AVIF the secondary.
add_filter(
'wp_upload_image_mime_transforms',
function( $transforms ) {
$transforms['image/jpeg'] = array( 'image/webp', 'image/avif' );
return $transforms;
}
);
list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $jpeg_file, 42 );
// WebP may not be supported by the server, in which case it will fall back to the original MIME type.
if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->assertSame( 'image/webp', $primary_mime_type );
} else {
$this->assertSame( 'image/jpeg', $primary_mime_type );
}
// AVIF may not be supported by the server, in which case it will be stripped from the results.
if ( wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
$this->assertSame( array( 'image/avif' ), $additional_mime_types );
} else {
$this->assertSame( array(), $additional_mime_types );
}
}
/**
* @ticket 55443
* @dataProvider data__wp_filter_image_sizes_additional_mime_type_support
*/
public function test__wp_filter_image_sizes_additional_mime_type_support( $input_size_data, $filter_callback, $expected_size_names ) {
remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );
if ( $filter_callback ) {
add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );
}
$expected_size_data = array_intersect_key( $input_size_data, array_flip( $expected_size_names ) );
$output_size_data = _wp_filter_image_sizes_additional_mime_type_support( $input_size_data, 42 );
$this->assertEqualSetsWithIndex( $expected_size_data, $output_size_data );
}
public function data__wp_filter_image_sizes_additional_mime_type_support() {
$thumbnail_data = array(
'width' => 150,
'height' => 150,
'crop' => true,
);
$medium_data = array(
'width' => 300,
'height' => 300,
'crop' => false,
);
$medium_large_data = array(
'width' => 768,
'height' => 0,
'crop' => false,
);
$large_data = array(
'width' => 1024,
'height' => 1024,
'crop' => false,
);
$custom_data = array(
'width' => 512,
'height' => 512,
'crop' => true,
);
return array(
array(
array(
'thumbnail' => $thumbnail_data,
'medium' => $medium_data,
'medium_large' => $medium_large_data,
'large' => $large_data,
),
null,
array( 'thumbnail', 'medium', 'medium_large', 'large' ),
),
array(
array(
'thumbnail' => $thumbnail_data,
'medium' => $medium_data,
'custom' => $custom_data,
),
null,
array( 'thumbnail', 'medium' ),
),
array(
array(
'thumbnail' => $thumbnail_data,
'medium' => $medium_data,
'medium_large' => $medium_large_data,
'large' => $large_data,
),
function( $enabled_sizes ) {
unset( $enabled_sizes['medium_large'], $enabled_sizes['large'] );
return $enabled_sizes;
},
array( 'thumbnail', 'medium' ),
),
array(
array(
'thumbnail' => $thumbnail_data,
'medium' => $medium_data,
'medium_large' => $medium_large_data,
'large' => $large_data,
),
function( $enabled_sizes ) {
$enabled_sizes['medium_large'] = false;
$enabled_sizes['large'] = false;
return $enabled_sizes;
},
array( 'thumbnail', 'medium' ),
),
array(
array(
'thumbnail' => $thumbnail_data,
'medium' => $medium_data,
'custom' => $custom_data,
),
function( $enabled_sizes ) {
unset( $enabled_sizes['medium'] );
$enabled_sizes['custom'] = true;
return $enabled_sizes;
},
array( 'thumbnail', 'custom' ),
),
);
}
/**
* Test the `_wp_maybe_scale_and_rotate_image()` function.
*
* @dataProvider data_test__wp_maybe_scale_and_rotate_image
*
* @ticket 55443
*/
public function test__wp_maybe_scale_and_rotate_image( $file, $imagesize, $mime_type, $expected ) {
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
$this->markTestSkipped( sprintf( 'This test requires %s support.', $mime_type ) );
}
$attributes = array( 'post_mime_type' => $mime_type );
$attachment_id = $this->factory->attachment->create_object( $file, 0, $attributes );
$exif_meta = wp_read_image_metadata( $file );
list( $editor, $resized, $rotated ) = _wp_maybe_scale_and_rotate_image( $file, $attachment_id, $imagesize, $exif_meta, $mime_type );
$this->assertSame( $expected['rotated'], $rotated );
$this->assertSame( $expected['resized'], $resized );
$this->assertSame( $expected['size'], $editor->get_size() );
}
/**
* Data provider for the `test__wp_maybe_scale_and_rotate_image()` test.
*
* @return array
*/
public function data_test__wp_maybe_scale_and_rotate_image() {
return array(
// Image that will be scaled.
array(
DIR_TESTDATA . '/images/test-image-large.jpg',
array( 3000, 2250 ),
'image/jpeg',
array(
'rotated' => false,
'resized' => true,
'size' => array(
'width' => 2560,
'height' => 1920,
),
),
),
// Image that will not be scaled.
array(
DIR_TESTDATA . '/images/canola.jpg',
array( 640, 480 ),
'image/jpeg',
array(
'rotated' => false,
'resized' => false,
'size' => array(
'width' => 640,
'height' => 480,
),
),
),
// Image that will be flipped.
array(
DIR_TESTDATA . '/images/test-image-upside-down.jpg',
array( 600, 450 ),
'image/jpeg',
array(
'rotated' => true,
'resized' => false,
'size' => array(
'width' => 600,
'height' => 450,
),
),
),
// Image that will be rotated.
array(
DIR_TESTDATA . '/images/test-image-rotated-90ccw.jpg',
array( 1200, 1800 ),
'image/jpeg',
array(
'rotated' => true,
'resized' => false,
'size' => array(
'width' => 1800,
'height' => 1200,
),
),
),
// Image that will not be rotated - WebP Exif is not supported in PHP.
array(
DIR_TESTDATA . '/images/test-image-rotated-90cw.webp',
array( 1024, 768 ),
'image/webp',
array(
'rotated' => false,
'resized' => false,
'size' => array(
'width' => 1024,
'height' => 768,
),
),
),
);
}
/**
* Test the `_wp_get_image_suffix()` function.
* @dataProvider data_test__wp_get_image_suffix
*
* @ticket 55443
*/
public function test__wp_get_image_suffix( $resized, $rotated, $expected ) {
$this->assertSame( $expected, _wp_get_image_suffix( $resized, $rotated ) );
}
/**
* Data provider for the `test__wp_get_image_suffix()` test.
*/
public function data_test__wp_get_image_suffix() {
return array(
array( false, false, '' ),
array( true, false, 'scaled' ),
array( false, true, 'rotated' ),
array( true, true, 'scaled' ),
);
}
}
+185
View File
@@ -2251,11 +2251,14 @@ EOF;
// Do not add width, height, and loading.
add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
}
/**
@@ -2289,9 +2292,12 @@ EOF;
$img = wp_img_tag_add_loading_attr( $img, 'test' );
$img = wp_img_tag_add_decoding_attr( $img, 'the_content' );
$img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img );
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
// The content filter should return the image unchanged.
$this->assertSame( $img, wp_filter_content_tags( $img ) );
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
}
/**
@@ -2361,6 +2367,7 @@ EOF;
add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' );
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
add_filter(
'wp_content_img_tag',
@@ -2423,6 +2430,7 @@ EOF;
* @requires function imagejpeg
*/
public function test_wp_filter_content_tags_schemes() {
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$image_meta = wp_get_attachment_metadata( self::$large_id );
$size_array = $this->get_image_size_array_from_meta( $image_meta, 'medium' );
@@ -2468,6 +2476,7 @@ EOF;
$actual = wp_filter_content_tags( $unfiltered );
$this->assertSame( $expected, $actual );
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
}
/**
@@ -2961,11 +2970,13 @@ EOF;
// Do not add loading, srcset, and sizes.
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
}
/**
@@ -3041,11 +3052,13 @@ EOF;
// Do not add width, height, srcset, and sizes.
add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
}
/**
@@ -3074,9 +3087,13 @@ EOF;
// Enable globally for all tags.
add_filter( 'wp_lazy_loading_enabled', '__return_true' );
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
remove_filter( 'wp_lazy_loading_enabled', '__return_true' );
remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
}
/**
@@ -3101,9 +3118,12 @@ EOF;
// Disable globally for all tags.
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$this->assertSame( $content, wp_filter_content_tags( $content ) );
remove_filter( 'wp_lazy_loading_enabled', '__return_false' );
remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
}
/**
@@ -3529,6 +3549,7 @@ EOF;
*/
function test_wp_filter_content_tags_with_wp_get_loading_attr_default() {
global $wp_query, $wp_the_query;
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$img1 = get_image_tag( self::$large_id, '', '', '', 'large' );
$iframe1 = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
@@ -3564,6 +3585,7 @@ EOF;
$content_filtered = wp_filter_content_tags( $content_unfiltered, 'the_content' );
remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
}
remove_filter( 'wp_content_image_mimes', '__return_empty_array' );
// After filtering, the first image should not be lazy-loaded while the other ones should be.
$this->assertSame( $content_expected, $content_filtered );
@@ -3613,6 +3635,169 @@ EOF;
// Clean up the above filter.
remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
}
/**
* @ticket 55443
*/
public function test_wp_image_use_alternate_mime_types_replaces_jpg_with_webp_where_available() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'This test requires WebP support.' );
}
// The attachment $large_id is a JPEG image, so it gets WebP files generated by default.
$tag = wp_get_attachment_image( self::$large_id, 'full' );
$expected_tag = $tag;
$metadata = wp_get_attachment_metadata( self::$large_id );
foreach ( $metadata['sizes'] as $size => $properties ) {
// Some sizes may not have WebP if the WebP file is larger than the JPEG for the size.
if ( ! isset( $properties['sources']['image/webp'] ) ) {
continue;
}
$expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag );
}
// Same applies to the full size.
if ( isset( $metadata['sources']['image/webp'] ) ) {
$expected_tag = str_replace( $metadata['sources']['image/jpeg']['file'], $metadata['sources']['image/webp']['file'], $expected_tag );
}
$this->assertNotSame( $tag, $expected_tag );
$this->assertSame( $expected_tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );
}
/**
* @ticket 55443
*/
public function test_wp_image_use_alternate_mime_types_does_not_replace_jpg_when_webp_is_not_available() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'This test requires WebP support.' );
}
// The attachment $large_id is a JPEG image, so it gets WebP files generated by default.
$tag = wp_get_attachment_image( self::$large_id, 'full' );
// Update attachment metadata as if the image had no WebP available for any sub-sizes and the full size.
$metadata = wp_get_attachment_metadata( self::$large_id );
foreach ( $metadata['sizes'] as $size => $properties ) {
unset( $metadata['sizes'][ $size ]['sources']['image/webp'] );
}
unset( $metadata['sources']['image/webp'] );
wp_update_attachment_metadata( self::$large_id, $metadata );
$this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );
}
/**
* @ticket 55443
*/
public function test_wp_image_use_alternate_mime_types_still_replaces_jpg_subsizes_when_webp_is_not_available_for_full_size() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'This test requires WebP support.' );
}
// The attachment $large_id is a JPEG image, so it gets WebP files generated by default.
$tag = wp_get_attachment_image( self::$large_id, 'full' );
$expected_tag = $tag;
// Update attachment metadata as if the image had no WebP available for the full size.
$metadata = wp_get_attachment_metadata( self::$large_id );
unset( $metadata['sources']['image/webp'] );
wp_update_attachment_metadata( self::$large_id, $metadata );
foreach ( $metadata['sizes'] as $size => $properties ) {
// Some sizes may not have WebP if the WebP file is larger than the JPEG for the size.
if ( ! isset( $properties['sources']['image/webp'] ) ) {
continue;
}
$expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag );
}
$this->assertNotSame( $tag, $expected_tag );
$this->assertSame( $expected_tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );
}
/**
* @ticket 55443
*/
public function test_wp_image_use_alternate_mime_types_respects_wp_content_image_mimes_filter() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'This test requires WebP support.' );
}
// The attachment $large_id is a JPEG image, so it gets WebP files generated by default.
$tag = wp_get_attachment_image( self::$large_id, 'full' );
// Invalid filter value results in no changes to content.
add_filter( 'wp_content_image_mimes', '__return_false' );
$this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );
// Empty array results in no changes to content.
add_filter( 'wp_content_image_mimes', '__return_empty_array' );
$this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );
// Preferring JPEG over WebP results in no changes to content.
add_filter(
'wp_content_image_mimes',
function() {
return array( 'image/jpeg', 'image/webp' );
}
);
$this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );
}
/**
* @ticket 55443
*/
public function test__wp_in_front_end_context_without_wp_query() {
unset( $GLOBALS['wp_query'] );
$this->assertFalse( _wp_in_front_end_context() );
}
/**
* @ticket 55443
*/
public function test__wp_in_front_end_context_with_feed() {
remove_all_actions( 'template_redirect' );
do_action( 'template_redirect' );
$GLOBALS['wp_query']->is_feed = true;
$this->assertFalse( _wp_in_front_end_context() );
}
/**
* @ticket 55443
*/
public function test__wp_in_front_end_context_before_and_after_template_redirect() {
$result = _wp_in_front_end_context();
remove_all_actions( 'template_redirect' );
do_action( 'template_redirect' );
$this->assertFalse( $result );
$this->assertTrue( _wp_in_front_end_context() );
}
/**
* @ticket 55443
*/
public function test__wp_in_front_end_context_within_wp_head() {
remove_all_actions( 'template_redirect' );
do_action( 'template_redirect' );
// Call function within a 'wp_head' callback.
remove_all_actions( 'wp_head' );
$result = null;
add_action(
'wp_head',
function() use ( &$result ) {
$result = _wp_in_front_end_context();
}
);
do_action( 'wp_head' );
$this->assertFalse( $result );
}
}
/**