Make WordPress Core

Ticket #35218: 35218.6.diff

File 35218.6.diff, 6.4 KB (added by kirasong, 7 years ago)

Only create meta if there is a creation date and only rely on small* test media files.

  • src/wp-admin/includes/media.php

    diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php
    index 6bb541dd35..56bbf05212 100644
    function wp_read_video_metadata( $file ) { 
    30823082                $metadata['audio'] = $data['audio'];
    30833083        }
    30843084
     3085        if ( empty( $metadata['created_timestamp'] ) ) {
     3086                $created_timestamp = wp_get_media_creation_timestamp( $data );
     3087
     3088                if ( $created_timestamp !== false ) {
     3089                        $metadata['created_timestamp'] = $created_timestamp;
     3090                }
     3091        }
     3092
    30853093        wp_add_id3_tag_data( $metadata, $data );
    30863094
    30873095        return $metadata;
    function wp_read_audio_metadata( $file ) { 
    31333141}
    31343142
    31353143/**
     3144 * Parse creation date from media metadata.
     3145 *
     3146 * The getID3 library doesn't have a standard method for getting creation dates,
     3147 * so the location of this data can vary based on the MIME type.
     3148 *
     3149 * @since 4.9.0
     3150 *
     3151 * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt
     3152 *
     3153 * @param array $metadata The metadata returned by getID3::analyze().
     3154 * @return int|bool A UNIX timestamp for the media's creation date if available
     3155 *                  or a boolean FALSE if a timestamp could not be determined.
     3156 */
     3157function wp_get_media_creation_timestamp( $metadata ) {
     3158        $creation_date = false;
     3159
     3160        if ( empty( $metadata['fileformat'] ) ) {
     3161                return $creation_date;
     3162        }
     3163
     3164        $file_format = $metadata['fileformat'];
     3165
     3166        switch ( $file_format ) {
     3167                case 'asf':
     3168                        if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) {
     3169                                $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix'];
     3170                        }
     3171                        break;
     3172
     3173                case 'matroska':
     3174                case 'webm':
     3175                        if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) {
     3176                                $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] );
     3177                        }
     3178                        elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) {
     3179                                $creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix'];
     3180                        }
     3181                        break;
     3182
     3183                case 'quicktime':
     3184                case 'mp4':
     3185                        if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) {
     3186                                $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'];
     3187                        }
     3188                        break;
     3189        }
     3190
     3191        /**
     3192         * Filters the media creation timestamp.
     3193         *
     3194         * @since 4.9.0
     3195         *
     3196         * @param bool|int $creation_date File creation timestamp, if found,
     3197         *                                false if it was not able to be parsed.
     3198         * @param string   $file_format   File format from getID3.
     3199         * @param array    $metadata      The metadata returned by getID3::analyze().
     3200         */
     3201        $creation_date = apply_filters( 'media_creation_timestamp', $creation_date, $file_format, $metadata );
     3202
     3203        return $creation_date;
     3204}
     3205
     3206/**
    31363207 * Encapsulate logic for Attach/Detach actions
    31373208 *
    31383209 * @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 8fb3cc09d7..6ef721b02c 100644
    EOF; 
    20672067                $attachment_id = wp_insert_attachment( $data, '', 0 );
    20682068                $this->assertSame( 0, $attachment_id );
    20692069        }
     2070
     2071        /**
     2072         * @ticket 35218
     2073         */
     2074        function test_wp_get_media_creation_timestamp_video_asf() {
     2075                $metadata = array(
     2076                        'fileformat' => 'asf',
     2077                        'asf'        => array(
     2078                                'file_properties_object' => array(
     2079                                        'creation_date_unix' => 123,
     2080                                ),
     2081                        ),
     2082                );
     2083
     2084                $this->assertEquals( 123, wp_get_media_creation_timestamp( $metadata ) );
     2085        }
     2086
     2087        /**
     2088         * @ticket 35218
     2089         */
     2090        function test_wp_get_media_creation_timestamp_video_matroska() {
     2091                $metadata = array(
     2092                        'fileformat' => 'matroska',
     2093                        'matroska'   => array(
     2094                                'comments' => array(
     2095                                        'creation_time' => array(
     2096                                                '2015-12-24T17:40:09Z'
     2097                                        ),
     2098                                ),
     2099                        ),
     2100                );
     2101
     2102                $this->assertEquals( 1450978809, wp_get_media_creation_timestamp( $metadata ) );
     2103        }
     2104
     2105        /**
     2106         * @ticket 35218
     2107         */
     2108        function test_wp_get_media_creation_timestamp_video_quicktime() {
     2109                $metadata = array(
     2110                        'fileformat' => 'quicktime',
     2111                        'quicktime'  => array(
     2112                                'moov' => array(
     2113                                        'subatoms' => array(
     2114                                                array(
     2115                                                        'creation_time_unix' => 1450978805,
     2116                                                ),
     2117                                        ),
     2118                                ),
     2119                        ),
     2120                );
     2121
     2122                $this->assertEquals( 1450978805, wp_get_media_creation_timestamp( $metadata ) );
     2123        }
     2124
     2125        /**
     2126         * @ticket 35218
     2127         */
     2128        function test_wp_get_media_creation_timestamp_video_webm() {
     2129                $metadata = array(
     2130                        'fileformat' => 'webm',
     2131                        'matroska'   => array(
     2132                                'info' => array(
     2133                                        array(
     2134                                                'DateUTC_unix' => 1265680539,
     2135                                        ),
     2136                                ),
     2137                        ),
     2138                );
     2139
     2140                $this->assertEquals( 1265680539, wp_get_media_creation_timestamp( $metadata ) );
     2141        }
     2142
     2143        /**
     2144         * @ticket 35218
     2145         */
     2146        function test_wp_read_video_metadata_adds_creation_date_with_quicktime() {
     2147                $video    = DIR_TESTDATA . '/uploads/small-video.mov';
     2148                $metadata = wp_read_video_metadata( $video );
     2149
     2150                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2151        }
     2152
     2153        /**
     2154         * @ticket 35218
     2155         */
     2156        function test_wp_read_video_metadata_adds_creation_date_with_mp4() {
     2157                $video    = DIR_TESTDATA . '/uploads/small-video.mp4';
     2158                $metadata = wp_read_video_metadata( $video );
     2159
     2160                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2161        }
     2162
     2163        /**
     2164         * @ticket 35218
     2165         */
     2166        function test_wp_read_video_metadata_adds_creation_date_with_mkv() {
     2167                $video    = DIR_TESTDATA . '/uploads/small-video.mkv';
     2168                $metadata = wp_read_video_metadata( $video );
     2169
     2170                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2171        }
     2172
     2173        /**
     2174         * @ticket 35218
     2175         */
     2176        function test_wp_read_video_metadata_adds_creation_date_with_webm() {
     2177                $video    = DIR_TESTDATA . '/uploads/small-video.webm';
     2178                $metadata = wp_read_video_metadata( $video );
     2179
     2180                $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
     2181        }
    20702182}
    20712183
    20722184/**