Make WordPress Core

Changeset 57380


Ignore:
Timestamp:
01/30/2024 08:37:04 AM (3 years ago)
Author:
swissspidy
Message:

REST API: Support assigning terms when creating attachments.

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r57312 r57380  
    185185                if ( is_wp_error( $fields_update ) ) {
    186186                        return $fields_update;
     187                }
     188
     189                $terms_update = $this->handle_terms( $attachment_id, $request );
     190
     191                if ( is_wp_error( $terms_update ) ) {
     192                        return $terms_update;
    187193                }
    188194
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r56549 r57380  
    10441044                $attachment = $response->get_data();
    10451045                $this->assertStringNotContainsString( ABSPATH, get_post_meta( $attachment['id'], '_wp_attached_file', true ) );
     1046        }
     1047
     1048        /**
     1049         * @ticket 57897
     1050         *
     1051         * @requires function imagejpeg
     1052         */
     1053        public function test_create_item_with_terms() {
     1054                wp_set_current_user( self::$author_id );
     1055                register_taxonomy_for_object_type( 'category', 'attachment' );
     1056                $category = wp_insert_term( 'Media Category', 'category' );
     1057                $request  = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1058                $request->set_header( 'Content-Type', 'image/jpeg' );
     1059                $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     1060
     1061                $request->set_body( file_get_contents( self::$test_file ) );
     1062                $request->set_param( 'categories', array( $category['term_id'] ) );
     1063                $response   = rest_get_server()->dispatch( $request );
     1064                $attachment = $response->get_data();
     1065
     1066                $term = wp_get_post_terms( $attachment['id'], 'category' );
     1067                $this->assertSame( $category['term_id'], $term[0]->term_id );
    10461068        }
    10471069
Note: See TracChangeset for help on using the changeset viewer.