diff --git a/src/wp-includes/customize/class-wp-customize-custom-css-setting.php b/src/wp-includes/customize/class-wp-customize-custom-css-setting.php
index ba4a58cf01..eb9379e425 100644
--- a/src/wp-includes/customize/class-wp-customize-custom-css-setting.php
+++ b/src/wp-includes/customize/class-wp-customize-custom-css-setting.php
@@ -100,10 +100,13 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
}
/**
- * Filter wp_get_custom_css for applying customized value to return value.
+ * Filter `wp_get_custom_css` for applying the customized value.
+ *
+ * This is used in the preview when `wp_get_custom_css()` is called for rendering the styles.
*
* @since 4.7.0
* @access private
+ * @see wp_get_custom_css()
*
* @param string $css Original CSS.
* @param string $stylesheet Current stylesheet.
@@ -120,18 +123,31 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
}
/**
- * Fetch the value of the setting.
+ * Fetch the value of the setting. Will return the previewed value when `preview()` is called.
*
* @since 4.7.0
* @access public
+ * @see WP_Customize_Setting::value()
*
* @return string
*/
public function value() {
- $value = wp_get_custom_css( $this->stylesheet );
+ $id_base = $this->id_data['base'];
+ if ( $this->is_previewed && null !== $this->post_value( null ) ) {
+ return $this->post_value();
+ }
+ $value = '';
+ $post = wp_get_custom_css_post( $this->stylesheet );
+ if ( $post ) {
+ $value = $post->post_content;
+ }
if ( empty( $value ) ) {
$value = $this->default;
}
+
+ /** This filter is documented in wp-includes/class-wp-customize-setting.php */
+ $value = apply_filters( "customize_value_{$id_base}", $value, $this );
+
return $value;
}
@@ -226,13 +242,56 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
* @return int|false The post ID or false if the value could not be saved.
*/
public function update( $css ) {
+ $setting = $this;
+
+ if ( empty( $css ) ) {
+ $css = '';
+ }
$args = array(
- 'post_content' => $css ? $css : '',
- 'post_title' => $this->stylesheet,
- 'post_name' => sanitize_title( $this->stylesheet ),
- 'post_type' => 'custom_css',
- 'post_status' => 'publish',
+ 'post_content' => $css,
+ 'post_content_filtered' => '',
+ );
+
+ /**
+ * Filters the `post_content` and `post_content_filtered` args for a `custom_css` post being updated.
+ *
+ * This filter can be used by plugin that offer CSS pre-processors, to store the original
+ * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
+ * When used in this way, the `post_content_filtered` should be supplied as the setting value
+ * instead of `post_content` via a the `customize_value_custom_css` filter, for example:
+ *
+ *
+ * add_filter( 'customize_value_custom_css', function( $value, $setting ) {
+ * $post = wp_get_custom_css_post( $setting->stylesheet );
+ * if ( $post && ! empty( $post->post_content_filtered ) ) {
+ * $css = $post->post_content_filtered;
+ * }
+ * return $css;
+ * }, 10, 2 );
+ *
+ *
+ * @since 4.7.0
+ * @param array $args {
+ * Content post args (unslashed) for `wp_update_post()`/`wp_insert_post()`.
+ *
+ * @type string $post_content CSS.
+ * @type string $post_content_filtered Pre-processed CSS. Normally empty string.
+ * }
+ * @param string $css Original CSS being updated.
+ * @param WP_Customize_Custom_CSS_Setting $setting Custom CSS Setting.
+ */
+ $args = apply_filters( 'customize_update_custom_css_post_content_args', $args, $css, $setting );
+ $args = wp_array_slice_assoc( $args, array( 'post_content', 'post_content_filtered' ) );
+
+ $args = array_merge(
+ $args,
+ array(
+ 'post_title' => $this->stylesheet,
+ 'post_name' => sanitize_title( $this->stylesheet ),
+ 'post_type' => 'custom_css',
+ 'post_status' => 'publish',
+ )
);
// Update post if it already exists, otherwise create a new one.
diff --git a/src/wp-includes/js/customize-preview.js b/src/wp-includes/js/customize-preview.js
index be24e414bf..9a3944f0dc 100644
--- a/src/wp-includes/js/customize-preview.js
+++ b/src/wp-includes/js/customize-preview.js
@@ -594,6 +594,63 @@
};
} )();
+ api.settingPreviewHandlers = {
+
+ /**
+ * Preview changes to custom logo.
+ *
+ * @param {number} attachmentId Attachment ID for custom logo.
+ * @returns {void}
+ */
+ custom_logo: function( attachmentId ) {
+ $( 'body' ).toggleClass( 'wp-custom-logo', !! attachmentId );
+ },
+
+ /**
+ * Preview changes to custom css.
+ *
+ * @param {string} value Custom CSS..
+ * @returns {void}
+ */
+ custom_css: function( value ) {
+ $( '#wp-custom-css' ).text( value );
+ },
+
+ /**
+ * Preview changes to any of the background settings.
+ *
+ * @returns {void}
+ */
+ background: function() {
+ var css = '', settings = {};
+
+ _.each( ['color', 'image', 'preset', 'position_x', 'position_y', 'size', 'repeat', 'attachment'], function( prop ) {
+ settings[ prop ] = api( 'background_' + prop );
+ } );
+
+ /*
+ * The body will support custom backgrounds if either the color or image are set.
+ *
+ * See get_body_class() in /wp-includes/post-template.php
+ */
+ $( document.body ).toggleClass( 'custom-background', !! ( settings.color() || settings.image() ) );
+
+ if ( settings.color() ) {
+ css += 'background-color: ' + settings.color() + ';';
+ }
+
+ if ( settings.image() ) {
+ css += 'background-image: url("' + settings.image() + '");';
+ css += 'background-size: ' + settings.size() + ';';
+ css += 'background-position: ' + settings.position_x() + ' ' + settings.position_y() + ';';
+ css += 'background-repeat: ' + settings.repeat() + ';';
+ css += 'background-attachment: ' + settings.attachment() + ';';
+ }
+
+ $( '#custom-background-css' ).text( 'body.custom-background { ' + css + ' }' );
+ }
+ };
+
$( function() {
var bg, setValue;
@@ -759,39 +816,9 @@
return 'background_' + prop;
} );
- api.when.apply( api, bg ).done( function( color, image, preset, positionX, positionY, size, repeat, attachment ) {
- var body = $(document.body),
- head = $('head'),
- style = $('#custom-background-css'),
- update;
-
- update = function() {
- var css = '';
-
- // The body will support custom backgrounds if either
- // the color or image are set.
- //
- // See get_body_class() in /wp-includes/post-template.php
- body.toggleClass( 'custom-background', !! ( color() || image() ) );
-
- if ( color() )
- css += 'background-color: ' + color() + ';';
-
- if ( image() ) {
- css += 'background-image: url("' + image() + '");';
- css += 'background-size: ' + size() + ';';
- css += 'background-position: ' + positionX() + ' ' + positionY() + ';';
- css += 'background-repeat: ' + repeat() + ';';
- css += 'background-attachment: ' + attachment() + ';';
- }
-
- // Refresh the stylesheet by removing and recreating it.
- style.remove();
- style = $('').appendTo( head );
- };
-
+ api.when.apply( api, bg ).done( function() {
$.each( arguments, function() {
- this.bind( update );
+ this.bind( api.settingPreviewHandlers.background );
});
});
@@ -802,17 +829,13 @@
*
* @since 4.5.0
*/
- api( 'custom_logo', function( setting ) {
- $( 'body' ).toggleClass( 'wp-custom-logo', !! setting.get() );
- setting.bind( function( attachmentId ) {
- $( 'body' ).toggleClass( 'wp-custom-logo', !! attachmentId );
- });
- });
+ api( 'custom_logo', function ( setting ) {
+ api.settingPreviewHandlers.custom_logo.call( setting, setting.get() );
+ setting.bind( api.settingPreviewHandlers.custom_logo );
+ } );
- api( 'custom_css[' + api.settings.theme.stylesheet + ']', function( value ) {
- value.bind( function( to ) {
- $( '#wp-custom-css' ).text( to );
- } );
+ api( 'custom_css[' + api.settings.theme.stylesheet + ']', function( setting ) {
+ setting.bind( api.settingPreviewHandlers.custom_css );
} );
api.trigger( 'preview-ready' );
diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php
index baad12f52d..bf1c55cd85 100644
--- a/src/wp-includes/theme.php
+++ b/src/wp-includes/theme.php
@@ -1500,8 +1500,12 @@ function _custom_background_cb() {
$color = false;
}
- if ( ! $background && ! $color )
+ if ( ! $background && ! $color ) {
+ if ( is_customize_preview() ) {
+ echo '';
+ }
return;
+ }
$style = $color ? "background-color: #$color;" : '';
@@ -1621,7 +1625,7 @@ function wp_get_custom_css_post( $stylesheet = '' ) {
}
/**
- * Fetch the saved Custom CSS content.
+ * Fetch the saved Custom CSS content for rendering.
*
* @since 4.7.0
* @access public
diff --git a/tests/phpunit/tests/customize/custom-css-setting.php b/tests/phpunit/tests/customize/custom-css-setting.php
index 63c7c814c2..cd82a724f1 100644
--- a/tests/phpunit/tests/customize/custom-css-setting.php
+++ b/tests/phpunit/tests/customize/custom-css-setting.php
@@ -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.
*