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 f43859a18d..cb0f151aed 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
@@ -241,69 +241,18 @@ 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,
- 'post_content_filtered' => '',
- );
+ $r = wp_update_custom_css_post( $css, array(
+ 'stylesheet' => $this->stylesheet,
+ ) );
- /**
- * 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.
- $post = wp_get_custom_css_post( $this->stylesheet );
- if ( $post ) {
- $args['ID'] = $post->ID;
- $post_id = wp_update_post( wp_slash( $args ) );
- } else {
- $post_id = wp_insert_post( wp_slash( $args ) );
- }
- if ( ! $post_id ) {
+ if ( $r instanceof WP_Error ) {
return false;
}
+ $post_id = $r->ID;
// Cache post ID in theme mod for performance to avoid additional DB query.
if ( $this->manager->get_stylesheet() === $this->stylesheet ) {
diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php
index 136ec840d7..cf1bb0fa33 100644
--- a/src/wp-includes/theme.php
+++ b/src/wp-includes/theme.php
@@ -1707,6 +1707,93 @@ function wp_get_custom_css( $stylesheet = '' ) {
return $css;
}
+/**
+ * Update the `custom_css` post for a given theme.
+ *
+ * Inserts a `custom_css` post when one doesn't yet exist.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param string $css CSS, stored in `post_content`.
+ * @param array $args {
+ * Args.
+ *
+ * @type string $preprocessed Pre-processed CSS, stored in `post_content_filtered`. Normally empty string. Optional.
+ * @type string $stylesheet Stylesheet (child theme) to update. Optional, defaults to current theme/stylesheet.
+ * }
+ * @return WP_Post|WP_Error Post on success, error on failure.
+ */
+function wp_update_custom_css_post( $css, $args = array() ) {
+ $args = wp_parse_args( $args, array(
+ 'preprocessed' => '',
+ 'stylesheet' => get_stylesheet(),
+ ) );
+
+ $data = array(
+ 'css' => $css,
+ 'preprocessed' => $args['preprocessed'],
+ );
+
+ /**
+ * Filters the `css` (`post_content`) and `preprocessed` (`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 $data {
+ * Custom CSS data.
+ *
+ * @type string $css CSS stored in `post_content`.
+ * @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. Normally empty string.
+ * }
+ * @param array $args {
+ * The args passed into `wp_update_custom_css_post()` merged with defaults.
+ *
+ * @type string $css The original CSS passed in to be updated.
+ * @type string $preprocessed The original preprocessed CSS passed in to be updated.
+ * @type string $stylesheet The stylesheet (theme) being updated.
+ * }
+ */
+ $data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) );
+
+ $post_data = array(
+ 'post_title' => $args['stylesheet'],
+ 'post_name' => sanitize_title( $args['stylesheet'] ),
+ 'post_type' => 'custom_css',
+ 'post_status' => 'publish',
+ 'post_content' => $data['css'],
+ 'post_content_filtered' => $data['preprocessed'],
+ );
+
+ // Update post if it already exists, otherwise create a new one.
+ $post = wp_get_custom_css_post( $args['stylesheet'] );
+ if ( $post ) {
+ $post_data['ID'] = $post->ID;
+ $r = wp_update_post( wp_slash( $post_data ), true );
+ } else {
+ $r = wp_insert_post( wp_slash( $post_data ), true );
+ }
+
+ if ( $r instanceof WP_Error ) {
+ return $r;
+ }
+ return get_post( $r );
+}
+
/**
* Add callback for custom TinyMCE editor stylesheets.
*
diff --git a/tests/phpunit/tests/customize/custom-css-setting.php b/tests/phpunit/tests/customize/custom-css-setting.php
index cd82a724f1..e787094479 100644
--- a/tests/phpunit/tests/customize/custom-css-setting.php
+++ b/tests/phpunit/tests/customize/custom-css-setting.php
@@ -151,6 +151,7 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase {
$this->assertTrue( false !== $saved );
$this->assertEquals( $updated_css, $this->setting->value() );
$this->assertEquals( $updated_css, wp_get_custom_css( $this->setting->stylesheet ) );
+ $this->assertEquals( $updated_css, get_post( $post_id )->post_content );
$previewed_css = 'body { color: red; }';
$this->wp_customize->set_post_value( $this->setting->id, $previewed_css );
@@ -158,6 +159,30 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase {
$this->assertEquals( $previewed_css, $this->setting->value() );
$this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) );
+ // Make sure that wp_update_custom_css_post() works as expected for updates.
+ $r = wp_update_custom_css_post( 'body { color:red; }', array(
+ 'stylesheet' => $this->setting->stylesheet,
+ 'preprocessed' => "body\n\tcolor:red;",
+ ) );
+ $this->assertInstanceOf( 'WP_Post', $r );
+ $this->assertEquals( $post_id, $r->ID );
+ $this->assertEquals( 'body { color:red; }', get_post( $r )->post_content );
+ $this->assertEquals( "body\n\tcolor:red;", get_post( $r )->post_content_filtered );
+ $r = wp_update_custom_css_post( 'body { content: "\o/"; }' );
+ $this->assertEquals( $this->wp_customize->get_stylesheet(), get_post( $r )->post_name );
+ $this->assertEquals( 'body { content: "\o/"; }', get_post( $r )->post_content );
+ $this->assertEquals( '', get_post( $r )->post_content_filtered );
+
+ // Make sure that wp_update_custom_css_post() works as expected for insertion.
+ $r = wp_update_custom_css_post( 'body { background:black; }', array(
+ 'stylesheet' => 'other',
+ ) );
+ $this->assertInstanceOf( 'WP_Post', $r );
+ $this->assertEquals( 'other', get_post( $r )->post_name );
+ $this->assertEquals( 'body { background:black; }', get_post( $r )->post_content );
+ $this->assertEquals( 'publish', get_post( $r )->post_status );
+
+ // Test deletion.
wp_delete_post( $post_id );
$this->assertNull( wp_get_custom_css_post() );
$this->assertNull( wp_get_custom_css_post( get_stylesheet() ) );
@@ -225,7 +250,7 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase {
$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 );
+ add_filter( 'update_custom_css_data', array( $this, 'filter_update_custom_css_data' ), 10, 3 );
$this->setting->save();
$post = get_post( $post_id );
@@ -238,22 +263,23 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase {
/**
* 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.
+ * @param array $data Data.
+ * @param string $args Args.
+ * @return array Data.
*/
- function filter_update_post_content_args( $args, $css, $setting ) {
+ function filter_update_custom_css_data( $data, $args ) {
+ $this->assertInternalType( 'array', $data );
+ $this->assertEqualSets( array( 'css', 'preprocessed' ), array_keys( $data ) );
+ $this->assertEquals( '', $data['preprocessed'] );
$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 );
+ $this->assertEqualSets( array( 'css', 'preprocessed', 'stylesheet' ), array_keys( $args ) );
+ $this->assertEquals( $args['css'], $data['css'] );
+ $this->assertEquals( $args['preprocessed'], $data['preprocessed'] );
- $args['post_content'] .= '/* filtered post_content */';
- $args['post_content_filtered'] = '/* filtered post_content_filtered */';
- $args['post_title'] = 'Ignored';
- return $args;
+ $data['css'] .= '/* filtered post_content */';
+ $data['preprocessed'] = '/* filtered post_content_filtered */';
+ $data['post_title'] = 'Ignored';
+ return $data;
}
/**