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:
parent
899fb2f7f6
commit
4670c8f3af
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue