Make WordPress Core

Changeset 49966


Ignore:
Timestamp:
01/17/2021 12:49:39 AM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Allow sending an empty array to delete multi meta keys.

Previously, only null was supported.

Fixes #50790.
Props chrisvanpatten.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r49927 r49966  
    146146                        }
    147147
     148                        $value = $meta[ $name ];
     149
    148150                        /*
    149151                         * A null value means reset the field, which is essentially deleting it
    150152                         * from the database and then relying on the default value.
     153                         *
     154                         * Non-single meta can also be removed by passing an empty array.
    151155                         */
    152                         if ( is_null( $meta[ $name ] ) ) {
     156                        if ( is_null( $value ) || ( array() === $value && ! $args['single'] ) ) {
    153157                                $args = $this->get_registered_fields()[ $meta_key ];
    154158
    … …  
    172176                                continue;
    173177                        }
    174 
    175                         $value = $meta[ $name ];
    176178
    177179                        if ( ! $args['single'] && is_array( $value ) && count( array_filter( $value, 'is_null' ) ) ) {
  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r49603 r49966  
    10261026                $post_updated = get_post( self::$post_id );
    10271027                $this->assertSame( $post_original->post_content, $post_updated->post_content );
     1028        }
     1029
     1030        /**
     1031         * @ticket 50790
     1032         */
     1033        public function test_remove_multi_value_with_empty_array() {
     1034                add_post_meta( self::$post_id, 'test_multi', 'val1' );
     1035                $values = get_post_meta( self::$post_id, 'test_multi', false );
     1036                $this->assertSame( array( 'val1' ), $values );
     1037
     1038                $this->grant_write_permission();
     1039
     1040                $data    = array(
     1041                        'meta' => array(
     1042                                'test_multi' => array(),
     1043                        ),
     1044                );
     1045                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1046                $request->set_body_params( $data );
     1047
     1048                $response = rest_get_server()->dispatch( $request );
     1049                $this->assertSame( 200, $response->get_status() );
     1050
     1051                $meta = get_post_meta( self::$post_id, 'test_multi', false );
     1052                $this->assertEmpty( $meta );
    10281053        }
    10291054
Note: See TracChangeset for help on using the changeset viewer.