Ticket #35218: 35218.2.diff
File 35218.2.diff, 4.9 KB (added by , 7 years ago) |
---|
-
src/wp-admin/includes/media.php
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; … … 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 if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) { 3171 $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] ); 3172 } 3173 break; 3174 3175 case 'quicktime': 3176 case 'mp4': 3177 if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) { 3178 $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix']; 3179 } 3180 break; 3181 3182 } 3183 3184 /** 3185 * Filters the media creation timestamp. 3186 * 3187 * @since 4.9.0 3188 * 3189 * @param bool|int $creation_date File creation timestamp, if found, 3190 * false if it was not able to be parsed. 3191 * @param string $file_format File format. 3192 * @param array $metadata The metadata returned by getID3::analyze(). 3193 */ 3194 $creation_date = apply_filters_ref_array( 'media_creation_timestamp', $creation_date, $file_format, $metadata ); 3195 3196 return $creation_date; 3197 } 3198 3199 /** 3136 3200 * Encapsulate logic for Attach/Detach actions 3137 3201 * 3138 3202 * @since 4.2.0 -
tests/phpunit/tests/media.php
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_read_video_metadata_adds_creation_date_with_quicktime() { 2127 $video = DIR_TESTDATA . '/videos/quicktime.mov'; 2128 $metadata = wp_read_video_metadata( $video ); 2129 2130 $this->assertEquals( 1450978957, $metadata['created_timestamp'] ); 2131 } 2132 2133 /** 2134 * @ticket 35218 2135 */ 2136 function test_wp_read_video_metadata_adds_creation_date_with_mp4() { 2137 $video = DIR_TESTDATA . '/videos/mp4.m4v'; 2138 $metadata = wp_read_video_metadata( $video ); 2139 2140 $this->assertEquals( 1450978805, $metadata['created_timestamp'] ); 2141 } 2142 2143 /** 2144 * @ticket 35218 2145 */ 2146 function test_wp_read_video_metadata_adds_creation_date_with_mkv() { 2147 $video = DIR_TESTDATA . '/videos/mkv.mkv'; 2148 $metadata = wp_read_video_metadata( $video ); 2149 2150 $this->assertNotEquals( false, $metadata['created_timestamp'] ); 2151 } 2065 2152 } 2066 2153 2067 2154 /**