Make WordPress Core

Ticket #48264: 48264.diff

File 48264.diff, 6.2 KB (added by TimothyBlynJacobs, 6 years ago)
  • src/wp-includes/formatting.php

    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index acc8de9188..1251508d2e 100644
    a b function wp_unslash( $value ) { 
    54035403        return stripslashes_deep( $value );
    54045404}
    54055405
     5406/**
     5407 * Adds slashes to only string values in an array of values.
     5408 *
     5409 * This should be used when preparing data for core API that expects slashed data.
     5410 * This should not be used to escape data going directly into an SQL query.
     5411 *
     5412 * @since 5.3.0
     5413 *
     5414 * @param mixed $value Scalar or array of scalars.
     5415 * @return mixed Slashes $value
     5416 */
     5417function wp_slash_strings_only( $value ) {
     5418        return map_deep( $value, 'addslashes_strings_only' );
     5419}
     5420
     5421/**
     5422 * Adds slashes to only string values.
     5423 *
     5424 * @since 5.3.0
     5425 *
     5426 * @param mixed $value
     5427 * @return mixed
     5428 */
     5429function addslashes_strings_only( $value ) {
     5430        return is_string( $value ) ? addslashes( $value ) : $value;
     5431}
     5432
    54065433/**
    54075434 * Extract and return the first URL from passed content.
    54085435 *
  • 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 b3ea89a86e..411fc5460c 100644
    a b abstract class WP_REST_Meta_Fields { 
    365365                        }
    366366                }
    367367
    368                 if ( ! update_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
     368                if ( ! update_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash_strings_only( $value ) ) ) {
    369369                        return new WP_Error(
    370370                                'rest_meta_database_error',
    371371                                /* translators: %s: Custom field key. */
  • 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 2007e77ef2..6b483ff0c9 100644
    a b class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    22642264                self::assertFalse( $registered );
    22652265        }
    22662266
     2267        /**
     2268         * @ticket 48264
     2269         */
     2270        public function test_update_array_of_ints_meta() {
     2271                $this->grant_write_permission();
     2272                register_post_meta(
     2273                        'post',
     2274                        'items',
     2275                        array(
     2276                                'single'       => true,
     2277                                'type'         => 'array',
     2278                                'show_in_rest' => array(
     2279                                        'schema' => array(
     2280                                                'items' => array(
     2281                                                        'type' => 'integer',
     2282                                                ),
     2283                                        ),
     2284                                ),
     2285                        )
     2286                );
     2287
     2288                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2289                $request->set_body_params(
     2290                        array(
     2291                                'meta' => array(
     2292                                        'items' => array( 1, 2, 3 ),
     2293                                ),
     2294                        )
     2295                );
     2296
     2297                rest_get_server()->dispatch( $request );
     2298                $response = rest_get_server()->dispatch( $request );
     2299                $this->assertEquals( 200, $response->get_status() );
     2300        }
     2301
     2302        /**
     2303         * @ticket 48264
     2304         */
     2305        public function test_update_array_of_ints_meta_stored_strings_are_updated() {
     2306                $this->grant_write_permission();
     2307                register_post_meta(
     2308                        'post',
     2309                        'items',
     2310                        array(
     2311                                'single'       => true,
     2312                                'type'         => 'array',
     2313                                'show_in_rest' => array(
     2314                                        'schema' => array(
     2315                                                'items' => array(
     2316                                                        'type' => 'integer',
     2317                                                ),
     2318                                        ),
     2319                                ),
     2320                        )
     2321                );
     2322
     2323                update_post_meta( self::$post_id, 'items', array( '1', '2', '3' ) );
     2324
     2325                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2326                $request->set_body_params(
     2327                        array(
     2328                                'meta' => array(
     2329                                        'items' => array( 1, 2, 3 ),
     2330                                ),
     2331                        )
     2332                );
     2333
     2334                $response = rest_get_server()->dispatch( $request );
     2335                $this->assertEquals( 200, $response->get_status() );
     2336                $this->assertSame( array( 1, 2, 3 ), get_post_meta( self::$post_id, 'items', true ) );
     2337        }
     2338
     2339        /**
     2340         * @ticket 48264
     2341         */
     2342        public function test_update_array_of_ints_meta_string_request_data_is_set_as_ints() {
     2343                $this->grant_write_permission();
     2344                register_post_meta(
     2345                        'post',
     2346                        'items',
     2347                        array(
     2348                                'single'       => true,
     2349                                'type'         => 'array',
     2350                                'show_in_rest' => array(
     2351                                        'schema' => array(
     2352                                                'items' => array(
     2353                                                        'type' => 'integer',
     2354                                                ),
     2355                                        ),
     2356                                ),
     2357                        )
     2358                );
     2359
     2360                update_post_meta( self::$post_id, 'items', array( 1, 2, 3 ) );
     2361
     2362                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2363                $request->set_body_params(
     2364                        array(
     2365                                'meta' => array(
     2366                                        'items' => array( '1', '2', '3' ),
     2367                                ),
     2368                        )
     2369                );
     2370
     2371                $response = rest_get_server()->dispatch( $request );
     2372                $this->assertEquals( 200, $response->get_status() );
     2373                $this->assertSame( array( 1, 2, 3 ), get_post_meta( self::$post_id, 'items', true ) );
     2374        }
     2375
     2376        /**
     2377         * @ticket 48264
     2378         */
     2379        public function test_update_array_of_ints_meta_string_request_data_and_string_stored_data() {
     2380                $this->grant_write_permission();
     2381                register_post_meta(
     2382                        'post',
     2383                        'items',
     2384                        array(
     2385                                'single'       => true,
     2386                                'type'         => 'array',
     2387                                'show_in_rest' => array(
     2388                                        'schema' => array(
     2389                                                'items' => array(
     2390                                                        'type' => 'integer',
     2391                                                ),
     2392                                        ),
     2393                                ),
     2394                        )
     2395                );
     2396
     2397                update_post_meta( self::$post_id, 'items', array( '1', '2', '3' ) );
     2398
     2399                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2400                $request->set_body_params(
     2401                        array(
     2402                                'meta' => array(
     2403                                        'items' => array( '1', '2', '3' ),
     2404                                ),
     2405                        )
     2406                );
     2407
     2408                $response = rest_get_server()->dispatch( $request );
     2409                $this->assertEquals( 200, $response->get_status() );
     2410                $this->assertSame( array( 1, 2, 3 ), get_post_meta( self::$post_id, 'items', true ) );
     2411        }
     2412
    22672413        /**
    22682414         * Internal function used to disable an insert query which
    22692415         * will trigger a wpdb error for testing purposes.
  • tests/qunit/fixtures/wp-api-generated.js

    diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js
    index b8217e729b..2b5f8d38e9 100644
    a b mockedApiResponse.Schema = { 
    23042304                }
    23052305            ]
    23062306        },
    2307         "/wp/v2/media/(?P<id>[\\d+])/post-process": {
     2307        "/wp/v2/media/(?P<id>[\\d]+)/post-process": {
    23082308            "namespace": "wp/v2",
    23092309            "methods": [
    23102310                "POST"