WordPress.org

Make WordPress Core

Changeset 38765


Ignore:
Timestamp:
10/09/16 20:07:16 (14 months ago)
Author:
westonruter
Message:

Customize: Ensure customize_validate_{$setting->id} filters apply on input post values for WP_Customize_Setting subclasses that neglect to apply the filter themselves.

Fixes #37638.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r38649 r38765  
    10041004            } 
    10051005            $validity = $setting->validate( $unsanitized_value ); 
     1006            if ( ! is_wp_error( $validity ) ) { 
     1007                /** This filter is documented in wp-includes/class-wp-customize-setting.php */ 
     1008                $late_validity = apply_filters( "customize_validate_{$setting->id}", new WP_Error(), $unsanitized_value, $setting ); 
     1009                if ( ! empty( $late_validity->errors ) ) { 
     1010                    $validity = $late_validity; 
     1011                } 
     1012            } 
    10061013            if ( ! is_wp_error( $validity ) ) { 
    10071014                $value = $setting->sanitize( $unsanitized_value ); 
  • trunk/tests/phpunit/tests/customize/manager.php

    r38624 r38765  
    277277        $this->assertEquals( 'invalid_value_in_validate', $error->get_error_code() ); 
    278278        $this->assertEquals( array( 'source' => 'filter_customize_validate_foo' ), $error->get_error_data() ); 
     279    } 
     280 
     281    /** 
     282     * Test WP_Customize_Manager::validate_setting_values(). 
     283     * 
     284     * @ticket 37638 
     285     * @covers WP_Customize_Manager::validate_setting_values() 
     286     */ 
     287    function test_late_validate_setting_values() { 
     288        $setting = new Test_Setting_Without_Applying_Validate_Filter( $this->manager, 'required' ); 
     289        $this->manager->add_setting( $setting ); 
     290 
     291        $this->assertInstanceOf( 'WP_Error', $setting->validate( '' ) ); 
     292        $setting_validities = $this->manager->validate_setting_values( array( $setting->id => '' ) ); 
     293        $this->assertInstanceOf( 'WP_Error', $setting_validities[ $setting->id ] ); 
     294 
     295        $this->assertTrue( $setting->validate( 'ok' ) ); 
     296        $setting_validities = $this->manager->validate_setting_values( array( $setting->id => 'ok' ) ); 
     297        $this->assertTrue( $setting_validities[ $setting->id ] ); 
     298 
     299        add_filter( "customize_validate_{$setting->id}", array( $this, 'late_validate_length' ), 10, 3 ); 
     300        $this->assertTrue( $setting->validate( 'bad' ) ); 
     301        $setting_validities = $this->manager->validate_setting_values( array( $setting->id => 'bad' ) ); 
     302        $validity = $setting_validities[ $setting->id ]; 
     303        $this->assertInstanceOf( 'WP_Error', $validity ); 
     304        $this->assertEquals( 'minlength', $validity->get_error_code() ); 
     305    } 
     306 
     307    /** 
     308     * Add a length constraint to a setting. 
     309     * 
     310     * Adds minimum-length error code if the length is less than 10. 
     311     * 
     312     * @param WP_Error             $validity Validity. 
     313     * @param mixed                $value    Value. 
     314     * @param WP_Customize_Setting $setting  Setting. 
     315     * @return WP_Error Validity. 
     316     */ 
     317    function late_validate_length( $validity, $value, $setting ) { 
     318        $this->assertInstanceOf( 'WP_Customize_Setting', $setting ); 
     319        if ( strlen( $value ) < 10 ) { 
     320            $validity->add( 'minlength', '' ); 
     321        } 
     322        return $validity; 
    279323    } 
    280324 
     
    10311075    public $custom; 
    10321076} 
     1077 
     1078/** 
     1079 * Class Test_Setting_Without_Applying_Validate_Filter. 
     1080 * 
     1081 * @see Tests_WP_Customize_Manager::test_late_validate_setting_values() 
     1082 */ 
     1083class Test_Setting_Without_Applying_Validate_Filter extends WP_Customize_Setting { 
     1084 
     1085    /** 
     1086     * Validates an input. 
     1087     * 
     1088     * @param mixed $value Value to validate. 
     1089     * @return true|WP_Error True if the input was validated, otherwise WP_Error. 
     1090     */ 
     1091    public function validate( $value ) { 
     1092        if ( empty( $value ) ) { 
     1093            return new WP_Error( 'empty_value', __( 'You must supply a value' ) ); 
     1094        } 
     1095        return true; 
     1096    } 
     1097 
     1098} 
Note: See TracChangeset for help on using the changeset viewer.