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
This commit is contained in:
parent
1ec860298d
commit
6e0ca0d8d9
@ -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 );
|
||||
|
@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user