Changeset 58447
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r58130 r58447 305 305 $attachment->guid = $url; 306 306 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. 307 318 if ( empty( $attachment->post_title ) ) { 308 319 $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); -
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r58400 r58447 2016 2016 2017 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 2071 /** 2018 2072 * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire 2019 2073 * once when attachments are updated.
Note: See TracChangeset
for help on using the changeset viewer.