Index: wp-admin/custom-background.php
===================================================================
--- wp-admin/custom-background.php	(revision 20171)
+++ wp-admin/custom-background.php	(working copy)
@@ -69,6 +69,8 @@
 		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);
+		add_action("load-$page", array(&$this, 'settings'), 50);
+		add_action('admin_notices', array(&$this, 'admin_notices'));
 
 		if ( $this->admin_header_callback )
 			add_action("admin_head-$page", $this->admin_header_callback, 51);
@@ -99,6 +101,165 @@
 		wp_enqueue_script('custom-background');
 		wp_enqueue_style('farbtastic');
 	}
+	
+	function settings() {
+		if ( $this->admin_image_div_callback ) {
+			call_user_func($this->admin_image_div_callback);
+		} else {
+			add_settings_section( 'background-image', __('Background Image'), '__return_false', 'custom-background-image' );
+			add_settings_field( 'preview', __('Preview'), array(&$this, 'preview_field'), 'custom-background-image', 'background-image' );
+
+			if ( get_background_image() )
+				add_settings_field( 'remove-background', __('Remove Image'), array(&$this, 'remove_background_field'), 'custom-background-image', 'background-image' );
+			
+			if ( defined( 'BACKGROUND_IMAGE' ) )
+				add_settings_field( 'reset-background', __('Restore Original Image'), array(&$this, 'reset_background_field'), 'custom-background-image', 'background-image' );
+				
+			add_settings_field( 'custom-background-upload', __('Upload Image'), array(&$this, 'background_upload_field'), 'custom-background-image', 'background-image' );
+		}
+		
+		add_settings_section( 'display-options', __('Display Options'), '__return_false', $this->page );
+		if ( get_background_image() ) {
+			add_settings_field( 'background-position', __('Position'), array(&$this, 'background_position_field'), $this->page, 'display-options' );
+			add_settings_field( 'background-repeat', __('Repeat'), array(&$this, 'background_repeat_field'), $this->page, 'display-options' );
+			add_settings_field( 'background-attachment', __('Attachment'), array(&$this, 'background_attachment_field'), $this->page, 'display-options' );
+		}
+		add_settings_field( 'background-color', __('Background Color'), array(&$this, 'background_color_field'), $this->page, 'display-options' );
+	}
+	
+	function preview_field() {
+		$background_styles = '';
+		if ( $bgcolor = get_background_color() )
+			$background_styles .= 'background-color: #' . $bgcolor . ';';
+		
+		if ( get_background_image() ) {
+			// background-image URL must be single quote, see below
+			$background_styles .= ' background-image: url(\'' . get_theme_mod('background_image_thumb', '') . '\');'
+				. ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
+				. ' background-position: top ' . get_theme_mod('background_position_x', 'left');
+		}
+		?>
+		<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
+		<?php if ( get_background_image() ) { ?>
+		<img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" /><br />
+		<img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" />
+		<?php } ?>
+		</div>
+		<?php
+	}
+	
+	function remove_background_field() {
+		echo '<form method="post" action="">';
+				wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove');
+				submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false );
+				echo '<br/>';
+				_e('This will remove the background image. You will not be able to restore any customizations.');
+		echo '</form>';
+	}
+	
+	function restore_background_field() {
+		echo '<form method="post" action="">';
+				wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset');
+				submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false );
+				echo '<br/>';
+				_e('This will restore the original background image. You will not be able to restore any customizations.');
+		echo '</form>';
+	}
+	
+	function background_upload_field() {
+		?>
+		<form enctype="multipart/form-data" id="upload-form" method="post" action="">
+		<label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br />
+		<input type="file" id="upload" name="import" />
+		<input type="hidden" name="action" value="save" />
+		<?php
+		wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload');
+		submit_button( __( 'Upload' ), 'button', 'submit', false );
+		?>
+		</form>
+		<?php
+	}
+	
+	function background_position_field() {
+		?>
+		<fieldset>
+			<legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
+			<label>
+				<input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
+				<?php _e('Left') ?>
+			</label>
+			<label>
+				<input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
+				<?php _e('Center') ?>
+			</label>
+			<label>
+				<input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
+				<?php _e('Right') ?>
+			</label>
+		</fieldset>
+		<?php
+	}
+	
+	function background_repeat_field() {
+		?>
+		<fieldset>
+			<legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
+			<label>
+				<input type="radio" name="background-repeat" value="no-repeat"<?php checked('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?> />
+				<?php _e('No Repeat'); ?>
+			</label>
+			<label>
+				<input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> />
+				<?php _e('Tile'); ?>
+			</label>
+			<label>
+				<input type="radio" name="background-repeat" value="repeat-x"<?php checked('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?> />
+				<?php _e('Tile Horizontally'); ?>
+			</label>
+			<label>
+				<input type="radio" name="background-repeat" value="repeat-y"<?php checked('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?> />
+				<?php _e('Tile Vertically'); ?>
+			</label>
+		</fieldset>
+		<?php
+	}
+	
+	function background_attachment_field() {
+		?>
+		<fieldset>
+			<legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
+			<label>
+				<input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
+				<?php _e('Scroll') ?>
+			</label>
+			<label>
+				<input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
+				<?php _e('Fixed') ?>
+			</label>
+		</fieldset>
+		<?php
+	}
+	
+	function background_color_field() {
+		?>
+		<fieldset>
+			<legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
+			<?php $show_clear = get_background_color() ? '' : ' style="display:none"'; ?>
+			<input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
+			<a class="hide-if-no-js" href="#" id="pickcolor"><?php _e('Select a Color'); ?></a> <span<?php echo $show_clear; ?> class="hide-if-no-js" id="clearcolor"> (<a href="#"><?php _e( 'Clear' ); ?></a>)</span>
+			<div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
+		</fieldset>
+		<?php
+	}
+	
+	function updated() {
+		if ( ! get_settings_errors('custom-background') )
+			add_settings_error( 'custom-background', 'background-updated',sprintf(__( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' )), 'updated' );
+	}
+	
+	function admin_notices() {
+		settings_errors( 'custom-background' );
+	}
 
 	/**
 	 * Execute custom background modification.
@@ -114,7 +275,7 @@
 			check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
 			remove_theme_mod('background_image');
 			remove_theme_mod('background_image_thumb');
-			$this->updated = true;
+			$this->updated();
 			return;
 		}
 
@@ -123,7 +284,7 @@
 			check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
 			set_theme_mod('background_image', '');
 			set_theme_mod('background_image_thumb', '');
-			$this->updated = true;
+			$this->updated();
 			return;
 		}
 
@@ -163,7 +324,7 @@
 				set_theme_mod('background_color', '');
 		}
 
-		$this->updated = true;
+		$this->updated();
 	}
 
 	/**
@@ -174,148 +335,18 @@
 	function admin_page() {
 ?>
 <div class="wrap" id="custom-background">
-<?php screen_icon(); ?>
-<h2><?php _e('Custom Background'); ?></h2>
-<?php if ( !empty($this->updated) ) { ?>
-<div id="message" class="updated">
-<p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
+	<?php screen_icon(); ?>
+	<h2><?php _e('Custom Background'); ?></h2>
+	
+	<?php do_settings_sections( 'custom-background-image' );?>
+	
+	<form method="post" action="">
+	<?php
+	do_settings_sections( $this->page );
+	wp_nonce_field( 'custom-background' );
+	submit_button( null, 'primary', 'save-background-options' ); ?>
+	</form>
 </div>
-<?php }
-
-	if ( $this->admin_image_div_callback ) {
-		call_user_func($this->admin_image_div_callback);
-	} else {
-?>
-<h3><?php _e('Background Image'); ?></h3>
-<table class="form-table">
-<tbody>
-<tr valign="top">
-<th scope="row"><?php _e('Preview'); ?></th>
-<td>
-<?php
-$background_styles = '';
-if ( $bgcolor = get_background_color() )
-	$background_styles .= 'background-color: #' . $bgcolor . ';';
-
-if ( get_background_image() ) {
-	// background-image URL must be single quote, see below
-	$background_styles .= ' background-image: url(\'' . get_theme_mod('background_image_thumb', '') . '\');'
-		. ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
-		. ' background-position: top ' . get_theme_mod('background_position_x', 'left');
-}
-?>
-<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
-<?php if ( get_background_image() ) { ?>
-<img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" /><br />
-<img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" />
-<?php } ?>
-</div>
-<?php } ?>
-</td>
-</tr>
-<?php if ( get_background_image() ) : ?>
-<tr valign="top">
-<th scope="row"><?php _e('Remove Image'); ?></th>
-<td>
-<form method="post" action="">
-<?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
-<?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
-<?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
-</form>
-</td>
-</tr>
-<?php endif; ?>
-
-<?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?>
-<tr valign="top">
-<th scope="row"><?php _e('Restore Original Image'); ?></th>
-<td>
-<form method="post" action="">
-<?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
-<?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
-<?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
-</form>
-</td>
-</tr>
-
-<?php endif; ?>
-<tr valign="top">
-<th scope="row"><?php _e('Upload Image'); ?></th>
-<td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
-<label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
-<input type="hidden" name="action" value="save" />
-<?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
-<?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
-</form>
-</td>
-</tr>
-</tbody>
-</table>
-
-<h3><?php _e('Display Options') ?></h3>
-<form method="post" action="">
-<table class="form-table">
-<tbody>
-<?php if ( get_background_image() ) : ?>
-<tr valign="top">
-<th scope="row"><?php _e( 'Position' ); ?></th>
-<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
-<label>
-<input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
-<?php _e('Left') ?>
-</label>
-<label>
-<input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
-<?php _e('Center') ?>
-</label>
-<label>
-<input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
-<?php _e('Right') ?>
-</label>
-</fieldset></td>
-</tr>
-
-<tr valign="top">
-<th scope="row"><?php _e( 'Repeat' ); ?></th>
-<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
-<label><input type="radio" name="background-repeat" value="no-repeat"<?php checked('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('No Repeat'); ?></label>
-	<label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
-	<label><input type="radio" name="background-repeat" value="repeat-x"<?php checked('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Horizontally'); ?></label>
-	<label><input type="radio" name="background-repeat" value="repeat-y"<?php checked('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Vertically'); ?></label>
-</fieldset></td>
-</tr>
-
-<tr valign="top">
-<th scope="row"><?php _e( 'Attachment' ); ?></th>
-<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
-<label>
-<input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
-<?php _e('Scroll') ?>
-</label>
-<label>
-<input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
-<?php _e('Fixed') ?>
-</label>
-</fieldset></td>
-</tr>
-<?php endif; // get_background_image() ?>
-<tr valign="top">
-<th scope="row"><?php _e( 'Background Color' ); ?></th>
-<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
-<?php $show_clear = get_background_color() ? '' : ' style="display:none"'; ?>
-<input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
-<a class="hide-if-no-js" href="#" id="pickcolor"><?php _e('Select a Color'); ?></a> <span<?php echo $show_clear; ?> class="hide-if-no-js" id="clearcolor"> (<a href="#"><?php _e( 'Clear' ); ?></a>)</span>
-<div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
-</fieldset></td>
-</tr>
-</tbody>
-</table>
-
-<?php wp_nonce_field('custom-background'); ?>
-<?php submit_button( null, 'primary', 'save-background-options' ); ?>
-</form>
-
-</div>
 <?php
 	}
 
@@ -363,7 +394,7 @@
 		set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
 
 		do_action('wp_create_file_in_uploads', $file, $id); // For replication
-		$this->updated = true;
+		$this->updated();
 	}
 
 }
