Changeset 31707
- Timestamp:
- 03/10/2015 11:04:12 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-customize-setting.php
r31705 r31707 115 115 } 116 116 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 */ 117 145 protected $_original_value; 118 146 … … 125 153 if ( ! isset( $this->_original_value ) ) { 126 154 $this->_original_value = $this->value(); 155 } 156 if ( ! isset( $this->_previewed_blog_id ) ) { 157 $this->_previewed_blog_id = get_current_blog_id(); 127 158 } 128 159 … … 170 201 * Callback function to filter the theme mods and options. 171 202 * 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 * 172 207 * @since 3.4.0 173 208 * @uses WP_Customize_Setting::multidimensional_replace() … … 177 212 */ 178 213 public function _preview_filter( $original ) { 214 if ( ! $this->is_current_blog_previewed() ) { 215 return $original; 216 } 217 179 218 $undefined = new stdClass(); // symbol hack 180 219 $post_value = $this->post_value( $undefined ); -
trunk/tests/phpunit/tests/customize/setting.php
r31705 r31707 366 366 $this->assertEquals( $default, $setting->value() ); 367 367 } 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 } 368 417 } 369 418
Note: See TracChangeset
for help on using the changeset viewer.