Ticket #43658: 43658.2.diff
File 43658.2.diff, 9.3 KB (added by , 6 years ago) |
---|
-
src/wp-includes/media.php
diff --git src/wp-includes/media.php src/wp-includes/media.php index ace9021fd7..a96e293ec7 100644
function image_constrain_size_for_editor( $width, $height, $size = 'medium', $co 66 66 if ( is_array( $size ) ) { 67 67 $max_width = $size[0]; 68 68 $max_height = $size[1]; 69 } elseif ( $size == 'thumb' || $size == 'thumbnail') {69 } elseif ( 'thumb' === $size || 'thumbnail' === $size ) { 70 70 $max_width = intval( get_option( 'thumbnail_size_w' ) ); 71 71 $max_height = intval( get_option( 'thumbnail_size_h' ) ); 72 72 // last chance thumbnail size defaults … … function image_constrain_size_for_editor( $width, $height, $size = 'medium', $co 74 74 $max_width = 128; 75 75 $max_height = 96; 76 76 } 77 } elseif ( $size == 'medium') {77 } elseif ( 'medium' === $size ) { 78 78 $max_width = intval( get_option( 'medium_size_w' ) ); 79 79 $max_height = intval( get_option( 'medium_size_h' ) ); 80 80 81 } elseif ( $size == 'medium_large') {81 } elseif ( 'medium_large' === $size ) { 82 82 $max_width = intval( get_option( 'medium_large_size_w' ) ); 83 83 $max_height = intval( get_option( 'medium_large_size_h' ) ); 84 84 85 85 if ( intval( $content_width ) > 0 ) { 86 86 $max_width = min( intval( $content_width ), $max_width ); 87 87 } 88 } elseif ( $size == 'large') {88 } elseif ( 'large' === $size ) { 89 89 /* 90 90 * We're inserting a large size image into the editor. If it's a really 91 91 * big image we'll scale it down to fit reasonably within the editor … … function image_downsize( $id, $size = 'medium' ) { 195 195 * @param array|string $size Size of image. Image size or array of width and height values (in that order). 196 196 * Default 'medium'. 197 197 */ 198 if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) { 198 $out = apply_filters( 'image_downsize', false, $id, $size ); 199 if ( $out ) { 199 200 return $out; 200 201 } 201 202 202 203 $img_url = wp_get_attachment_url( $id ); 203 204 $meta = wp_get_attachment_metadata( $id ); 204 $width = $height = 0; 205 $height = 0; 206 $width = $height; 205 207 $is_intermediate = false; 206 208 $img_url_basename = wp_basename( $img_url ); 209 $intermediate = image_get_intermediate_size( $id, $size ); 210 $thumb_file = wp_get_attachment_thumb_file( $id ); 211 $info = $thumb_file && getimagesize( $thumb_file ); 207 212 208 213 // If the file isn't an image, attempt to replace its URL with a rendered image from its meta. 209 214 // Otherwise, a non-image type could be returned. … … function image_downsize( $id, $size = 'medium' ) { 219 224 } 220 225 221 226 // try for a new style intermediate size 222 if ( $intermediate = image_get_intermediate_size( $id, $size )) {227 if ( $intermediate ) { 223 228 $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); 224 229 $width = $intermediate['width']; 225 230 $height = $intermediate['height']; 226 231 $is_intermediate = true; 227 } elseif ( $size == 'thumbnail') {232 } elseif ( 'thumbnail' === $size ) { 228 233 // fall back to the old thumbnail 229 if ( ( $thumb_file = wp_get_attachment_thumb_file( $id ) ) && $info = getimagesize( $thumb_file )) {234 if ( $thumb_file && $info ) { 230 235 $img_url = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url ); 231 236 $width = $info[0]; 232 237 $height = $info[1]; … … function wp_constrain_dimensions( $current_width, $current_height, $max_width = 412 417 return array( $current_width, $current_height ); 413 418 } 414 419 415 $width_ratio = $height_ratio = 1.0; 416 $did_width = $did_height = false; 420 $height_ratio = 1.0; 421 $width_ratio = $height_ratio; 422 $did_height = false; 423 $did_width = $did_height; 417 424 418 425 if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) { 419 426 $width_ratio = $max_width / $current_width; … … function wp_image_matches_ratio( $source_width, $source_height, $target_width, $ 687 694 * } 688 695 */ 689 696 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 690 if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) { 697 $imagedata = wp_get_attachment_metadata( $post_id ); 698 if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { 691 699 return false; 692 700 } 693 701 … … function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon 826 834 if ( ! $image ) { 827 835 $src = false; 828 836 829 if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) { 837 if ( $icon && wp_mime_type_icon( $attachment_id ) ) { 838 $src = wp_mime_type_icon( $attachment_id ); 830 839 /** This filter is documented in wp-includes/post.php */ 831 840 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 832 841 … … function _wp_get_attachment_relative_path( $file ) { 984 993 * or false if the size doesn't exist. 985 994 */ 986 995 function _wp_get_image_size_from_meta( $size_name, $image_meta ) { 987 if ( $size_name === 'full') {996 if ( 'full' === $size_name ) { 988 997 return array( 989 998 absint( $image_meta['width'] ), 990 999 absint( $image_meta['height'] ), … … function _wp_get_image_size_from_meta( $size_name, $image_meta ) { 1014 1023 * @return string|bool A 'srcset' value string or false. 1015 1024 */ 1016 1025 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { 1017 if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 1026 $image = wp_get_attachment_image_src( $attachment_id, $size ); 1027 if ( ! $image ) { 1018 1028 return false; 1019 1029 } 1020 1030 … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1146 1156 1147 1157 // If the file name is part of the `src`, we've confirmed a match. 1148 1158 if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) { 1149 $src_matched = $is_src = true; 1159 $is_src = true; 1160 $src_matched = $is_src; 1150 1161 } 1151 1162 1152 1163 // Filter out images that are from previous edits. … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1232 1243 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 1233 1244 */ 1234 1245 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { 1235 if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 1246 $image = wp_get_attachment_image_src( $attachment_id, $size ); 1247 if ( ! $image ) { 1236 1248 return false; 1237 1249 } 1238 1250 … … function wp_enqueue_media( $args = array() ) { 3576 3588 $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year ); 3577 3589 } 3578 3590 3591 // Filter to show only available mime types. 3592 $avail_post_mime_types = get_available_post_mime_types( 'attachment' ); 3593 $mimeTypes = wp_list_pluck( get_post_mime_types(), 0 ); 3594 foreach ( $mimeTypes as $mime_type => $label ) { 3595 if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) { 3596 unset( $mimeTypes[ $mime_type ] ); 3597 } 3598 } 3579 3599 $settings = array( 3580 3600 'tabs' => $tabs, 3581 3601 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ), 3582 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ),3602 'mimeTypes' => $mimeTypes, 3583 3603 /** This filter is documented in wp-admin/includes/media.php */ 3584 3604 'captions' => ! apply_filters( 'disable_captions', '' ), 3585 3605 'nonce' => array( -
tests/phpunit/tests/functions.php
diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php index 88b9a46475..94a1c48c69 100644
class Tests_Functions extends WP_UnitTestCase { 465 465 $this->assertNotEmpty( $mimes ); 466 466 } 467 467 468 function check_settings_for_single( $settings ) { 469 $this->assertEquals( array( 'image' ), array_keys( $settings['mimeTypes'] ) ); 470 return $settings; 471 } 472 473 function check_settings_for_multiple( $settings ) { 474 $this->assertEquals( array( 'image', 'audio' ), array_keys( $settings['mimeTypes'] ) ); 475 return $settings; 476 } 477 478 /** 479 * Test that the media grid uses the correct available single media type. 480 * @ticket 43658 481 */ 482 function test_wp_enqueue_media_single_mime_type() { 483 $filename = DIR_TESTDATA . '/images/test-image.jpg'; 484 $contents = file_get_contents( $filename ); 485 $upload = wp_upload_bits( basename( $filename ), null, $contents ); 486 $attachment_id = $this->_make_attachment( $upload ); 487 488 add_filter( 489 'media_view_settings', 490 array( $this, 'check_settings_for_single' ) 491 ); 492 wp_enqueue_media(); 493 remove_all_filters( 'media_view_settings' ); 494 } 495 496 /** 497 * Test that the media grid uses the correct available multiple media types. 498 * @ticket 43658 499 */ 500 function test_wp_enqueue_media_multiple_mime_types() { 501 $filename = DIR_TESTDATA . '/images/test-image.jpg'; 502 $contents = file_get_contents( $filename ); 503 $upload = wp_upload_bits( basename( $filename ), null, $contents ); 504 $attachment_id = $this->_make_attachment( $upload ); 505 506 $filename = DIR_TESTDATA . '/uploads/small-audio.mp3'; 507 $contents = file_get_contents( $filename ); 508 $upload = wp_upload_bits( basename( $filename ), null, $contents ); 509 $attachment_id = $this->_make_attachment( $upload ); 510 511 add_filter( 512 'media_view_settings', 513 array( $this, 'check_settings_for_multiple' ) 514 ); 515 wp_enqueue_media(); 516 } 517 468 518 /** 469 519 * @ticket 21594 470 520 */