REST API: Support assigning terms when creating attachments.

Props mukesh27, Dharm1025, Ankit K Gupta, swissspidy, dharm1025, tanjimtc71, timothyblynjacobs, spacedmonkey.
Fixes #57897.

git-svn-id: https://develop.svn.wordpress.org/trunk@57380 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2024-01-30 08:37:04 +00:00
parent 94e6fe9f8a
commit 0e0b10092c
2 changed files with 28 additions and 0 deletions

View File

@ -186,6 +186,12 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
return $fields_update;
}
$terms_update = $this->handle_terms( $attachment_id, $request );
if ( is_wp_error( $terms_update ) ) {
return $terms_update;
}
$request->set_param( 'context', 'edit' );
/**

View File

@ -1045,6 +1045,28 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
$this->assertStringNotContainsString( ABSPATH, get_post_meta( $attachment['id'], '_wp_attached_file', true ) );
}
/**
* @ticket 57897
*
* @requires function imagejpeg
*/
public function test_create_item_with_terms() {
wp_set_current_user( self::$author_id );
register_taxonomy_for_object_type( 'category', 'attachment' );
$category = wp_insert_term( 'Media Category', 'category' );
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
$request->set_header( 'Content-Type', 'image/jpeg' );
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
$request->set_body( file_get_contents( self::$test_file ) );
$request->set_param( 'categories', array( $category['term_id'] ) );
$response = rest_get_server()->dispatch( $request );
$attachment = $response->get_data();
$term = wp_get_post_terms( $attachment['id'], 'category' );
$this->assertSame( $category['term_id'], $term[0]->term_id );
}
public function test_update_item() {
wp_set_current_user( self::$editor_id );
$attachment_id = self::factory()->attachment->create_object(