Make WordPress Core

Changeset 39048


Ignore:
Timestamp:
10/31/2016 05:44:56 AM (8 years ago)
Author:
pento
Message:

REST API: Allow parameters defined as array to be sent as CSVs.

This allows parameters that are often handled as CSVs to be properly parsed.

Fixes #38586.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r39046 r39048  
    997997    if ( 'array' === $args['type'] ) {
    998998        if ( ! is_array( $value ) ) {
    999             return new WP_Error( 'rest_invalid_param', sprintf( /* translators: 1: parameter, 2: type name */ __( '%1$s is not of type %2$s.' ), $param, 'array' ) );
     999            $value = preg_split( '/[\s,]+/', $value );
    10001000        }
    10011001        foreach ( $value as $index => $v ) {
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r39046 r39048  
    104104        $this->assertWPError( rest_validate_value_from_schema( array( true ), $schema ) );
    105105    }
     106
     107    public function test_type_array_as_csv() {
     108        $schema = array(
     109            'type' => 'array',
     110            'items' => array(
     111                'type' => 'number',
     112            ),
     113        );
     114        $this->assertTrue( rest_validate_value_from_schema( '1', $schema ) );
     115        $this->assertTrue( rest_validate_value_from_schema( '1,2,3', $schema ) );
     116        $this->assertWPError( rest_validate_value_from_schema( 'lol', $schema ) );
     117    }
    106118}
Note: See TracChangeset for help on using the changeset viewer.