mirror of
git://develop.git.wordpress.org/
synced 2025-04-23 05:32:41 +02:00
Media: improve titles when inserting via REST API.
Match the naming behavior for uploaded media in the REST API to the way media is named when uploading in the media library. Fix an issue where dashes were replacing spaces unnecessarily. Props abitofmind, kadamwhite, spacedmonkey, adamsilverstein, audrasjb, hellofromTonya. Fixes #57957. git-svn-id: https://develop.svn.wordpress.org/trunk@58447 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
3bb533ae10
commit
2358de1767
@ -304,6 +304,17 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
||||
$attachment->post_mime_type = $type;
|
||||
$attachment->guid = $url;
|
||||
|
||||
// If the title was not set, use the original filename.
|
||||
if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) {
|
||||
// Remove the file extension (after the last `.`)
|
||||
$tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) );
|
||||
|
||||
if ( ! empty( $tmp_title ) ) {
|
||||
$attachment->post_title = $tmp_title;
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to the original approach.
|
||||
if ( empty( $attachment->post_title ) ) {
|
||||
$attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
|
||||
}
|
||||
|
@ -2014,6 +2014,60 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$this->assertSame( 1, self::$rest_after_insert_attachment_count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the naming behavior of REST media uploads matches core media uploads.
|
||||
*
|
||||
* In particular, filenames with spaces should maintain the spaces rather than
|
||||
* replacing them with hyphens.
|
||||
*
|
||||
* @ticket 57957
|
||||
*
|
||||
* @covers WP_REST_Attachments_Controller::insert_attachment
|
||||
* @dataProvider rest_upload_filename_spaces
|
||||
*/
|
||||
public function test_rest_upload_filename_spaces( $filename, $expected ) {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$request->set_file_params(
|
||||
array(
|
||||
'file' => array(
|
||||
'file' => file_get_contents( self::$test_file2 ),
|
||||
'name' => $filename,
|
||||
'size' => filesize( self::$test_file2 ),
|
||||
'tmp_name' => self::$test_file2,
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 201, $response->get_status(), 'The file was not uploaded.' );
|
||||
$this->assertSame( $expected, $data['title']['raw'], 'An incorrect filename was returned.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for text_rest_upload_filename_spaces.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rest_upload_filename_spaces() {
|
||||
return array(
|
||||
'filename with spaces' => array(
|
||||
'Filename With Spaces.jpg',
|
||||
'Filename With Spaces',
|
||||
),
|
||||
'filename.with.periods' => array(
|
||||
'Filename.With.Periods.jpg',
|
||||
'Filename.With.Periods',
|
||||
),
|
||||
'filename-with-dashes' => array(
|
||||
'Filename-With-Dashes.jpg',
|
||||
'Filename-With-Dashes',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire
|
||||
* once when attachments are updated.
|
||||
|
Loading…
x
Reference in New Issue
Block a user