WordPress.org

Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#18185 closed defect (bug) (fixed)

media_handle_sideload clears $title if $desc is not set

Reported by: baldgoat Owned by: duck_
Milestone: 3.3 Priority: normal
Severity: normal Version: 3.2.1
Component: Media Keywords: has-patch commit
Focuses: Cc:

Description

media_handle_sideload() -- found in wp-admin>includes>media.php -- is setting $title to an empty string if isset($desc) returns false.

function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
	$overrides = array('test_form'=>false);

	$file = wp_handle_sideload($file_array, $overrides);
	if ( isset($file['error']) )
		return new WP_Error( 'upload_error', $file['error'] );

	$url = $file['url'];
	$type = $file['type'];
	$file = $file['file'];
	$title = preg_replace('/\.[^.]+$/', '', basename($file));
	$content = '';

	// use image exif/iptc data for title and caption defaults if possible
	if ( $image_meta = @wp_read_image_metadata($file) ) {
		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
			$title = $image_meta['title'];
		if ( trim( $image_meta['caption'] ) )
			$content = $image_meta['caption'];
	}

	$title = isset($desc) ? $desc : '';

	// Construct the attachment array
	$attachment = array_merge( array(
		'post_mime_type' => $type,
		'guid' => $url,
		'post_parent' => $post_id,
		'post_title' => $title,
		'post_content' => $content,
	), $post_data );

	// Save the attachment metadata
	$id = wp_insert_attachment($attachment, $file, $post_id);
	if ( !is_wp_error($id) )
		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

	return $id;
}

Please note this line:

$title = isset($desc) ? $desc : '';

$desc is NULL by default. So if a value is not passed in the function call, $title will be set to an empty string.

It appears that it should be changed to this

$title = isset($desc) ? $desc : $title;

Attachments (1)

18185.diff (424 bytes) - added by kawauso 10 years ago.

Download all attachments as: .zip

Change History (5)

@kawauso
10 years ago

#1 @kawauso
10 years ago

  • Keywords has-patch added

Patch using if() for code readability

#2 @nacin
9 years ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 3.3

Looks good.

#3 @jane
9 years ago

  • Component changed from General to Media

#4 @duck_
9 years ago

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

In [18698]:

Don't obliterate $title if $desc isn't set. Props kawauso. Fixes #18185.

Note: See TracTickets for help on using tickets.