Ticket #35218: 35218.5.diff
File 35218.5.diff, 6.4 KB (added by , 7 years ago) |
---|
-
src/wp-admin/includes/media.php
diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php index 6bb541dd35..a4e7b63df8 100644
function wp_read_video_metadata( $file ) { 3082 3082 $metadata['audio'] = $data['audio']; 3083 3083 } 3084 3084 3085 if ( empty( $metadata['created_timestamp'] ) ) { 3086 $metadata['created_timestamp'] = wp_get_media_creation_timestamp( $data ); 3087 } 3088 3085 3089 wp_add_id3_tag_data( $metadata, $data ); 3086 3090 3087 3091 return $metadata; … … function wp_read_audio_metadata( $file ) { 3133 3137 } 3134 3138 3135 3139 /** 3140 * Attempt to parse a date out of ID3 data. 3141 * 3142 * The getID3 library doesn't have a standard method for getting creation dates, 3143 * so the location of this data can vary based on the MIME type. 3144 * 3145 * @since 4.9.0 3146 * 3147 * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt 3148 * 3149 * @param array $metadata The metadata returned by getID3::analyze(). 3150 * @return int|bool A UNIX timestamp for the media's creation date if available 3151 * or a boolean FALSE if a timestamp could not be determined. 3152 */ 3153 function wp_get_media_creation_timestamp( $metadata ) { 3154 $creation_date = false; 3155 3156 if ( empty( $metadata['fileformat'] ) ) { 3157 return $creation_date; 3158 } 3159 3160 $file_format = $metadata['fileformat']; 3161 3162 switch ( $file_format ) { 3163 case 'asf': 3164 if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) { 3165 $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix']; 3166 } 3167 break; 3168 3169 case 'matroska': 3170 case 'webm': 3171 if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) { 3172 $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] ); 3173 } 3174 elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) { 3175 $creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix']; 3176 } 3177 break; 3178 3179 case 'quicktime': 3180 case 'mp4': 3181 if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) { 3182 $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix']; 3183 } 3184 break; 3185 3186 } 3187 3188 /** 3189 * Filters the media creation timestamp. 3190 * 3191 * @since 4.9.0 3192 * 3193 * @param bool|int $creation_date File creation timestamp, if found, 3194 * false if it was not able to be parsed. 3195 * @param string $file_format File format. 3196 * @param array $metadata The metadata returned by getID3::analyze(). 3197 */ 3198 $creation_date = apply_filters( 'media_creation_timestamp', $creation_date, $file_format, $metadata ); 3199 3200 return $creation_date; 3201 } 3202 3203 /** 3136 3204 * Encapsulate logic for Attach/Detach actions 3137 3205 * 3138 3206 * @since 4.2.0 -
tests/phpunit/tests/media.php
diff --git tests/phpunit/data/uploads/small-video.mkv tests/phpunit/data/uploads/small-video.mkv new file mode 100644 index 0000000000..2b3308441f Binary files /dev/null and tests/phpunit/data/uploads/small-video.mkv differ diff --git tests/phpunit/data/uploads/small-video.mov tests/phpunit/data/uploads/small-video.mov new file mode 100644 index 0000000000..2cfbb86464 Binary files /dev/null and tests/phpunit/data/uploads/small-video.mov differ diff --git tests/phpunit/data/uploads/small-video.webm tests/phpunit/data/uploads/small-video.webm new file mode 100644 index 0000000000..63a974986b Binary files /dev/null and tests/phpunit/data/uploads/small-video.webm differ diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index 392c039fdb..d0c5e41347 100644
EOF; 2062 2062 $attachment_id = wp_insert_attachment( $data, '', 0 ); 2063 2063 $this->assertSame( 0, $attachment_id ); 2064 2064 } 2065 2066 /** 2067 * @ticket 35218 2068 */ 2069 function test_wp_get_media_creation_timestamp_video_asf() { 2070 $metadata = array( 2071 'fileformat' => 'asf', 2072 'asf' => array( 2073 'file_properties_object' => array( 2074 'creation_date_unix' => 123, 2075 ), 2076 ), 2077 ); 2078 2079 $this->assertEquals( 123, wp_get_media_creation_timestamp( $metadata ) ); 2080 } 2081 2082 /** 2083 * @ticket 35218 2084 */ 2085 function test_wp_get_media_creation_timestamp_video_matroska() { 2086 $metadata = array( 2087 'fileformat' => 'matroska', 2088 'matroska' => array( 2089 'comments' => array( 2090 'creation_time' => array( 2091 '2015-12-24T17:40:09Z' 2092 ), 2093 ), 2094 ), 2095 ); 2096 2097 $this->assertEquals( 1450978809, wp_get_media_creation_timestamp( $metadata ) ); 2098 } 2099 2100 /** 2101 * @ticket 35218 2102 */ 2103 function test_wp_get_media_creation_timestamp_video_quicktime() { 2104 $video = DIR_TESTDATA . '/videos/mp4.m4v'; 2105 $metadata = wp_read_video_metadata( $video ); 2106 2107 $metadata = array( 2108 'fileformat' => 'quicktime', 2109 'quicktime' => array( 2110 'moov' => array( 2111 'subatoms' => array( 2112 array( 2113 'creation_time_unix' => 1450978805, 2114 ), 2115 ), 2116 ), 2117 ), 2118 ); 2119 2120 $this->assertEquals( 1450978805, wp_get_media_creation_timestamp( $metadata ) ); 2121 } 2122 2123 /** 2124 * @ticket 35218 2125 */ 2126 function test_wp_get_media_creation_timestamp_video_webm() { 2127 $metadata = array( 2128 'fileformat' => 'webm', 2129 'matroska' => array( 2130 'info' => array( 2131 array( 2132 'DateUTC_unix' => 1265680539, 2133 ), 2134 ), 2135 ), 2136 ); 2137 2138 $this->assertEquals( 1265680539, wp_get_media_creation_timestamp( $metadata ) ); 2139 } 2140 2141 /** 2142 * @ticket 35218 2143 */ 2144 function test_wp_read_video_metadata_adds_creation_date_with_quicktime() { 2145 $video = DIR_TESTDATA . '/uploads/small-video.mov'; 2146 $metadata = wp_read_video_metadata( $video ); 2147 2148 $this->assertEquals( 1269120551, $metadata['created_timestamp'] ); 2149 } 2150 2151 /** 2152 * @ticket 35218 2153 */ 2154 function test_wp_read_video_metadata_adds_creation_date_with_mp4() { 2155 $video = DIR_TESTDATA . '/uploads/small-video.mp4'; 2156 $metadata = wp_read_video_metadata( $video ); 2157 2158 $this->assertEquals( 1269120551, $metadata['created_timestamp'] ); 2159 } 2160 2161 /** 2162 * @ticket 35218 2163 */ 2164 function test_wp_read_video_metadata_adds_creation_date_with_mkv() { 2165 $video = DIR_TESTDATA . '/uploads/small-video.mkv'; 2166 $metadata = wp_read_video_metadata( $video ); 2167 2168 $this->assertEquals( 1269120551, $metadata['created_timestamp'] ); 2169 } 2170 2171 /** 2172 * @ticket 35218 2173 */ 2174 function test_wp_read_video_metadata_adds_creation_date_with_webm() { 2175 $video = DIR_TESTDATA . '/uploads/small-video.webm'; 2176 $metadata = wp_read_video_metadata( $video ); 2177 2178 $this->assertEquals( 1269120551, $metadata['created_timestamp'] ); 2179 } 2180 2065 2181 } 2066 2182 2067 2183 /**