Make WordPress Core

Opened 11 years ago

Closed 10 years ago

#29646 closed defect (bug) (fixed)

wp_insert_attachment() doesn't allow to add post_parent for orphan attachments

Reported by: dikiy_forester's profile dikiy_forester Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 4.0.1 Priority: normal
Severity: normal Version: 4.0
Component: Media Keywords: has-patch commit fixed-major
Focuses: Cc:

Description

If you try to insert orphan attachment with post_parent=0 to the post and pass parameter $parent, it won't has effect, because 'post_parent' from $args array won't be replaced with another one from $defaults array.

function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
	$defaults = array(
		'file'        => $file,
		'post_parent' => $parent
	);
	$data = wp_parse_args( $args, $defaults );

	$data['post_type'] = 'attachment';

	return wp_insert_post( $data );
}

In the WP3.9.2 it was treated with following code:

	if ( !empty($parent) )
		$object['post_parent'] = $parent;

Attachments (2)

29646.diff (1.9 KB) - added by jesin 11 years ago.
29646.2.diff (1.8 KB) - added by jesin 11 years ago.

Download all attachments as: .zip

Change History (12)

#1 @SergeyBiryukov
11 years ago

  • Component changed from General to Posts, Post Types
  • Milestone changed from Awaiting Review to 4.0.1

Sounds like a regression. Could you provide a piece of code to reproduce the issue?

#2 @SergeyBiryukov
11 years ago

  • Component changed from Posts, Post Types to Media

#3 follow-up: @dikiy_forester
11 years ago

Sorry, my explanation was not clear, please look the code below, hope it's better:

// get the some orphan attachment
$attachment = get_post( $attachment_id );

// ! Important $attachment->post_parent = 0

// then try to insert attachment to some post with ID 999 for ex.
$attach_id = wp_insert_attachment( $attachment, '', $post_id = 999 );

// now look to the WP4.0 wp_insert_attachment() code

$defaults = array(
        'file'        => $file,
        'post_parent' => $parent
);

// where $parent is passed $post_id value
// and $args is passed $attachment array/object

// then function is merging two arrays 
$data = wp_parse_args( $args, $defaults );

// $args['post_parent'] = 0
// $defaults['post_parent'] = 999

// Result:
// $data['post_parent'] = 0
// but not 999 as expected.

// attachment is still be orphan
Last edited 11 years ago by dikiy_forester (previous) (diff)

@jesin
11 years ago

#4 follow-up: @jesin
11 years ago

Problem caused by 28579 when wp_insert_attachment() was made as a wrapper to wp_insert_post() in ticket 21963.

Patch 29646.diff hardcodes the values of $defaults and assigns the value of $parent to $data['post_parent'].

It also includes a unit test which

  1. Creates an attachment
  2. Asserts that the attachment is an orphan
  3. Creates a post
  4. Sets this post as the parent of the attachment
  5. Asserts that post_parent of the attachment has changed

#5 in reply to: ↑ 4 ; follow-up: @dikiy_forester
11 years ago

Replying to jesin:

Patch doesn't allows to process $file parameter. It always will false.

@jesin
11 years ago

#6 in reply to: ↑ 5 @jesin
11 years ago

Replying to dikiy_forester:

Patch doesn't allows to process $file parameter. It always will false.

Patch 29646.2.diff changes only the post_parent value of $defaults.

#7 in reply to: ↑ 3 @SergeyBiryukov
11 years ago

  • Keywords has-patch commit added

Replying to dikiy_forester:

Sorry, my explanation was not clear, please look the code below, hope it's better

It is, thank you.

#8 @SergeyBiryukov
11 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 29745:

Make sure the $parent argument of wp_insert_attachment() still works as expected after [28579].

prop jesin, dikiy_forester.
fixes #29646 for trunk.

#9 @SergeyBiryukov
11 years ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for 4.0.1.

#10 @nacin
10 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 30258:

Make sure the $parent argument of wp_insert_attachment() still works as expected after [28579].

Merges [29745] to the 4.0 branch.

props jesin, dikiy_forester.
fixes #29646.

Note: See TracTickets for help on using tickets.