Make WordPress Core

Ticket #51020: 51020#.2.diff

File 51020#.2.diff, 3.6 KB (added by raubvogel, 6 years ago)

version 2

  • src/wp-includes/rest-api.php

     
    15391539}
    15401540
    15411541/**
     1542 * Get all valid JSON schema properties.
     1543 *
     1544 * @since 5.6.0
     1545 *
     1546 * @return string[] All valid JSON schema properties.
     1547 */
     1548function rest_get_allowed_schema_keywords() {
     1549        return array(
     1550                        'type',
     1551                        'format',
     1552                        'enum',
     1553                        'items',
     1554                        'properties',
     1555                        'additionalProperties',
     1556                        'minimum',
     1557                        'maximum',
     1558                        'exclusiveMinimum',
     1559                        'exclusiveMaximum',
     1560                        'minLength',
     1561                        'maxLength',
     1562                        'pattern',
     1563                        'minItems',
     1564                        'maxItems',
     1565                        'uniqueItems',
     1566        );
     1567}
     1568
     1569/**
    15421570 * Validate a value based on a schema.
    15431571 *
    15441572 * @since 4.7.0
  • src/wp-includes/rest-api/class-wp-rest-server.php

     
    12801280                        }
    12811281                }
    12821282
     1283                $allowed_schema_keywords = rest_get_allowed_schema_keywords();
    12831284                $route = preg_replace( '#\(\?P<(\w+?)>.*?\)#', '{$1}', $route );
    12841285
    12851286                foreach ( $callbacks as $callback ) {
    … …  
    12971298                                $endpoint_data['args'] = array();
    12981299
    12991300                                foreach ( $callback['args'] as $key => $opts ) {
    1300                                         $arg_data = array(
    1301                                                 'required' => ! empty( $opts['required'] ),
    1302                                         );
    1303                                         if ( isset( $opts['default'] ) ) {
    1304                                                 $arg_data['default'] = $opts['default'];
    1305                                         }
    1306                                         if ( isset( $opts['enum'] ) ) {
    1307                                                 $arg_data['enum'] = $opts['enum'];
    1308                                         }
    1309                                         if ( isset( $opts['description'] ) ) {
    1310                                                 $arg_data['description'] = $opts['description'];
    1311                                         }
    1312                                         if ( isset( $opts['type'] ) ) {
    1313                                                 $arg_data['type'] = $opts['type'];
    1314                                         }
    1315                                         if ( isset( $opts['items'] ) ) {
    1316                                                 $arg_data['items'] = $opts['items'];
    1317                                         }
    1318                                         $endpoint_data['args'][ $key ] = $arg_data;
     1301                                        $arg_data = array( 'required' => ! empty( $opts['required'] ) );
     1302
     1303                                        // take valid schema properties only
     1304                                        foreach ( $allowed_schema_keywords as $allowed_schema_keyword )
     1305                                                if ( array_key_exists( $allowed_schema_keyword, $opts ) )
     1306                                                        $arg_data[$allowed_schema_keyword] = $opts[$allowed_schema_keyword];
     1307
     1308                                        $endpoint_data['args'][$key] = $arg_data;
    13191309                                }
    13201310                        }
    13211311
  • src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

     
    629629                $schema                  = $this->get_item_schema();
    630630                $schema_properties       = ! empty( $schema['properties'] ) ? $schema['properties'] : array();
    631631                $endpoint_args           = array();
    632                 $valid_schema_properties = array(
    633                         'type',
    634                         'format',
    635                         'enum',
    636                         'items',
    637                         'properties',
    638                         'additionalProperties',
    639                         'minimum',
    640                         'maximum',
    641                         'exclusiveMinimum',
    642                         'exclusiveMaximum',
    643                         'minLength',
    644                         'maxLength',
    645                         'pattern',
    646                         'minItems',
    647                         'maxItems',
    648                         'uniqueItems',
    649                 );
     632                $allowed_schema_keywords = rest_get_allowed_schema_keywords();
    650633
    651634                foreach ( $schema_properties as $field_id => $params ) {
    652635
    … …  
    672655                                $endpoint_args[ $field_id ]['required'] = true;
    673656                        }
    674657
    675                         foreach ( $valid_schema_properties as $schema_prop ) {
     658                        foreach ($allowed_schema_keywords as $schema_prop ) {
    676659                                if ( isset( $params[ $schema_prop ] ) ) {
    677660                                        $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
    678661                                }