Make WordPress Core

Ticket #50700: 50700.diff

File 50700.diff, 5.1 KB (added by dlh, 6 years ago)
  • src/wp-includes/rest-api.php

    diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
    index d3bb80a3d6..4d9708e82a 100644
    function rest_filter_response_by_context( $data, $schema, $context ) { 
    20382038                return $data;
    20392039        }
    20402040
     2041        $is_array_type  = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) );
     2042        $is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) );
     2043
     2044        if ( $is_array_type && $is_object_type ) {
     2045                if ( rest_is_array( $data ) ) {
     2046                        $is_object_type = false;
     2047                } else {
     2048                        $is_array_type = false;
     2049                }
     2050        }
     2051
     2052        $has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] );
     2053
    20412054        foreach ( $data as $key => $value ) {
    20422055                $check = array();
    20432056
    2044                 if ( 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) ) ) {
     2057                if ( $is_array_type ) {
    20452058                        $check = isset( $schema['items'] ) ? $schema['items'] : array();
    2046                 } elseif ( 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ) ) {
     2059                } elseif ( $is_object_type ) {
    20472060                        if ( isset( $schema['properties'][ $key ] ) ) {
    20482061                                $check = $schema['properties'][ $key ];
    2049                         } elseif ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
     2062                        } elseif ( $has_additional_properties ) {
    20502063                                $check = $schema['additionalProperties'];
    20512064                        }
    20522065                }
    function rest_filter_response_by_context( $data, $schema, $context ) { 
    20582071                if ( ! in_array( $context, $check['context'], true ) ) {
    20592072                        if ( is_object( $data ) ) {
    20602073                                unset( $data->$key );
    2061                         } else {
     2074                        } elseif ( $is_object_type ) {
    20622075                                unset( $data[ $key ] );
     2076                        } else {
     2077                                // All array items share schema, so there's no need to check each one.
     2078                                $data = array();
     2079                                break;
    20632080                        }
    20642081                } elseif ( is_array( $value ) || is_object( $value ) ) {
    20652082                        $new_value = rest_filter_response_by_context( $value, $check, $context );
  • tests/phpunit/tests/rest-api.php

    diff --git tests/phpunit/tests/rest-api.php tests/phpunit/tests/rest-api.php
    index 789c4e19b3..7497d13017 100644
    class Tests_REST_API extends WP_UnitTestCase { 
    13141314                                        ),
    13151315                                ),
    13161316                        ),
     1317                        'object with no matching properties' => array(
     1318                                array(
     1319                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1320                                        'type'       => 'object',
     1321                                        'properties' => array(
     1322                                                'a' => array(
     1323                                                        'type'    => 'string',
     1324                                                        'context' => array( 'edit' ),
     1325                                                ),
     1326                                                'b' => array(
     1327                                                        'type'    => 'string',
     1328                                                        'context' => array( 'edit' ),
     1329                                                ),
     1330                                        ),
     1331                                ),
     1332                                array(
     1333                                        'a' => 'hi',
     1334                                        'b' => 'hello',
     1335                                ),
     1336                                array(),
     1337                        ),
     1338                        'array whose type does not match' => array(
     1339                                array(
     1340                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1341                                        'type'       => 'object',
     1342                                        'properties' => array(
     1343                                                'arr' => array(
     1344                                                        'type'  => 'array',
     1345                                                        'context' => array( 'view' ),
     1346                                                        'items' => array(
     1347                                                                'type'    => 'string',
     1348                                                                'context' => array( 'edit' ),
     1349                                                        ),
     1350                                                ),
     1351                                        ),
     1352                                ),
     1353                                array(
     1354                                        'arr' => array( 'foo', 'bar', 'baz' ),
     1355                                ),
     1356                                array( 'arr' => array() ),
     1357                        ),
     1358                        'array and object type passed object' => array(
     1359                                array(
     1360                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1361                                        'type'       => array( 'array', 'object' ),
     1362                                        'properties' => array(
     1363                                                'a' => array(
     1364                                                        'type'    => 'string',
     1365                                                        'context' => array( 'view' ),
     1366                                                ),
     1367                                                'b' => array(
     1368                                                        'type'    => 'string',
     1369                                                        'context' => array( 'view' ),
     1370                                                ),
     1371                                        ),
     1372                                        'items'      => array(
     1373                                                'type'       => 'object',
     1374                                                'context'    => array( 'edit' ),
     1375                                                'properties' => array(
     1376                                                        'a' => array(
     1377                                                                'type'    => 'string',
     1378                                                                'context' => array( 'view' ),
     1379                                                        ),
     1380                                                        'b' => array(
     1381                                                                'type'    => 'string',
     1382                                                                'context' => array( 'view' ),
     1383                                                        ),
     1384                                                ),
     1385                                        ),
     1386                                ),
     1387                                array(
     1388                                        'a' => 'foo',
     1389                                        'b' => 'bar',
     1390                                ),
     1391                                array(
     1392                                        'a' => 'foo',
     1393                                        'b' => 'bar',
     1394                                ),
     1395                        ),
     1396                        'array and object type passed array' => array(
     1397                                array(
     1398                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1399                                        'type'       => array( 'array', 'object' ),
     1400                                        'properties' => array(
     1401                                                'a' => array(
     1402                                                        'type'    => 'string',
     1403                                                        'context' => array( 'view' ),
     1404                                                ),
     1405                                                'b' => array(
     1406                                                        'type'    => 'string',
     1407                                                        'context' => array( 'view' ),
     1408                                                ),
     1409                                        ),
     1410                                        'items'      => array(
     1411                                                'type'       => 'object',
     1412                                                'context'    => array( 'edit' ),
     1413                                                'properties' => array(
     1414                                                        'a' => array(
     1415                                                                'type'    => 'string',
     1416                                                                'context' => array( 'view' ),
     1417                                                        ),
     1418                                                        'b' => array(
     1419                                                                'type'    => 'string',
     1420                                                                'context' => array( 'view' ),
     1421                                                        ),
     1422                                                ),
     1423                                        ),
     1424                                ),
     1425                                array(
     1426                                        array(
     1427                                                'a' => 'foo',
     1428                                                'b' => 'bar',
     1429                                        ),
     1430                                        array(
     1431                                                'a' => 'foo',
     1432                                                'b' => 'bar',
     1433                                        ),
     1434                                ),
     1435                                array(),
     1436                        ),
    13171437                );
    13181438        }
    13191439