<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39350] trunk: Customize: Refactor logic for updating `custom_css` posts by introducing `wp_update_custom_css_post()` function and renaming update filter.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39350">39350</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39350","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-23 17:33:21 +0000 (Wed, 23 Nov 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Customize: Refactor logic for updating `custom_css` posts by introducing `wp_update_custom_css_post()` function and renaming update filter.

* Moves logic from `WP_Customize_Custom_CSS_Setting::update()` into a re-usable `wp_update_custom_css_post()` function, useful for future REST API endpoint, WP-CLI command, or plugin migrations.
* Renames `customize_update_custom_css_post_content_args` filter to `update_custom_css_data` and improves the naming of the parameters. Instead of passing `post_content` and `post_content_filtered` the filtered array now contains `css` and `preprocessed` respectively. 
* The second context param for the `update_custom_css_data` filter is now an array of the original args passed to `wp_update_custom_css_post()` and there is now no more `$setting` arg since it isn't necessarily being called in the customizer context.

Props westonruter, georgestephanis.
See <a href="https://core.trac.wordpress.org/ticket/35395">#35395</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/38672">#38672</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescustomizeclasswpcustomizecustomcsssettingphp">trunk/src/wp-includes/customize/class-wp-customize-custom-css-setting.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
<li><a href="#trunktestsphpunittestscustomizecustomcsssettingphp">trunk/tests/phpunit/tests/customize/custom-css-setting.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescustomizeclasswpcustomizecustomcsssettingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/customize/class-wp-customize-custom-css-setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/customize/class-wp-customize-custom-css-setting.php 2016-11-23 16:14:08 UTC (rev 39349)
+++ trunk/src/wp-includes/customize/class-wp-customize-custom-css-setting.php   2016-11-23 17:33:21 UTC (rev 39350)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -241,69 +241,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return int|false The post ID or false if the value could not be saved.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function update( $css ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $setting = $this;
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( empty( $css ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $css = '';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args = array(
-                       'post_content' => $css,
-                       'post_content_filtered' => '',
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $r = wp_update_custom_css_post( $css, array(
+                       'stylesheet' => $this->stylesheet,
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                /**
-                * 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:
-                *
-                * <code>
-                * 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 );
-                * </code>
-                *
-                * @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 ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $r instanceof WP_Error ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return false;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $post_id = $r->ID;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Cache post ID in theme mod for performance to avoid additional DB query.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $this->manager->get_stylesheet() === $this->stylesheet ) {
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme.php   2016-11-23 16:14:08 UTC (rev 39349)
+++ trunk/src/wp-includes/theme.php     2016-11-23 17:33:21 UTC (rev 39350)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1708,6 +1708,93 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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:
+        *
+        * <code>
+        * 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 );
+        * </code>
+        *
+        * @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 );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Add callback for custom TinyMCE editor stylesheets.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * The parameter $stylesheet is the name of the stylesheet, relative to
</span></span></pre></div>
<a id="trunktestsphpunittestscustomizecustomcsssettingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/custom-css-setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/custom-css-setting.php        2016-11-23 16:14:08 UTC (rev 39349)
+++ trunk/tests/phpunit/tests/customize/custom-css-setting.php  2016-11-23 17:33:21 UTC (rev 39350)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -151,6 +151,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( false !== $saved );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $updated_css, $this->setting->value() );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $updated_css, wp_get_custom_css( $this->setting->stylesheet ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertEquals( $updated_css, get_post( $post_id )->post_content );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $previewed_css = 'body { color: red; }';
</span><span class="cx" style="display: block; padding: 0 10px">                $this->wp_customize->set_post_value( $this->setting->id, $previewed_css );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -158,6 +159,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $previewed_css, $this->setting->value() );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // 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.
</ins><span class="cx" style="display: block; padding: 0 10px">                 wp_delete_post( $post_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( wp_get_custom_css_post() );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( wp_get_custom_css_post( get_stylesheet() ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -225,7 +250,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $post = get_post( $post_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $original_title = $post->post_title;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_filter( 'customize_update_custom_css_post_content_args', array( $this, 'filter_update_post_content_args' ), 10, 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_filter( 'update_custom_css_data', array( $this, 'filter_update_custom_css_data' ), 10, 3 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->setting->save();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $post = get_post( $post_id );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -238,22 +263,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filter `customize_update_custom_css_post_content_args`.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array                $args    Post array.
-        * @param string               $css     CSS.
-        * @param WP_Customize_Setting $setting Setting.
-        * @return array Args.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array  $data Data.
+        * @param string $args Args.
+        * @return array Data.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function filter_update_post_content_args( $args, $css, $setting ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function filter_update_custom_css_data( $data, $args ) {
+               $this->assertInternalType( 'array', $data );
+               $this->assertEqualSets( array( 'css', 'preprocessed' ), array_keys( $data ) );
+               $this->assertEquals( '', $data['preprocessed'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertInternalType( 'array', $args );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $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 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEqualSets( array( 'css', 'preprocessed', 'stylesheet' ), array_keys( $args ) );
+               $this->assertEquals( $args['css'], $data['css'] );
+               $this->assertEquals( $args['preprocessed'], $data['preprocessed'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args['post_content'] .= '/* filtered post_content */';
-               $args['post_content_filtered'] = '/* filtered post_content_filtered */';
-               $args['post_title'] = 'Ignored';
-               return $args;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $data['css'] .= '/* filtered post_content */';
+               $data['preprocessed'] = '/* filtered post_content_filtered */';
+               $data['post_title'] = 'Ignored';
+               return $data;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre>
</div>
</div>

</body>
</html>