Customize: Improve extensibility of Custom CSS.

* Add `customize_value_custom_css` filter to `WP_Customize_Custom_CSS::value()` method.
* Introduce `customize_update_custom_css_post_content_args` filter in `WP_Customize_Custom_CSS::update()` method.
* Make clear that `wp_get_custom_css()` and `wp_get_custom_css` filter are specifically for obtaining the value to render/display. Eliminate use of `wp_get_custom_css()` when getting the setting value. Use the underlying `post_value` directly when `is_previewed`.
* Move anonymous functions handing JS previewing for `custom_logo`, `custom_css`, and `background` into named functions on the `wp.customize.settingPreviewHandlers` to allow plugins to override/extend preview logic.
* Update `_custom_background_cb` to always print a `style` tag wen in the customizer preview, and update background preview logic to replace existing style element instead of appending a new style to the head so that background changes don't unexpectedly override any Custom CSS in the preview's stylesheet cascade.

Props westonruter, georgestephanis.
See #22058.
Fixes #38672.


git-svn-id: https://develop.svn.wordpress.org/trunk@39209 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter
2016-11-13 02:42:04 +00:00
parent 97ae697d6a
commit c68c1c8ec7
4 changed files with 227 additions and 52 deletions

View File

@@ -167,6 +167,95 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase {
$this->assertEquals( '', wp_get_custom_css( 'twentyten' ) );
}
/**
* Test crud methods on WP_Customize_Custom_CSS_Setting.
*
* @covers WP_Customize_Custom_CSS_Setting::value()
*/
function test_value_filter() {
add_filter( 'customize_value_custom_css', array( $this, 'filter_value' ), 10, 2 );
$this->setting->default = '/*default*/';
$this->assertEquals( '/*default*//*filtered*/', $this->setting->value() );
$this->factory()->post->create( array(
'post_title' => $this->setting->stylesheet,
'post_name' => $this->setting->stylesheet,
'post_content' => '/*custom*/',
'post_status' => 'publish',
'post_type' => 'custom_css',
) );
$this->assertEquals( '/*custom*//*filtered*/', $this->setting->value() );
$this->wp_customize->set_post_value( $this->setting->id, '/*overridden*/' );
$this->setting->preview();
$this->assertEquals( '/*overridden*/', $this->setting->value(), 'Expected value to not be filtered since post value is present.' );
}
/**
* Filter value.
*
* @param string $value Value.
* @param WP_Customize_Setting $setting Setting.
* @return string
*/
function filter_value( $value, $setting ) {
$this->assertInstanceOf( 'WP_Customize_Custom_CSS_Setting', $setting );
$value .= '/*filtered*/';
return $value;
}
/**
* Test update filter on WP_Customize_Custom_CSS_Setting.
*
* @covers WP_Customize_Custom_CSS_Setting::update()
*/
function test_update_filter() {
$original_css = 'body { color:red; }';
$post_id = $this->factory()->post->create( array(
'post_title' => $this->setting->stylesheet,
'post_name' => $this->setting->stylesheet,
'post_content' => $original_css,
'post_status' => 'publish',
'post_type' => 'custom_css',
) );
$overridden_css = 'body { color:green; }';
$this->wp_customize->set_post_value( $this->setting->id, $overridden_css );
$post = get_post( $post_id );
$original_title = $post->post_title;
add_filter( 'customize_update_custom_css_post_content_args', array( $this, 'filter_update_post_content_args' ), 10, 3 );
$this->setting->save();
$post = get_post( $post_id );
$this->assertEquals( $original_title, $post->post_title );
$this->assertContains( $overridden_css, $post->post_content );
$this->assertContains( '/* filtered post_content */', $post->post_content );
$this->assertContains( '/* filtered post_content_filtered */', $post->post_content_filtered );
}
/**
* Filter `customize_update_custom_css_post_content_args`.
*
* @param array $args Post array.
* @param string $css CSS.
* @param WP_Customize_Setting $setting Setting.
* @return array Args.
*/
function filter_update_post_content_args( $args, $css, $setting ) {
$this->assertInternalType( 'array', $args );
$this->assertEqualSets( array( 'post_content', 'post_content_filtered' ), array_keys( $args ) );
$this->assertEquals( $css, $args['post_content'] );
$this->assertEquals( '', $args['post_content_filtered'] );
$this->assertInstanceOf( 'WP_Customize_Custom_CSS_Setting', $setting );
$args['post_content'] .= '/* filtered post_content */';
$args['post_content_filtered'] = '/* filtered post_content_filtered */';
$args['post_title'] = 'Ignored';
return $args;
}
/**
* Tests that validation errors are caught appropriately.
*