Customizer: Ensure WP_Customize_Setting::update()
returns boolean value.
Adds unit tests for `WP_Customize_Setting::save()` (and `WP_Customize_Setting::update()`), along with the actions `customize_update_{$type}`, and `customize_save_{$id_base}` which they trigger. Fixes #34140. git-svn-id: https://develop.svn.wordpress.org/trunk@34838 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d8c9450179
commit
84e475dd80
@ -304,12 +304,13 @@ class WP_Customize_Setting {
|
|||||||
* @since 3.4.0
|
* @since 3.4.0
|
||||||
*
|
*
|
||||||
* @param mixed $value The value to update.
|
* @param mixed $value The value to update.
|
||||||
* @return mixed The result of saving the value.
|
* @return bool The result of saving the value.
|
||||||
*/
|
*/
|
||||||
protected function update( $value ) {
|
protected function update( $value ) {
|
||||||
switch( $this->type ) {
|
switch ( $this->type ) {
|
||||||
case 'theme_mod' :
|
case 'theme_mod' :
|
||||||
return $this->_update_theme_mod( $value );
|
$this->_update_theme_mod( $value );
|
||||||
|
return true;
|
||||||
|
|
||||||
case 'option' :
|
case 'option' :
|
||||||
return $this->_update_option( $value );
|
return $this->_update_option( $value );
|
||||||
@ -327,7 +328,9 @@ class WP_Customize_Setting {
|
|||||||
* @param mixed $value Value of the setting.
|
* @param mixed $value Value of the setting.
|
||||||
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
* @param WP_Customize_Setting $this WP_Customize_Setting instance.
|
||||||
*/
|
*/
|
||||||
return do_action( 'customize_update_' . $this->type, $value, $this );
|
do_action( "customize_update_{$this->type}", $value, $this );
|
||||||
|
|
||||||
|
return has_action( "customize_update_{$this->type}" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,6 +366,64 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( $default, $setting->value() );
|
$this->assertEquals( $default, $setting->value() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test setting save method for custom type.
|
||||||
|
*
|
||||||
|
* @see WP_Customize_Setting::save()
|
||||||
|
* @see WP_Customize_Setting::update()
|
||||||
|
*/
|
||||||
|
function test_update_custom_type() {
|
||||||
|
$type = 'custom';
|
||||||
|
$name = 'foo';
|
||||||
|
$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
|
||||||
|
$this->manager->add_setting( $setting );
|
||||||
|
add_action( 'customize_update_custom', array( $this, 'handle_customize_update_custom_foo_action' ), 10, 2 );
|
||||||
|
add_action( 'customize_save_foo', array( $this, 'handle_customize_save_custom_foo_action' ), 10, 2 );
|
||||||
|
|
||||||
|
// Try saving before value set.
|
||||||
|
$this->assertTrue( 0 === did_action( 'customize_update_custom' ) );
|
||||||
|
$this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
|
||||||
|
$this->assertFalse( $setting->save() );
|
||||||
|
$this->assertTrue( 0 === did_action( 'customize_update_custom' ) );
|
||||||
|
$this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
|
||||||
|
|
||||||
|
// Try setting post value without user as admin.
|
||||||
|
$this->manager->set_post_value( $setting->id, 'hello world' );
|
||||||
|
$this->assertFalse( $setting->save() );
|
||||||
|
$this->assertTrue( 0 === did_action( 'customize_update_custom' ) );
|
||||||
|
$this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
|
||||||
|
|
||||||
|
// Satisfy all requirements for save to happen.
|
||||||
|
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||||
|
$this->assertTrue( false !== $setting->save() );
|
||||||
|
$this->assertTrue( 1 === did_action( 'customize_update_custom' ) );
|
||||||
|
$this->assertTrue( 1 === did_action( 'customize_save_foo' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check customize_update_custom action.
|
||||||
|
*
|
||||||
|
* @see Tests_WP_Customize_Setting::test_update_custom_type()
|
||||||
|
* @param mixed $value
|
||||||
|
* @param WP_Customize_Setting $setting
|
||||||
|
*/
|
||||||
|
function handle_customize_update_custom_foo_action( $value, $setting = null ) {
|
||||||
|
$this->assertEquals( 'hello world', $value );
|
||||||
|
$this->assertInstanceOf( 'WP_Customize_Setting', $setting );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check customize_save_foo action.
|
||||||
|
*
|
||||||
|
* @see Tests_WP_Customize_Setting::test_update_custom_type()
|
||||||
|
* @param WP_Customize_Setting $setting
|
||||||
|
*/
|
||||||
|
function handle_customize_save_custom_foo_action( $setting ) {
|
||||||
|
$this->assertInstanceOf( 'WP_Customize_Setting', $setting );
|
||||||
|
$this->assertEquals( 'custom', $setting->type );
|
||||||
|
$this->assertEquals( 'foo', $setting->id );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that is_current_blog_previewed returns the expected values.
|
* Ensure that is_current_blog_previewed returns the expected values.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user