<!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>[13788] trunk: Cleanup of Custom Background.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13788">13788</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2010-03-21 06:06:18 +0000 (Sun, 21 Mar 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Cleanup of Custom Background. Move JS to external file, Add @since, Remove redundant steps functionality, Preview of Image alignment. 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="#trunkwpincludesscriptloaderphp">trunk/wp-includes/script-loader.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<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 (13787 => 13788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/custom-background.php        2010-03-21 02:52:00 UTC (rev 13787)
+++ trunk/wp-admin/custom-background.php        2010-03-21 06:06:18 UTC (rev 13788)
</span><span class="lines">@@ -1,15 +1,15 @@
</span><span class="cx"> <?php
</span><span class="cx"> /**
</span><del>- * The custom background image script.
</del><ins>+ * The custom background script.
</ins><span class="cx"> *
</span><span class="cx"> * @package WordPress
</span><span class="cx"> * @subpackage Administration
</span><span class="cx"> */
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * The custom background image class.
</del><ins>+ * The custom background class.
</ins><span class="cx"> *
</span><del>- * @since unknown
</del><ins>+ * @since 3.0
</ins><span class="cx"> * @package WordPress
</span><span class="cx"> * @subpackage Administration
</span><span class="cx"> */
</span><span class="lines">@@ -28,7 +28,7 @@
</span><span class="cx">          * Callback for header div.
</span><span class="cx">          *
</span><span class="cx">          * @var callback
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          * @access private
</span><span class="cx">          */
</span><span class="cx">         var $admin_image_div_callback;
</span><span class="lines">@@ -36,7 +36,7 @@
</span><span class="cx">         /**
</span><span class="cx">          * PHP4 Constructor - Register administration header callback.
</span><span class="cx">          *
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          * @param callback $admin_header_callback
</span><span class="cx">          * @param callback $admin_image_div_callback Optional custom image div output callback.
</span><span class="cx">          * @return Custom_Background
</span><span class="lines">@@ -49,7 +49,7 @@
</span><span class="cx">         /**
</span><span class="cx">          * Set up the hooks for the Custom Background admin page.
</span><span class="cx">          *
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          */
</span><span class="cx">         function init() {
</span><span class="cx">                 if ( ! current_user_can('switch_themes') )
</span><span class="lines">@@ -57,66 +57,46 @@
</span><span class="cx">
</span><span class="cx">                 $page = add_theme_page(__('Background'), __('Background'), 'switch_themes', 'custom-background', array(&$this, 'admin_page'));
</span><span class="cx">
</span><del>-                add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
-                add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
-                add_action("admin_head-$page", array(&$this, 'js'), 50);
-                add_action("admin_head-$page", array(&$this, 'take_action'), 49);
</del><ins>+                add_action("load-$page", array(&$this, 'admin_load'));
+                add_action("load-$page", array(&$this, 'take_action'), 49);
+                add_action("load-$page", array(&$this, 'handle_upload'), 49);
+
</ins><span class="cx">                 if ( $this->admin_header_callback )
</span><span class="cx">                         add_action("admin_head-$page", $this->admin_header_callback, 51);
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><del>-         * Get the current step.
</del><ins>+         * Set up the enqueue for the CSS & JavaScript files.
</ins><span class="cx">          *
</span><del>-         * @since unknown
-         *
-         * @return int Current step
</del><ins>+         * @since 3.0
</ins><span class="cx">          */
</span><del>-        function step() {
-                if ( ! isset( $_GET['step'] ) )
-                        return 1;
-
-                $step = (int) $_GET['step'];
-                if ( $step < 1 || 3 < $step )
-                        $step = 1;
-
-                return $step;
-        }
-
-        /**
-         * Set up the enqueue for the JavaScript files.
-         *
-         * @since unknown
-         */
-        function js_includes() {
-                wp_enqueue_script('farbtastic');
-        }
-
-        /**
-         * Set up the enqueue for the CSS files
-         *
-         * @since unknown
-         */
-        function css_includes() {
</del><ins>+        function admin_load() {
+                wp_enqueue_script('custom-background');
</ins><span class="cx">                 wp_enqueue_style('farbtastic');
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><span class="cx">          * Execute custom background modification.
</span><span class="cx">          *
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          */
</span><span class="cx">         function take_action() {
</span><del>-                if ( ! current_user_can('switch_themes') )
-                        return;
</del><span class="cx">
</span><span class="cx">                 if ( empty($_POST) )
</span><span class="cx">                         return;
</span><span class="cx">
</span><span class="cx">                 check_admin_referer('custom-background');
</span><span class="cx">
</span><del>-                if ( isset($_POST['reset-background']) )
</del><ins>+                // @TODO: No UI entry point for this:
+                if ( isset($_POST['reset-background']) ) {
</ins><span class="cx">                         remove_theme_mods();
</span><ins>+                        return;
+                }
+                if ( isset($_POST['remove-background']) ) {
+                        // @TODO: Uploaded files are not removed here.
+                        set_theme_mod('background_image', '');
+                }
+
</ins><span class="cx">                 if ( isset($_POST['background-repeat']) ) {
</span><span class="cx">                         if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat')) )
</span><span class="cx">                                 $repeat = $_POST['background-repeat'];
</span><span class="lines">@@ -138,9 +118,7 @@
</span><span class="cx">                                 $attachment = 'fixed';
</span><span class="cx">                         set_theme_mod('background_attachment', $attachment);
</span><span class="cx">                 }
</span><del>-                if ( isset($_POST['remove-background']) )
-                        set_theme_mod('background_image', '');
-                if ( isset( $_POST['background-color'] ) ) {
</del><ins>+                if ( isset($_POST['background-color']) ) {
</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">@@ -152,78 +130,11 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><del>-         * Execute Javascript depending on step.
</del><ins>+         * Display the custom background page.
</ins><span class="cx">          *
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          */
</span><del>-        function js() {
-                $this->js_1();
-        }
-
-        /**
-         * Display Javascript based on Step 1.
-         *
-         * @since unknown
-         */
-        function js_1() { ?>
-<script type="text/javascript">
-        var buttons = ['#pickcolor'],
-                farbtastic;
-
-        function pickColor(color) {
-                jQuery('#background-color').val(color);
-                farbtastic.setColor(color);
-                jQuery('#custom-background-image').css('background-color', color);
-        }
-
-        jQuery(document).ready(function() {
-                jQuery('#pickcolor').click(function() {
-                        jQuery('#colorPickerDiv').show();
-                });
-                jQuery('#background-color').keyup(function() {
-                        var _hex = jQuery('#background-color').val();
-                        var hex = _hex;
-                        if ( hex[0] != '#' )
-                                hex = '#' + hex;
-                        hex = hex.replace(/[^#a-fA-F0-9]+/, '');
-                        if ( hex != _hex )
-                                jQuery('#background-color').val(hex);
-                        if ( hex.length == 4 || hex.length == 7 )
-                                pickColor( hex );
-                });
-
-                farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
-                pickColor('#<?php background_color(); ?>');
-        });
-
-        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);
-                        }
-                });
-        }
-
-</script>
-<?php
-        }
-
-        /**
-         * Display first step of custom background image page.
-         *
-         * @since unknown
-         */
-        function step_1() {
</del><ins>+        function admin_page() {
</ins><span class="cx"> ?>
</span><span class="cx"> <div class="wrap" id="custom-background">
</span><span class="cx"> <?php screen_icon(); ?>
</span><span class="lines">@@ -239,18 +150,20 @@
</span><span class="cx">                 call_user_func($this->admin_image_div_callback);
</span><span class="cx">         } else {
</span><span class="cx">                 if ( $bgcolor = get_background_color() )
</span><del>-                        $bgcolor = ' style="background-color: #' . $bgcolor . ';"';
-                else
-                        $bgcolor = '';
</del><ins>+                        $bgcolor = 'background-color: #' . $bgcolor . ';';
+
+                if ( $align = get_theme_mod('background_position', 'left') )
+                        $align = "text-align: $align;";                
</ins><span class="cx"> ?>
</span><del>-<div id="custom-background-image"<?php echo $bgcolor; ?>>
</del><ins>+<div id="custom-background-image" style="<?php echo $bgcolor, $align ?>">
</ins><span class="cx"> <?php if ( get_background_image() ) { ?>
</span><span class="cx"> <img class="custom-background-image" src="<?php background_image(); ?>" />
</span><span class="cx"> <?php } ?>
</span><ins>+<br class="clear" />
</ins><span class="cx"> </div>
</span><span class="cx"> <?php } ?>
</span><span class="cx"> <h3><?php _e('Change Display Options') ?></h3>
</span><del>-<form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>">
</del><ins>+<form method="post" action="">
</ins><span class="cx"> <table>
</span><span class="cx"> <thead>
</span><span class="cx"> <tr>
</span><span class="lines">@@ -314,7 +227,7 @@
</span><span class="cx"> </form>
</span><span class="cx">
</span><span class="cx"> <h3><?php _e('Upload New Background Image'); ?></h3>
</span><del>-<form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo esc_attr(add_query_arg('step', 2)) ?>">
</del><ins>+<form enctype="multipart/form-data" id="uploadForm" method="POST" action="">
</ins><span class="cx"> <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
</span><span class="cx"> <input type="hidden" name="action" value="save" />
</span><span class="cx"> <?php wp_nonce_field('custom-background') ?>
</span><span class="lines">@@ -326,22 +239,26 @@
</span><span class="cx"> <?php if ( get_background_image() ) : ?>
</span><span class="cx"> <h3><?php _e('Remove Background Image'); ?></h3>
</span><span class="cx"> <p><?php _e('This will remove the background image. You will not be able to retrieve any customizations.') ?></p>
</span><del>-<form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>">
</del><ins>+<form method="post" action="">
</ins><span class="cx"> <?php wp_nonce_field('custom-background'); ?>
</span><span class="cx"> <input type="submit" class="button" name="remove-background" value="<?php esc_attr_e('Remove Background'); ?>" />
</span><span class="cx"> </form>
</span><del>-
</del><span class="cx"> <?php endif; ?>
</span><ins>+
</ins><span class="cx"> </div>
</span><span class="cx"> <?php
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><del>-         * Display second step of custom background image page.
</del><ins>+         * Handle a Image upload for the background image.
</ins><span class="cx">          *
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          */
</span><del>-        function step_2() {
</del><ins>+        function handle_upload() {
+
+                if ( empty($_FILES) )
+                        return;
+
</ins><span class="cx">                 check_admin_referer('custom-background');
</span><span class="cx">                 $overrides = array('test_form' => false);
</span><span class="cx">                 $file = wp_handle_upload($_FILES['import'], $overrides);
</span><span class="lines">@@ -371,32 +288,7 @@
</span><span class="cx">                 set_theme_mod('background_image', esc_url($url));
</span><span class="cx">                 do_action('wp_create_file_in_uploads', $file, $id); // For replication
</span><span class="cx">                 $this->updated = true;
</span><del>-                return $this->finished();
</del><span class="cx">         }
</span><span class="cx">
</span><del>-        /**
-         * Display last step of custom header image page.
-         *
-         * @since unknown
-         */
-        function finished() {
-                $this->step_1();
-        }
-
-        /**
-         * Display the page based on the current step.
-         *
-         * @since unknown
-         */
-        function admin_page() {
-                if ( ! current_user_can('switch_themes') )
-                        wp_die(__('You do not have permission to customize the background.'));
-                $step = $this->step();
-                if ( 1 == $step )
-                        $this->step_1();
-                elseif ( 2 == $step )
-                        $this->step_2();
-        }
-
</del><span class="cx"> }
</span><span class="cx"> ?>
</span></span></pre></div>
<a id="trunkwpadminjscustombackgrounddevjs"></a>
<div class="addfile"><h4>Added: trunk/wp-admin/js/custom-background.dev.js (0 => 13788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-background.dev.js         (rev 0)
+++ trunk/wp-admin/js/custom-background.dev.js        2010-03-21 06:06:18 UTC (rev 13788)
</span><span class="lines">@@ -0,0 +1,49 @@
</span><ins>+var buttons = ['#pickcolor'], farbtastic;
+
+function pickColor(color) {
+        jQuery('#background-color').val(color);
+        farbtastic.setColor(color);
+        jQuery('#custom-background-image').css('background-color', color);
+}
+
+jQuery(document).ready(function() {
+        jQuery('#pickcolor').click(function() {
+                jQuery('#colorPickerDiv').show();
+        });
+        jQuery('#background-color').keyup(function() {
+                var _hex = jQuery('#background-color').val();
+                var hex = _hex;
+                if ( hex[0] != '#' )
+                        hex = '#' + hex;
+                hex = hex.replace(/[^#a-fA-F0-9]+/, '');
+                if ( hex != _hex )
+                        jQuery('#background-color').val(hex);
+                if ( hex.length == 4 || hex.length == 7 )
+                        pickColor( hex );
+        });
+        jQuery('input[name="background-position"]').change(function() {
+                jQuery('#custom-background-image img').attr('align', jQuery(this).val() );
+        });
+
+        farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) {
+                pickColor(color);
+        });
+        pickColor(customBackgroundL10n.backgroundcolor);
+});
+
+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);
+        });
+}
</ins><span class="cx">\ No newline at end of file
</span><span class="cx">Property changes on: trunk/wp-admin/js/custom-background.dev.js
</span><span class="cx">___________________________________________________________________
</span><span class="cx">Name: svn:eol-style
</span><span class="cx"> + native
</span></span></pre></div>
<a id="trunkwpadminjscustombackgroundjs"></a>
<div class="addfile"><h4>Added: trunk/wp-admin/js/custom-background.js (0 => 13788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-background.js         (rev 0)
+++ trunk/wp-admin/js/custom-background.js        2010-03-21 06:06:18 UTC (rev 13788)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+var buttons=["#pickcolor"],farbtastic;function pickColor(color){jQuery("#background-color").val(color);farbtastic.setColor(color);jQuery("#custom-background-image").css("background-color",color)}jQuery(document).ready(function(){jQuery("#pickcolor").click(function(){jQuery("#colorPickerDiv").show()});jQuery("#background-color").keyup(function(){var _hex=jQuery("#background-color").val();var hex=_hex;if(hex[0]!="#"){hex="#"+hex}hex=hex.replace(/[^#a-fA-F0-9]+/,"");if(hex!=_hex){jQuery("#background-color").val(hex)}if(hex.length==4||hex.length==7){pickColor(hex)}});jQuery('input[name="background-position"]').change(function(){jQuery("#custom-background-image img").attr("align",jQuery(this).val())});farbtastic=jQuery.farbtastic("#colorPickerDiv",function(color){pickColor(color)});pickColor(customBackgroundL10n.backgroundcolor)});jQuery(document).mousedown(function(){hide_picker()});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)}})};
</ins><span class="cx">\ No newline at end of file
</span><span class="cx">Property changes on: trunk/wp-admin/js/custom-background.js
</span><span class="cx">___________________________________________________________________
</span><span class="cx">Name: svn:eol-style
</span><span class="cx"> + native
</span></span></pre></div>
<a id="trunkwpincludesscriptloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/script-loader.php (13787 => 13788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/script-loader.php        2010-03-21 02:52:00 UTC (rev 13787)
+++ trunk/wp-includes/script-loader.php        2010-03-21 06:06:18 UTC (rev 13788)
</span><span class="lines">@@ -406,6 +406,11 @@
</span><span class="cx">                         'edit' => _x('Edit', 'menu item edit text'),
</span><span class="cx">                         'warnDelete' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ),
</span><span class="cx">                 ) );
</span><ins>+
+                $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), '20100321' );
+                $scripts->add_data( 'custom-background', 'group', 1 );
+                // See wp_just_in_time_script_localization() for translation data for this object
+
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -509,7 +514,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Load localized script just in time for MCE.
</del><ins>+ * Load localized data on print rather than initialization.
</ins><span class="cx"> *
</span><span class="cx"> * These localizations require information that may not be loaded even by init.
</span><span class="cx"> *
</span><span class="lines">@@ -526,6 +531,10 @@
</span><span class="cx">                 'saveAlert' => __('The changes you made will be lost if you navigate away from this page.'),
</span><span class="cx">                 'l10n_print_after' => 'try{convertEntities(autosaveL10n);}catch(e){};'
</span><span class="cx">         ) );
</span><ins>+
+        wp_localize_script( 'custom-background', 'customBackgroundL10n', array(
+                'backgroundcolor' => '#' . get_background_color(),
+        ) );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>
</body>
</html>