Make WordPress Core

Changeset 47261


Ignore:
Timestamp:
02/11/2020 03:37:17 AM (5 years ago)
Author:
kadamwhite
Message:

REST API: Allow meta to be set when creating a new media record via REST.

Props TimothyBlynJacobs, apermo.
Fixes #44567.

Location:
trunk
Files:
2 edited

Legend:

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

    r47122 r47261  
    142142        }
    143143
     144        $schema = $this->get_item_schema();
     145
    144146        // Extract by name.
    145147        $attachment_id = $insert['attachment_id'];
     
    148150        if ( isset( $request['alt_text'] ) ) {
    149151            update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) );
     152        }
     153
     154        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
     155            $meta_update = $this->meta->update_value( $request['meta'], $attachment_id );
     156
     157            if ( is_wp_error( $meta_update ) ) {
     158                return $meta_update;
     159            }
    150160        }
    151161
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r47122 r47261  
    17411741    }
    17421742
     1743    /**
     1744     * @ticket 44567
     1745     */
     1746    public function test_create_item_with_meta_values() {
     1747        register_post_meta(
     1748            'attachment',
     1749            'best_cannoli',
     1750            array(
     1751                'type'         => 'string',
     1752                'single'       => true,
     1753                'show_in_rest' => true,
     1754            )
     1755        );
     1756
     1757        wp_set_current_user( self::$author_id );
     1758
     1759        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1760        $request->set_header( 'Content-Type', 'image/jpeg' );
     1761        $request->set_header( 'Content-Disposition', 'attachment; filename=cannoli.jpg' );
     1762        $request->set_param( 'meta', array( 'best_cannoli' => 'Chocolate-dipped, no filling' ) );
     1763
     1764        $request->set_body( file_get_contents( $this->test_file ) );
     1765        $response = rest_get_server()->dispatch( $request );
     1766        $data     = $response->get_data();
     1767
     1768        $this->assertEquals( 201, $response->get_status() );
     1769        $this->assertEquals( 'Chocolate-dipped, no filling', get_post_meta( $response->get_data()['id'], 'best_cannoli', true ) );
     1770    }
     1771
    17431772    public function filter_rest_insert_attachment( $attachment ) {
    17441773        self::$rest_insert_attachment_count++;
Note: See TracChangeset for help on using the changeset viewer.