Make WordPress Core


Ignore:
Timestamp:
03/23/2021 11:34:59 PM (4 years ago)
Author:
peterwilsoncc
Message:

REST API: Prevent database error when deleting meta data.

Add a check to WP_REST_Meta_Fields::delete_meta_value() ensuring meta data is set before attempting to delete it from the database. If the data does not exist, the delete is considered successful as the data matches the desired state.

Props BrechtVds, goaroundagain, TimothyBlynJacobs.
Fixes #52787.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r49966 r50567  
    10511051        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    10521052        $this->assertEmpty( $meta );
     1053    }
     1054
     1055    /**
     1056     * Ensure deleting non-existant meta data behaves gracefully.
     1057     *
     1058     * @ticket 52787
     1059     * @dataProvider data_delete_does_not_trigger_error_if_no_meta_values
     1060     *
     1061     * @param array|null $delete_value Value used to delete meta data.
     1062     */
     1063    public function test_delete_does_not_trigger_error_if_no_meta_values( $delete_value ) {
     1064        $this->grant_write_permission();
     1065
     1066        $data    = array(
     1067            'meta' => array(
     1068                'test_multi' => $delete_value,
     1069            ),
     1070        );
     1071        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1072        $request->set_body_params( $data );
     1073
     1074        $response = rest_get_server()->dispatch( $request );
     1075
     1076        $this->assertSame( 200, $response->get_status() );
     1077    }
     1078
     1079    /**
     1080     * Data provider for test_delete_does_not_trigger_error_if_no_meta_values().
     1081     *
     1082     * @return array[] Array of test parameters.
     1083     */
     1084    public function data_delete_does_not_trigger_error_if_no_meta_values() {
     1085        return array(
     1086            array( array() ),
     1087            array( null ),
     1088        );
    10531089    }
    10541090
Note: See TracChangeset for help on using the changeset viewer.