wp/wp-admin/custom-background.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    42 	 *
    42 	 *
    43 	 * @since 3.0.0
    43 	 * @since 3.0.0
    44 	 * @param callable $admin_header_callback
    44 	 * @param callable $admin_header_callback
    45 	 * @param callable $admin_image_div_callback Optional custom image div output callback.
    45 	 * @param callable $admin_image_div_callback Optional custom image div output callback.
    46 	 */
    46 	 */
    47 	public function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
    47 	public function __construct( $admin_header_callback = '', $admin_image_div_callback = '' ) {
    48 		$this->admin_header_callback = $admin_header_callback;
    48 		$this->admin_header_callback    = $admin_header_callback;
    49 		$this->admin_image_div_callback = $admin_image_div_callback;
    49 		$this->admin_image_div_callback = $admin_image_div_callback;
    50 
    50 
    51 		add_action( 'admin_menu', array( $this, 'init' ) );
    51 		add_action( 'admin_menu', array( $this, 'init' ) );
    52 
    52 
    53 		add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) );
    53 		add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) );
    80 	 * Set up the enqueue for the CSS & JavaScript files.
    80 	 * Set up the enqueue for the CSS & JavaScript files.
    81 	 *
    81 	 *
    82 	 * @since 3.0.0
    82 	 * @since 3.0.0
    83 	 */
    83 	 */
    84 	public function admin_load() {
    84 	public function admin_load() {
    85 		get_current_screen()->add_help_tab( array(
    85 		get_current_screen()->add_help_tab(
    86 			'id'      => 'overview',
    86 			array(
    87 			'title'   => __('Overview'),
    87 				'id'      => 'overview',
    88 			'content' =>
    88 				'title'   => __( 'Overview' ),
    89 				'<p>' . __( 'You can customize the look of your site without touching any of your theme&#8217;s code by using a custom background. Your background can be an image or a color.' ) . '</p>' .
    89 				'content' =>
    90 				'<p>' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '</p>' .
    90 					'<p>' . __( 'You can customize the look of your site without touching any of your theme&#8217;s code by using a custom background. Your background can be an image or a color.' ) . '</p>' .
    91 				'<p>' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. &#8220;#ff0000&#8221; for red, or by choosing a color using the color picker.' ) . '</p>' .
    91 					'<p>' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '</p>' .
    92 				'<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>'
    92 					'<p>' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. &#8220;#ff0000&#8221; for red, or by choosing a color using the color picker.' ) . '</p>' .
    93 		) );
    93 					'<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>',
       
    94 			)
       
    95 		);
    94 
    96 
    95 		get_current_screen()->set_help_sidebar(
    97 		get_current_screen()->set_help_sidebar(
    96 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
    98 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
    97 			'<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Background_Screen">Documentation on Custom Background</a>' ) . '</p>' .
    99 			'<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Background_Screen">Documentation on Custom Background</a>' ) . '</p>' .
    98 			'<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
   100 			'<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
    99 		);
   101 		);
   100 
   102 
   101 		wp_enqueue_media();
   103 		wp_enqueue_media();
   102 		wp_enqueue_script('custom-background');
   104 		wp_enqueue_script( 'custom-background' );
   103 		wp_enqueue_style('wp-color-picker');
   105 		wp_enqueue_style( 'wp-color-picker' );
   104 	}
   106 	}
   105 
   107 
   106 	/**
   108 	/**
   107 	 * Execute custom background modification.
   109 	 * Execute custom background modification.
   108 	 *
   110 	 *
   109 	 * @since 3.0.0
   111 	 * @since 3.0.0
   110 	 */
   112 	 */
   111 	public function take_action() {
   113 	public function take_action() {
   112 		if ( empty($_POST) )
   114 		if ( empty( $_POST ) ) {
   113 			return;
   115 			return;
   114 
   116 		}
   115 		if ( isset($_POST['reset-background']) ) {
   117 
   116 			check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
   118 		if ( isset( $_POST['reset-background'] ) ) {
   117 			remove_theme_mod('background_image');
   119 			check_admin_referer( 'custom-background-reset', '_wpnonce-custom-background-reset' );
   118 			remove_theme_mod('background_image_thumb');
   120 			remove_theme_mod( 'background_image' );
       
   121 			remove_theme_mod( 'background_image_thumb' );
   119 			$this->updated = true;
   122 			$this->updated = true;
   120 			return;
   123 			return;
   121 		}
   124 		}
   122 
   125 
   123 		if ( isset($_POST['remove-background']) ) {
   126 		if ( isset( $_POST['remove-background'] ) ) {
   124 			// @TODO: Uploaded files are not removed here.
   127 			// @TODO: Uploaded files are not removed here.
   125 			check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
   128 			check_admin_referer( 'custom-background-remove', '_wpnonce-custom-background-remove' );
   126 			set_theme_mod('background_image', '');
   129 			set_theme_mod( 'background_image', '' );
   127 			set_theme_mod('background_image_thumb', '');
   130 			set_theme_mod( 'background_image_thumb', '' );
   128 			$this->updated = true;
   131 			$this->updated = true;
   129 			wp_safe_redirect( $_POST['_wp_http_referer'] );
   132 			wp_safe_redirect( $_POST['_wp_http_referer'] );
   130 			return;
   133 			return;
   131 		}
   134 		}
   132 
   135 
   197 			}
   200 			}
   198 
   201 
   199 			set_theme_mod( 'background_attachment', $attachment );
   202 			set_theme_mod( 'background_attachment', $attachment );
   200 		}
   203 		}
   201 
   204 
   202 		if ( isset($_POST['background-color']) ) {
   205 		if ( isset( $_POST['background-color'] ) ) {
   203 			check_admin_referer('custom-background');
   206 			check_admin_referer( 'custom-background' );
   204 			$color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
   207 			$color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['background-color'] );
   205 			if ( strlen($color) == 6 || strlen($color) == 3 )
   208 			if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) {
   206 				set_theme_mod('background_color', $color);
   209 				set_theme_mod( 'background_color', $color );
   207 			else
   210 			} else {
   208 				set_theme_mod('background_color', '');
   211 				set_theme_mod( 'background_color', '' );
       
   212 			}
   209 		}
   213 		}
   210 
   214 
   211 		$this->updated = true;
   215 		$this->updated = true;
   212 	}
   216 	}
   213 
   217 
   215 	 * Display the custom background page.
   219 	 * Display the custom background page.
   216 	 *
   220 	 *
   217 	 * @since 3.0.0
   221 	 * @since 3.0.0
   218 	 */
   222 	 */
   219 	public function admin_page() {
   223 	public function admin_page() {
   220 ?>
   224 		?>
   221 <div class="wrap" id="custom-background">
   225 <div class="wrap" id="custom-background">
   222 <h1><?php _e( 'Custom Background' ); ?></h1>
   226 <h1><?php _e( 'Custom Background' ); ?></h1>
   223 
   227 
   224 <?php if ( current_user_can( 'customize' ) ) { ?>
   228 		<?php if ( current_user_can( 'customize' ) ) { ?>
   225 <div class="notice notice-info hide-if-no-customize">
   229 <div class="notice notice-info hide-if-no-customize">
   226 	<p>
   230 	<p>
   227 		<?php
   231 			<?php
   228 		printf(
   232 				printf(
   229 			__( 'You can now manage and live-preview Custom Backgrounds in the <a href="%1$s">Customizer</a>.' ),
   233 					__( 'You can now manage and live-preview Custom Backgrounds in the <a href="%1$s">Customizer</a>.' ),
   230 			admin_url( 'customize.php?autofocus[control]=background_image' )
   234 					admin_url( 'customize.php?autofocus[control]=background_image' )
   231 		);
   235 				);
   232 		?>
   236 			?>
   233 	</p>
   237 	</p>
   234 </div>
   238 </div>
   235 <?php } ?>
   239 		<?php } ?>
   236 
   240 
   237 <?php if ( ! empty( $this->updated ) ) { ?>
   241 		<?php if ( ! empty( $this->updated ) ) { ?>
   238 <div id="message" class="updated">
   242 <div id="message" class="updated">
   239 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
   243 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
   240 </div>
   244 </div>
   241 <?php } ?>
   245 		<?php } ?>
   242 
   246 
   243 <h3><?php _e( 'Background Image' ); ?></h3>
   247 <h2><?php _e( 'Background Image' ); ?></h2>
   244 
   248 
   245 <table class="form-table">
   249 <table class="form-table" role="presentation">
   246 <tbody>
   250 <tbody>
   247 <tr>
   251 <tr>
   248 <th scope="row"><?php _e( 'Preview' ); ?></th>
   252 <th scope="row"><?php _e( 'Preview' ); ?></th>
   249 <td>
   253 <td>
   250 	<?php
   254 		<?php
   251 	if ( $this->admin_image_div_callback ) {
   255 		if ( $this->admin_image_div_callback ) {
   252 		call_user_func( $this->admin_image_div_callback );
   256 			call_user_func( $this->admin_image_div_callback );
   253 	} else {
   257 		} else {
   254 		$background_styles = '';
   258 			$background_styles = '';
   255 		if ( $bgcolor = get_background_color() )
   259 			if ( $bgcolor = get_background_color() ) {
   256 			$background_styles .= 'background-color: #' . $bgcolor . ';';
   260 				$background_styles .= 'background-color: #' . $bgcolor . ';';
   257 
   261 			}
   258 		$background_image_thumb = get_background_image();
   262 
   259 		if ( $background_image_thumb ) {
   263 			$background_image_thumb = get_background_image();
   260 			$background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
   264 			if ( $background_image_thumb ) {
   261 			$background_position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
   265 				$background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
   262 			$background_position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
   266 				$background_position_x  = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
   263 			$background_size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
   267 				$background_position_y  = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
   264 			$background_repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
   268 				$background_size        = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
   265 			$background_attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
   269 				$background_repeat      = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
   266 
   270 				$background_attachment  = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
   267 			// Background-image URL must be single quote, see below.
   271 
   268 			$background_styles .= " background-image: url('$background_image_thumb');"
   272 				// Background-image URL must be single quote, see below.
       
   273 				$background_styles .= " background-image: url('$background_image_thumb');"
   269 				. " background-size: $background_size;"
   274 				. " background-size: $background_size;"
   270 				. " background-position: $background_position_x $background_position_y;"
   275 				. " background-position: $background_position_x $background_position_y;"
   271 				. " background-repeat: $background_repeat;"
   276 				. " background-repeat: $background_repeat;"
   272 				. " background-attachment: $background_attachment;";
   277 				. " background-attachment: $background_attachment;";
   273 		}
   278 			}
   274 	?>
   279 			?>
   275 	<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
   280 	<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
   276 		<?php if ( $background_image_thumb ) { ?>
   281 			<?php if ( $background_image_thumb ) { ?>
   277 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
   282 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
   278 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
   283 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
   279 		<?php } ?>
   284 		<?php } ?>
   280 	</div>
   285 	</div>
   281 	<?php } ?>
   286 	<?php } ?>
   282 </td>
   287 </td>
   283 </tr>
   288 </tr>
   284 
   289 
   285 <?php if ( get_background_image() ) : ?>
   290 		<?php if ( get_background_image() ) : ?>
   286 <tr>
   291 <tr>
   287 <th scope="row"><?php _e('Remove Image'); ?></th>
   292 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
   288 <td>
   293 <td>
   289 <form method="post">
   294 <form method="post">
   290 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
   295 			<?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?>
   291 <?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
   296 			<?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
   292 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
   297 			<?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ); ?>
   293 </form>
   298 </form>
   294 </td>
   299 </td>
   295 </tr>
   300 </tr>
   296 <?php endif; ?>
   301 		<?php endif; ?>
   297 
   302 
   298 <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
   303 		<?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
   299 <?php if ( $default_image && get_background_image() != $default_image ) : ?>
   304 		<?php if ( $default_image && get_background_image() != $default_image ) : ?>
   300 <tr>
   305 <tr>
   301 <th scope="row"><?php _e('Restore Original Image'); ?></th>
   306 <th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
   302 <td>
   307 <td>
   303 <form method="post">
   308 <form method="post">
   304 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
   309 			<?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?>
   305 <?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
   310 			<?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
   306 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
   311 			<?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ); ?>
   307 </form>
   312 </form>
   308 </td>
   313 </td>
   309 </tr>
   314 </tr>
   310 <?php endif; ?>
   315 		<?php endif; ?>
   311 
   316 
   312 <?php if ( current_user_can( 'upload_files' ) ): ?>
   317 		<?php if ( current_user_can( 'upload_files' ) ) : ?>
   313 <tr>
   318 <tr>
   314 <th scope="row"><?php _e('Select Image'); ?></th>
   319 <th scope="row"><?php _e( 'Select Image' ); ?></th>
   315 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
   320 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
   316 	<p>
   321 	<p>
   317 		<label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
   322 		<label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
   318 		<input type="file" id="upload" name="import" />
   323 		<input type="file" id="upload" name="import" />
   319 		<input type="hidden" name="action" value="save" />
   324 		<input type="hidden" name="action" value="save" />
   320 		<?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
   325 			<?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
   321 		<?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
   326 			<?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
   322 	</p>
   327 	</p>
   323 	<p>
   328 	<p>
   324 		<label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
   329 		<label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
   325 		<button id="choose-from-library-link" class="button"
   330 		<button id="choose-from-library-link" class="button"
   326 			data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
   331 			data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
   327 			data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button>
   332 			data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button>
   328 	</p>
   333 	</p>
   329 	</form>
   334 	</form>
   330 </td>
   335 </td>
   331 </tr>
   336 </tr>
   332 <?php endif; ?>
   337 		<?php endif; ?>
   333 </tbody>
   338 </tbody>
   334 </table>
   339 </table>
   335 
   340 
   336 <h3><?php _e( 'Display Options' ); ?></h3>
   341 <h2><?php _e( 'Display Options' ); ?></h2>
   337 <form method="post">
   342 <form method="post">
   338 <table class="form-table">
   343 <table class="form-table" role="presentation">
   339 <tbody>
   344 <tbody>
   340 <?php if ( get_background_image() ) : ?>
   345 		<?php if ( get_background_image() ) : ?>
   341 <input name="background-preset" type="hidden" value="custom">
   346 <input name="background-preset" type="hidden" value="custom">
   342 
   347 
   343 <?php
   348 			<?php
   344 $background_position = sprintf(
   349 			$background_position = sprintf(
   345 	'%s %s',
   350 				'%s %s',
   346 	get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ),
   351 				get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ),
   347 	get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) )
   352 				get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) )
   348 );
   353 			);
   349 
   354 
   350 $background_position_options = array(
   355 			$background_position_options = array(
   351 	array(
   356 				array(
   352 		'left top'   => array( 'label' => __( 'Top Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
   357 					'left top'   => array(
   353 		'center top' => array( 'label' => __( 'Top' ), 'icon' => 'dashicons dashicons-arrow-up-alt' ),
   358 						'label' => __( 'Top Left' ),
   354 		'right top'  => array( 'label' => __( 'Top Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
   359 						'icon'  => 'dashicons dashicons-arrow-left-alt',
   355 	),
   360 					),
   356 	array(
   361 					'center top' => array(
   357 		'left center'   => array( 'label' => __( 'Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
   362 						'label' => __( 'Top' ),
   358 		'center center' => array( 'label' => __( 'Center' ), 'icon' => 'background-position-center-icon' ),
   363 						'icon'  => 'dashicons dashicons-arrow-up-alt',
   359 		'right center'  => array( 'label' => __( 'Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
   364 					),
   360 	),
   365 					'right top'  => array(
   361 	array(
   366 						'label' => __( 'Top Right' ),
   362 		'left bottom'   => array( 'label' => __( 'Bottom Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
   367 						'icon'  => 'dashicons dashicons-arrow-right-alt',
   363 		'center bottom' => array( 'label' => __( 'Bottom' ), 'icon' => 'dashicons dashicons-arrow-down-alt' ),
   368 					),
   364 		'right bottom'  => array( 'label' => __( 'Bottom Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
   369 				),
   365 	),
   370 				array(
   366 );
   371 					'left center'   => array(
   367 ?>
   372 						'label' => __( 'Left' ),
       
   373 						'icon'  => 'dashicons dashicons-arrow-left-alt',
       
   374 					),
       
   375 					'center center' => array(
       
   376 						'label' => __( 'Center' ),
       
   377 						'icon'  => 'background-position-center-icon',
       
   378 					),
       
   379 					'right center'  => array(
       
   380 						'label' => __( 'Right' ),
       
   381 						'icon'  => 'dashicons dashicons-arrow-right-alt',
       
   382 					),
       
   383 				),
       
   384 				array(
       
   385 					'left bottom'   => array(
       
   386 						'label' => __( 'Bottom Left' ),
       
   387 						'icon'  => 'dashicons dashicons-arrow-left-alt',
       
   388 					),
       
   389 					'center bottom' => array(
       
   390 						'label' => __( 'Bottom' ),
       
   391 						'icon'  => 'dashicons dashicons-arrow-down-alt',
       
   392 					),
       
   393 					'right bottom'  => array(
       
   394 						'label' => __( 'Bottom Right' ),
       
   395 						'icon'  => 'dashicons dashicons-arrow-right-alt',
       
   396 					),
       
   397 				),
       
   398 			);
       
   399 			?>
   368 <tr>
   400 <tr>
   369 <th scope="row"><?php _e( 'Image Position' ); ?></th>
   401 <th scope="row"><?php _e( 'Image Position' ); ?></th>
   370 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
   402 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
   371 <div class="background-position-control">
   403 <div class="background-position-control">
   372 <?php foreach ( $background_position_options as $group ) : ?>
   404 			<?php foreach ( $background_position_options as $group ) : ?>
   373 	<div class="button-group">
   405 	<div class="button-group">
   374 	<?php foreach ( $group as $value => $input ) : ?>
   406 				<?php foreach ( $group as $value => $input ) : ?>
   375 		<label>
   407 		<label>
   376 			<input class="screen-reader-text" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>>
   408 			<input class="screen-reader-text" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>>
   377 			<span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
   409 			<span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
   378 			<span class="screen-reader-text"><?php echo $input['label']; ?></span>
   410 			<span class="screen-reader-text"><?php echo $input['label']; ?></span>
   379 		</label>
   411 		</label>
   412 </tr>
   444 </tr>
   413 <?php endif; // get_background_image() ?>
   445 <?php endif; // get_background_image() ?>
   414 <tr>
   446 <tr>
   415 <th scope="row"><?php _e( 'Background Color' ); ?></th>
   447 <th scope="row"><?php _e( 'Background Color' ); ?></th>
   416 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
   448 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
   417 <?php
   449 		<?php
   418 $default_color = '';
   450 		$default_color = '';
   419 if ( current_theme_supports( 'custom-background', 'default-color' ) )
   451 		if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
   420 	$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
   452 			$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
   421 ?>
   453 		}
   422 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color ?>>
   454 		?>
       
   455 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color; ?>>
   423 </fieldset></td>
   456 </fieldset></td>
   424 </tr>
   457 </tr>
   425 </tbody>
   458 </tbody>
   426 </table>
   459 </table>
   427 
   460 
   428 <?php wp_nonce_field('custom-background'); ?>
   461 		<?php wp_nonce_field( 'custom-background' ); ?>
   429 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
   462 		<?php submit_button( null, 'primary', 'save-background-options' ); ?>
   430 </form>
   463 </form>
   431 
   464 
   432 </div>
   465 </div>
   433 <?php
   466 		<?php
   434 	}
   467 	}
   435 
   468 
   436 	/**
   469 	/**
   437 	 * Handle an Image upload for the background image.
   470 	 * Handle an Image upload for the background image.
   438 	 *
   471 	 *
   439 	 * @since 3.0.0
   472 	 * @since 3.0.0
   440 	 */
   473 	 */
   441 	public function handle_upload() {
   474 	public function handle_upload() {
   442 		if ( empty($_FILES) )
   475 		if ( empty( $_FILES ) ) {
   443 			return;
   476 			return;
   444 
   477 		}
   445 		check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
   478 
   446 		$overrides = array('test_form' => false);
   479 		check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );
       
   480 		$overrides = array( 'test_form' => false );
   447 
   481 
   448 		$uploaded_file = $_FILES['import'];
   482 		$uploaded_file = $_FILES['import'];
   449 		$wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
   483 		$wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
   450 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
   484 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
   451 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
   485 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
   452 
   486 		}
   453 		$file = wp_handle_upload($uploaded_file, $overrides);
   487 
   454 
   488 		$file = wp_handle_upload( $uploaded_file, $overrides );
   455 		if ( isset($file['error']) )
   489 
       
   490 		if ( isset( $file['error'] ) ) {
   456 			wp_die( $file['error'] );
   491 			wp_die( $file['error'] );
   457 
   492 		}
   458 		$url = $file['url'];
   493 
   459 		$type = $file['type'];
   494 		$url      = $file['url'];
   460 		$file = $file['file'];
   495 		$type     = $file['type'];
   461 		$filename = basename($file);
   496 		$file     = $file['file'];
       
   497 		$filename = wp_basename( $file );
   462 
   498 
   463 		// Construct the object array
   499 		// Construct the object array
   464 		$object = array(
   500 		$object = array(
   465 			'post_title' => $filename,
   501 			'post_title'     => $filename,
   466 			'post_content' => $url,
   502 			'post_content'   => $url,
   467 			'post_mime_type' => $type,
   503 			'post_mime_type' => $type,
   468 			'guid' => $url,
   504 			'guid'           => $url,
   469 			'context' => 'custom-background'
   505 			'context'        => 'custom-background',
   470 		);
   506 		);
   471 
   507 
   472 		// Save the data
   508 		// Save the data
   473 		$id = wp_insert_attachment($object, $file);
   509 		$id = wp_insert_attachment( $object, $file );
   474 
   510 
   475 		// Add the meta-data
   511 		// Add the meta-data
   476 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
   512 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
   477 		update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
   513 		update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
   478 
   514 
   479 		set_theme_mod('background_image', esc_url_raw($url));
   515 		set_theme_mod( 'background_image', esc_url_raw( $url ) );
   480 
   516 
   481 		$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
   517 		$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
   482 		set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
   518 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
   483 
   519 
   484 		/** This action is documented in wp-admin/custom-header.php */
   520 		/** This action is documented in wp-admin/custom-header.php */
   485 		do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
   521 		do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
   486 		$this->updated = true;
   522 		$this->updated = true;
   487 	}
   523 	}
   488 
   524 
   489 	/**
   525 	/**
   490 	 * Ajax handler for adding custom background context to an attachment.
   526 	 * Ajax handler for adding custom background context to an attachment.
   491 	 *
   527 	 *
   492 	 * Triggered when the user adds a new background image from the
   528 	 * Triggers when the user adds a new background image from the
   493 	 * Media Manager.
   529 	 * Media Manager.
   494 	 *
   530 	 *
   495 	 * @since 4.1.0
   531 	 * @since 4.1.0
   496 	 */
   532 	 */
   497 	public function ajax_background_add() {
   533 	public function ajax_background_add() {
   510 
   546 
   511 		wp_send_json_success();
   547 		wp_send_json_success();
   512 	}
   548 	}
   513 
   549 
   514 	/**
   550 	/**
   515 	 *
       
   516 	 * @since 3.4.0
   551 	 * @since 3.4.0
   517 	 * @deprecated 3.5.0
   552 	 * @deprecated 3.5.0
   518 	 *
   553 	 *
   519 	 * @param array $form_fields
   554 	 * @param array $form_fields
   520 	 * @return array $form_fields
   555 	 * @return array $form_fields
   522 	public function attachment_fields_to_edit( $form_fields ) {
   557 	public function attachment_fields_to_edit( $form_fields ) {
   523 		return $form_fields;
   558 		return $form_fields;
   524 	}
   559 	}
   525 
   560 
   526 	/**
   561 	/**
   527 	 *
       
   528 	 * @since 3.4.0
   562 	 * @since 3.4.0
   529 	 * @deprecated 3.5.0
   563 	 * @deprecated 3.5.0
   530 	 *
   564 	 *
   531 	 * @param array $tabs
   565 	 * @param array $tabs
   532 	 * @return array $tabs
   566 	 * @return array $tabs
   534 	public function filter_upload_tabs( $tabs ) {
   568 	public function filter_upload_tabs( $tabs ) {
   535 		return $tabs;
   569 		return $tabs;
   536 	}
   570 	}
   537 
   571 
   538 	/**
   572 	/**
   539 	 *
       
   540 	 * @since 3.4.0
   573 	 * @since 3.4.0
   541 	 * @deprecated 3.5.0
   574 	 * @deprecated 3.5.0
   542 	 */
   575 	 */
   543 	public function wp_set_background_image() {
   576 	public function wp_set_background_image() {
   544 		if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
   577 		if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
   545 		$attachment_id = absint($_POST['attachment_id']);
   578 			exit;
       
   579 		}
       
   580 		$attachment_id = absint( $_POST['attachment_id'] );
   546 		/** This filter is documented in wp-admin/includes/media.php */
   581 		/** This filter is documented in wp-admin/includes/media.php */
   547 		$sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
   582 		$sizes = array_keys(
   548 		$size = 'thumbnail';
   583 			apply_filters(
   549 		if ( in_array( $_POST['size'], $sizes ) )
   584 				'image_size_names_choose',
       
   585 				array(
       
   586 					'thumbnail' => __( 'Thumbnail' ),
       
   587 					'medium'    => __( 'Medium' ),
       
   588 					'large'     => __( 'Large' ),
       
   589 					'full'      => __( 'Full Size' ),
       
   590 				)
       
   591 			)
       
   592 		);
       
   593 		$size  = 'thumbnail';
       
   594 		if ( in_array( $_POST['size'], $sizes ) ) {
   550 			$size = esc_attr( $_POST['size'] );
   595 			$size = esc_attr( $_POST['size'] );
   551 
   596 		}
   552 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
   597 
   553 		$url = wp_get_attachment_image_src( $attachment_id, $size );
   598 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
       
   599 		$url       = wp_get_attachment_image_src( $attachment_id, $size );
   554 		$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
   600 		$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
   555 		set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
   601 		set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
   556 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
   602 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
   557 		exit;
   603 		exit;
   558 	}
   604 	}