Ticket #43658: 43658.3.diff
File 43658.3.diff, 16.1 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..38f69b0c41 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_make_content_images_responsive( $content ) { 1318 1330 return $content; 1319 1331 } 1320 1332 1321 $selected_images = $attachment_ids = array(); 1333 $attachment_ids = array(); 1334 $selected_images = $attachment_ids; 1322 1335 1323 1336 foreach ( $matches[0] as $image ) { 1324 if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && 1325 ( $attachment_id = absint( $class_id[1] ) ) ) { 1326 1327 /* 1328 * If exactly the same image tag is used more than once, overwrite it. 1329 * All identical tags will be replaced later with 'str_replace()'. 1330 */ 1331 $selected_images[ $image ] = $attachment_id; 1332 // Overwrite the ID when the same image is included more than once. 1333 $attachment_ids[ $attachment_id ] = true; 1337 if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { 1338 $attachment_id = absint( $class_id[1] ); 1339 if ( $attachment_id ) { 1340 1341 /* 1342 * If exactly the same image tag is used more than once, overwrite it. 1343 * All identical tags will be replaced later with 'str_replace()'. 1344 */ 1345 $selected_images[ $image ] = $attachment_id; 1346 // Overwrite the ID when the same image is included more than once. 1347 $attachment_ids[ $attachment_id ] = true; 1348 } 1334 1349 } 1335 1350 } 1336 1351 … … function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 1394 1409 */ 1395 1410 $image_filename = wp_basename( $image_src ); 1396 1411 1397 if ( $image_filename === wp_basename( $image_meta['file'] )) {1412 if ( wp_basename( $image_meta['file'] ) === $image_filename ) { 1398 1413 $width = (int) $image_meta['width']; 1399 1414 $height = (int) $image_meta['height']; 1400 1415 } else { … … function img_caption_shortcode( $attr, $content = null ) { 1539 1554 * @param string $content The image element, possibly wrapped in a hyperlink. 1540 1555 */ 1541 1556 $output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); 1542 if ( $output != '') {1557 if ( '' != $output ) { 1543 1558 return $output; 1544 1559 } 1545 1560 … … function img_caption_shortcode( $attr, $content = null ) { 1561 1576 return $content; 1562 1577 } 1563 1578 1564 $id = $caption_id = $describedby = ''; 1579 $describedby = ''; 1580 $caption_id = $describedby 1581 $id = $caption_id; 1565 1582 1566 1583 if ( $atts['id'] ) { 1567 1584 $atts['id'] = sanitize_html_class( $atts['id'] ); … … function gallery_shortcode( $attr ) { 1705 1722 * @param int $instance Unique numeric ID of this gallery shortcode instance. 1706 1723 */ 1707 1724 $output = apply_filters( 'post_gallery', '', $attr, $instance ); 1708 if ( $output != '') {1725 if ( '' != $output ) { 1709 1726 return $output; 1710 1727 } 1711 1728 … … function gallery_shortcode( $attr ) { 1878 1895 </{$captiontag}>"; 1879 1896 } 1880 1897 $output .= "</{$itemtag}>"; 1881 if ( ! $html5 && $columns > 0 && ++$i % $columns == 0) {1898 if ( ! $html5 && $columns > 0 && 0 == ++$i % $columns ) { 1882 1899 $output .= '<br style="clear: both" />'; 1883 1900 } 1884 1901 } 1885 1902 1886 if ( ! $html5 && $columns > 0 && $i % $columns !== 0) {1903 if ( ! $html5 && $columns > 0 && $i % 0 !== $columns ) { 1887 1904 $output .= " 1888 1905 <br style='clear: both' />"; 1889 1906 } … … function wp_playlist_shortcode( $attr ) { 2025 2042 * @param int $instance Unique numeric ID of this playlist shortcode instance. 2026 2043 */ 2027 2044 $output = apply_filters( 'post_playlist', '', $attr, $instance ); 2028 if ( $output != '') {2045 if ( '' != $output ) { 2029 2046 return $output; 2030 2047 } 2031 2048 … … function wp_playlist_shortcode( $attr ) { 2049 2066 2050 2067 $id = intval( $atts['id'] ); 2051 2068 2052 if ( $atts['type'] !== 'audio') {2069 if ( 'audio' !== $atts['type'] ) { 2053 2070 $atts['type'] = 'video'; 2054 2071 } 2055 2072 … … function wp_video_shortcode( $attr, $content = '' ) { 2582 2599 } 2583 2600 } 2584 2601 2585 $is_vimeo = $is_youtube = false; 2602 $is_youtube = false; 2603 $is_vimeo = $is_youtube 2586 2604 $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#'; 2587 2605 $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#'; 2588 2606 … … function get_attachment_taxonomies( $attachment, $output = 'names' ) { 2891 2909 2892 2910 $taxonomies = array(); 2893 2911 foreach ( $objects as $object ) { 2894 if ( $taxes = get_object_taxonomies( $object, $output ) ) { 2912 $taxes = get_object_taxonomies( $object, $output ); 2913 if ( $taxes ) { 2895 2914 $taxonomies = array_merge( $taxonomies, $taxes ); 2896 2915 } 2897 2916 } … … function wp_plupload_default_settings() { 3193 3212 * @return array|void Array of attachment details. 3194 3213 */ 3195 3214 function wp_prepare_attachment_for_js( $attachment ) { 3196 if ( ! $attachment = get_post( $attachment ) ) { 3215 $attachment = get_post( $attachment ); 3216 if ( ! $attachment ) { 3197 3217 return; 3198 3218 } 3199 3219 … … function wp_prepare_attachment_for_js( $attachment ) { 3316 3336 foreach ( $possible_sizes as $size => $label ) { 3317 3337 3318 3338 /** This filter is documented in wp-includes/media.php */ 3319 if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) { 3339 $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) 3340 if ( $downsize ) { 3320 3341 if ( empty( $downsize[3] ) ) { 3321 3342 continue; 3322 3343 } … … function wp_enqueue_media( $args = array() ) { 3576 3597 $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year ); 3577 3598 } 3578 3599 3600 // Filter to show only available mime types. 3601 $avail_post_mime_types = get_available_post_mime_types( 'attachment' ); 3602 $mime_types = wp_list_pluck( get_post_mime_types(), 0 ); 3603 foreach ( $mime_types as $mime_type => $label ) { 3604 if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) { 3605 unset( $mime_types[ $mime_type ] ); 3606 } 3607 } 3579 3608 $settings = array( 3580 3609 'tabs' => $tabs, 3581 3610 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ), 3582 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ),3611 'mimeTypes' => $mime_types, 3583 3612 /** This filter is documented in wp-admin/includes/media.php */ 3584 3613 'captions' => ! apply_filters( 'disable_captions', '' ), 3585 3614 'nonce' => array( … … function wp_enqueue_media( $args = array() ) { 3815 3844 * @return array Found attachments. 3816 3845 */ 3817 3846 function get_attached_media( $type, $post = 0 ) { 3818 if ( ! $post = get_post( $post ) ) { 3847 $post = get_post( $post ); 3848 if ( ! $post ) { 3819 3849 return array(); 3820 3850 } 3821 3851 … … function get_media_embedded_in_content( $content, $types = null ) { 3905 3935 * from the expanded shortcode. 3906 3936 */ 3907 3937 function get_post_galleries( $post, $html = true ) { 3908 if ( ! $post = get_post( $post ) ) { 3938 $post = get_post( $post ); 3939 if ( ! $post ) { 3909 3940 return array(); 3910 3941 } 3911 3942 … … function get_post_gallery_images( $post = 0 ) { 4026 4057 * @param WP_Post $attachment Attachment object. 4027 4058 */ 4028 4059 function wp_maybe_generate_attachment_metadata( $attachment ) { 4029 if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id =(int) $attachment->ID ) ) {4060 if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! (int) $attachment->ID ) ) { 4030 4061 return; 4031 4062 } 4032 4033 $file = get_attached_file( $attachment_id );4034 $meta = wp_get_attachment_metadata( $attachment_id );4063 $attachment_id = (int) $attachment->ID; 4064 $file = get_attached_file( $attachment_id ); 4065 $meta = wp_get_attachment_metadata( $attachment_id ); 4035 4066 if ( empty( $meta ) && file_exists( $file ) ) { 4036 4067 $_meta = get_post_meta( $attachment_id ); 4037 4068 $regeneration_lock = 'wp_generating_att_' . $attachment_id; … … function attachment_url_to_postid( $url ) { 4071 4102 $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); 4072 4103 } 4073 4104 4074 $sql = $wpdb->prepare( 4075 "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", 4076 $path 4105 $post_id = $wpdb->get_var( 4106 $wpdb->prepare( 4107 "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", 4108 $path 4109 ) 4077 4110 ); 4078 $post_id = $wpdb->get_var( $sql );4079 4111 4080 4112 /** 4081 4113 * Filters an attachment id found by URL. -
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 */