From 886a02686b19fb7cb1363cd65ff3622ccd013d0d Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Tue, 11 Feb 2020 03:37:17 +0000 Subject: [PATCH] REST API: Allow meta to be set when creating a new media record via REST. Props TimothyBlynJacobs, apermo. Fixes #44567. git-svn-id: https://develop.svn.wordpress.org/trunk@47261 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-attachments-controller.php | 10 +++++++ .../rest-api/rest-attachments-controller.php | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+) 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 1d78928fc4..91b21587b1 100644 --- 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 @@ -141,6 +141,8 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { return $insert; } + $schema = $this->get_item_schema(); + // Extract by name. $attachment_id = $insert['attachment_id']; $file = $insert['file']; @@ -149,6 +151,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); } + if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { + $meta_update = $this->meta->update_value( $request['meta'], $attachment_id ); + + if ( is_wp_error( $meta_update ) ) { + return $meta_update; + } + } + $attachment = get_post( $attachment_id ); $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index b8b9e05975..f6adcf78c8 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -1740,6 +1740,35 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control $this->assertSame( 1, self::$rest_after_insert_attachment_count ); } + /** + * @ticket 44567 + */ + public function test_create_item_with_meta_values() { + register_post_meta( + 'attachment', + 'best_cannoli', + array( + 'type' => 'string', + 'single' => true, + 'show_in_rest' => true, + ) + ); + + wp_set_current_user( self::$author_id ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); + $request->set_header( 'Content-Type', 'image/jpeg' ); + $request->set_header( 'Content-Disposition', 'attachment; filename=cannoli.jpg' ); + $request->set_param( 'meta', array( 'best_cannoli' => 'Chocolate-dipped, no filling' ) ); + + $request->set_body( file_get_contents( $this->test_file ) ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 201, $response->get_status() ); + $this->assertEquals( 'Chocolate-dipped, no filling', get_post_meta( $response->get_data()['id'], 'best_cannoli', true ) ); + } + public function filter_rest_insert_attachment( $attachment ) { self::$rest_insert_attachment_count++; }