Changeset 38601
- Timestamp:
- 09/14/2016 03:49:37 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/class-wp-rest-request.php
r37674 r38601 781 781 * @access public 782 782 * 783 * @return true| null True if there are no parameters to sanitize, null otherwise.783 * @return true|WP_Error True if parameters were sanitized, WP_Error if an error occurred during sanitization. 784 784 */ 785 785 public function sanitize_params() { 786 787 786 $attributes = $this->get_attributes(); 788 787 … … 793 792 794 793 $order = $this->get_parameter_order(); 794 795 $invalid_params = array(); 795 796 796 797 foreach ( $order as $type ) { … … 800 801 foreach ( $this->params[ $type ] as $key => $value ) { 801 802 // Check if this param has a sanitize_callback added. 802 if ( isset( $attributes['args'][ $key ] ) && ! empty( $attributes['args'][ $key ]['sanitize_callback'] ) ) { 803 $this->params[ $type ][ $key ] = call_user_func( $attributes['args'][ $key ]['sanitize_callback'], $value, $this, $key ); 803 if ( ! isset( $attributes['args'][ $key ] ) || empty( $attributes['args'][ $key ]['sanitize_callback'] ) ) { 804 continue; 805 } 806 807 $sanitized_value = call_user_func( $attributes['args'][ $key ]['sanitize_callback'], $value, $this, $key ); 808 809 if ( is_wp_error( $sanitized_value ) ) { 810 $invalid_params[ $key ] = $sanitized_value->get_error_message(); 811 } else { 812 $this->params[ $type ][ $key ] = $sanitized_value; 804 813 } 805 814 } 806 815 } 807 return null; 816 817 if ( $invalid_params ) { 818 return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), array( 'status' => 400, 'params' => $invalid_params ) ); 819 } 820 821 return true; 808 822 } 809 823 … … 818 832 */ 819 833 public function has_valid_params() { 820 821 834 $attributes = $this->get_attributes(); 822 835 $required = array(); -
trunk/src/wp-includes/rest-api/class-wp-rest-server.php
r38037 r38601 867 867 if ( is_wp_error( $check_required ) ) { 868 868 $response = $check_required; 869 } else { 870 $check_sanitized = $request->sanitize_params(); 871 if ( is_wp_error( $check_sanitized ) ) { 872 $response = $check_sanitized; 873 } 869 874 } 870 871 $request->sanitize_params();872 875 } 873 876 -
trunk/tests/phpunit/tests/rest-api/rest-request.php
r36678 r38601 308 308 $this->assertEquals( 123, $this->request->get_param( 'someinteger' ) ); 309 309 $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() ); 310 331 } 311 332
Note: See TracChangeset
for help on using the changeset viewer.