Make WordPress Core

Changeset 54131


Ignore:
Timestamp:
09/11/2022 11:28:39 PM (2 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add support for settings to specify their own additionalProperties.

This switches the Settings Controller to use rest_default_additional_properties_to_false and deprecates its own method.

Props anna.bansaghi.
Fixes #56493.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

    r51000 r54131  
    259259            }
    260260
    261             $rest_args['schema'] = $this->set_additional_properties_to_false( $rest_args['schema'] );
     261            $rest_args['schema'] = rest_default_additional_properties_to_false( $rest_args['schema'] );
    262262
    263263            $rest_options[ $rest_args['name'] ] = $rest_args;
     
    323323
    324324    /**
    325      * Recursively add additionalProperties = false to all objects in a schema.
    326      *
    327      * This is need to restrict properties of objects in settings values to only
     325     * Recursively add additionalProperties = false to all objects in a schema
     326     * if no additionalProperties setting is specified.
     327     *
     328     * This is needed to restrict properties of objects in settings values to only
    328329     * registered items, as the REST API will allow additional properties by
    329330     * default.
    330331     *
    331332     * @since 4.9.0
     333     * @deprecated 6.1.0 Use {@see rest_default_additional_properties_to_false()} instead.
    332334     *
    333335     * @param array $schema The schema array.
     
    335337     */
    336338    protected function set_additional_properties_to_false( $schema ) {
    337         switch ( $schema['type'] ) {
    338             case 'object':
    339                 foreach ( $schema['properties'] as $key => $child_schema ) {
    340                     $schema['properties'][ $key ] = $this->set_additional_properties_to_false( $child_schema );
    341                 }
    342 
    343                 $schema['additionalProperties'] = false;
    344                 break;
    345             case 'array':
    346                 $schema['items'] = $this->set_additional_properties_to_false( $schema['items'] );
    347                 break;
    348         }
    349 
    350         return $schema;
     339        _deprecated_function( __METHOD__, '6.1.0', 'rest_default_additional_properties_to_false()' );
     340
     341        return rest_default_additional_properties_to_false( $schema );
    351342    }
    352343}
  • trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php

    r54058 r54131  
    738738        );
    739739    }
     740
     741    /**
     742     * @ticket 56493
     743     */
     744    public function test_register_setting_with_custom_additional_properties_value() {
     745        wp_set_current_user( self::$administrator );
     746
     747        register_setting(
     748            'somegroup',
     749            'mycustomsetting',
     750            array(
     751                'type'         => 'object',
     752                'show_in_rest' => array(
     753                    'schema' => array(
     754                        'type'                 => 'object',
     755                        'properties'           => array(
     756                            'test1' => array(
     757                                'type' => 'string',
     758                            ),
     759                        ),
     760                        'additionalProperties' => array(
     761                            'type' => 'integer',
     762                        ),
     763                    ),
     764                ),
     765            )
     766        );
     767
     768        $data    = array(
     769            'mycustomsetting' => array(
     770                'test1' => 'my-string',
     771                'test2' => '2',
     772                'test3' => 3,
     773            ),
     774        );
     775        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
     776        $request->add_header( 'content-type', 'application/json' );
     777        $request->set_body( wp_json_encode( $data ) );
     778
     779        $response = rest_do_request( $request );
     780
     781        $this->assertSame( 200, $response->get_status() );
     782        $this->assertSame( 'my-string', $response->data['mycustomsetting']['test1'] );
     783        $this->assertSame( 2, $response->data['mycustomsetting']['test2'] );
     784        $this->assertSame( 3, $response->data['mycustomsetting']['test3'] );
     785    }
    740786}
Note: See TracChangeset for help on using the changeset viewer.