Customizer: Return the original value when filtering theme mods/options and the current blog has changed.

props westonruter.
fixes #31428.

git-svn-id: https://develop.svn.wordpress.org/trunk@31707 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2015-03-10 23:04:12 +00:00
parent 72fcd6e2ce
commit 32a124a884
2 changed files with 88 additions and 0 deletions

View File

@ -114,6 +114,34 @@ class WP_Customize_Setting {
add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
}
/**
* The ID for the current blog when the preview() method was called.
*
* @since 4.2.0
* @var int
*/
protected $_previewed_blog_id;
/**
* Return true if the current blog is not the same as the previewed blog.
*
* @since 4.2.0
* @return bool|null Returns null if preview() has not been called yet.
*/
public function is_current_blog_previewed() {
if ( ! isset( $this->_previewed_blog_id ) ) {
return null;
}
return ( get_current_blog_id() === $this->_previewed_blog_id );
}
/**
* Original non-previewed value stored by the preview method.
*
* @see WP_Customize_Setting::preview()
* @since 4.1.1
* @var mixed
*/
protected $_original_value;
/**
@ -125,6 +153,9 @@ class WP_Customize_Setting {
if ( ! isset( $this->_original_value ) ) {
$this->_original_value = $this->value();
}
if ( ! isset( $this->_previewed_blog_id ) ) {
$this->_previewed_blog_id = get_current_blog_id();
}
switch( $this->type ) {
case 'theme_mod' :
@ -169,6 +200,10 @@ class WP_Customize_Setting {
/**
* Callback function to filter the theme mods and options.
*
* If switch_to_blog() was called after the preview() method, and the current
* blog is now not the same blog, then this method does a no-op and returns
* the original value.
*
* @since 3.4.0
* @uses WP_Customize_Setting::multidimensional_replace()
*
@ -176,6 +211,10 @@ class WP_Customize_Setting {
* @return mixed New or old value.
*/
public function _preview_filter( $original ) {
if ( ! $this->is_current_blog_previewed() ) {
return $original;
}
$undefined = new stdClass(); // symbol hack
$post_value = $this->post_value( $undefined );
if ( $undefined === $post_value ) {

View File

@ -365,5 +365,54 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
$this->assertEquals( $default, get_option( $name, $this->undefined ), sprintf( 'Expected get_option(%s) to return setting default: %s.', $name, $default ) );
$this->assertEquals( $default, $setting->value() );
}
/**
* Ensure that is_current_blog_previewed returns the expected values.
*
* This is applicable to both single and multisite. This doesn't do switch_to_blog()
*
* @ticket 31428
*/
function test_is_current_blog_previewed() {
$type = 'option';
$name = 'blogname';
$post_value = rand_str();
$this->manager->set_post_value( $name, $post_value );
$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
$this->assertNull( $setting->is_current_blog_previewed() );
$setting->preview();
$this->assertTrue( $setting->is_current_blog_previewed() );
$this->assertEquals( $post_value, $setting->value() );
$this->assertEquals( $post_value, get_option( $name ) );
}
/**
* Ensure that previewing a setting is disabled when the current blog is switched.
*
* @ticket 31428
* @group multisite
*/
function test_previewing_with_switch_to_blog() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Cannot test WP_Customize_Setting::is_current_blog_previewed() with switch_to_blog() if not on multisite.' );
}
$type = 'option';
$name = 'blogdescription';
$post_value = rand_str();
$this->manager->set_post_value( $name, $post_value );
$setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
$this->assertNull( $setting->is_current_blog_previewed() );
$setting->preview();
$this->assertTrue( $setting->is_current_blog_previewed() );
$blog_id = $this->factory->blog->create();
switch_to_blog( $blog_id );
$this->assertFalse( $setting->is_current_blog_previewed() );
$this->assertNotEquals( $post_value, $setting->value() );
$this->assertNotEquals( $post_value, get_option( $name ) );
restore_current_blog();
}
}