diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
index cae96c2587..684bf6a814 100644
a
|
b
|
class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { |
275 | 275 | $attachment->post_mime_type = $type; |
276 | 276 | $attachment->guid = $url; |
277 | 277 | |
| 278 | // If the title was not set, use the filename extracted from the request header. |
278 | 279 | if ( empty( $attachment->post_title ) ) { |
279 | | $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); |
| 280 | $content_disposition = $request->get_header( 'content_disposition' ); |
| 281 | |
| 282 | if ( ! empty( $content_disposition ) ) { |
| 283 | $lower_content_disposition = strtolower( $content_disposition ); |
| 284 | |
| 285 | if ( str_starts_with( $lower_content_disposition, 'attachment; filename=' ) ) { |
| 286 | $tmpfname_disposition = explode( '.', substr( $content_disposition, 21 ) ); |
| 287 | if ( ! empty( $tmpfname_disposition ) ) { |
| 288 | $attachment->post_title = $tmpfname_disposition[0]; |
| 289 | } |
| 290 | } |
| 291 | } else { |
| 292 | $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); |
| 293 | } |
280 | 294 | } |
281 | 295 | |
282 | 296 | // $post_parent is inherited from $attachment['post_parent']. |
diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php
index 55cf74f3d7..a306cede6d 100644
a
|
b
|
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control |
1923 | 1923 | $this->assertSame( 1, self::$rest_after_insert_attachment_count ); |
1924 | 1924 | } |
1925 | 1925 | |
| 1926 | /** |
| 1927 | * Tests that the naming behavior of REST media uploads matches core media uploads. |
| 1928 | * |
| 1929 | * In particular, filenames with spaces should maintain the spaces rather than |
| 1930 | * replacing them with hyphens. |
| 1931 | * |
| 1932 | * @ticket 57957 |
| 1933 | * |
| 1934 | * @covers WP_REST_Attachments_Controller::insert_attachment |
| 1935 | */ |
| 1936 | public function test_rest_upload_filename_spaces() { |
| 1937 | wp_set_current_user( self::$editor_id ); |
| 1938 | $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); |
| 1939 | $request->set_header( 'Content-Type', 'image/jpeg' ); |
| 1940 | $request->set_header( 'Content-Disposition', 'attachment; filename=Filename With Spaces.jpg' ); |
| 1941 | $request->set_body( file_get_contents( self::$test_file ) ); |
| 1942 | |
| 1943 | $response = rest_get_server()->dispatch( $request ); |
| 1944 | $data = $response->get_data(); |
| 1945 | $this->assertSame( 201, $response->get_status(), 'The file was not uploaded.' ); |
| 1946 | $this->assertSame( 'Filename With Spaces', $data['title']['raw'], 'An incorrect filename was returned.' ); |
| 1947 | } |
| 1948 | |
1926 | 1949 | /** |
1927 | 1950 | * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire |
1928 | 1951 | * once when attachments are updated. |