From 6e0ca0d8d988f33fd09c6a2ea17332d4ad6ba279 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 15 Sep 2014 14:23:31 +0000 Subject: [PATCH] Make sure the $parent argument of wp_insert_attachment() still works as expected after [28579]. prop jesin, dikiy_forester. fixes #29646 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@29745 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 7 ++++++- tests/phpunit/tests/post/attachments.php | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index d9d48a56fe..94ec8f2c50 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -4719,10 +4719,15 @@ function is_local_attachment($url) { function wp_insert_attachment( $args, $file = false, $parent = 0 ) { $defaults = array( 'file' => $file, - 'post_parent' => $parent + 'post_parent' => 0 ); + $data = wp_parse_args( $args, $defaults ); + if ( ! empty( $parent ) ) { + $data['post_parent'] = $parent; + } + $data['post_type'] = 'attachment'; return wp_insert_post( $data ); diff --git a/tests/phpunit/tests/post/attachments.php b/tests/phpunit/tests/post/attachments.php index 9548af48e5..d079654959 100644 --- a/tests/phpunit/tests/post/attachments.php +++ b/tests/phpunit/tests/post/attachments.php @@ -254,4 +254,28 @@ class Tests_Post_Attachments extends WP_UnitTestCase { $this->assertEquals( $attached_file, get_post_meta( $id, '_wp_attached_file', true ) ); } + /** + * @ticket 29646 + */ + function test_update_orphan_attachment_parent() { + $filename = ( DIR_TESTDATA . '/images/test-image.jpg' ); + $contents = file_get_contents( $filename ); + + $upload = wp_upload_bits( basename( $filename ), null, $contents ); + $this->assertTrue( empty( $upload['error'] ) ); + + $attachment_id = $this->_make_attachment( $upload ); + + // Assert that the attachment is an orphan + $attachment = get_post( $attachment_id ); + $this->assertEquals( $attachment->post_parent, 0 ); + + $post_id = wp_insert_post( array( 'post_content' => rand_str(), 'post_title' => rand_str() ) ); + + // Assert that the attachment has a parent + wp_insert_attachment( $attachment, '', $post_id ); + $attachment = get_post( $attachment_id ); + $this->assertEquals( $attachment->post_parent, $post_id ); + } + }