Make WordPress Core

Ticket #48819: 48819.2.diff

File 48819.2.diff, 12.3 KB (added by TimothyBlynJacobs, 5 years ago)
  • src/wp-includes/rest-api.php

    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index 5add20bc60..9913901a6b 100644
    a b function rest_parse_embed_param( $embed ) { 
    15931593
    15941594        return $rels;
    15951595}
     1596
     1597
     1598/**
     1599 * Filters the response to remove any fields not available in the given context.
     1600 *
     1601 * @since 5.4.0
     1602 *
     1603 * @param array|object $data    The response data to modify.
     1604 * @param array        $schema  The schema for the endpoint used to filter the response.
     1605 * @param string       $context The requested context.
     1606 * @return array|object The filtered response data.
     1607 */
     1608function rest_filter_response_by_context( $data, $schema, $context ) {
     1609        if ( ! is_array( $data ) && ! is_object( $data ) ) {
     1610                return $data;
     1611        }
     1612
     1613        if ( isset( $schema['type'] ) ) {
     1614                $type = $schema['type'];
     1615        } elseif ( isset( $schema['properties'] ) ) {
     1616                $type = 'object'; // Back compat if a developer accidentally omitted the type.
     1617        } else {
     1618                return $data;
     1619        }
     1620
     1621        foreach ( $data as $key => $value ) {
     1622                $check = array();
     1623
     1624                if ( 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) ) ) {
     1625                        $check = isset( $schema['items'] ) ? $schema['items'] : array();
     1626                } elseif ( 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ) ) {
     1627                        if ( isset( $schema['properties'][ $key ] ) ) {
     1628                                $check = $schema['properties'][ $key ];
     1629                        } elseif ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
     1630                                $check = $schema['additionalProperties'];
     1631                        }
     1632                }
     1633
     1634                if ( ! isset( $check['context'] ) ) {
     1635                        continue;
     1636                }
     1637
     1638                if ( ! in_array( $context, $check['context'], true ) ) {
     1639                        if ( is_object( $data ) ) {
     1640                                unset( $data->$key );
     1641                        } else {
     1642                                unset( $data[ $key ] );
     1643                        }
     1644                } else {
     1645                        $new_value = rest_filter_response_by_context( $value, $check, $context );
     1646
     1647                        if ( is_object( $data ) ) {
     1648                                $data->$key = $new_value;
     1649                        } else {
     1650                                $data[ $key ] = $new_value;
     1651                        }
     1652                }
     1653        }
     1654
     1655        return $data;
     1656}
  • src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
    index dd40d93d2b..c8460942e3 100644
    a b abstract class WP_REST_Controller { 
    286286         *
    287287         * @since 4.7.0
    288288         *
    289          * @param array  $data    Response data to fiter.
     289         * @param array  $data    Response data to filter.
    290290         * @param string $context Context defined in the schema.
    291291         * @return array Filtered response.
    292292         */
    abstract class WP_REST_Controller { 
    294294
    295295                $schema = $this->get_item_schema();
    296296
    297                 foreach ( $data as $key => $value ) {
    298                         if ( empty( $schema['properties'][ $key ] ) || empty( $schema['properties'][ $key ]['context'] ) ) {
    299                                 continue;
    300                         }
    301 
    302                         if ( ! in_array( $context, $schema['properties'][ $key ]['context'], true ) ) {
    303                                 unset( $data[ $key ] );
    304                                 continue;
    305                         }
    306 
    307                         if ( 'object' === $schema['properties'][ $key ]['type'] && ! empty( $schema['properties'][ $key ]['properties'] ) ) {
    308                                 foreach ( $schema['properties'][ $key ]['properties'] as $attribute => $details ) {
    309                                         if ( empty( $details['context'] ) ) {
    310                                                 continue;
    311                                         }
    312 
    313                                         if ( ! in_array( $context, $details['context'], true ) ) {
    314                                                 if ( isset( $data[ $key ][ $attribute ] ) ) {
    315                                                         unset( $data[ $key ][ $attribute ] );
    316                                                 }
    317                                         }
    318                                 }
    319                         }
    320                 }
    321 
    322                 return $data;
     297                return rest_filter_response_by_context( $data, $schema, $context );
    323298        }
    324299
    325300        /**
  • tests/phpunit/tests/rest-api.php

    diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php
    index c0eea9804f..3470422367 100644
    a b class Tests_REST_API extends WP_UnitTestCase { 
    933933                        array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), array( 'https://api.w.org/term', 'https://api.w.org/attachment' ) ),
    934934                );
    935935        }
     936
     937        /**
     938         * @dataProvider _dp_rest_filter_response_by_context
     939         */
     940        public function test_rest_filter_response_by_context( $schema, $data, $expected ) {
     941                $this->assertEquals( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) );
     942        }
     943
     944        public function _dp_rest_filter_response_by_context() {
     945                return array(
     946                        'default'                                                   => array(
     947                                array(
     948                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     949                                        'type'       => 'object',
     950                                        'properties' => array(
     951                                                'first'  => array(
     952                                                        'type'    => 'string',
     953                                                        'context' => array( 'view', 'edit' ),
     954                                                ),
     955                                                'second' => array(
     956                                                        'type'    => 'string',
     957                                                        'context' => array( 'edit' ),
     958                                                ),
     959                                        ),
     960                                ),
     961                                array(
     962                                        'first'  => 'a',
     963                                        'second' => 'b',
     964                                ),
     965                                array( 'first' => 'a' ),
     966                        ),
     967                        'keeps missing context'                                     => array(
     968                                array(
     969                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     970                                        'type'       => 'object',
     971                                        'properties' => array(
     972                                                'first'  => array(
     973                                                        'type'    => 'string',
     974                                                        'context' => array( 'view', 'edit' ),
     975                                                ),
     976                                                'second' => array(
     977                                                        'type' => 'string',
     978                                                ),
     979                                        ),
     980                                ),
     981                                array(
     982                                        'first'  => 'a',
     983                                        'second' => 'b',
     984                                ),
     985                                array(
     986                                        'first'  => 'a',
     987                                        'second' => 'b',
     988                                ),
     989                        ),
     990                        'removes empty context'                                     => array(
     991                                array(
     992                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     993                                        'type'       => 'object',
     994                                        'properties' => array(
     995                                                'first'  => array(
     996                                                        'type'    => 'string',
     997                                                        'context' => array( 'view', 'edit' ),
     998                                                ),
     999                                                'second' => array(
     1000                                                        'type'    => 'string',
     1001                                                        'context' => array(),
     1002                                                ),
     1003                                        ),
     1004                                ),
     1005                                array(
     1006                                        'first'  => 'a',
     1007                                        'second' => 'b',
     1008                                ),
     1009                                array( 'first' => 'a' ),
     1010                        ),
     1011                        'nested properties'                                         => array(
     1012                                array(
     1013                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1014                                        'type'       => 'object',
     1015                                        'properties' => array(
     1016                                                'parent' => array(
     1017                                                        'type'       => 'object',
     1018                                                        'context'    => array( 'view', 'edit' ),
     1019                                                        'properties' => array(
     1020                                                                'child'  => array(
     1021                                                                        'type'    => 'string',
     1022                                                                        'context' => array( 'view', 'edit' ),
     1023                                                                ),
     1024                                                                'hidden' => array(
     1025                                                                        'type'    => 'string',
     1026                                                                        'context' => array( 'edit' ),
     1027                                                                ),
     1028                                                        ),
     1029                                                ),
     1030                                        ),
     1031                                ),
     1032                                array(
     1033                                        'parent' => array(
     1034                                                'child'  => 'hi',
     1035                                                'hidden' => 'there',
     1036                                        ),
     1037                                ),
     1038                                array( 'parent' => array( 'child' => 'hi' ) ),
     1039                        ),
     1040                        'grand child properties'                                    => array(
     1041                                array(
     1042                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1043                                        'type'       => 'object',
     1044                                        'properties' => array(
     1045                                                'parent' => array(
     1046                                                        'type'       => 'object',
     1047                                                        'context'    => array( 'view', 'edit' ),
     1048                                                        'properties' => array(
     1049                                                                'child' => array(
     1050                                                                        'type'       => 'object',
     1051                                                                        'context'    => array( 'view', 'edit' ),
     1052                                                                        'properties' => array(
     1053                                                                                'grand'  => array(
     1054                                                                                        'type'    => 'string',
     1055                                                                                        'context' => array( 'view', 'edit' ),
     1056                                                                                ),
     1057                                                                                'hidden' => array(
     1058                                                                                        'type'    => 'string',
     1059                                                                                        'context' => array( 'edit' ),
     1060                                                                                ),
     1061                                                                        ),
     1062                                                                ),
     1063                                                        ),
     1064                                                ),
     1065                                        ),
     1066                                ),
     1067                                array(
     1068                                        'parent' => array(
     1069                                                'child' => array(
     1070                                                        'grand' => 'hi',
     1071                                                ),
     1072                                        ),
     1073                                ),
     1074                                array( 'parent' => array( 'child' => array( 'grand' => 'hi' ) ) ),
     1075                        ),
     1076                        'array'                                                     => array(
     1077                                array(
     1078                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1079                                        'type'       => 'object',
     1080                                        'properties' => array(
     1081                                                'arr' => array(
     1082                                                        'type'    => 'array',
     1083                                                        'context' => array( 'view', 'edit' ),
     1084                                                        'items'   => array(
     1085                                                                'type'       => 'object',
     1086                                                                'context'    => array( 'view', 'edit' ),
     1087                                                                'properties' => array(
     1088                                                                        'visible' => array(
     1089                                                                                'type'    => 'string',
     1090                                                                                'context' => array( 'view', 'edit' ),
     1091                                                                        ),
     1092                                                                        'hidden'  => array(
     1093                                                                                'type'    => 'string',
     1094                                                                                'context' => array( 'edit' ),
     1095                                                                        ),
     1096                                                                ),
     1097                                                        ),
     1098                                                ),
     1099                                        ),
     1100                                ),
     1101                                array(
     1102                                        'arr' => array(
     1103                                                array(
     1104                                                        'visible' => 'hi',
     1105                                                        'hidden'  => 'there',
     1106                                                ),
     1107                                        ),
     1108                                ),
     1109                                array( 'arr' => array( array( 'visible' => 'hi' ) ) ),
     1110                        ),
     1111                        'additional properties'                                     => array(
     1112                                array(
     1113                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1114                                        'type'       => 'object',
     1115                                        'properties' => array(
     1116                                                'additional' => array(
     1117                                                        'type'                 => 'object',
     1118                                                        'context'              => array( 'view', 'edit' ),
     1119                                                        'properties'           => array(
     1120                                                                'a' => array(
     1121                                                                        'type'    => 'string',
     1122                                                                        'context' => array( 'view', 'edit' ),
     1123                                                                ),
     1124                                                                'b' => array(
     1125                                                                        'type'    => 'string',
     1126                                                                        'context' => array( 'edit' ),
     1127                                                                ),
     1128                                                        ),
     1129                                                        'additionalProperties' => array(
     1130                                                                'type'    => 'string',
     1131                                                                'context' => array( 'edit' ),
     1132                                                        ),
     1133                                                ),
     1134                                        ),
     1135                                ),
     1136                                array(
     1137                                        'additional' => array(
     1138                                                'a' => '1',
     1139                                                'b' => '2',
     1140                                                'c' => '3',
     1141                                        ),
     1142                                ),
     1143                                array( 'additional' => array( 'a' => '1' ) ),
     1144                        ),
     1145                        'multiple types object'                                     => array(
     1146                                array(
     1147                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1148                                        'type'       => 'object',
     1149                                        'properties' => array(
     1150                                                'multi' => array(
     1151                                                        'type'       => array( 'object', 'string' ),
     1152                                                        'context'    => array( 'view', 'edit' ),
     1153                                                        'properties' => array(
     1154                                                                'a' => array(
     1155                                                                        'type'    => 'string',
     1156                                                                        'context' => array( 'view', 'edit' ),
     1157                                                                ),
     1158                                                                'b' => array(
     1159                                                                        'type'    => 'string',
     1160                                                                        'context' => array( 'edit' ),
     1161                                                                ),
     1162                                                        ),
     1163                                                ),
     1164                                        ),
     1165                                ),
     1166                                array(
     1167                                        'multi' => array(
     1168                                                'a' => '1',
     1169                                                'b' => '2',
     1170                                        ),
     1171                                ),
     1172                                array( 'multi' => array( 'a' => '1' ) ),
     1173                        ),
     1174                        'multiple types array'                                      => array(
     1175                                array(
     1176                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1177                                        'type'       => 'object',
     1178                                        'properties' => array(
     1179                                                'multi' => array(
     1180                                                        'type'    => array( 'array', 'string' ),
     1181                                                        'context' => array( 'view', 'edit' ),
     1182                                                        'items'   => array(
     1183                                                                'type'       => 'object',
     1184                                                                'context'    => array( 'view', 'edit' ),
     1185                                                                'properties' => array(
     1186                                                                        'visible' => array(
     1187                                                                                'type'    => 'string',
     1188                                                                                'context' => array( 'view', 'edit' ),
     1189                                                                        ),
     1190                                                                        'hidden'  => array(
     1191                                                                                'type'    => 'string',
     1192                                                                                'context' => array( 'edit' ),
     1193                                                                        ),
     1194                                                                ),
     1195                                                        ),
     1196                                                ),
     1197                                        ),
     1198                                ),
     1199                                array(
     1200                                        'multi' => array(
     1201                                                array(
     1202                                                        'visible' => '1',
     1203                                                        'hidden'  => '2',
     1204                                                ),
     1205                                        ),
     1206                                ),
     1207                                array( 'multi' => array( array( 'visible' => '1' ) ) ),
     1208                        ),
     1209                        'grand child properties does not traverses missing context' => array(
     1210                                array(
     1211                                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1212                                        'type'       => 'object',
     1213                                        'properties' => array(
     1214                                                'parent' => array(
     1215                                                        'type'       => 'object',
     1216                                                        'context'    => array( 'view', 'edit' ),
     1217                                                        'properties' => array(
     1218                                                                'child' => array(
     1219                                                                        'type'       => 'object',
     1220                                                                        'properties' => array(
     1221                                                                                'grand'  => array(
     1222                                                                                        'type'    => 'string',
     1223                                                                                        'context' => array( 'view', 'edit' ),
     1224                                                                                ),
     1225                                                                                'hidden' => array(
     1226                                                                                        'type'    => 'string',
     1227                                                                                        'context' => array( 'edit' ),
     1228                                                                                ),
     1229                                                                        ),
     1230                                                                ),
     1231                                                        ),
     1232                                                ),
     1233                                        ),
     1234                                ),
     1235                                array(
     1236                                        'parent' => array(
     1237                                                'child' => array(
     1238                                                        'grand'  => 'hi',
     1239                                                        'hidden' => 'there',
     1240                                                ),
     1241                                        ),
     1242                                ),
     1243                                array(
     1244                                        'parent' => array(
     1245                                                'child' => array(
     1246                                                        'grand'  => 'hi',
     1247                                                        'hidden' => 'there',
     1248                                                ),
     1249                                        ),
     1250                                ),
     1251                        ),
     1252                );
     1253        }
    9361254}