Make WordPress Core


Ignore:
Timestamp:
09/14/2016 03:49:37 PM (8 years ago)
Author:
joehoyle
Message:

REST API: Enable sanitize_callback to return WP_Error.

Give developers the opportunity to reject incoming data without using the validation callback. It also enables us to do sanitization and validation in one function in instances where this could be useful.

Props websupporter, rmccue.
Fixes #37560.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r36678 r38601  
    308308        $this->assertEquals( 123, $this->request->get_param( 'someinteger' ) );
    309309        $this->assertEquals( 0, $this->request->get_param( 'somestring' ) );
     310    }
     311
     312    public function test_sanitize_params_error() {
     313        $this->request->set_url_params( array(
     314            'successparam' => '123',
     315            'failparam'    => '123',
     316        ));
     317        $this->request->set_attributes( array(
     318            'args' => array(
     319                'successparam' => array(
     320                    'sanitize_callback' => 'absint',
     321                ),
     322                'failparam' => array(
     323                    'sanitize_callback' => array( $this, '_return_wp_error_on_validate_callback' ),
     324                ),
     325            ),
     326        ));
     327
     328        $valid = $this->request->sanitize_params();
     329        $this->assertWPError( $valid );
     330        $this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
    310331    }
    311332
Note: See TracChangeset for help on using the changeset viewer.