WordPress.org

Make WordPress Core

Changeset 50573


Ignore:
Timestamp:
03/25/2021 12:02:35 AM (7 months 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.
Merges [50567] to the 5.7 branch.
Fixes #52787.

Location:
branches/5.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.7

  • branches/5.7/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r49966 r50573  
    231231                )
    232232            );
     233        }
     234
     235        if ( null === get_metadata_raw( $meta_type, $object_id, wp_slash( $meta_key ) ) ) {
     236            return true;
    233237        }
    234238
  • branches/5.7/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r49966 r50573  
    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.