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.


git-svn-id: https://develop.svn.wordpress.org/trunk@38765 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2016-10-09 20:07:16 +00:00
parent 2216b8ba37
commit f4779221a3
2 changed files with 73 additions and 0 deletions

View File

@ -1003,6 +1003,13 @@ final class WP_Customize_Manager {
continue;
}
$validity = $setting->validate( $unsanitized_value );
if ( ! is_wp_error( $validity ) ) {
/** This filter is documented in wp-includes/class-wp-customize-setting.php */
$late_validity = apply_filters( "customize_validate_{$setting->id}", new WP_Error(), $unsanitized_value, $setting );
if ( ! empty( $late_validity->errors ) ) {
$validity = $late_validity;
}
}
if ( ! is_wp_error( $validity ) ) {
$value = $setting->sanitize( $unsanitized_value );
if ( is_null( $value ) ) {

View File

@ -278,6 +278,50 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
$this->assertEquals( array( 'source' => 'filter_customize_validate_foo' ), $error->get_error_data() );
}
/**
* Test WP_Customize_Manager::validate_setting_values().
*
* @ticket 37638
* @covers WP_Customize_Manager::validate_setting_values()
*/
function test_late_validate_setting_values() {
$setting = new Test_Setting_Without_Applying_Validate_Filter( $this->manager, 'required' );
$this->manager->add_setting( $setting );
$this->assertInstanceOf( 'WP_Error', $setting->validate( '' ) );
$setting_validities = $this->manager->validate_setting_values( array( $setting->id => '' ) );
$this->assertInstanceOf( 'WP_Error', $setting_validities[ $setting->id ] );
$this->assertTrue( $setting->validate( 'ok' ) );
$setting_validities = $this->manager->validate_setting_values( array( $setting->id => 'ok' ) );
$this->assertTrue( $setting_validities[ $setting->id ] );
add_filter( "customize_validate_{$setting->id}", array( $this, 'late_validate_length' ), 10, 3 );
$this->assertTrue( $setting->validate( 'bad' ) );
$setting_validities = $this->manager->validate_setting_values( array( $setting->id => 'bad' ) );
$validity = $setting_validities[ $setting->id ];
$this->assertInstanceOf( 'WP_Error', $validity );
$this->assertEquals( 'minlength', $validity->get_error_code() );
}
/**
* Add a length constraint to a setting.
*
* Adds minimum-length error code if the length is less than 10.
*
* @param WP_Error $validity Validity.
* @param mixed $value Value.
* @param WP_Customize_Setting $setting Setting.
* @return WP_Error Validity.
*/
function late_validate_length( $validity, $value, $setting ) {
$this->assertInstanceOf( 'WP_Customize_Setting', $setting );
if ( strlen( $value ) < 10 ) {
$validity->add( 'minlength', '' );
}
return $validity;
}
/**
* Test the WP_Customize_Manager::validate_setting_values() method to make sure that the validation and sanitization are done in the right order.
*
@ -1030,3 +1074,25 @@ class Test_Dynamic_Customize_Setting extends WP_Customize_Setting {
public $type = 'dynamic';
public $custom;
}
/**
* Class Test_Setting_Without_Applying_Validate_Filter.
*
* @see Tests_WP_Customize_Manager::test_late_validate_setting_values()
*/
class Test_Setting_Without_Applying_Validate_Filter extends WP_Customize_Setting {
/**
* Validates an input.
*
* @param mixed $value Value to validate.
* @return true|WP_Error True if the input was validated, otherwise WP_Error.
*/
public function validate( $value ) {
if ( empty( $value ) ) {
return new WP_Error( 'empty_value', __( 'You must supply a value' ) );
}
return true;
}
}