WordPress.org

Make WordPress Core

Changeset 41964


Ignore:
Timestamp:
10/21/2017 01:26:24 PM (4 years ago)
Author:
pento
Message:

Media: Don't backdate media uploaded to pages.

When media is uploaded to a post, the upload directory is set according to the date of the post, so that the media URLs in the post match when the post was published.

A page is a slightly different beast, pages often live for years, and are regularly updated to stay relevant. This change causes media uploaded to pages to use the upload date to determine the upload directory.

Fixes #10752.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/media.php

    r41746 r41964  
    275275    $time = current_time('mysql');
    276276    if ( $post = get_post($post_id) ) {
    277         if ( substr( $post->post_date, 0, 4 ) > 0 )
     277        // The post date doesn't usually matter for pages, so don't backdate this upload.
     278        if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 )
    278279            $time = $post->post_date;
    279280    }
  • trunk/tests/phpunit/tests/media.php

    r41746 r41964  
    22512251        $this->assertEquals( 1269120551, $metadata['created_timestamp'] );
    22522252    }
     2253
     2254    /**
     2255     * @ticket 10752
     2256     */
     2257    public function test_media_handle_upload_uses_post_parent_for_directory_date() {
     2258        $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
     2259
     2260        // Make a copy of this file as it gets moved during the file upload
     2261        $tmp_name = wp_tempnam( $iptc_file );
     2262
     2263        copy( $iptc_file, $tmp_name );
     2264
     2265        $_FILES['upload'] = array(
     2266            'tmp_name' => $tmp_name,
     2267            'name'     => 'test-image-iptc.jpg',
     2268            'type'     => 'image/jpeg',
     2269            'error'    => 0,
     2270            'size'     => filesize( $iptc_file )
     2271        );
     2272
     2273        $parent_id = self::factory()->post->create( array( 'post_date' => '2010-01-01' ) );
     2274
     2275        $post_id = media_handle_upload( 'upload', $parent_id, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ) );
     2276
     2277        unset( $_FILES['upload'] );
     2278
     2279        $url = wp_get_attachment_url( $post_id );
     2280
     2281        // Clean up.
     2282        wp_delete_attachment( $post_id );
     2283        wp_delete_post( $parent_id );
     2284
     2285        $this->assertSame( 'http://example.org/wp-content/uploads/2010/01/test-image-iptc.jpg', $url );
     2286    }
     2287
     2288    /**
     2289     * @ticket 10752
     2290     */
     2291    public function test_media_handle_upload_ignores_page_parent_for_directory_date() {
     2292        $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
     2293
     2294        // Make a copy of this file as it gets moved during the file upload
     2295        $tmp_name = wp_tempnam( $iptc_file );
     2296
     2297        copy( $iptc_file, $tmp_name );
     2298
     2299        $_FILES['upload'] = array(
     2300            'tmp_name' => $tmp_name,
     2301            'name'     => 'test-image-iptc.jpg',
     2302            'type'     => 'image/jpeg',
     2303            'error'    => 0,
     2304            'size'     => filesize( $iptc_file )
     2305        );
     2306
     2307        $parent_id = self::factory()->post->create( array( 'post_date' => '2010-01-01', 'post_type' => 'page' ) );
     2308        $parent = get_post( $parent_id );
     2309
     2310        $post_id = media_handle_upload( 'upload', $parent_id, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ) );
     2311
     2312        unset( $_FILES['upload'] );
     2313
     2314        $url = wp_get_attachment_url( $post_id );
     2315
     2316        $uploads_dir = wp_upload_dir( current_time( 'mysql' ) );
     2317
     2318        $expected = $uploads_dir['url'] . 'test-image-iptc.jpg';
     2319
     2320        // Clean up.
     2321        wp_delete_attachment( $post_id );
     2322        wp_delete_post( $parent_id );
     2323
     2324        $this->assertNotEquals( $expected, $url );
     2325    }
    22532326}
    22542327
Note: See TracChangeset for help on using the changeset viewer.