<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[14670] trunk/wp-admin:
  Validation and nonce improvements to custom background UI.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/14670">14670</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-05-15 19:47:03 +0000 (Sat, 15 May 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Validation and nonce improvements to custom background UI. props ocean90, see <a href="http://trac.wordpress.org/ticket/12186">#12186</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmincustombackgroundphp">trunk/wp-admin/custom-background.php</a></li>
<li><a href="#trunkwpadminjscustombackgrounddevjs">trunk/wp-admin/js/custom-background.dev.js</a></li>
<li><a href="#trunkwpadminjscustombackgroundjs">trunk/wp-admin/js/custom-background.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmincustombackgroundphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/custom-background.php (14669 => 14670)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/custom-background.php        2010-05-15 19:27:02 UTC (rev 14669)
+++ trunk/wp-admin/custom-background.php        2010-05-15 19:47:03 UTC (rev 14670)
</span><span class="lines">@@ -85,18 +85,22 @@
</span><span class="cx">                 if ( empty($_POST) )
</span><span class="cx">                         return;
</span><span class="cx"> 
</span><del>-                check_admin_referer('custom-background');
-
</del><span class="cx">                 if ( isset($_POST['reset-background']) ) {
</span><del>-                        remove_theme_mod( 'background_image' );
</del><ins>+                        check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
+                        remove_theme_mod('background_image');
+                        remove_theme_mod('background_image_thumb');
</ins><span class="cx">                         return;
</span><span class="cx">                 }
</span><span class="cx">                 if ( isset($_POST['remove-background']) ) {
</span><span class="cx">                         // @TODO: Uploaded files are not removed here.
</span><ins>+                        check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
</ins><span class="cx">                         set_theme_mod('background_image', '');
</span><ins>+                        set_theme_mod('background_image_thumb', '');
+                        return;
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 if ( isset($_POST['background-repeat']) ) {
</span><ins>+                        check_admin_referer('custom-background');
</ins><span class="cx">                         if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
</span><span class="cx">                                 $repeat = $_POST['background-repeat'];
</span><span class="cx">                         else
</span><span class="lines">@@ -104,6 +108,7 @@
</span><span class="cx">                         set_theme_mod('background_repeat', $repeat);
</span><span class="cx">                 }
</span><span class="cx">                 if ( isset($_POST['background-position']) ) {
</span><ins>+                        check_admin_referer('custom-background');
</ins><span class="cx">                         if ( in_array($_POST['background-position'], array('center', 'right', 'left')) )
</span><span class="cx">                                 $position = $_POST['background-position'];
</span><span class="cx">                         else
</span><span class="lines">@@ -111,6 +116,7 @@
</span><span class="cx">                         set_theme_mod('background_position', $position);
</span><span class="cx">                 }
</span><span class="cx">                 if ( isset($_POST['background-attachment']) ) {
</span><ins>+                        check_admin_referer('custom-background');
</ins><span class="cx">                         if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
</span><span class="cx">                                 $attachment = $_POST['background-attachment'];
</span><span class="cx">                         else
</span><span class="lines">@@ -118,6 +124,7 @@
</span><span class="cx">                         set_theme_mod('background_attachment', $attachment);
</span><span class="cx">                 }
</span><span class="cx">                 if ( isset($_POST['background-color']) ) {
</span><ins>+                        check_admin_referer('custom-background');
</ins><span class="cx">                         $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
</span><span class="cx">                         if ( strlen($color) == 6 || strlen($color) == 3 )
</span><span class="cx">                                 set_theme_mod('background_color', $color);
</span><span class="lines">@@ -156,23 +163,23 @@
</span><span class="cx"> &lt;td&gt;
</span><span class="cx"> &lt;?php
</span><span class="cx"> $background_styles = '';
</span><del>-if ( get_background_color() ) {
-        $background_styles .= &quot;background-color: #&quot; . get_background_color() . &quot;;&quot;;
</del><ins>+if ( $bgcolor = get_background_color() ) {
+        $background_styles .= &quot;background-color: #{$bgcolor};&quot;;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> if ( get_background_image() ) { 
</span><span class="cx">         $background_styles .= &quot;
</span><span class="cx">         background-image: url(&quot; . get_theme_mod('background_image_thumb', '') . &quot;);
</span><del>-        background-repeat: &quot;. get_theme_mod('background_repeat', 'no-repeat') . &quot;;
-        background-position: top &quot;. get_theme_mod('background_position', 'left') . &quot;;
-        background-attachment: &quot; . get_theme_mod('background_position', 'fixed') . &quot;;
</del><ins>+        background-repeat: &quot;. get_theme_mod('background_repeat', 'repeat') . &quot;;
+        background-position: &quot;. get_theme_mod('background_position', 'left') . &quot; top;
+        background-attachment: &quot; . get_theme_mod('background_attachment', 'fixed') . &quot;;
</ins><span class="cx">         &quot;;
</span><span class="cx"> }
</span><span class="cx"> ?&gt;
</span><span class="cx"> &lt;div id=&quot;custom-background-image&quot; style=&quot;&lt;?php echo $background_styles; ?&gt;&quot;&gt;
</span><span class="cx"> &lt;?php if ( get_background_image() ) { ?&gt;
</span><del>-&lt;img class=&quot;custom-background-image&quot; src=&quot;&lt;?php echo get_theme_mod('background_image_thumb', ''); ?&gt;&quot; style=&quot;visibility:hidden;&quot; /&gt;&lt;br /&gt;
-&lt;img class=&quot;custom-background-image&quot; src=&quot;&lt;?php echo get_theme_mod('background_image_thumb', ''); ?&gt;&quot; style=&quot;visibility:hidden;&quot; /&gt;
</del><ins>+&lt;img class=&quot;custom-background-image&quot; src=&quot;&lt;?php echo get_theme_mod('background_image_thumb', ''); ?&gt;&quot; style=&quot;visibility:hidden;&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;
+&lt;img class=&quot;custom-background-image&quot; src=&quot;&lt;?php echo get_theme_mod('background_image_thumb', ''); ?&gt;&quot; style=&quot;visibility:hidden;&quot; alt=&quot;&quot; /&gt;
</ins><span class="cx"> &lt;?php } ?&gt;
</span><span class="cx"> &lt;br class=&quot;clear&quot; /&gt;
</span><span class="cx"> &lt;/div&gt;
</span><span class="lines">@@ -184,7 +191,7 @@
</span><span class="cx"> &lt;th scope=&quot;row&quot;&gt;&lt;?php _e('Remove Image'); ?&gt;&lt;/th&gt;
</span><span class="cx"> &lt;td&gt;&lt;p&gt;&lt;?php _e('This will remove the background image. You will not be able to restore any customizations.') ?&gt;&lt;/p&gt;
</span><span class="cx"> &lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
</span><del>-&lt;?php wp_nonce_field('custom-background'); ?&gt;
</del><ins>+&lt;?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?&gt;
</ins><span class="cx"> &lt;input type=&quot;submit&quot; class=&quot;button&quot; name=&quot;remove-background&quot; value=&quot;&lt;?php esc_attr_e('Remove Background'); ?&gt;&quot; /&gt;
</span><span class="cx"> &lt;/form&gt;
</span><span class="cx"> &lt;/td&gt;
</span><span class="lines">@@ -196,19 +203,19 @@
</span><span class="cx"> &lt;th scope=&quot;row&quot;&gt;&lt;?php _e('Restore Original Image'); ?&gt;&lt;/th&gt;
</span><span class="cx"> &lt;td&gt;&lt;p&gt;&lt;?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?&gt;&lt;/p&gt;
</span><span class="cx"> &lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
</span><del>-&lt;?php wp_nonce_field('custom-background'); ?&gt;
</del><ins>+&lt;?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?&gt;
</ins><span class="cx"> &lt;input type=&quot;submit&quot; class=&quot;button&quot; name=&quot;reset-background&quot; value=&quot;&lt;?php esc_attr_e('Restore Original Image'); ?&gt;&quot; /&gt;
</span><span class="cx"> &lt;/form&gt;
</span><span class="cx"> &lt;/td&gt;
</span><span class="cx"> &lt;/tr&gt;
</span><del>-&lt;/form&gt;
</del><ins>+
</ins><span class="cx"> &lt;?php endif; ?&gt;
</span><span class="cx"> &lt;tr valign=&quot;top&quot;&gt;
</span><span class="cx"> &lt;th scope=&quot;row&quot;&gt;&lt;?php _e('Upload Image'); ?&gt;&lt;/th&gt;
</span><del>-&lt;td&gt;&lt;form enctype=&quot;multipart/form-data&quot; id=&quot;uploadForm&quot; method=&quot;post&quot; action=&quot;&quot;&gt;
</del><ins>+&lt;td&gt;&lt;form enctype=&quot;multipart/form-data&quot; id=&quot;upload-form&quot; method=&quot;post&quot; action=&quot;&quot;&gt;
</ins><span class="cx"> &lt;label for=&quot;upload&quot;&gt;&lt;?php _e('Choose an image from your computer:'); ?&gt;&lt;/label&gt;&lt;br /&gt;&lt;input type=&quot;file&quot; id=&quot;upload&quot; name=&quot;import&quot; /&gt;
</span><span class="cx"> &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;save&quot; /&gt;
</span><del>-&lt;?php wp_nonce_field('custom-background') ?&gt;
</del><ins>+&lt;?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?&gt;
</ins><span class="cx"> &lt;p class=&quot;submit&quot;&gt;
</span><span class="cx"> &lt;input type=&quot;submit&quot; value=&quot;&lt;?php esc_attr_e('Upload'); ?&gt;&quot; /&gt;
</span><span class="cx"> &lt;/p&gt;
</span><span class="lines">@@ -299,7 +306,7 @@
</span><span class="cx">                 if ( empty($_FILES) )
</span><span class="cx">                         return;
</span><span class="cx"> 
</span><del>-                check_admin_referer('custom-background');
</del><ins>+                check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
</ins><span class="cx">                 $overrides = array('test_form' =&gt; false);
</span><span class="cx">                 $file = wp_handle_upload($_FILES['import'], $overrides);
</span><span class="cx"> 
</span><span class="lines">@@ -329,10 +336,6 @@
</span><span class="cx"> 
</span><span class="cx">                 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
</span><span class="cx">                 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
</span><del>-                
-                set_theme_mod('background_position', get_theme_mod('background_position', 'left') );
-                set_theme_mod('background_repeat', get_theme_mod('background_repeat', 'tile') );
-                set_theme_mod('background-attachment',  get_theme_mod('background_position', 'fixed') );
</del><span class="cx"> 
</span><span class="cx">                 do_action('wp_create_file_in_uploads', $file, $id); // For replication
</span><span class="cx">                 $this-&gt;updated = true;
</span></span></pre></div>
<a id="trunkwpadminjscustombackgrounddevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/custom-background.dev.js (14669 => 14670)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-background.dev.js        2010-05-15 19:27:02 UTC (rev 14669)
+++ trunk/wp-admin/js/custom-background.dev.js        2010-05-15 19:47:03 UTC (rev 14670)
</span><span class="lines">@@ -1,8 +1,8 @@
</span><del>-var buttons = ['#pickcolor'], farbtastic;
</del><ins>+var farbtastic;
</ins><span class="cx"> 
</span><span class="cx"> function pickColor(color) {
</span><del>-        jQuery('#background-color').val(color);
</del><span class="cx">         farbtastic.setColor(color);
</span><ins>+        jQuery('#background-color').val(color);
</ins><span class="cx">         jQuery('#custom-background-image').css('background-color', color);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -10,6 +10,7 @@
</span><span class="cx">         jQuery('#pickcolor').click(function() {
</span><span class="cx">                 jQuery('#colorPickerDiv').show();
</span><span class="cx">         });
</span><ins>+
</ins><span class="cx">         jQuery('#background-color').keyup(function() {
</span><span class="cx">                 var _hex = jQuery('#background-color').val();
</span><span class="cx">                 var hex = _hex;
</span><span class="lines">@@ -21,32 +22,29 @@
</span><span class="cx">                 if ( hex.length == 4 || hex.length == 7 )
</span><span class="cx">                         pickColor( hex );
</span><span class="cx">         });
</span><ins>+
</ins><span class="cx">         jQuery('input[name=&quot;background-position&quot;]').change(function() {
</span><del>-                jQuery('#custom-background-image').css('background-position', 'top '+jQuery(this).val());
</del><ins>+                jQuery('#custom-background-image').css('background-position', 'top ' + jQuery(this).val());
</ins><span class="cx">         });
</span><ins>+
</ins><span class="cx">         jQuery('select[name=&quot;background-repeat&quot;]').change(function() {
</span><span class="cx">                 jQuery('#custom-background-image').css('background-repeat', jQuery(this).val());
</span><span class="cx">         });
</span><ins>+        
+        jQuery('input[name=&quot;background-attachment&quot;]').change(function() {
+                jQuery('#custom-background-image').css('background-attachment', jQuery(this).val());
+        });
</ins><span class="cx"> 
</span><span class="cx">         farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) {
</span><span class="cx">                 pickColor(color);
</span><span class="cx">         });
</span><del>-        pickColor(customBackgroundL10n.backgroundcolor);
-});
</del><ins>+        pickColor(jQuery('#background-color').val());
</ins><span class="cx"> 
</span><del>-jQuery(document).mousedown(function(){
-        hide_picker(); // Make the picker disappear if you click outside its div element
-});
-
-function hide_picker(what) {
-        var update = false;
-        jQuery('#colorPickerDiv').each(function(){
-                var id = jQuery(this).attr('id');
-                if ( id == what )
-                        return;
-
-                var display = jQuery(this).css('display');
-                if ( display == 'block' )
-                        jQuery(this).fadeOut(2);
</del><ins>+        jQuery(document).mousedown(function(){
+                jQuery('#colorPickerDiv').each(function(){
+                        var display = jQuery(this).css('display');
+                        if ( display == 'block' )
+                                jQuery(this).fadeOut(2);
+                });
</ins><span class="cx">         });
</span><del>-}
</del><span class="cx">\ No newline at end of file
</span><ins>+});
</ins></span></pre></div>
<a id="trunkwpadminjscustombackgroundjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/custom-background.js (14669 => 14670)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-background.js        2010-05-15 19:27:02 UTC (rev 14669)
+++ trunk/wp-admin/js/custom-background.js        2010-05-15 19:47:03 UTC (rev 14670)
</span><span class="lines">@@ -1 +1 @@
</span><del>-var buttons=[&quot;#pickcolor&quot;],farbtastic;function pickColor(color){jQuery(&quot;#background-color&quot;).val(color);farbtastic.setColor(color);jQuery(&quot;#custom-background-image&quot;).css(&quot;background-color&quot;,color)}jQuery(document).ready(function(){jQuery(&quot;#pickcolor&quot;).click(function(){jQuery(&quot;#colorPickerDiv&quot;).show()});jQuery(&quot;#background-color&quot;).keyup(function(){var _hex=jQuery(&quot;#background-color&quot;).val();var hex=_hex;if(hex[0]!=&quot;#&quot;){hex=&quot;#&quot;+hex}hex=hex.replace(/[^#a-fA-F0-9]+/,&quot;&quot;);if(hex!=_hex){jQuery(&quot;#background-color&quot;).val(hex)}if(hex.length==4||hex.length==7){pickColor(hex)}});jQuery('input[name=&quot;background-position&quot;]').change(function(){jQuery(&quot;#custom-background-image&quot;).css(&quot;background-position&quot;,&quot;top &quot;+jQuery(this).val())});jQuery('select[name=&quot;background-repeat&quot;]').change(function(){jQuery(&quot;#custom-background-image&quot;).css(&quot;background-repeat&quot;,jQuery(this).val())});farbtastic=jQuery.farbtastic(&quot;#colorPickerDiv&quot;,function(color){pickColor(color)});pickColor(customBackgroundL10n.backgroundcolor)});jQuery(document).mousedown(function(){hide_picker()});function hide_picker(what){var update=false;jQuery(&quot;#colorPickerDiv&quot;).each(function(){var id=jQuery(this).attr(&quot;id&quot;);if(id==what){return}var display=jQuery(this).css(&quot;display&quot;);if(display==&quot;block&quot;){jQuery(this).fadeOut(2)}})};
</del><span class="cx">\ No newline at end of file
</span><ins>+var farbtastic;function pickColor(a){farbtastic.setColor(a);jQuery(&quot;#background-color&quot;).val(a);jQuery(&quot;#custom-background-image&quot;).css(&quot;background-color&quot;,a)}jQuery(document).ready(function(){jQuery(&quot;#pickcolor&quot;).click(function(){jQuery(&quot;#colorPickerDiv&quot;).show()});jQuery(&quot;#background-color&quot;).keyup(function(){var b=jQuery(&quot;#background-color&quot;).val();var a=b;if(a[0]!=&quot;#&quot;){a=&quot;#&quot;+a}a=a.replace(/[^#a-fA-F0-9]+/,&quot;&quot;);if(a!=b){jQuery(&quot;#background-color&quot;).val(a)}if(a.length==4||a.length==7){pickColor(a)}});jQuery('input[name=&quot;background-position&quot;]').change(function(){jQuery(&quot;#custom-background-image&quot;).css(&quot;background-position&quot;,&quot;top &quot;+jQuery(this).val())});jQuery('select[name=&quot;background-repeat&quot;]').change(function(){jQuery(&quot;#custom-background-image&quot;).css(&quot;background-repeat&quot;,jQuery(this).val())});jQuery('input[name=&quot;background-attachment&quot;]').change(function(){jQuery(&quot;#custom-background-image&quot;).css(&quot;background-attachment&quot;,jQuery(this).val())});farbtastic=jQuery.farbtastic(&quot;#colorPickerDiv&quot;,function(a){pickColor(a)});pickColor(jQuery(&quot;#background-color&quot;).val());jQuery(document).mousedown(function(){jQuery(&quot;#colorPickerDiv&quot;).each(function(){var a=jQuery(this).css(&quot;display&quot;);if(a==&quot;block&quot;){jQuery(this).fadeOut(2)}})})});
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>