Skip to content

Commit 2358de1

Browse files
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
1 parent 3bb533a commit 2358de1

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ protected function insert_attachment( $request ) {
304304
$attachment->post_mime_type = $type;
305305
$attachment->guid = $url;
306306

307+
// If the title was not set, use the original filename.
308+
if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) {
309+
// Remove the file extension (after the last `.`)
310+
$tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) );
311+
312+
if ( ! empty( $tmp_title ) ) {
313+
$attachment->post_title = $tmp_title;
314+
}
315+
}
316+
317+
// Fall back to the original approach.
307318
if ( empty( $attachment->post_title ) ) {
308319
$attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
309320
}

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,60 @@ public function test_rest_insert_attachment_hooks_fire_once_on_create() {
20142014
$this->assertSame( 1, self::$rest_after_insert_attachment_count );
20152015
}
20162016

2017+
/**
2018+
* Tests that the naming behavior of REST media uploads matches core media uploads.
2019+
*
2020+
* In particular, filenames with spaces should maintain the spaces rather than
2021+
* replacing them with hyphens.
2022+
*
2023+
* @ticket 57957
2024+
*
2025+
* @covers WP_REST_Attachments_Controller::insert_attachment
2026+
* @dataProvider rest_upload_filename_spaces
2027+
*/
2028+
public function test_rest_upload_filename_spaces( $filename, $expected ) {
2029+
wp_set_current_user( self::$editor_id );
2030+
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
2031+
$request->set_header( 'Content-Type', 'image/jpeg' );
2032+
$request->set_body( file_get_contents( self::$test_file ) );
2033+
$request->set_file_params(
2034+
array(
2035+
'file' => array(
2036+
'file' => file_get_contents( self::$test_file2 ),
2037+
'name' => $filename,
2038+
'size' => filesize( self::$test_file2 ),
2039+
'tmp_name' => self::$test_file2,
2040+
),
2041+
)
2042+
);
2043+
$response = rest_get_server()->dispatch( $request );
2044+
$data = $response->get_data();
2045+
$this->assertSame( 201, $response->get_status(), 'The file was not uploaded.' );
2046+
$this->assertSame( $expected, $data['title']['raw'], 'An incorrect filename was returned.' );
2047+
}
2048+
2049+
/**
2050+
* Data provider for text_rest_upload_filename_spaces.
2051+
*
2052+
* @return array
2053+
*/
2054+
public function rest_upload_filename_spaces() {
2055+
return array(
2056+
'filename with spaces' => array(
2057+
'Filename With Spaces.jpg',
2058+
'Filename With Spaces',
2059+
),
2060+
'filename.with.periods' => array(
2061+
'Filename.With.Periods.jpg',
2062+
'Filename.With.Periods',
2063+
),
2064+
'filename-with-dashes' => array(
2065+
'Filename-With-Dashes.jpg',
2066+
'Filename-With-Dashes',
2067+
),
2068+
);
2069+
}
2070+
20172071
/**
20182072
* Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire
20192073
* once when attachments are updated.

0 commit comments

Comments
 (0)