Ticket #34430: 34430.9.diff
File 34430.9.diff, 9.4 KB (added by , 9 years ago) |
---|
-
src/wp-includes/media.php
817 817 818 818 if ( is_array( $image_meta ) ) { 819 819 $size_array = array( absint( $width ), absint( $height ) ); 820 $s ources= wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );821 $sizes 820 $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id ); 821 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id ); 822 822 823 if ( count( $sources ) > 1&& ( $sizes || ! empty( $attr['sizes'] ) ) ) {824 $attr['srcset'] = wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id );823 if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { 824 $attr['srcset'] = $srcset; 825 825 826 826 if ( empty( $attr['sizes'] ) ) { 827 827 $attr['sizes'] = $sizes; … … 943 943 absint( $image[2] ) 944 944 ); 945 945 946 // Calculate the sources for the 'srcset'. 947 $sources = wp_calculate_image_srcset( $image_url, $size_array, $image_meta, $attachment_id ); 948 949 // Only return a 'srcset' value if there is more than one source. 950 if ( count( $sources ) < 2 ) { 951 return false; 952 } 953 954 return wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id ); 946 return wp_calculate_image_srcset( $image_url, $size_array, $image_meta, $attachment_id ); 955 947 } 956 948 957 958 949 /** 959 * A helper function to concatenate and filter the 'srcset' attribute value.960 *961 * @since 4.4.0962 *963 * @param array $sources The array containing image sizes data as returned by 'wp_calculate_image_srcset()'.964 * @param array $size_array Array of width and height values in pixels (in that order).965 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.966 * @param int $attachment_id The image attachment ID to pass to the filter.967 * @return string The 'srcset' attribute value.968 */969 function wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id ) {970 $srcset = '';971 972 foreach ( $sources as $source ) {973 $srcset .= $source['url'] . ' ' . $source['value'] . $source['descriptor'] . ', ';974 }975 976 /**977 * Filter the output of 'wp_image_srcset_attr()'.978 *979 * @since 4.4.0980 *981 * @param string $srcset A source set formatted for a 'srcset' attribute.982 * @param int $attachment_id Image attachment ID.983 * @param array|string $size Image size. Image size name, or an array of width and height984 * values in pixels (in that order).985 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.986 */987 return apply_filters( 'wp_image_srcset', rtrim( $srcset, ', ' ), $attachment_id, $size_array, $image_meta );988 }989 990 /**991 950 * A helper function to calculate the image sources to include in a 'srcset' attribute. 992 951 * 993 952 * @since 4.4.0 … … 996 955 * @param array $size_array Array of width and height values in pixels (in that order). 997 956 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 998 957 * @param int $attachment_id Optional. The image attachment ID to pass to the filter. 999 * @return array|bool $sources { 1000 * Array image candidate values containing a URL, descriptor type, and 1001 * descriptor value. False if none exist. 1002 * 1003 * @type array $values { 1004 * @type string $url An image URL. 1005 * @type string $descriptor A width or density descriptor used in a 'srcset'. 1006 * @type int $value The descriptor value representing a width or 1007 * or pixel density. 1008 * } 1009 * } 958 * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. 1010 959 */ 1011 960 function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $attachment_id = 0 ) { 1012 961 if ( empty( $image_meta['sizes'] ) ) { … … 1102 1051 * 1103 1052 * @since 4.4.0 1104 1053 * 1105 * @param array $sources An array of image URLs and widths. 1106 * @param int $attachment_id Image attachment ID. 1107 * @param array|string $size Image size. Image size name, or an array of width and height 1108 * values in pixels (in that order). 1109 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 1054 * @param array $sources An array of image URLs and widths. 1055 * @param int $attachment_id Image attachment ID. 1056 * @param array $size_array Array of width and height values in pixels (in that order). 1057 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 1110 1058 1111 1059 */ 1112 return apply_filters( 'wp_calculate_image_srcset', array_values( $sources ), $attachment_id, $size_array, $image_meta ); 1060 $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $attachment_id, $size_array, $image_meta ); 1061 1062 // Only return a 'srcset' value if there is more than one source. 1063 if ( count( $sources ) < 2 ) { 1064 return false; 1065 } 1066 1067 $srcset = ''; 1068 1069 foreach ( $sources as $source ) { 1070 $srcset .= $source['url'] . ' ' . $source['value'] . $source['descriptor'] . ', '; 1071 } 1072 1073 return rtrim( $srcset, ', ' ); 1113 1074 } 1114 1075 1115 1076 /** … … 1276 1237 } 1277 1238 1278 1239 $size_array = array( $width, $height ); 1279 $s ources= wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );1240 $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id ); 1280 1241 1281 $srcset = $sizes = ''; 1282 // Only calculate 'srcset' and 'sizes' values if there is more than one source. 1283 if ( count( $sources ) > 1 ) { 1284 $srcset = wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id ); 1242 if ( $srcset ) { 1285 1243 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id ); 1286 1244 } 1287 1245 -
tests/phpunit/tests/media.php
739 739 function test_wp_calculate_image_srcset() { 740 740 $year_month = date('Y/m'); 741 741 $image_meta = wp_get_attachment_metadata( self::$large_id ); 742 $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; 742 743 743 $expected = array( 744 array( 745 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image_meta['sizes']['medium']['file'], 746 'descriptor' => 'w', 747 'value' => $image_meta['sizes']['medium']['width'], 748 ), 749 array( 750 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image_meta['sizes']['large']['file'], 751 'descriptor' => 'w', 752 'value' => $image_meta['sizes']['large']['width'], 753 ), 754 array( 755 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'], 756 'descriptor' => 'w', 757 'value' => $image_meta['width'], 758 ), 759 ); 744 $expected = $uploads_dir_url . $year_month . '/' . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, ' . 745 $uploads_dir_url . $year_month . '/' . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, ' . 746 $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w'; 760 747 761 748 // Set up test cases for all expected size names and a random one. 762 749 $sizes = array( 'medium', 'large', 'full', 'yoav' ); … … 783 770 $id = self::factory()->attachment->create_upload_object( $filename ); 784 771 785 772 $image_meta = wp_get_attachment_metadata( $id ); 773 $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; 786 774 787 $expected = array( 788 array( 789 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['medium']['file'], 790 'descriptor' => 'w', 791 'value' => $image_meta['sizes']['medium']['width'], 792 ), 793 array( 794 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'], 795 'descriptor' => 'w', 796 'value' => $image_meta['sizes']['large']['width'], 797 ), 798 array( 799 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'], 800 'descriptor' => 'w', 801 'value' => $image_meta['width'], 802 ), 803 ); 775 $expected = $uploads_dir_url . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, ' . 776 $uploads_dir_url . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, ' . 777 $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w'; 804 778 805 779 // Set up test cases for all expected size names and a random one. 806 780 $sizes = array( 'medium', 'large', 'full', 'yoav' ); … … 835 809 $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] ); 836 810 837 811 // Calculate a srcset array. 838 $sizes = wp_calculate_image_srcset( $image_url, $size_array, $image_meta);812 $sizes = explode( ', ', wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) ); 839 813 840 814 // Test to confirm all sources in the array include the same edit hash. 841 815 foreach ( $sizes as $size ) { 842 $this->assertTrue( false !== strpos( $size ['url'], $hash ) );816 $this->assertTrue( false !== strpos( $size, $hash ) ); 843 817 } 844 818 } 845 819