Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
28e98e1
Add a boolean ‘output_mimes’ parameter to the add_image_size function…
adamsilverstein Jul 26, 2022
b8d4beb
remove phpbf changes
adamsilverstein Jul 26, 2022
700b225
Remove test changes that aren’t required
adamsilverstein Jul 27, 2022
3da79dc
Update parameter name and doc block - use “additional_mimes”
adamsilverstein Jul 27, 2022
f5451c1
Skip setting output data on size
adamsilverstein Jul 27, 2022
647bac5
Remove output mimes parameter from set_post_thumbnail_size
adamsilverstein Jul 27, 2022
d7eb9cb
revert changes on one more test file
adamsilverstein Jul 27, 2022
642041b
revert unrelated changes from trunk
adamsilverstein Jul 27, 2022
a7383d5
Merge branch 'trunk' into add/add-image-size-mime-support
adamsilverstein Jul 27, 2022
bec2656
complete rename of output_mimes to additional_mimes
adamsilverstein Jul 27, 2022
94f85c3
add test coverage for add_image_size additional mimes parameter
adamsilverstein Jul 27, 2022
b58585e
Add test for _wp_filter_image_sizes_additional_mime_type_support with…
adamsilverstein Jul 27, 2022
763f97a
Update src/wp-admin/includes/image.php
adamsilverstein Aug 15, 2022
8853869
Remove ‘post-thumbnail’ from enabled_sizes in _wp_filter_image_sizes_…
adamsilverstein Aug 15, 2022
bcefc3a
Use wp_get_registered_image_subsizes() for all_images in test__wp_fil…
adamsilverstein Aug 15, 2022
5f70585
Merge branch 'trunk' into add/add-image-size-mime-support
adamsilverstein Aug 15, 2022
2474368
Improve assertion in public function test__wp_filter_image_sizes_addi…
adamsilverstein Aug 15, 2022
3a218e8
Set default for parameter to null and warn developers with `_doing_it…
adamsilverstein Aug 15, 2022
fc40a45
Update doc block
adamsilverstein Aug 15, 2022
2616d7d
Add the additional_mimes parameter in all test calls to add_image_size
adamsilverstein Aug 15, 2022
ecbdd13
Update src/wp-includes/media.php
adamsilverstein Aug 16, 2022
1b5e084
Update src/wp-includes/media.php
adamsilverstein Aug 16, 2022
3ace8e9
Update src/wp-includes/media.php
adamsilverstein Aug 16, 2022
3ed40df
Merge branch 'trunk' into add/add-image-size-mime-support
adamsilverstein Aug 16, 2022
4e674dc
Merge branch 'trunk' into add/add-image-size-mime-support
adamsilverstein Aug 17, 2022
12986a3
remove _doing_it_wrong warning (and related test changes)
adamsilverstein Aug 17, 2022
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
13 changes: 12 additions & 1 deletion src/wp-admin/includes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,20 @@ function _wp_filter_image_sizes_additional_mime_type_support( $sizes, $attachmen
'medium' => true,
'medium_large' => true,
'large' => true,
'post-thumbnail' => true,
);

// Include any sizes that were added with `add_image_size`.
$additional_image_sizes = array_map(
'__return_true',
array_filter(
wp_get_additional_image_sizes(),
function( $size ) {
return isset( $size['additional_mimes'] ) && $size['additional_mimes'];
}
)
);
$enabled_sizes = array_merge( $enabled_sizes, $additional_image_sizes );

/**
* Filter the sizes that support secondary mime type output. Developers can use this
* to control the output of additional mime type sub-sized images.
Expand Down
4 changes: 2 additions & 2 deletions src/wp-content/themes/twentyeleven/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ function twentyeleven_setup() {
* Add Twenty Eleven's custom image sizes.
* Used for large feature (header) images.
*/
add_image_size( 'large-feature', $custom_header_support['width'], $custom_header_support['height'], true );
add_image_size( 'large-feature', $custom_header_support['width'], $custom_header_support['height'], true, true );
// Used for featured posts if a large-feature doesn't exist.
add_image_size( 'small-feature', 500, 300 );
add_image_size( 'small-feature', 500, 300, false, true );

// Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
register_default_headers(
Expand Down
2 changes: 1 addition & 1 deletion src/wp-content/themes/twentyfourteen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function twentyfourteen_setup() {
// Enable support for Post Thumbnails, and declare two sizes.
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 672, 372, true );
add_image_size( 'twentyfourteen-full-width', 1038, 576, true );
add_image_size( 'twentyfourteen-full-width', 1038, 576, true, true );

// This theme uses wp_nav_menu() in two locations.
register_nav_menus(
Expand Down
4 changes: 2 additions & 2 deletions src/wp-content/themes/twentyseventeen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ function twentyseventeen_setup() {
*/
add_theme_support( 'post-thumbnails' );

add_image_size( 'twentyseventeen-featured-image', 2000, 1200, true );
add_image_size( 'twentyseventeen-featured-image', 2000, 1200, true, true );

add_image_size( 'twentyseventeen-thumbnail-avatar', 100, 100, true );
add_image_size( 'twentyseventeen-thumbnail-avatar', 100, 100, true, true );

// Set the default content width.
$GLOBALS['content_width'] = 525;
Expand Down
2 changes: 1 addition & 1 deletion src/wp-content/themes/twentytwenty/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function twentytwenty_theme_support() {
set_post_thumbnail_size( 1200, 9999 );

// Add custom image size used in Cover Template.
add_image_size( 'twentytwenty-fullscreen', 1980, 9999 );
add_image_size( 'twentytwenty-fullscreen', 1980, 9999, false, true );

// Custom logo.
$logo_width = 120;
Expand Down
42 changes: 26 additions & 16 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,26 +275,36 @@ function image_downsize( $id, $size = 'medium' ) {
* Register a new image size.
*
* @since 2.9.0
* @since 6.1.0 Add the $additional_mimes parameter.
*
* @global array $_wp_additional_image_sizes Associative array of additional image sizes.
*
* @param string $name Image size identifier.
* @param int $width Optional. Image width in pixels. Default 0.
* @param int $height Optional. Image height in pixels. Default 0.
* @param bool|array $crop Optional. Image cropping behavior. If false, the image will be scaled (default),
* If true, image will be cropped to the specified dimensions using center positions.
* If an array, the image will be cropped using the array to specify the crop location.
* Array values must be in the format: array( x_crop_position, y_crop_position ) where:
* - x_crop_position accepts: 'left', 'center', or 'right'.
* - y_crop_position accepts: 'top', 'center', or 'bottom'.
* @param string $name Image size identifier.
* @param int $width Optional. Image width in pixels. Default 0.
* @param int $height Optional. Image height in pixels. Default 0.
* @param bool|array $crop Optional. Image cropping behavior. If false, the image will be scaled (default),
* If true, image will be cropped to the specified dimensions using center positions.
* If an array, the image will be cropped using the array to specify the crop location.
* Array values must be in the format: array( x_crop_position, y_crop_position ) where:
* - x_crop_position accepts: 'left', 'center', or 'right'.
* - y_crop_position accepts: 'top', 'center', or 'bottom'.
* @param bool|null $additional_mimes Optional. Whether to output secondary mimes for this image size. If not passed,
* WordPres core will determine whether true or false. Sizes should be registered
* with false only if they are not intended for use in a front-end context.
*/
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
function add_image_size( $name, $width = 0, $height = 0, $crop = false, $additional_mimes = null ) {
global $_wp_additional_image_sizes;

if ( null === $additional_mimes ) {
// In 6.20, trigger a warning if the size was registered without providing an `additional_mimes` parameter.
$additional_mimes = false;
}

$_wp_additional_image_sizes[ $name ] = array(
'width' => absint( $width ),
'height' => absint( $height ),
'crop' => $crop,
'width' => absint( $width ),
'height' => absint( $height ),
'crop' => $crop,
'additional_mimes' => $additional_mimes,
);
}

Expand Down Expand Up @@ -345,7 +355,7 @@ function remove_image_size( $name ) {
* An array can specify positioning of the crop area. Default false.
*/
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
add_image_size( 'post-thumbnail', $width, $height, $crop );
add_image_size( 'post-thumbnail', $width, $height, $crop, true );
}

/**
Expand Down Expand Up @@ -5316,9 +5326,9 @@ function wp_media_personal_data_exporter( $email_address, $page = 1 ) {
*/
function _wp_add_additional_image_sizes() {
// 2x medium_large size.
add_image_size( '1536x1536', 1536, 1536 );
add_image_size( '1536x1536', 1536, 1536, false, true );
// 2x large size.
add_image_size( '2048x2048', 2048, 2048 );
add_image_size( '2048x2048', 2048, 2048, false, true );
Comment thread
adamsilverstein marked this conversation as resolved.
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,45 @@ public function test_add_image_size() {
$this->assertSame( 600, $sizes['test-size']['height'] );
}

/**
* @ticket 56288
*/
public function test_add_image_size_additional_mimes() {
add_image_size( 'test-size-with-additional-mimes', 200, 600, false, true );
add_image_size( 'test-size-without-additional-mimes', 200, 600, false, false );

$sizes = wp_get_additional_image_sizes();

remove_image_size( 'test-size-with-additional-mimes' );
remove_image_size( 'test-size-without-additional-mimes' );

$this->assertArrayHasKey( 'test-size-with-additional-mimes', $sizes );
$this->assertArrayHasKey( 'test-size-without-additional-mimes', $sizes );
$this->assertTrue( $sizes['test-size-with-additional-mimes']['additional_mimes'] );
$this->assertFalse( $sizes['test-size-without-additional-mimes']['additional_mimes'] );
}

/**
* @ticket 56288
*/
public function test__wp_filter_image_sizes_additional_mime_type_support_with_add_image_size() {
$default_sizes = wp_get_registered_image_subsizes();

add_image_size( 'test-size-with-additional-mimes', 200, 600, false, true );
add_image_size( 'test-size-without-additional-mimes', 200, 600, false, false );

$all_sizes = wp_get_registered_image_subsizes();
$filtered_sizes = _wp_filter_image_sizes_additional_mime_type_support( $all_sizes, $this->large_id );

remove_image_size( 'test-size-with-additional-mimes' );
remove_image_size( 'test-size-without-additional-mimes' );

$expected_size_names = array_keys( $default_sizes );
$expected_size_names[] = 'test-size-with-additional-mimes';

$this->assertSame( $expected_size_names, array_keys( $filtered_sizes ) );
}

/**
* @ticket 26768
*/
Expand Down