<!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>[39185] trunk: Customize: Split out `custom_css` query logic from `wp_get_custom_css()` into a re-usable `wp_get_custom_css_post()` function to also be used when updating.</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/39185">39185</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/39185","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-09 20:42:22 +0000 (Wed, 09 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: Split out `custom_css` query logic from `wp_get_custom_css()` into a re-usable `wp_get_custom_css_post()` function to also be used when updating.

Props georgestephanis, westonruter.
See <a href="https://core.trac.wordpress.org/ticket/38672">#38672</a>, <a href="https://core.trac.wordpress.org/ticket/35395">#35395</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-09 16:54:51 UTC (rev 39184)
+++ trunk/src/wp-includes/customize/class-wp-customize-custom-css-setting.php   2016-11-09 20:42:22 UTC (rev 39185)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -236,20 +236,9 @@
</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">                // Update post if it already exists, otherwise create a new one.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $post_id = null;
-               $query = new WP_Query( array(
-                       'post_type' => 'custom_css',
-                       'post_status' => get_post_stati(),
-                       'name' => sanitize_title( $this->stylesheet ),
-                       'number' => 1,
-                       'no_found_rows' => true,
-                       'cache_results' => true,
-                       'update_post_meta_cache' => false,
-                       'update_term_meta_cache' => false,
-                       'suppress_filters' => true,
-               ) );
-               if ( ! empty( $query->post ) ) {
-                       $args['ID'] = $query->post->ID;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $post = wp_get_custom_css_post( $this->stylesheet );
+               if ( $post ) {
+                       $args['ID'] = $post->ID;
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_id = wp_update_post( wp_slash( $args ) );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><span class="cx" style="display: block; padding: 0 10px">                        $post_id = wp_insert_post( wp_slash( $args ) );
</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-09 16:54:51 UTC (rev 39184)
+++ trunk/src/wp-includes/theme.php     2016-11-09 20:42:22 UTC (rev 39185)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1574,32 +1574,26 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Fetch the saved Custom CSS content.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Fetch the `custom_css` post for a given theme.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Gets the content of a Custom CSS post that matches the
- * current theme.
- *
</del><span class="cx" style="display: block; padding: 0 10px">  * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @access public
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
- * @return string The Custom CSS Post content.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_Post|null The custom_css post or null if none exists.
</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 wp_get_custom_css( $stylesheet = '' ) {
-       $css = '';
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_get_custom_css_post( $stylesheet = '' ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( empty( $stylesheet ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $stylesheet = get_stylesheet();
</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">        $custom_css_query_vars = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'post_type' => 'custom_css',
-               'post_status' => get_post_stati(),
-               'name' => sanitize_title( $stylesheet ),
-               'number' => 1,
-               'no_found_rows' => true,
-               'cache_results' => true,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'post_type'              => 'custom_css',
+               'post_status'            => get_post_stati(),
+               'name'                   => sanitize_title( $stylesheet ),
+               'number'                 => 1,
+               'no_found_rows'          => true,
+               'cache_results'          => true,
</ins><span class="cx" style="display: block; padding: 0 10px">                 'update_post_meta_cache' => false,
</span><span class="cx" style="display: block; padding: 0 10px">                'update_term_meta_cache' => false,
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1607,10 +1601,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $post = null;
</span><span class="cx" style="display: block; padding: 0 10px">        if ( get_stylesheet() === $stylesheet ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $post_id = get_theme_mod( 'custom_css_post_id' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $post_id ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $post_id || ! get_post( $post_id ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $query = new WP_Query( $custom_css_query_vars );
</span><span class="cx" style="display: block; padding: 0 10px">                        $post = $query->post;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px">                         /*
</span><span class="cx" style="display: block; padding: 0 10px">                         * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update().
</span><span class="cx" style="display: block; padding: 0 10px">                         * @todo This should get cleared if a custom_css post is added/removed.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1624,6 +1617,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $post = $query->post;
</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">+        return $post;
+}
+
+/**
+ * Fetch the saved Custom CSS content.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
+ * @return string The Custom CSS Post content.
+ */
+function wp_get_custom_css( $stylesheet = '' ) {
+       $css = '';
+
+       if ( empty( $stylesheet ) ) {
+               $stylesheet = get_stylesheet();
+       }
+
+       $post = wp_get_custom_css_post( $stylesheet );
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( $post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $css = $post->post_content;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</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-09 16:54:51 UTC (rev 39184)
+++ trunk/tests/phpunit/tests/customize/custom-css-setting.php  2016-11-09 20:42:22 UTC (rev 39185)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -45,7 +45,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_customize = $this->wp_customize;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( 'customize_register', $this->wp_customize );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentysixteen]' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[' . get_stylesheet() . ']' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->wp_customize->add_setting( $this->setting );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,7 +78,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        function test_construct() {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( post_type_exists( 'custom_css' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'custom_css', $this->setting->type );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 'twentysixteen', $this->setting->stylesheet );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( get_stylesheet(), $this->setting->stylesheet );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 'edit_css', $this->setting->capability );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $exception = null;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -113,17 +113,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->setting->default = '/* Hello World */';
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $this->setting->default, $this->setting->value() );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertNull( wp_get_custom_css_post() );
+               $this->assertNull( wp_get_custom_css_post( $this->setting->stylesheet ) );
+               $this->assertNull( wp_get_custom_css_post( 'twentyten' ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $original_css = 'body { color: black; }';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->factory()->post->create( array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $post_id = $this->factory()->post->create( array(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'post_title' => $this->setting->stylesheet,
</span><span class="cx" style="display: block; padding: 0 10px">                        'post_name' => $this->setting->stylesheet,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'post_content' => 'body { color: black; }',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'post_content' => $original_css,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'post_status' => 'publish',
</span><span class="cx" style="display: block; padding: 0 10px">                        'post_type' => 'custom_css',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $twentyten_css = 'body { color: red; }';
+               $twentyten_post_id = $this->factory()->post->create( array(
+                       'post_title' => 'twentyten',
+                       'post_name' => 'twentyten',
+                       'post_content' => $twentyten_css,
+                       'post_status' => 'publish',
+                       'post_type' => 'custom_css',
+               ) );
+               $twentyten_setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentyten]' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertEquals( $post_id, wp_get_custom_css_post()->ID );
+               $this->assertEquals( $post_id, wp_get_custom_css_post( $this->setting->stylesheet )->ID );
+               $this->assertEquals( $twentyten_post_id, wp_get_custom_css_post( 'twentyten' )->ID );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( $original_css, wp_get_custom_css( $this->setting->stylesheet ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $original_css, $this->setting->value() );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertEquals( $twentyten_css, wp_get_custom_css( 'twentyten' ) );
+               $this->assertEquals( $twentyten_css, $twentyten_setting->value() );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $updated_css = 'body { color: blue; }';
</span><span class="cx" style="display: block; padding: 0 10px">                $this->wp_customize->set_post_value( $this->setting->id, $updated_css );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,6 +157,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->setting->preview();
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               wp_delete_post( $post_id );
+               $this->assertNull( wp_get_custom_css_post() );
+               $this->assertNull( wp_get_custom_css_post( get_stylesheet() ) );
+               $this->assertEquals( $previewed_css, wp_get_custom_css( get_stylesheet() ), 'Previewed value remains in spite of deleted post.' );
+               wp_delete_post( $twentyten_post_id );
+               $this->assertNull( wp_get_custom_css_post( 'twentyten' ) );
+               $this->assertEquals( '', wp_get_custom_css( 'twentyten' ) );
</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>