Ticket #53668: 53668.2.diff
File 53668.2.diff, 8.0 KB (added by , 3 years ago) |
---|
-
src/wp-includes/class-wp-image-editor.php
591 591 * @return string|false 592 592 */ 593 593 protected static function get_extension( $mime_type = null ) { 594 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) ); 595 596 if ( empty( $extensions[0] ) ) { 597 return false; 598 } 599 600 return $extensions[0]; 594 return wp_default_extension_for_mime_type( $mime_type ); 601 595 } 602 596 } 603 597 -
src/wp-includes/functions.php
2595 2595 } 2596 2596 } 2597 2597 } 2598 2599 // If a different file type might be produced for an image, check filename uniqueness for that format. 2600 $filename = _wp_check_alternate_output_format_uniqueness( $filename, $ext, $dir ); 2598 2601 } 2599 2602 2600 2603 /** … … 2645 2648 } 2646 2649 2647 2650 /** 2651 * Helper function for wp_unique_filename to check potential alternate output formats for images. 2652 * 2653 * @since 5.8.1 2654 * @private 2655 * 2656 * @param string $filename 2657 * @param string $ext 2658 * @param string $dir 2659 * 2660 * @return string 2661 */ 2662 function _wp_check_alternate_output_format_uniqueness( $filename, $ext, $dir ) { 2663 static $checking_alternates; 2664 2665 if ( empty( $checking_alternates ) ) { 2666 $checking_alternates = true; 2667 $file_type = wp_check_filetype_and_ext( trailingslashit( $dir ) . $filename, $filename ); 2668 $mime_type = $file_type['type']; 2669 2670 if ( ! empty( $mime_type ) && 0 === strpos( $mime_type, 'image/' ) ) { 2671 $output_formats = apply_filters( 'image_editor_output_format', array(), trailingslashit( $dir ) . $filename, $mime_type ); 2672 2673 if ( 2674 ! empty( $output_formats ) && 2675 is_array( $output_formats ) && 2676 ( ! empty( $output_formats[ $mime_type ] ) || in_array( $mime_type, $output_formats ) ) 2677 ) { 2678 $alt_mime_types = array(); 2679 2680 foreach ( $output_formats as $source => $target ) { 2681 if ( $source === $mime_type && $target === $mime_type ) { 2682 continue; 2683 } elseif ( $source === $mime_type ) { 2684 $alt_mime_types[] = $target; 2685 } elseif ( $target === $mime_type ) { 2686 $alt_mime_types[] = $source; 2687 } 2688 } 2689 2690 $alt_mime_types = array_unique( $alt_mime_types ); 2691 2692 foreach ( $alt_mime_types as $alt_mime_type ) { 2693 $alt_ext = wp_default_extension_for_mime_type( $alt_mime_type ); 2694 2695 if ( ! empty( $alt_ext ) && ".{$alt_ext}" !== $ext ) { 2696 $alt_filename = wp_basename( $filename, $ext ) . ".{$alt_ext}"; 2697 $alt_filename2 = wp_unique_filename( $dir, $alt_filename ); 2698 2699 // If a potential clash was found for alternate format, use its unique filename. 2700 if ( $alt_filename2 !== $alt_filename ) { 2701 $filename = wp_basename( $alt_filename2, ".{$alt_ext}" ) . $ext; 2702 } 2703 } 2704 } 2705 } 2706 } 2707 $checking_alternates = false; 2708 } 2709 2710 return $filename; 2711 } 2712 2713 /** 2648 2714 * Create a file in the upload folder with given content. 2649 2715 * 2650 2716 * If there is an error, then the key 'error' will exist with the error message. … … 3042 3108 } 3043 3109 3044 3110 /** 3111 * Returns first matched extension from Mime-type, 3112 * as mapped from wp_get_mime_types() 3113 * 3114 * @since 5.8.1 3115 * 3116 * @param string $mime_type 3117 * 3118 * @return string|false 3119 */ 3120 function wp_default_extension_for_mime_type( $mime_type = null ) { 3121 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) ); 3122 3123 if ( empty( $extensions[0] ) ) { 3124 return false; 3125 } 3126 3127 return $extensions[0]; 3128 } 3129 3130 /** 3045 3131 * Returns the real mime type of an image file. 3046 3132 * 3047 3133 * This depends on exif_imagetype() or getimagesize() to determine real mime types. -
tests/phpunit/tests/functions.php
222 222 } 223 223 224 224 /** 225 * @ticket 53668 226 */ 227 function test__wp_check_alternate_output_format_uniqueness() { 228 $testdir = DIR_TESTDATA . '/images/'; 229 230 add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); 231 232 // Standard test that wp_unique_filename allows usage if file does not exist yet. 233 $this->assertSame( 'abcdef.png', wp_unique_filename( $testdir, 'abcdef.png' ), 'non-existent file should not have name changed' ); 234 // Difference in extension does not affect wp_unique_filename by default (canola.jpg exists). 235 $this->assertSame( 'canola.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should not have name changed' ); 236 // Run again to prove no memory. 237 $this->assertSame( 'canola.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should not have name changed' ); 238 // Actual clash recognized. 239 $this->assertSame( 'canola-1.jpg', wp_unique_filename( $testdir, 'canola.jpg' ), 'existing file should have name changed' ); 240 241 // Now output jpg thumbnails for png files. 242 add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); 243 244 // Standard test that wp_unique_filename allows usage if file does not exist yet. 245 $this->assertSame( 'abcdef.png', wp_unique_filename( $testdir, 'abcdef.png' ), 'non-existent file should not have name changed' ); 246 // Difference in extension does affect wp_unique_filename when thumbnails use existing file's type. 247 $this->assertSame( 'canola-1.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should have name changed when thumbnails use existing file\'s type' ); 248 // Run again to prove no memory. 249 $this->assertSame( 'canola-1.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should have name changed when thumbnails use existing file\'s type' ); 250 // Actual clash recognized. 251 $this->assertSame( 'canola-1.jpg', wp_unique_filename( $testdir, 'canola.jpg' ), 'existing file should have name changed' ); 252 253 remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); 254 remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); 255 } 256 257 /** 258 * Changes the output format for a png file's generated thumbnails to be jpg. 259 * 260 * @param array $formats 261 * 262 * @return array 263 */ 264 public function image_editor_output_format_handler( $formats ) { 265 $formats['image/png'] = 'image/jpeg'; 266 267 return $formats; 268 } 269 270 /** 225 271 * @dataProvider data_is_not_serialized 226 272 */ 227 273 function test_maybe_serialize( $value ) { … … 1946 1992 array( 'application/activity+json, application/nojson', true ), 1947 1993 ); 1948 1994 } 1995 1996 /** 1997 * @ticket 53668 1998 */ 1999 public function test_wp_default_extension_for_mime_type() { 2000 $this->assertEquals( 'jpg', wp_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' ); 2001 $this->assertNotEquals( 'jpeg', wp_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' ); 2002 $this->assertEquals( 'png', wp_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' ); 2003 $this->assertFalse( wp_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' ); 2004 $this->assertFalse( wp_default_extension_for_mime_type(), 'false not returned when no mime type supplied' ); 2005 $this->assertFalse( wp_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' ); 2006 $this->assertFalse( wp_default_extension_for_mime_type( ' ' ), 'false not returned when empty string as mime type supplied' ); 2007 $this->assertFalse( wp_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' ); 2008 $this->assertFalse( wp_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' ); 2009 } 1949 2010 }