Make WordPress Core

Ticket #42069: 42069.8.diff

File 42069.8.diff, 4.3 KB (added by TimothyBlynJacobs, 6 years ago)
  • src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    diff --git a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
    index 46c2d06250..54e0498132 100644
    a b abstract class WP_REST_Meta_Fields { 
    295295                        );
    296296                }
    297297
    298                 $meta_key   = wp_slash( $meta_key );
    299                 $meta_value = wp_slash( $value );
    300 
    301298                // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
    302299                $old_value = get_metadata( $meta_type, $object_id, $meta_key );
     300                $subtype   = get_object_subtype( $meta_type, $object_id );
    303301
    304302                if ( 1 === count( $old_value ) ) {
    305                         if ( $old_value[0] === $meta_value ) {
     303                        if ( (string) sanitize_meta( $meta_key, $value, $meta_type, $subtype ) === $old_value[0] ) {
    306304                                return true;
    307305                        }
    308306                }
    309307
    310                 if ( ! update_metadata( $meta_type, $object_id, $meta_key, $meta_value ) ) {
     308                if ( ! update_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
    311309                        return new WP_Error(
    312310                                'rest_meta_database_error',
    313311                                __( 'Could not update meta value in database.' ),
  • tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    diff --git a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
    index cfeec389c5..154cdb0ac9 100644
    a b class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    130130                        'auth_callback'  => '__return_false',
    131131                ) );
    132132
     133                register_meta(
     134                        'post', 'test_boolean_update', array(
     135                                'single'            => true,
     136                                'type'              => 'boolean',
     137                                'sanitize_callback' => 'absint',
     138                                'show_in_rest'      => true,
     139                        )
     140                );
     141
     142                register_meta(
     143                        'post', 'test_textured_text_update', array(
     144                                'single'            => true,
     145                                'type'              => 'string',
     146                                'sanitize_callback' => 'sanitize_text_field',
     147                                'show_in_rest'      => true,
     148                        )
     149                );
     150
     151                register_meta(
     152                        'post', 'test_json_encoded', array(
     153                                'single'       => true,
     154                                'type'         => 'string',
     155                                'show_in_rest' => true,
     156                        )
     157                );
     158
     159                register_meta(
     160                        'post', 'test\'slashed\'key', array(
     161                                'single'       => true,
     162                                'type'         => 'string',
     163                                'show_in_rest' => true,
     164                        )
     165                );
     166
    133167                /** @var WP_REST_Server $wp_rest_server */
    134168                global $wp_rest_server;
    135169                $this->server = $wp_rest_server = new Spy_REST_Server;
    class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    11611195                return $data;
    11621196        }
    11631197
     1198        /**
     1199         * @ticket 42069
     1200         * @dataProvider data_update_value_return_success_with_same_value
     1201         */
     1202        public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) {
     1203                add_post_meta( self::$post_id, $meta_key, $meta_value );
     1204
     1205                $this->grant_write_permission();
     1206
     1207                $data = array(
     1208                        'meta' => array(
     1209                                $meta_key => $meta_value,
     1210                        ),
     1211                );
     1212
     1213                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1214                $request->set_body_params( $data );
     1215
     1216                $response = rest_get_server()->dispatch( $request );
     1217
     1218                $this->assertEquals( 200, $response->get_status() );
     1219        }
     1220
     1221        public function data_update_value_return_success_with_same_value() {
     1222                return array(
     1223                        array( 'test_boolean_update', false ),
     1224                        array( 'test_boolean_update', true ),
     1225                        array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ),
     1226                        array( 'test_textured_text_update', "He's about to do something rash..." ),
     1227                        array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ),
     1228                        array( 'test\'slashed\'key', 'Hello' ),
     1229                );
     1230        }
     1231
     1232        public function test_slashed_meta_key() {
     1233
     1234                add_post_meta( self::$post_id, 'test\'slashed\'key', 'Hello' );
     1235
     1236                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1237
     1238                $response = rest_get_server()->dispatch( $request );
     1239                $data     = $response->get_data();
     1240
     1241                $this->assertArrayHasKey( 'test\'slashed\'key', $data['meta'] );
     1242                $this->assertEquals( 'Hello', $data['meta']['test\'slashed\'key'] );
     1243        }
     1244
    11641245        /**
    11651246         * Internal function used to disable an insert query which
    11661247         * will trigger a wpdb error for testing purposes.