Changeset 54226
- Timestamp:
- 09/19/2022 10:51:53 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/image-edit.php
r54086 r54226 918 918 919 919 // Save the full-size file, also needed to create sub-sizes. 920 $saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ); 921 if ( ! $saved ) { 920 if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) { 922 921 $return->error = esc_js( __( 'Unable to save the image.' ) ); 923 922 return $return; 924 923 } 925 $new_path = $saved['path'];926 924 927 925 if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) { -
trunk/src/wp-admin/includes/image.php
r54097 r54226 452 452 if ( method_exists( $editor, 'make_subsize' ) ) { 453 453 foreach ( $new_sizes as $new_size_name => $new_size_data ) { 454 // Include size name in the data.455 $new_size_data['name'] = $new_size_name;456 457 454 $new_size_meta = $editor->make_subsize( $new_size_data ); 458 455 -
trunk/src/wp-includes/class-wp-image-editor-gd.php
r54210 r54226 228 228 * 229 229 * @param array $sizes { 230 * A ssociative array of image size names and their data.230 * An array of image size data arrays. 231 231 * 232 232 * Either a height or width must be provided. … … 248 248 249 249 foreach ( $sizes as $size => $size_data ) { 250 // Include size name in the data.251 $size_data['name'] = $size;252 253 250 $meta = $this->make_subsize( $size_data ); 254 251 … … 265 262 * 266 263 * @since 5.3.0 267 * @since 6.1.0 The $sizes parameter may now include a $name key for each entry.268 264 * 269 265 * @param array $size_data { 270 266 * Array of size data. 271 267 * 272 * @type int $width The maximum width in pixels. 273 * @type int $height The maximum height in pixels. 274 * @type bool $crop Whether to crop the image to exact dimensions. 275 * @type string $name Image size name. 268 * @type int $width The maximum width in pixels. 269 * @type int $height The maximum height in pixels. 270 * @type bool $crop Whether to crop the image to exact dimensions. 276 271 * } 277 272 * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, … … 283 278 } 284 279 285 $orig_size = $this->size; 286 $orig_size_name = $this->size_name; 280 $orig_size = $this->size; 287 281 288 282 if ( ! isset( $size_data['width'] ) ) { … … 296 290 if ( ! isset( $size_data['crop'] ) ) { 297 291 $size_data['crop'] = false; 298 }299 300 if ( isset( $size_data['name'] ) ) {301 $this->update_size_name( $size_data['name'] );302 292 } 303 293 … … 311 301 } 312 302 313 $this->size = $orig_size; 314 $this->size_name = $orig_size_name; 303 $this->size = $orig_size; 315 304 316 305 if ( ! is_wp_error( $saved ) ) { -
trunk/src/wp-includes/class-wp-image-editor-imagick.php
r54097 r54226 439 439 * 440 440 * @param array $sizes { 441 * A ssociative array of image size names and their data.441 * An array of image size data arrays. 442 442 * 443 443 * Either a height or width must be provided. … … 459 459 460 460 foreach ( $sizes as $size => $size_data ) { 461 // Include size name in the data.462 $size_data['name'] = $size;463 464 461 $meta = $this->make_subsize( $size_data ); 465 462 … … 476 473 * 477 474 * @since 5.3.0 478 * @since 6.1.0 The $sizes parameter may now include a $name key for each entry.479 475 * 480 476 * @param array $size_data { 481 477 * Array of size data. 482 478 * 483 * @type int $width The maximum width in pixels. 484 * @type int $height The maximum height in pixels. 485 * @type bool $crop Whether to crop the image to exact dimensions. 486 * @type string $name Image size name. 479 * @type int $width The maximum width in pixels. 480 * @type int $height The maximum height in pixels. 481 * @type bool $crop Whether to crop the image to exact dimensions. 487 482 * } 488 483 * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, … … 494 489 } 495 490 496 $orig_size = $this->size; 497 $orig_size_name = $this->size_name; 498 $orig_image = $this->image->getImage(); 491 $orig_size = $this->size; 492 $orig_image = $this->image->getImage(); 499 493 500 494 if ( ! isset( $size_data['width'] ) ) { … … 508 502 if ( ! isset( $size_data['crop'] ) ) { 509 503 $size_data['crop'] = false; 510 }511 512 if ( isset( $size_data['name'] ) ) {513 $this->update_size_name( $size_data['name'] );514 504 } 515 505 … … 526 516 } 527 517 528 $this->size = $orig_size; 529 $this->size_name = $orig_size_name; 530 $this->image = $orig_image; 518 $this->size = $orig_size; 519 $this->image = $orig_image; 531 520 532 521 if ( ! is_wp_error( $saved ) ) { -
trunk/src/wp-includes/class-wp-image-editor.php
r54133 r54226 16 16 protected $file = null; 17 17 protected $size = null; 18 protected $size_name = '';19 18 protected $mime_type = null; 20 19 protected $output_mime_type = null; … … 120 119 * 121 120 * @param array $sizes { 122 * A ssociative array of image size names and their data. Default sizes are 'small', 'medium', 'large'.121 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. 123 122 * 124 123 * @type array ...$0 { … … 188 187 * @since 3.5.0 189 188 * 190 * @return array{189 * @return int[] { 191 190 * Dimensions of the image. 192 191 * … … 204 203 * @since 3.5.0 205 204 * 206 * @param int $width The image width.207 * @param int $height The image height.208 * @return true True on success, false on failure.205 * @param int $width 206 * @param int $height 207 * @return true 209 208 */ 210 209 protected function update_size( $width = null, $height = null ) { … … 214 213 ); 215 214 return true; 216 }217 218 /**219 * Gets the current image size name.220 *221 * @since 6.1.0222 *223 * @return string Image size name, or empty string if none set.224 */225 public function get_size_name() {226 return $this->size_name;227 }228 229 /**230 * Sets the current image size name.231 *232 * @since 6.1.0233 *234 * @param string $size_name The image size name.235 */236 protected function update_size_name( $size_name ) {237 $this->size_name = (string) $size_name;238 215 } 239 216 … … 389 366 * 390 367 * @since 5.8.0 391 * @since 6.1.0 The $size_name parameter was added.392 368 * 393 369 * @param string[] $output_format { … … 399 375 * @param string $filename Path to the image. 400 376 * @param string $mime_type The source image mime type. 401 * @param string $size_name The image size name to create, or empty string if not set.402 377 */ 403 $output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type , $this->size_name);378 $output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type ); 404 379 405 380 if ( isset( $output_format[ $mime_type ] ) … … 451 426 } 452 427 453 /** 454 * Builds an output filename based on current file, and adding proper suffix. 455 * 456 * @since 3.5.0 457 * @since 6.1.0 Skips adding a suffix when set to an empty string. When the 458 * file extension being generated doesn't match the image file extension, 459 * add the extension to the suffix 460 * 461 * @param string $suffix Optional. Suffix to add to the filename. The default null 462 * will result in a 'widthxheight' suffix. Passing 463 * an empty string will result in no suffix. 464 * @param string $dest_path Optional. The path to save the file to. The default null 465 * will use the image file path. 466 * @param string $extension Optional. The file extension to use. The default null 467 * will use the image file extension. 468 * @return string filename The generated file name. 428 /** 429 * Builds an output filename based on current file, and adding proper suffix 430 * 431 * @since 3.5.0 432 * 433 * @param string $suffix 434 * @param string $dest_path 435 * @param string $extension 436 * @return string filename 469 437 */ 470 438 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { 471 439 // $suffix will be appended to the destination filename, just before the extension. 472 if ( null ===$suffix ) {440 if ( ! $suffix ) { 473 441 $suffix = $this->get_suffix(); 474 442 } … … 491 459 } 492 460 493 if ( empty( $suffix ) ) { 494 $suffix = ''; 495 } else { 496 $suffix = "-{$suffix}"; 497 } 498 499 // When the file extension being generated doesn't match the image file extension, 500 // add the extension to the suffix to ensure a unique file name. Prevents 501 // name conflicts when a single image type can have multiple extensions, 502 // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions. 503 if ( ! empty( $extension ) && $extension !== $ext ) { 504 $suffix .= "-{$ext}"; 505 } 506 507 return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}"; 461 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}"; 508 462 } 509 463 -
trunk/src/wp-includes/default-filters.php
r54214 r54226 637 637 add_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 ); 638 638 639 add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping', 10, 4 );640 641 639 // Embeds. 642 640 add_action( 'rest_api_init', 'wp_oembed_register_route' ); -
trunk/src/wp-includes/functions.php
r54176 r54226 2691 2691 if ( $is_image ) { 2692 2692 /** This filter is documented in wp-includes/class-wp-image-editor.php */ 2693 $output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type , '');2693 $output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type ); 2694 2694 $alt_types = array(); 2695 2695 -
trunk/src/wp-includes/media.php
r54173 r54226 3928 3928 3929 3929 /** 3930 * Filters the default image output mapping.3931 *3932 * With this filter callback, WebP image files will be generated for certain JPEG source files.3933 *3934 * @since 6.1.03935 *3936 * @param array $output_mapping Map of mime type to output format.3937 * @param string $filename Path to the image.3938 * @param string $mime_type The source image mime type.3939 * @param string $size_name Optional. The image size name to create, or empty string if not set. Default empty string.3940 * @return array The adjusted default output mapping.3941 */3942 function wp_default_image_output_mapping( $output_mapping, $filename, $mime_type, $size_name = '' ) {3943 // If size name is specified, check whether the size supports additional MIME types like WebP.3944 if ( $size_name ) {3945 // Include only the core sizes that do not rely on add_image_size(). Additional image sizes are opt-in.3946 $enabled_sizes = array(3947 'thumbnail' => true,3948 'medium' => true,3949 'medium_large' => true,3950 'large' => true,3951 'post-thumbnail' => true,3952 );3953 3954 /**3955 * Filters the sizes that support secondary mime type output. Developers can use this3956 * to control the generation of additional mime type sub-sized images.3957 *3958 * @since 6.1.03959 *3960 * @param array $enabled_sizes Map of size names and whether they support secondary mime type output.3961 */3962 $enabled_sizes = apply_filters( 'wp_image_sizes_with_additional_mime_type_support', $enabled_sizes );3963 3964 // Bail early if the size does not support additional MIME types.3965 if ( empty( $enabled_sizes[ $size_name ] ) ) {3966 return $output_mapping;3967 }3968 }3969 3970 $output_mapping['image/jpeg'] = 'image/webp';3971 return $output_mapping;3972 }3973 3974 /**3975 3930 * Prints default Plupload arguments. 3976 3931 * -
trunk/tests/phpunit/tests/image/editor.php
r54086 r54226 19 19 20 20 require_once DIR_TESTDATA . '/../includes/mock-image-editor.php'; 21 add_filter( 'image_editor_output_format', '__return_empty_array' );22 21 23 22 // This needs to come after the mock image editor class is loaded. 24 23 parent::set_up(); 25 }26 27 /**28 * Tear down the class.29 */30 public function tear_down() {31 remove_filter( 'image_editor_output_format', '__return_empty_array' );32 parent::tear_down();33 24 } 34 25 … … 236 227 237 228 // Test with a suffix only. 238 $this->assertSame( 'canola-100x50 -jpg.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );229 $this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) ); 239 230 240 231 // Combo! 241 $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new -jpg.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );232 $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) ); 242 233 243 234 // Test with a stream destination. -
trunk/tests/phpunit/tests/image/editorGd.php
r54086 r54226 18 18 require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; 19 19 20 add_filter( 'image_editor_output_format', '__return_empty_array' );21 22 20 // This needs to come after the mock image editor class is loaded. 23 21 parent::set_up(); … … 32 30 33 31 $this->remove_added_uploads(); 34 35 remove_filter( 'image_editor_output_format', '__return_empty_array' );36 32 37 33 parent::tear_down(); -
trunk/tests/phpunit/tests/image/editorImagick.php
r54086 r54226 19 19 require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php'; 20 20 21 add_filter( 'image_editor_output_format', '__return_empty_array' );22 23 21 // This needs to come after the mock image editor class is loaded. 24 22 parent::set_up(); … … 33 31 34 32 $this->remove_added_uploads(); 35 36 remove_filter( 'image_editor_output_format', '__return_empty_array' );37 33 38 34 parent::tear_down(); -
trunk/tests/phpunit/tests/image/functions.php
r54090 r54226 26 26 unlink( $file ); 27 27 } 28 29 add_filter( 'image_editor_output_format', '__return_empty_array' );30 }31 32 /**33 * Tear down the class.34 */35 public function tear_down() {36 remove_filter( 'image_editor_output_format', '__return_empty_array' );37 parent::tear_down();38 28 } 39 29 -
trunk/tests/phpunit/tests/image/intermediateSize.php
r54086 r54226 6 6 */ 7 7 class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 8 /**9 * Set up the test fixture.10 */11 public function set_up() {12 add_filter( 'image_editor_output_format', '__return_empty_array' );13 14 parent::set_up();15 }16 17 8 public function tear_down() { 18 9 $this->remove_added_uploads(); … … 22 13 remove_image_size( 'false-width' ); 23 14 remove_image_size( 'off-by-one' ); 24 25 remove_filter( 'image_editor_output_format', '__return_empty_array' );26 27 15 parent::tear_down(); 28 16 } -
trunk/tests/phpunit/tests/image/resize.php
r54086 r54226 15 15 16 16 add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) ); 17 add_filter( 'image_editor_output_format', '__return_empty_array' );18 }19 20 /**21 * Tear down the class.22 */23 public function tear_down() {24 remove_filter( 'image_editor_output_format', '__return_empty_array' );25 parent::tear_down();26 17 } 27 18 -
trunk/tests/phpunit/tests/media.php
r54128 r54226 34 34 $GLOBALS['_wp_additional_image_sizes'] = array(); 35 35 36 $filename = DIR_TESTDATA . '/images/' . self::$large_filename; 37 add_filter( 'image_editor_output_format', '__return_empty_array' ); 36 $filename = DIR_TESTDATA . '/images/' . self::$large_filename; 38 37 self::$large_id = $factory->attachment->create_upload_object( $filename ); 39 38 … … 70 69 public static function wpTearDownAfterClass() { 71 70 $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes; 72 remove_filter( 'image_editor_output_format', '__return_empty_array' );73 71 } 74 72 … … 3624 3622 remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 ); 3625 3623 } 3626 3627 /**3628 * Test the wp_default_image_output_mapping function.3629 *3630 * @ticket 554433631 */3632 public function test_wp_default_image_output_mapping() {3633 $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', '' );3634 $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );3635 }3636 3637 /**3638 * Test that wp_default_image_output_mapping doesn't overwrite existing mappings.3639 *3640 * @ticket 554433641 */3642 public function test_wp_default_image_output_mapping_existing() {3643 $mapping = array( 'mime/png' => 'mime/webp' );3644 $mapping = wp_default_image_output_mapping( $mapping, 'test.jpg', 'image/jpeg', '' );3645 $this->assertSame(3646 array(3647 'mime/png' => 'mime/webp',3648 'image/jpeg' => 'image/webp',3649 ),3650 $mapping3651 );3652 }3653 3654 /**3655 * Test that the image editor default output for JPEGs is WebP.3656 *3657 * @ticket 554433658 */3659 public function test_wp_image_editor_default_output_maps_to_webp() {3660 remove_filter( 'image_editor_output_format', '__return_empty_array' );3661 3662 $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );3663 $this->assertNotWPError( $editor );3664 3665 $resized = $editor->resize( 100, 100, false );3666 $this->assertNotWPError( $resized );3667 3668 $saved = $editor->save();3669 $this->assertNotWPError( $saved );3670 3671 if ( $editor->supports_mime_type( 'image/webp' ) ) {3672 $this->assertSame( 'image/webp', $saved['mime-type'] );3673 $this->assertSame( 'canola-100x75-jpg.webp', $saved['file'] );3674 } else {3675 $this->assertSame( 'image/jpeg', $saved['mime-type'] );3676 $this->assertSame( 'canola-100x75.jpg', $saved['file'] );3677 }3678 }3679 3680 /**3681 * @ticket 565263682 * @dataProvider data_wp_default_image_output_mapping_size_filter3683 */3684 public function test_wp_default_image_output_mapping_size_filter( $size_name, $filter_callback, $expects_webp ) {3685 remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );3686 if ( $filter_callback ) {3687 add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );3688 }3689 3690 $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', $size_name );3691 if ( $expects_webp ) {3692 $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );3693 } else {3694 $this->assertSame( array(), $mapping );3695 }3696 }3697 3698 public function data_wp_default_image_output_mapping_size_filter() {3699 return array(3700 'default size thumbnail' => array(3701 'thumbnail',3702 null,3703 true,3704 ),3705 'default size medium' => array(3706 'medium',3707 null,3708 true,3709 ),3710 'default size medium_large' => array(3711 'medium_large',3712 null,3713 true,3714 ),3715 'default size large' => array(3716 'large',3717 null,3718 true,3719 ),3720 'default size unset' => array(3721 'medium',3722 function( $enabled_sizes ) {3723 unset( $enabled_sizes['medium'] );3724 return $enabled_sizes;3725 },3726 false,3727 ),3728 'default size set to false' => array(3729 'medium',3730 function( $enabled_sizes ) {3731 $enabled_sizes['medium'] = false;3732 return $enabled_sizes;3733 },3734 false,3735 ),3736 'custom size' => array(3737 'custom',3738 null,3739 false,3740 ),3741 'custom size opted in' => array(3742 'custom',3743 function( $enabled_sizes ) {3744 $enabled_sizes['custom'] = true;3745 return $enabled_sizes;3746 },3747 true,3748 ),3749 );3750 }3751 3624 } 3752 3625 -
trunk/tests/phpunit/tests/post/attachments.php
r54086 r54226 7 7 */ 8 8 class Tests_Post_Attachments extends WP_UnitTestCase { 9 /**10 * Set up the test fixture.11 */12 public function set_up() {13 add_filter( 'image_editor_output_format', '__return_empty_array' );14 15 parent::set_up();16 }17 9 18 10 public function tear_down() { 19 11 // Remove all uploads. 20 12 $this->remove_added_uploads(); 21 remove_filter( 'image_editor_output_format', '__return_empty_array' );22 13 parent::tear_down(); 23 14 } -
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r54090 r54226 94 94 add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 95 95 add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 ); 96 add_filter( 'image_editor_output_format', '__return_empty_array' );97 96 } 98 97 … … 122 121 WP_Image_Editor_Mock::$size_return = null; 123 122 } 124 125 remove_filter( 'image_editor_output_format', '__return_empty_array' );126 123 127 124 parent::tear_down();
Note: See TracChangeset
for help on using the changeset viewer.