wp/wp-admin/custom-background.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 22 8c2e4d02f4ef
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * The custom background script.
     3  * Custom background script.
     4  *
     4  *
       
     5  * This file is deprecated, use 'wp-admin/includes/class-custom-background.php' instead.
       
     6  *
       
     7  * @deprecated 5.3.0
     5  * @package WordPress
     8  * @package WordPress
     6  * @subpackage Administration
     9  * @subpackage Administration
     7  */
    10  */
     8 
    11 
     9 /**
    12 _deprecated_file( basename( __FILE__ ), '5.3.0', 'wp-admin/includes/class-custom-background.php' );
    10  * The custom background class.
       
    11  *
       
    12  * @since 3.0.0
       
    13  */
       
    14 class Custom_Background {
       
    15 
    13 
    16 	/**
    14 /** Custom_Background class */
    17 	 * Callback for administration header.
    15 require_once ABSPATH . 'wp-admin/includes/class-custom-background.php';
    18 	 *
       
    19 	 * @var callable
       
    20 	 * @since 3.0.0
       
    21 	 */
       
    22 	public $admin_header_callback;
       
    23 
       
    24 	/**
       
    25 	 * Callback for header div.
       
    26 	 *
       
    27 	 * @var callable
       
    28 	 * @since 3.0.0
       
    29 	 */
       
    30 	public $admin_image_div_callback;
       
    31 
       
    32 	/**
       
    33 	 * Used to trigger a success message when settings updated and set to true.
       
    34 	 *
       
    35 	 * @since 3.0.0
       
    36 	 * @var bool
       
    37 	 */
       
    38 	private $updated;
       
    39 
       
    40 	/**
       
    41 	 * Constructor - Register administration header callback.
       
    42 	 *
       
    43 	 * @since 3.0.0
       
    44 	 * @param callable $admin_header_callback
       
    45 	 * @param callable $admin_image_div_callback Optional custom image div output callback.
       
    46 	 */
       
    47 	public function __construct( $admin_header_callback = '', $admin_image_div_callback = '' ) {
       
    48 		$this->admin_header_callback    = $admin_header_callback;
       
    49 		$this->admin_image_div_callback = $admin_image_div_callback;
       
    50 
       
    51 		add_action( 'admin_menu', array( $this, 'init' ) );
       
    52 
       
    53 		add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) );
       
    54 
       
    55 		// Unused since 3.5.0.
       
    56 		add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
       
    57 	}
       
    58 
       
    59 	/**
       
    60 	 * Set up the hooks for the Custom Background admin page.
       
    61 	 *
       
    62 	 * @since 3.0.0
       
    63 	 */
       
    64 	public function init() {
       
    65 		$page = add_theme_page( __( 'Background' ), __( 'Background' ), 'edit_theme_options', 'custom-background', array( $this, 'admin_page' ) );
       
    66 		if ( ! $page ) {
       
    67 			return;
       
    68 		}
       
    69 
       
    70 		add_action( "load-$page", array( $this, 'admin_load' ) );
       
    71 		add_action( "load-$page", array( $this, 'take_action' ), 49 );
       
    72 		add_action( "load-$page", array( $this, 'handle_upload' ), 49 );
       
    73 
       
    74 		if ( $this->admin_header_callback ) {
       
    75 			add_action( "admin_head-$page", $this->admin_header_callback, 51 );
       
    76 		}
       
    77 	}
       
    78 
       
    79 	/**
       
    80 	 * Set up the enqueue for the CSS & JavaScript files.
       
    81 	 *
       
    82 	 * @since 3.0.0
       
    83 	 */
       
    84 	public function admin_load() {
       
    85 		get_current_screen()->add_help_tab(
       
    86 			array(
       
    87 				'id'      => 'overview',
       
    88 				'title'   => __( 'Overview' ),
       
    89 				'content' =>
       
    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>' . __( '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>' . __( '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 					'<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>',
       
    94 			)
       
    95 		);
       
    96 
       
    97 		get_current_screen()->set_help_sidebar(
       
    98 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
       
    99 			'<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Background_Screen">Documentation on Custom Background</a>' ) . '</p>' .
       
   100 			'<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
       
   101 		);
       
   102 
       
   103 		wp_enqueue_media();
       
   104 		wp_enqueue_script( 'custom-background' );
       
   105 		wp_enqueue_style( 'wp-color-picker' );
       
   106 	}
       
   107 
       
   108 	/**
       
   109 	 * Execute custom background modification.
       
   110 	 *
       
   111 	 * @since 3.0.0
       
   112 	 */
       
   113 	public function take_action() {
       
   114 		if ( empty( $_POST ) ) {
       
   115 			return;
       
   116 		}
       
   117 
       
   118 		if ( isset( $_POST['reset-background'] ) ) {
       
   119 			check_admin_referer( 'custom-background-reset', '_wpnonce-custom-background-reset' );
       
   120 			remove_theme_mod( 'background_image' );
       
   121 			remove_theme_mod( 'background_image_thumb' );
       
   122 			$this->updated = true;
       
   123 			return;
       
   124 		}
       
   125 
       
   126 		if ( isset( $_POST['remove-background'] ) ) {
       
   127 			// @TODO: Uploaded files are not removed here.
       
   128 			check_admin_referer( 'custom-background-remove', '_wpnonce-custom-background-remove' );
       
   129 			set_theme_mod( 'background_image', '' );
       
   130 			set_theme_mod( 'background_image_thumb', '' );
       
   131 			$this->updated = true;
       
   132 			wp_safe_redirect( $_POST['_wp_http_referer'] );
       
   133 			return;
       
   134 		}
       
   135 
       
   136 		if ( isset( $_POST['background-preset'] ) ) {
       
   137 			check_admin_referer( 'custom-background' );
       
   138 
       
   139 			if ( in_array( $_POST['background-preset'], array( 'default', 'fill', 'fit', 'repeat', 'custom' ), true ) ) {
       
   140 				$preset = $_POST['background-preset'];
       
   141 			} else {
       
   142 				$preset = 'default';
       
   143 			}
       
   144 
       
   145 			set_theme_mod( 'background_preset', $preset );
       
   146 		}
       
   147 
       
   148 		if ( isset( $_POST['background-position'] ) ) {
       
   149 			check_admin_referer( 'custom-background' );
       
   150 
       
   151 			$position = explode( ' ', $_POST['background-position'] );
       
   152 
       
   153 			if ( in_array( $position[0], array( 'left', 'center', 'right' ), true ) ) {
       
   154 				$position_x = $position[0];
       
   155 			} else {
       
   156 				$position_x = 'left';
       
   157 			}
       
   158 
       
   159 			if ( in_array( $position[1], array( 'top', 'center', 'bottom' ), true ) ) {
       
   160 				$position_y = $position[1];
       
   161 			} else {
       
   162 				$position_y = 'top';
       
   163 			}
       
   164 
       
   165 			set_theme_mod( 'background_position_x', $position_x );
       
   166 			set_theme_mod( 'background_position_y', $position_y );
       
   167 		}
       
   168 
       
   169 		if ( isset( $_POST['background-size'] ) ) {
       
   170 			check_admin_referer( 'custom-background' );
       
   171 
       
   172 			if ( in_array( $_POST['background-size'], array( 'auto', 'contain', 'cover' ), true ) ) {
       
   173 				$size = $_POST['background-size'];
       
   174 			} else {
       
   175 				$size = 'auto';
       
   176 			}
       
   177 
       
   178 			set_theme_mod( 'background_size', $size );
       
   179 		}
       
   180 
       
   181 		if ( isset( $_POST['background-repeat'] ) ) {
       
   182 			check_admin_referer( 'custom-background' );
       
   183 
       
   184 			$repeat = $_POST['background-repeat'];
       
   185 
       
   186 			if ( 'no-repeat' !== $repeat ) {
       
   187 				$repeat = 'repeat';
       
   188 			}
       
   189 
       
   190 			set_theme_mod( 'background_repeat', $repeat );
       
   191 		}
       
   192 
       
   193 		if ( isset( $_POST['background-attachment'] ) ) {
       
   194 			check_admin_referer( 'custom-background' );
       
   195 
       
   196 			$attachment = $_POST['background-attachment'];
       
   197 
       
   198 			if ( 'fixed' !== $attachment ) {
       
   199 				$attachment = 'scroll';
       
   200 			}
       
   201 
       
   202 			set_theme_mod( 'background_attachment', $attachment );
       
   203 		}
       
   204 
       
   205 		if ( isset( $_POST['background-color'] ) ) {
       
   206 			check_admin_referer( 'custom-background' );
       
   207 			$color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['background-color'] );
       
   208 			if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) {
       
   209 				set_theme_mod( 'background_color', $color );
       
   210 			} else {
       
   211 				set_theme_mod( 'background_color', '' );
       
   212 			}
       
   213 		}
       
   214 
       
   215 		$this->updated = true;
       
   216 	}
       
   217 
       
   218 	/**
       
   219 	 * Display the custom background page.
       
   220 	 *
       
   221 	 * @since 3.0.0
       
   222 	 */
       
   223 	public function admin_page() {
       
   224 		?>
       
   225 <div class="wrap" id="custom-background">
       
   226 <h1><?php _e( 'Custom Background' ); ?></h1>
       
   227 
       
   228 		<?php if ( current_user_can( 'customize' ) ) { ?>
       
   229 <div class="notice notice-info hide-if-no-customize">
       
   230 	<p>
       
   231 			<?php
       
   232 				printf(
       
   233 					__( 'You can now manage and live-preview Custom Backgrounds in the <a href="%1$s">Customizer</a>.' ),
       
   234 					admin_url( 'customize.php?autofocus[control]=background_image' )
       
   235 				);
       
   236 			?>
       
   237 	</p>
       
   238 </div>
       
   239 		<?php } ?>
       
   240 
       
   241 		<?php if ( ! empty( $this->updated ) ) { ?>
       
   242 <div id="message" class="updated">
       
   243 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
       
   244 </div>
       
   245 		<?php } ?>
       
   246 
       
   247 <h2><?php _e( 'Background Image' ); ?></h2>
       
   248 
       
   249 <table class="form-table" role="presentation">
       
   250 <tbody>
       
   251 <tr>
       
   252 <th scope="row"><?php _e( 'Preview' ); ?></th>
       
   253 <td>
       
   254 		<?php
       
   255 		if ( $this->admin_image_div_callback ) {
       
   256 			call_user_func( $this->admin_image_div_callback );
       
   257 		} else {
       
   258 			$background_styles = '';
       
   259 			if ( $bgcolor = get_background_color() ) {
       
   260 				$background_styles .= 'background-color: #' . $bgcolor . ';';
       
   261 			}
       
   262 
       
   263 			$background_image_thumb = get_background_image();
       
   264 			if ( $background_image_thumb ) {
       
   265 				$background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
       
   266 				$background_position_x  = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
       
   267 				$background_position_y  = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
       
   268 				$background_size        = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
       
   269 				$background_repeat      = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
       
   270 				$background_attachment  = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
       
   271 
       
   272 				// Background-image URL must be single quote, see below.
       
   273 				$background_styles .= " background-image: url('$background_image_thumb');"
       
   274 				. " background-size: $background_size;"
       
   275 				. " background-position: $background_position_x $background_position_y;"
       
   276 				. " background-repeat: $background_repeat;"
       
   277 				. " background-attachment: $background_attachment;";
       
   278 			}
       
   279 			?>
       
   280 	<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
       
   281 			<?php if ( $background_image_thumb ) { ?>
       
   282 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
       
   283 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
       
   284 		<?php } ?>
       
   285 	</div>
       
   286 	<?php } ?>
       
   287 </td>
       
   288 </tr>
       
   289 
       
   290 		<?php if ( get_background_image() ) : ?>
       
   291 <tr>
       
   292 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
       
   293 <td>
       
   294 <form method="post">
       
   295 			<?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?>
       
   296 			<?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
       
   297 			<?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ); ?>
       
   298 </form>
       
   299 </td>
       
   300 </tr>
       
   301 		<?php endif; ?>
       
   302 
       
   303 		<?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
       
   304 		<?php if ( $default_image && get_background_image() != $default_image ) : ?>
       
   305 <tr>
       
   306 <th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
       
   307 <td>
       
   308 <form method="post">
       
   309 			<?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?>
       
   310 			<?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
       
   311 			<?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ); ?>
       
   312 </form>
       
   313 </td>
       
   314 </tr>
       
   315 		<?php endif; ?>
       
   316 
       
   317 		<?php if ( current_user_can( 'upload_files' ) ) : ?>
       
   318 <tr>
       
   319 <th scope="row"><?php _e( 'Select Image' ); ?></th>
       
   320 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
       
   321 	<p>
       
   322 		<label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
       
   323 		<input type="file" id="upload" name="import" />
       
   324 		<input type="hidden" name="action" value="save" />
       
   325 			<?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
       
   326 			<?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
       
   327 	</p>
       
   328 	<p>
       
   329 		<label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
       
   330 		<button id="choose-from-library-link" class="button"
       
   331 			data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
       
   332 			data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button>
       
   333 	</p>
       
   334 	</form>
       
   335 </td>
       
   336 </tr>
       
   337 		<?php endif; ?>
       
   338 </tbody>
       
   339 </table>
       
   340 
       
   341 <h2><?php _e( 'Display Options' ); ?></h2>
       
   342 <form method="post">
       
   343 <table class="form-table" role="presentation">
       
   344 <tbody>
       
   345 		<?php if ( get_background_image() ) : ?>
       
   346 <input name="background-preset" type="hidden" value="custom">
       
   347 
       
   348 			<?php
       
   349 			$background_position = sprintf(
       
   350 				'%s %s',
       
   351 				get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ),
       
   352 				get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) )
       
   353 			);
       
   354 
       
   355 			$background_position_options = array(
       
   356 				array(
       
   357 					'left top'   => array(
       
   358 						'label' => __( 'Top Left' ),
       
   359 						'icon'  => 'dashicons dashicons-arrow-left-alt',
       
   360 					),
       
   361 					'center top' => array(
       
   362 						'label' => __( 'Top' ),
       
   363 						'icon'  => 'dashicons dashicons-arrow-up-alt',
       
   364 					),
       
   365 					'right top'  => array(
       
   366 						'label' => __( 'Top Right' ),
       
   367 						'icon'  => 'dashicons dashicons-arrow-right-alt',
       
   368 					),
       
   369 				),
       
   370 				array(
       
   371 					'left center'   => array(
       
   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 			?>
       
   400 <tr>
       
   401 <th scope="row"><?php _e( 'Image Position' ); ?></th>
       
   402 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
       
   403 <div class="background-position-control">
       
   404 			<?php foreach ( $background_position_options as $group ) : ?>
       
   405 	<div class="button-group">
       
   406 				<?php foreach ( $group as $value => $input ) : ?>
       
   407 		<label>
       
   408 			<input class="screen-reader-text" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>>
       
   409 			<span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
       
   410 			<span class="screen-reader-text"><?php echo $input['label']; ?></span>
       
   411 		</label>
       
   412 	<?php endforeach; ?>
       
   413 	</div>
       
   414 <?php endforeach; ?>
       
   415 </div>
       
   416 </fieldset></td>
       
   417 </tr>
       
   418 
       
   419 <tr>
       
   420 <th scope="row"><label for="background-size"><?php _e( 'Image Size' ); ?></label></th>
       
   421 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Size' ); ?></span></legend>
       
   422 <select id="background-size" name="background-size">
       
   423 <option value="auto"<?php selected( 'auto', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _ex( 'Original', 'Original Size' ); ?></option>
       
   424 <option value="contain"<?php selected( 'contain', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fit to Screen' ); ?></option>
       
   425 <option value="cover"<?php selected( 'cover', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fill Screen' ); ?></option>
       
   426 </select>
       
   427 </fieldset></td>
       
   428 </tr>
       
   429 
       
   430 <tr>
       
   431 <th scope="row"><?php _ex( 'Repeat', 'Background Repeat' ); ?></th>
       
   432 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Repeat', 'Background Repeat' ); ?></span></legend>
       
   433 <input name="background-repeat" type="hidden" value="no-repeat">
       
   434 <label><input type="checkbox" name="background-repeat" value="repeat"<?php checked( 'repeat', get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) ) ); ?>> <?php _e( 'Repeat Background Image' ); ?></label>
       
   435 </fieldset></td>
       
   436 </tr>
       
   437 
       
   438 <tr>
       
   439 <th scope="row"><?php _ex( 'Scroll', 'Background Scroll' ); ?></th>
       
   440 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Scroll', 'Background Scroll' ); ?></span></legend>
       
   441 <input name="background-attachment" type="hidden" value="fixed">
       
   442 <label><input name="background-attachment" type="checkbox" value="scroll" <?php checked( 'scroll', get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) ) ); ?>> <?php _e( 'Scroll with Page' ); ?></label>
       
   443 </fieldset></td>
       
   444 </tr>
       
   445 <?php endif; // get_background_image() ?>
       
   446 <tr>
       
   447 <th scope="row"><?php _e( 'Background Color' ); ?></th>
       
   448 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
       
   449 		<?php
       
   450 		$default_color = '';
       
   451 		if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
       
   452 			$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
       
   453 		}
       
   454 		?>
       
   455 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color; ?>>
       
   456 </fieldset></td>
       
   457 </tr>
       
   458 </tbody>
       
   459 </table>
       
   460 
       
   461 		<?php wp_nonce_field( 'custom-background' ); ?>
       
   462 		<?php submit_button( null, 'primary', 'save-background-options' ); ?>
       
   463 </form>
       
   464 
       
   465 </div>
       
   466 		<?php
       
   467 	}
       
   468 
       
   469 	/**
       
   470 	 * Handle an Image upload for the background image.
       
   471 	 *
       
   472 	 * @since 3.0.0
       
   473 	 */
       
   474 	public function handle_upload() {
       
   475 		if ( empty( $_FILES ) ) {
       
   476 			return;
       
   477 		}
       
   478 
       
   479 		check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );
       
   480 		$overrides = array( 'test_form' => false );
       
   481 
       
   482 		$uploaded_file = $_FILES['import'];
       
   483 		$wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
       
   484 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
       
   485 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
       
   486 		}
       
   487 
       
   488 		$file = wp_handle_upload( $uploaded_file, $overrides );
       
   489 
       
   490 		if ( isset( $file['error'] ) ) {
       
   491 			wp_die( $file['error'] );
       
   492 		}
       
   493 
       
   494 		$url      = $file['url'];
       
   495 		$type     = $file['type'];
       
   496 		$file     = $file['file'];
       
   497 		$filename = wp_basename( $file );
       
   498 
       
   499 		// Construct the object array
       
   500 		$object = array(
       
   501 			'post_title'     => $filename,
       
   502 			'post_content'   => $url,
       
   503 			'post_mime_type' => $type,
       
   504 			'guid'           => $url,
       
   505 			'context'        => 'custom-background',
       
   506 		);
       
   507 
       
   508 		// Save the data
       
   509 		$id = wp_insert_attachment( $object, $file );
       
   510 
       
   511 		// Add the meta-data
       
   512 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
       
   513 		update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
       
   514 
       
   515 		set_theme_mod( 'background_image', esc_url_raw( $url ) );
       
   516 
       
   517 		$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
       
   518 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
       
   519 
       
   520 		/** This action is documented in wp-admin/custom-header.php */
       
   521 		do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
       
   522 		$this->updated = true;
       
   523 	}
       
   524 
       
   525 	/**
       
   526 	 * Ajax handler for adding custom background context to an attachment.
       
   527 	 *
       
   528 	 * Triggers when the user adds a new background image from the
       
   529 	 * Media Manager.
       
   530 	 *
       
   531 	 * @since 4.1.0
       
   532 	 */
       
   533 	public function ajax_background_add() {
       
   534 		check_ajax_referer( 'background-add', 'nonce' );
       
   535 
       
   536 		if ( ! current_user_can( 'edit_theme_options' ) ) {
       
   537 			wp_send_json_error();
       
   538 		}
       
   539 
       
   540 		$attachment_id = absint( $_POST['attachment_id'] );
       
   541 		if ( $attachment_id < 1 ) {
       
   542 			wp_send_json_error();
       
   543 		}
       
   544 
       
   545 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
       
   546 
       
   547 		wp_send_json_success();
       
   548 	}
       
   549 
       
   550 	/**
       
   551 	 * @since 3.4.0
       
   552 	 * @deprecated 3.5.0
       
   553 	 *
       
   554 	 * @param array $form_fields
       
   555 	 * @return array $form_fields
       
   556 	 */
       
   557 	public function attachment_fields_to_edit( $form_fields ) {
       
   558 		return $form_fields;
       
   559 	}
       
   560 
       
   561 	/**
       
   562 	 * @since 3.4.0
       
   563 	 * @deprecated 3.5.0
       
   564 	 *
       
   565 	 * @param array $tabs
       
   566 	 * @return array $tabs
       
   567 	 */
       
   568 	public function filter_upload_tabs( $tabs ) {
       
   569 		return $tabs;
       
   570 	}
       
   571 
       
   572 	/**
       
   573 	 * @since 3.4.0
       
   574 	 * @deprecated 3.5.0
       
   575 	 */
       
   576 	public function wp_set_background_image() {
       
   577 		if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
       
   578 			exit;
       
   579 		}
       
   580 		$attachment_id = absint( $_POST['attachment_id'] );
       
   581 		/** This filter is documented in wp-admin/includes/media.php */
       
   582 		$sizes = array_keys(
       
   583 			apply_filters(
       
   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 ) ) {
       
   595 			$size = esc_attr( $_POST['size'] );
       
   596 		}
       
   597 
       
   598 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
       
   599 		$url       = wp_get_attachment_image_src( $attachment_id, $size );
       
   600 		$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
       
   601 		set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
       
   602 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
       
   603 		exit;
       
   604 	}
       
   605 }