diff --git a/lib/endpoints/class-wp-rest-attachments-controller.php b/lib/endpoints/class-wp-rest-attachments-controller.php index e4e3ab9ff9..38c86ba60f 100755 --- a/lib/endpoints/class-wp-rest-attachments-controller.php +++ b/lib/endpoints/class-wp-rest-attachments-controller.php @@ -109,6 +109,11 @@ public function create_item( $request ) { $attachment->file = $file; $attachment->post_mime_type = $type; $attachment->guid = $url; + + if ( empty( $attachment->post_title ) ) { + $attachment->post_title = preg_replace( '/\.[^.]+$/', '', basename( $file ) ); + } + $id = wp_insert_post( $attachment, true ); if ( is_wp_error( $id ) ) { if ( in_array( $id->get_error_code(), array( 'db_update_error' ) ) ) { diff --git a/tests/data/codeispoetry.png b/tests/data/codeispoetry.png new file mode 100644 index 0000000000..130fbbdae9 --- /dev/null +++ b/tests/data/codeispoetry.png @@ -0,0 +1,579 @@ + + + + + + + + + + + + + + + + + + + + + + + + wp-cli.github.com/codeispoetry.png at master · wp-cli/wp-cli.github.com · GitHub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Skip to content + + + + + + + + + + + + +
+ +
+
+ + +
+
+
+ +
+
+ + + + + +

+ + /wp-cli.github.com + + + + + +

+ +
+ +
+ +
+
+ + + + + + + +
+ + + +
+ + Find file + + +
+ +
+ + +
+ + + 9a03d83 + + + + + + + + +
+ +
+
+
+ +
+ Raw + History +
+ + +
+ +
+ +
+ 15.6 KB +
+
+ + + +
+
+ codeispoetry.png +
+
+ +
+ +Jump to Line + + +
+ +
+ + +
+
+ +
+ +
+ +
+ + + + + + + +
+ + + Something went wrong with that request. Please try again. +
+ + + + + + + + + + + + + + diff --git a/tests/test-rest-attachments-controller.php b/tests/test-rest-attachments-controller.php index 0c612b7afc..0627dbbdd3 100644 --- a/tests/test-rest-attachments-controller.php +++ b/tests/test-rest-attachments-controller.php @@ -24,6 +24,9 @@ public function setUp() { $orig_file = dirname( __FILE__ ) . '/data/canola.jpg'; $this->test_file = '/tmp/canola.jpg'; copy( $orig_file, $this->test_file ); + $orig_file2 = dirname( __FILE__ ) . '/data/codeispoetry.png'; + $this->test_file2 = '/tmp/codeispoetry.png'; + copy( $orig_file2, $this->test_file2 ); } @@ -378,6 +381,24 @@ public function test_create_item() { $this->assertEquals( 'The description for the image', $data['caption'] ); } + public function test_create_item_default_filename_title() { + wp_set_current_user( $this->author_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); + $request->set_file_params( array( + 'file' => array( + 'file' => file_get_contents( $this->test_file2 ), + 'name' => 'codeispoetry.jpg', + 'size' => filesize( $this->test_file2 ), + 'tmp_name' => $this->test_file2, + ), + ) ); + $request->set_header( 'Content-MD5', md5_file( $this->test_file2 ) ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 201, $response->get_status() ); + $data = $response->get_data(); + $this->assertEquals( 'codeispoetry', $data['title']['raw'] ); + } + public function test_create_item_with_files() { wp_set_current_user( $this->author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); @@ -702,6 +723,9 @@ public function tearDown() { if ( file_exists( $this->test_file ) ) { unlink( $this->test_file ); } + if ( file_exists( $this->test_file2 ) ) { + unlink( $this->test_file2 ); + } } protected function check_post_data( $attachment, $data, $context = 'view' ) {