Ticket #34659: 34659.3.diff
File 34659.3.diff, 3.2 KB (added by , 8 years ago) |
---|
-
src/wp-includes/rest-api/class-wp-rest-request.php
828 828 continue; 829 829 } 830 830 831 $sanitized_value = call_user_func( $attributes['args'][ $key ]['sanitize_callback'], $value, $this, $key ); 831 if ( isset( $attributes['args'][ $key ]['sanitize_callback_single_arg'] ) && true === $attributes['args'][ $key ]['sanitize_callback_single_arg'] ) { 832 $sanitized_value = call_user_func( $attributes['args'][ $key ]['sanitize_callback'], $value ); 833 } else { 834 $sanitized_value = call_user_func( $attributes['args'][ $key ]['sanitize_callback'], $value, $this, $key ); 835 } 832 836 833 837 if ( is_wp_error( $sanitized_value ) ) { 834 838 $invalid_params[ $key ] = $sanitized_value->get_error_message(); … … 893 897 $param = $this->get_param( $key ); 894 898 895 899 if ( null !== $param && ! empty( $arg['validate_callback'] ) ) { 896 $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key ); 900 if ( isset( $arg['validate_callback_single_arg'] ) && true === $arg['validate_callback_single_arg'] ) { 901 $valid_check = call_user_func( $arg['validate_callback'], $param ); 902 } else { 903 $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key ); 904 } 897 905 898 906 if ( false === $valid_check ) { 899 907 $invalid_params[ $key ] = __( 'Invalid parameter.' ); -
tests/phpunit/tests/rest-api/rest-request.php
342 342 $this->assertEquals( 'rest_invalid_param', $valid->get_error_code() ); 343 343 } 344 344 345 public function test_sanitize_callback_single_arg() { 346 $this->request->set_url_params( array( 347 'someinteger' => 'aaa', 348 ) ); 349 350 $this->request->set_attributes( array( 351 'args' => array( 352 'someinteger' => array( 353 'sanitize_callback' => 'is_numeric', 354 'sanitize_callback_single_arg' => true, 355 ), 356 ) 357 ) ); 358 359 $valid = $this->request->sanitize_params(); 360 $this->assertTrue( $valid ); 361 $this->assertEquals( false, $this->request->get_param( 'someinteger' ), 'Should return false instead of NULL when run through is_numeric' ); 362 } 363 345 364 public function test_has_valid_params_required_flag() { 346 365 $this->request->set_attributes( array( 347 366 'args' => array( … … 380 399 $this->assertTrue( in_array( 'someotherinteger', $data['params'] ) ); 381 400 } 382 401 402 public function test_validate_callback_single_arg() { 403 $this->request->set_url_params( array( 404 'someinteger' => 'aaa', 405 ) ); 406 407 $this->request->set_attributes( array( 408 'args' => array( 409 'someinteger' => array( 410 'validate_callback' => 'is_numeric', 411 'validate_callback_single_arg' => true, 412 ), 413 ) 414 ) ); 415 416 $valid = $this->request->has_valid_params(); 417 $this->assertWPError( $valid, 'Should call is_numeric with one argument and fail validation' ); 418 } 419 420 421 383 422 public function test_has_valid_params_validate_callback() { 384 423 $this->request->set_url_params( array( 385 424 'someinteger' => '123',