From 4670c8f3af8e7b1ff96aa535918f15362546a178 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Sat, 21 Oct 2017 13:26:24 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@41964 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/media.php | 3 +- tests/phpunit/tests/media.php | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index f60fd6bd82..2ee607d2d5 100644 --- a/src/wp-admin/includes/media.php +++ b/src/wp-admin/includes/media.php @@ -274,7 +274,8 @@ function media_handle_upload($file_id, $post_id, $post_data = array(), $override $time = current_time('mysql'); if ( $post = get_post($post_id) ) { - if ( substr( $post->post_date, 0, 4 ) > 0 ) + // The post date doesn't usually matter for pages, so don't backdate this upload. + if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) $time = $post->post_date; } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 025b4de841..20b670fab1 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -2250,6 +2250,79 @@ EOF; $this->assertEquals( 1269120551, $metadata['created_timestamp'] ); } + + /** + * @ticket 10752 + */ + public function test_media_handle_upload_uses_post_parent_for_directory_date() { + $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg'; + + // Make a copy of this file as it gets moved during the file upload + $tmp_name = wp_tempnam( $iptc_file ); + + copy( $iptc_file, $tmp_name ); + + $_FILES['upload'] = array( + 'tmp_name' => $tmp_name, + 'name' => 'test-image-iptc.jpg', + 'type' => 'image/jpeg', + 'error' => 0, + 'size' => filesize( $iptc_file ) + ); + + $parent_id = self::factory()->post->create( array( 'post_date' => '2010-01-01' ) ); + + $post_id = media_handle_upload( 'upload', $parent_id, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ) ); + + unset( $_FILES['upload'] ); + + $url = wp_get_attachment_url( $post_id ); + + // Clean up. + wp_delete_attachment( $post_id ); + wp_delete_post( $parent_id ); + + $this->assertSame( 'http://example.org/wp-content/uploads/2010/01/test-image-iptc.jpg', $url ); + } + + /** + * @ticket 10752 + */ + public function test_media_handle_upload_ignores_page_parent_for_directory_date() { + $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg'; + + // Make a copy of this file as it gets moved during the file upload + $tmp_name = wp_tempnam( $iptc_file ); + + copy( $iptc_file, $tmp_name ); + + $_FILES['upload'] = array( + 'tmp_name' => $tmp_name, + 'name' => 'test-image-iptc.jpg', + 'type' => 'image/jpeg', + 'error' => 0, + 'size' => filesize( $iptc_file ) + ); + + $parent_id = self::factory()->post->create( array( 'post_date' => '2010-01-01', 'post_type' => 'page' ) ); + $parent = get_post( $parent_id ); + + $post_id = media_handle_upload( 'upload', $parent_id, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ) ); + + unset( $_FILES['upload'] ); + + $url = wp_get_attachment_url( $post_id ); + + $uploads_dir = wp_upload_dir( current_time( 'mysql' ) ); + + $expected = $uploads_dir['url'] . 'test-image-iptc.jpg'; + + // Clean up. + wp_delete_attachment( $post_id ); + wp_delete_post( $parent_id ); + + $this->assertNotEquals( $expected, $url ); + } } /**