<!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>[20936] trunk: Theme Customizer: Ensure that JS color controls always use real color values, even if the server-side value is a hex value without a hash.</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, #logmsg > ol { margin-left: 0; 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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/20936">20936</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-05-26 18:44:31 +0000 (Sat, 26 May 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Theme Customizer: Ensure that JS color controls always use real color values, even if the server-side value is a hex value without a hash. fixes <a href="http://core.trac.wordpress.org/ticket/20448">#20448</a>, see <a href="http://core.trac.wordpress.org/ticket/19910">#19910</a>.

Adds WP_Customize_Setting-&gt;sanitize_js_callback and 'customize_sanitize_js_$settingID' filter, to filter values before they're passed to JS using WP_Customize_Setting-&gt;js_value().

Adds support for regular hex colors to the color picker.

Changes color methods:
* sanitize_hex_color() accepts 3 and 6 digit hex colors (with hashes) and the empty string.
* sanitize_hex_color_no_hash() accepts 3 and 6 digit hex colors (without hashes) and the empty string.
* maybe_hash_hex_color() ensures that a hex color has a hash, and otherwise leaves the value untouched.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpcontentthemestwentyelevenincthemeoptionsphp">trunk/wp-content/themes/twentyeleven/inc/theme-options.php</a></li>
<li><a href="#trunkwpincludesclasswpcustomizecontrolphp">trunk/wp-includes/class-wp-customize-control.php</a></li>
<li><a href="#trunkwpincludesclasswpcustomizemanagerphp">trunk/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunkwpincludesclasswpcustomizesettingphp">trunk/wp-includes/class-wp-customize-setting.php</a></li>
<li><a href="#trunkwpincludescsscustomizecontrolsdevcss">trunk/wp-includes/css/customize-controls.dev.css</a></li>
<li><a href="#trunkwpincludesjscustomizecontrolsdevjs">trunk/wp-includes/js/customize-controls.dev.js</a></li>
<li><a href="#trunkwpincludesjscustomizepreviewdevjs">trunk/wp-includes/js/customize-preview.dev.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpcontentthemestwentyelevenincthemeoptionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-content/themes/twentyeleven/inc/theme-options.php (20935 => 20936)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-content/themes/twentyeleven/inc/theme-options.php        2012-05-26 15:58:30 UTC (rev 20935)
+++ trunk/wp-content/themes/twentyeleven/inc/theme-options.php        2012-05-26 18:44:31 UTC (rev 20936)
</span><span class="lines">@@ -483,7 +483,7 @@
</span><span class="cx">         $wp_customize-&gt;add_setting( 'twentyeleven_theme_options[link_color]', array(
</span><span class="cx">                 'default'           =&gt; twentyeleven_get_default_link_color( $options['color_scheme'] ),
</span><span class="cx">                 'type'              =&gt; 'option',
</span><del>-                'sanitize_callback' =&gt; 'twentyeleven_sanitize_hexcolor',
</del><ins>+                'sanitize_callback' =&gt; 'sanitize_hex_color',
</ins><span class="cx">                 'capability'        =&gt; 'edit_theme_options',
</span><span class="cx">         ) );
</span><span class="cx"> 
</span><span class="lines">@@ -520,17 +520,6 @@
</span><span class="cx"> add_action( 'customize_register', 'twentyeleven_customize_register' );
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Sanitize user input hex color value
- *
- * @uses sanitize_hexcolor()
- * @param $color string
- * @return string sanitized with prefixed # character
- */
-function twentyeleven_sanitize_hexcolor( $color ) {
-        return '#' . sanitize_hexcolor( $color );
-}
-
-/**
</del><span class="cx">  * Bind JS handlers to make Theme Customizer preview reload changes asynchronously.
</span><span class="cx">  * Used with blogname and blogdescription.
</span><span class="cx">  *
</span></span></pre></div>
<a id="trunkwpincludesclasswpcustomizecontrolphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-customize-control.php (20935 => 20936)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-customize-control.php        2012-05-26 15:58:30 UTC (rev 20935)
+++ trunk/wp-includes/class-wp-customize-control.php        2012-05-26 18:44:31 UTC (rev 20936)
</span><span class="lines">@@ -265,10 +265,7 @@
</span><span class="cx">                                         &lt;/div&gt;
</span><span class="cx">                                         &lt;div class=&quot;dropdown-arrow&quot;&gt;&lt;/div&gt;
</span><span class="cx">                                 &lt;/div&gt;
</span><del>-                                &lt;div class=&quot;color-picker-hex&quot;&gt;
-                                        &lt;span&gt;#&lt;/span&gt;
-                                        &lt;input type=&quot;text&quot; &lt;?php $this-&gt;link(); ?&gt; /&gt;
-                                &lt;/div&gt;
</del><ins>+                                &lt;input class=&quot;color-picker-hex&quot; type=&quot;text&quot; maxlength=&quot;7&quot; placeholder=&quot;&lt;?php esc_attr_e('Hex Value'); ?&gt;&quot; /&gt;
</ins><span class="cx">                         &lt;/div&gt;
</span><span class="cx">                         &lt;div class=&quot;farbtastic-placeholder&quot;&gt;&lt;/div&gt;
</span><span class="cx">                 &lt;/label&gt;
</span></span></pre></div>
<a id="trunkwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-customize-manager.php (20935 => 20936)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-customize-manager.php        2012-05-26 15:58:30 UTC (rev 20935)
+++ trunk/wp-includes/class-wp-customize-manager.php        2012-05-26 18:44:31 UTC (rev 20936)
</span><span class="lines">@@ -667,9 +667,11 @@
</span><span class="cx">                 ) );
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;add_setting( 'header_textcolor', array(
</span><del>-                        'sanitize_callback' =&gt; 'sanitize_header_textcolor',
</del><span class="cx">                         'theme_supports' =&gt; array( 'custom-header', 'header-text' ),
</span><span class="cx">                         'default'        =&gt; get_theme_support( 'custom-header', 'default-text-color' ),
</span><ins>+
+                        'sanitize_callback'    =&gt; array( $this, '_sanitize_header_textcolor' ),
+                        'sanitize_js_callback' =&gt; 'maybe_hash_hex_color',
</ins><span class="cx">                 ) );
</span><span class="cx"> 
</span><span class="cx">                 // Input type: checkbox
</span><span class="lines">@@ -689,9 +691,11 @@
</span><span class="cx">                 // Input type: Color
</span><span class="cx">                 // With sanitize_callback
</span><span class="cx">                 $this-&gt;add_setting( 'background_color', array(
</span><del>-                        'default'           =&gt; get_theme_support( 'custom-background', 'default-color' ),
-                        'sanitize_callback' =&gt; 'sanitize_hexcolor',
-                        'theme_supports'    =&gt; 'custom-background',
</del><ins>+                        'default'        =&gt; get_theme_support( 'custom-background', 'default-color' ),
+                        'theme_supports' =&gt; 'custom-background',
+
+                        'sanitize_callback'    =&gt; 'sanitize_hex_color_no_hash',
+                        'sanitize_js_callback' =&gt; 'maybe_hash_hex_color',
</ins><span class="cx">                 ) );
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
</span><span class="lines">@@ -876,23 +880,69 @@
</span><span class="cx">                         'type'       =&gt; 'dropdown-pages',
</span><span class="cx">                 ) );
</span><span class="cx">         }
</span><ins>+
+        /**
+         * Callback for validating the header_textcolor value.
+         *
+         * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
+         *
+         * @since 3.4.0
+         */
+        public function _sanitize_header_textcolor( $color ) {
+                return ( 'blank' === $color ) ? 'blank' : sanitize_hex_color_no_hash( $color );
+        }
</ins><span class="cx"> };
</span><span class="cx"> 
</span><del>-// Callback function for sanitizing the header textcolor setting.
-function sanitize_header_textcolor( $color ) {
-        if ( $color == 'blank' )
-                return 'blank';
</del><ins>+/**
+ * Validates a hex color.
+ *
+ * Returns either '', a 3 or 6 digit hex color (with #), or null.
+ * For validating values without a #, see sanitize_hex_color_no_hash().
+ *
+ * @since 3.4.0
+ */
+function sanitize_hex_color( $color ) {
+        if ( '' === $color )
+                return '';
</ins><span class="cx"> 
</span><del>-        return sanitize_hexcolor( $color );
-}
-
-// Callback function for sanitizing a hex color
-function sanitize_hexcolor( $color ) {
-        $color = preg_replace( '/[^0-9a-fA-F]/', '', $color );
-
</del><span class="cx">         // 3 or 6 hex digits, or the empty string.
</span><del>-        if ( preg_match('|^([A-Fa-f0-9]{3}){0,2}$|', $color ) )
</del><ins>+        if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
</ins><span class="cx">                 return $color;
</span><span class="cx"> 
</span><span class="cx">         return null;
</span><span class="cx"> }
</span><ins>+
+/**
+ * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
+ *
+ * Saving hex colors without a hash puts the burden of adding the hash on the
+ * UI, which makes it difficult to use or upgrade to other color types such as
+ * rgba, hsl, rgb, and html color names.
+ *
+ * Returns either '', a 3 or 6 digit hex color (without a #), or null.
+ *
+ * @since 3.4.0
+ */
+function sanitize_hex_color_no_hash( $color ) {
+        $color = ltrim( $color, '#' );
+
+        if ( '' === $color )
+                return '';
+
+        return sanitize_hex_color( '#' . $color ) ? $color : null;
+}
+
+/**
+ * Ensures that any hex color is properly hashed.
+ * Otherwise, returns value untouched.
+ *
+ * This method should only be necessary if using sanitize_hex_color_no_hash().
+ *
+ * @since 3.4.0
+ */
+function maybe_hash_hex_color( $color ) {
+        if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
+                return '#' . $unhashed;
+
+        return $color;
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkwpincludesclasswpcustomizesettingphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-customize-setting.php (20935 => 20936)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-customize-setting.php        2012-05-26 15:58:30 UTC (rev 20935)
+++ trunk/wp-includes/class-wp-customize-setting.php        2012-05-26 18:44:31 UTC (rev 20936)
</span><span class="lines">@@ -11,13 +11,15 @@
</span><span class="cx">         public $manager;
</span><span class="cx">         public $id;
</span><span class="cx"> 
</span><del>-        public $type              = 'theme_mod';
-        public $capability        = 'edit_theme_options';
-        public $theme_supports    = '';
-        public $default           = '';
-        public $sanitize_callback = '';
-        public $transport         = 'refresh';
</del><ins>+        public $type            = 'theme_mod';
+        public $capability      = 'edit_theme_options';
+        public $theme_supports  = '';
+        public $default         = '';
+        public $transport       = 'refresh';
</ins><span class="cx"> 
</span><ins>+        public $sanitize_callback    = '';
+        public $sanitize_js_callback = '';
+
</ins><span class="cx">         protected $id_data = array();
</span><span class="cx">         private $_post_value; // Cached, sanitized $_POST value.
</span><span class="cx"> 
</span><span class="lines">@@ -49,9 +51,12 @@
</span><span class="cx">                 if ( ! empty( $this-&gt;id_data[ 'keys' ] ) )
</span><span class="cx">                         $this-&gt;id .= '[' . implode( '][', $this-&gt;id_data[ 'keys' ] ) . ']';
</span><span class="cx"> 
</span><del>-                if ( $this-&gt;sanitize_callback != '' )
-                        add_filter( &quot;customize_sanitize_{$this-&gt;id}&quot;, $this-&gt;sanitize_callback );
</del><ins>+                if ( $this-&gt;sanitize_callback )
+                        add_filter( &quot;customize_sanitize_{$this-&gt;id}&quot;, $this-&gt;sanitize_callback, 10, 2 );
</ins><span class="cx"> 
</span><ins>+                if ( $this-&gt;sanitize_js_callback )
+                        add_filter( &quot;customize_sanitize_js_{$this-&gt;id}&quot;, $this-&gt;sanitize_js_callback, 10, 2 );
+
</ins><span class="cx">                 return $this;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -138,7 +143,7 @@
</span><span class="cx">          */
</span><span class="cx">         public function sanitize( $value ) {
</span><span class="cx">                 $value = stripslashes_deep( $value );
</span><del>-                return apply_filters( &quot;customize_sanitize_{$this-&gt;id}&quot;, $value );
</del><ins>+                return apply_filters( &quot;customize_sanitize_{$this-&gt;id}&quot;, $value, $this );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -238,7 +243,7 @@
</span><span class="cx">          * @return mixed The requested escaped value.
</span><span class="cx">          */
</span><span class="cx">         public function js_value() {
</span><del>-                $value = $this-&gt;value();
</del><ins>+                $value = apply_filters( &quot;customize_sanitize_js_{$this-&gt;id}&quot;, $this-&gt;value(), $this );
</ins><span class="cx"> 
</span><span class="cx">                 if ( is_string( $value ) )
</span><span class="cx">                         return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
</span></span></pre></div>
<a id="trunkwpincludescsscustomizecontrolsdevcss"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/css/customize-controls.dev.css (20935 => 20936)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/css/customize-controls.dev.css        2012-05-26 15:58:30 UTC (rev 20935)
+++ trunk/wp-includes/css/customize-controls.dev.css        2012-05-26 18:44:31 UTC (rev 20936)
</span><span class="lines">@@ -319,37 +319,19 @@
</span><span class="cx">         border-color: rgba( 0, 0, 0, 0.25 );
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-.customize-section .color-picker-hex {
</del><ins>+.customize-section input[type=&quot;text&quot;].color-picker-hex {
</ins><span class="cx">         float: left;
</span><del>-        width: 70px;
</del><ins>+        width: 85px;
</ins><span class="cx">         font-family: monospace;
</span><del>-        background-color: #fff;
-        color: #777;
-        border: 1px solid #ccc;
-        -webkit-border-radius: 3px;
-        border-radius: 3px;
</del><ins>+        text-align: center;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-.customize-section .color-picker-hex span {
-        float: left;
-        display: block;
-        margin: 1px -2px 0 0;
-        line-height: 16px;
-        padding: 3px 0 3px 8px;
-        text-align: right;
-        -webkit-border-radius: 3px 0 0 3px;
-        border-radius: 3px 0 0 3px;
</del><ins>+/* The centered cursor overlaps the placeholder in webkit. Hide it when selected. */
+.customize-section input[type=&quot;text&quot;].color-picker-hex:focus::-webkit-input-placeholder {
+        color: transparent;
</ins><span class="cx"> }
</span><del>-
-.customize-section .color-picker-hex input[type=&quot;text&quot;] {
-        width: 50px;
-        height: 22px;
-        line-height: 16px;
-        color: #777;
-        background: transparent;
-        border: 0;
-        -webkit-border-radius: 0 3px 3px 0;
-        border-radius: 0 3px 3px 0;
</del><ins>+.customize-section input[type=&quot;text&quot;].color-picker-hex:-moz-placeholder {
+        color: #999;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> .customize-control-color .farbtastic-placeholder {
</span></span></pre></div>
<a id="trunkwpincludesjscustomizecontrolsdevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/customize-controls.dev.js (20935 => 20936)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/customize-controls.dev.js        2012-05-26 15:58:30 UTC (rev 20935)
+++ trunk/wp-includes/js/customize-controls.dev.js        2012-05-26 18:44:31 UTC (rev 20936)
</span><span class="lines">@@ -109,19 +109,23 @@
</span><span class="cx">         api.ColorControl = api.Control.extend({
</span><span class="cx">                 ready: function() {
</span><span class="cx">                         var control = this,
</span><del>-                                spot, text, update;
</del><ins>+                                rhex, spot, input, text, update;
</ins><span class="cx"> 
</span><ins>+                        rhex   = /^#([A-Fa-f0-9]{3}){0,2}$/;
</ins><span class="cx">                         spot   = this.container.find('.dropdown-content');
</span><ins>+                        input  = new api.Element( this.container.find('.color-picker-hex') );
</ins><span class="cx">                         update = function( color ) {
</span><del>-                                color = color ? '#' + color : '';
</del><span class="cx">                                 spot.css( 'background', color );
</span><span class="cx">                                 control.farbtastic.setColor( color );
</span><span class="cx">                         };
</span><span class="cx"> 
</span><del>-                        this.farbtastic = $.farbtastic( this.container.find('.farbtastic-placeholder'), function( color ) {
-                                control.setting.set( color.replace( '#', '' ) );
-                        });
</del><ins>+                        this.farbtastic = $.farbtastic( this.container.find('.farbtastic-placeholder'), control.setting.set );
</ins><span class="cx"> 
</span><ins>+                        // Only pass through values that are valid hexes/empty.
+                        input.link( this.setting ).validate = function( to ) {
+                                return rhex.test( to ) ? to : null;
+                        };
+
</ins><span class="cx">                         this.setting.bind( update );
</span><span class="cx">                         update( this.setting() );
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesjscustomizepreviewdevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/customize-preview.dev.js (20935 => 20936)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/customize-preview.dev.js        2012-05-26 15:58:30 UTC (rev 20935)
+++ trunk/wp-includes/js/customize-preview.dev.js        2012-05-26 18:44:31 UTC (rev 20936)
</span><span class="lines">@@ -101,7 +101,7 @@
</span><span class="cx">                                 body.toggleClass( 'custom-background', !! ( color() || image() ) );
</span><span class="cx"> 
</span><span class="cx">                                 if ( color() )
</span><del>-                                        css += 'background-color: #' + color() + ';';
</del><ins>+                                        css += 'background-color: ' + color() + ';';
</ins><span class="cx"> 
</span><span class="cx">                                 if ( image() ) {
</span><span class="cx">                                         css += 'background-image: url(&quot;' + image() + '&quot;);';
</span></span></pre>
</div>
</div>

</body>
</html>