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
This commit is contained in:
Gary Pendergast 2017-10-21 13:26:24 +00:00
parent 899fb2f7f6
commit 4670c8f3af
2 changed files with 75 additions and 1 deletions

View File

@ -274,7 +274,8 @@ function media_handle_upload($file_id, $post_id, $post_data = array(), $override
$time = current_time('mysql'); $time = current_time('mysql');
if ( $post = get_post($post_id) ) { 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; $time = $post->post_date;
} }

View File

@ -2250,6 +2250,79 @@ EOF;
$this->assertEquals( 1269120551, $metadata['created_timestamp'] ); $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 );
}
} }
/** /**