WordPress.org

Make WordPress Core

Changeset 31707


Ignore:
Timestamp:
03/10/2015 11:04:12 PM (5 years ago)
Author:
ocean90
Message:

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

props westonruter.
fixes #31428.

Location:
trunk
Files:
2 edited

Legend:

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

    r31705 r31707  
    115115    }
    116116
     117    /**
     118     * The ID for the current blog when the preview() method was called.
     119     *
     120     * @since 4.2.0
     121     * @var int
     122     */
     123    protected $_previewed_blog_id;
     124
     125    /**
     126     * Return true if the current blog is not the same as the previewed blog.
     127     *
     128     * @since 4.2.0
     129     * @return bool|null Returns null if preview() has not been called yet.
     130     */
     131    public function is_current_blog_previewed() {
     132        if ( ! isset( $this->_previewed_blog_id ) ) {
     133            return null;
     134        }
     135        return ( get_current_blog_id() === $this->_previewed_blog_id );
     136    }
     137
     138    /**
     139     * Original non-previewed value stored by the preview method.
     140     *
     141     * @see WP_Customize_Setting::preview()
     142     * @since 4.1.1
     143     * @var mixed
     144     */
    117145    protected $_original_value;
    118146
     
    125153        if ( ! isset( $this->_original_value ) ) {
    126154            $this->_original_value = $this->value();
     155        }
     156        if ( ! isset( $this->_previewed_blog_id ) ) {
     157            $this->_previewed_blog_id = get_current_blog_id();
    127158        }
    128159
     
    170201     * Callback function to filter the theme mods and options.
    171202     *
     203     * If switch_to_blog() was called after the preview() method, and the current
     204     * blog is now not the same blog, then this method does a no-op and returns
     205     * the original value.
     206     *
    172207     * @since 3.4.0
    173208     * @uses WP_Customize_Setting::multidimensional_replace()
     
    177212     */
    178213    public function _preview_filter( $original ) {
     214        if ( ! $this->is_current_blog_previewed() ) {
     215            return $original;
     216        }
     217
    179218        $undefined = new stdClass(); // symbol hack
    180219        $post_value = $this->post_value( $undefined );
  • trunk/tests/phpunit/tests/customize/setting.php

    r31705 r31707  
    366366        $this->assertEquals( $default, $setting->value() );
    367367    }
     368
     369    /**
     370     * Ensure that is_current_blog_previewed returns the expected values.
     371     *
     372     * This is applicable to both single and multisite. This doesn't do switch_to_blog()
     373     *
     374     * @ticket 31428
     375     */
     376    function test_is_current_blog_previewed() {
     377        $type = 'option';
     378        $name = 'blogname';
     379        $post_value = rand_str();
     380        $this->manager->set_post_value( $name, $post_value );
     381        $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
     382        $this->assertNull( $setting->is_current_blog_previewed() );
     383        $setting->preview();
     384        $this->assertTrue( $setting->is_current_blog_previewed() );
     385
     386        $this->assertEquals( $post_value, $setting->value() );
     387        $this->assertEquals( $post_value, get_option( $name ) );
     388    }
     389
     390    /**
     391     * Ensure that previewing a setting is disabled when the current blog is switched.
     392     *
     393     * @ticket 31428
     394     * @group multisite
     395     */
     396    function test_previewing_with_switch_to_blog() {
     397        if ( ! is_multisite() ) {
     398            $this->markTestSkipped( 'Cannot test WP_Customize_Setting::is_current_blog_previewed() with switch_to_blog() if not on multisite.' );
     399        }
     400
     401        $type = 'option';
     402        $name = 'blogdescription';
     403        $post_value = rand_str();
     404        $this->manager->set_post_value( $name, $post_value );
     405        $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type' ) );
     406        $this->assertNull( $setting->is_current_blog_previewed() );
     407        $setting->preview();
     408        $this->assertTrue( $setting->is_current_blog_previewed() );
     409
     410        $blog_id = $this->factory->blog->create();
     411        switch_to_blog( $blog_id );
     412        $this->assertFalse( $setting->is_current_blog_previewed() );
     413        $this->assertNotEquals( $post_value, $setting->value() );
     414        $this->assertNotEquals( $post_value, get_option( $name ) );
     415        restore_current_blog();
     416    }
    368417}
    369418
Note: See TracChangeset for help on using the changeset viewer.