wp/wp-admin/includes/class-custom-background.php
changeset 16 a86126ab1dd4
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
       
     1 <?php
       
     2 /**
       
     3  * The custom background script.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Administration
       
     7  */
       
     8 
       
     9 /**
       
    10  * The custom background class.
       
    11  *
       
    12  * @since 3.0.0
       
    13  */
       
    14 class Custom_Background {
       
    15 
       
    16 	/**
       
    17 	 * Callback for administration header.
       
    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 				/* translators: %s: URL to background image configuration in Customizer. */
       
   234 				__( 'You can now manage and live-preview Custom Backgrounds in the <a href="%s">Customizer</a>.' ),
       
   235 				admin_url( 'customize.php?autofocus[control]=background_image' )
       
   236 			);
       
   237 			?>
       
   238 	</p>
       
   239 </div>
       
   240 		<?php } ?>
       
   241 
       
   242 		<?php if ( ! empty( $this->updated ) ) { ?>
       
   243 <div id="message" class="updated">
       
   244 	<p>
       
   245 			<?php
       
   246 			/* translators: %s: Home URL. */
       
   247 			printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) );
       
   248 			?>
       
   249 	</p>
       
   250 </div>
       
   251 		<?php } ?>
       
   252 
       
   253 <h2><?php _e( 'Background Image' ); ?></h2>
       
   254 
       
   255 <table class="form-table" role="presentation">
       
   256 <tbody>
       
   257 <tr>
       
   258 <th scope="row"><?php _e( 'Preview' ); ?></th>
       
   259 <td>
       
   260 		<?php
       
   261 		if ( $this->admin_image_div_callback ) {
       
   262 			call_user_func( $this->admin_image_div_callback );
       
   263 		} else {
       
   264 			$background_styles = '';
       
   265 			$bgcolor           = get_background_color();
       
   266 			if ( $bgcolor ) {
       
   267 				$background_styles .= 'background-color: #' . $bgcolor . ';';
       
   268 			}
       
   269 
       
   270 			$background_image_thumb = get_background_image();
       
   271 			if ( $background_image_thumb ) {
       
   272 				$background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
       
   273 				$background_position_x  = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
       
   274 				$background_position_y  = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
       
   275 				$background_size        = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
       
   276 				$background_repeat      = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
       
   277 				$background_attachment  = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
       
   278 
       
   279 				// Background-image URL must be single quote, see below.
       
   280 				$background_styles .= " background-image: url('$background_image_thumb');"
       
   281 				. " background-size: $background_size;"
       
   282 				. " background-position: $background_position_x $background_position_y;"
       
   283 				. " background-repeat: $background_repeat;"
       
   284 				. " background-attachment: $background_attachment;";
       
   285 			}
       
   286 			?>
       
   287 	<div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // Must be double quote, see above. ?>
       
   288 			<?php if ( $background_image_thumb ) { ?>
       
   289 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
       
   290 		<img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
       
   291 		<?php } ?>
       
   292 	</div>
       
   293 	<?php } ?>
       
   294 </td>
       
   295 </tr>
       
   296 
       
   297 		<?php if ( get_background_image() ) : ?>
       
   298 <tr>
       
   299 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
       
   300 <td>
       
   301 <form method="post">
       
   302 			<?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?>
       
   303 			<?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
       
   304 			<?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ); ?>
       
   305 </form>
       
   306 </td>
       
   307 </tr>
       
   308 		<?php endif; ?>
       
   309 
       
   310 		<?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
       
   311 		<?php if ( $default_image && get_background_image() != $default_image ) : ?>
       
   312 <tr>
       
   313 <th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
       
   314 <td>
       
   315 <form method="post">
       
   316 			<?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?>
       
   317 			<?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
       
   318 			<?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ); ?>
       
   319 </form>
       
   320 </td>
       
   321 </tr>
       
   322 		<?php endif; ?>
       
   323 
       
   324 		<?php if ( current_user_can( 'upload_files' ) ) : ?>
       
   325 <tr>
       
   326 <th scope="row"><?php _e( 'Select Image' ); ?></th>
       
   327 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
       
   328 	<p>
       
   329 		<label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
       
   330 		<input type="file" id="upload" name="import" />
       
   331 		<input type="hidden" name="action" value="save" />
       
   332 			<?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
       
   333 			<?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
       
   334 	</p>
       
   335 	<p>
       
   336 		<label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
       
   337 		<button id="choose-from-library-link" class="button"
       
   338 			data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
       
   339 			data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button>
       
   340 	</p>
       
   341 	</form>
       
   342 </td>
       
   343 </tr>
       
   344 		<?php endif; ?>
       
   345 </tbody>
       
   346 </table>
       
   347 
       
   348 <h2><?php _e( 'Display Options' ); ?></h2>
       
   349 <form method="post">
       
   350 <table class="form-table" role="presentation">
       
   351 <tbody>
       
   352 		<?php if ( get_background_image() ) : ?>
       
   353 <input name="background-preset" type="hidden" value="custom">
       
   354 
       
   355 			<?php
       
   356 			$background_position = sprintf(
       
   357 				'%s %s',
       
   358 				get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ),
       
   359 				get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) )
       
   360 			);
       
   361 
       
   362 			$background_position_options = array(
       
   363 				array(
       
   364 					'left top'   => array(
       
   365 						'label' => __( 'Top Left' ),
       
   366 						'icon'  => 'dashicons dashicons-arrow-left-alt',
       
   367 					),
       
   368 					'center top' => array(
       
   369 						'label' => __( 'Top' ),
       
   370 						'icon'  => 'dashicons dashicons-arrow-up-alt',
       
   371 					),
       
   372 					'right top'  => array(
       
   373 						'label' => __( 'Top Right' ),
       
   374 						'icon'  => 'dashicons dashicons-arrow-right-alt',
       
   375 					),
       
   376 				),
       
   377 				array(
       
   378 					'left center'   => array(
       
   379 						'label' => __( 'Left' ),
       
   380 						'icon'  => 'dashicons dashicons-arrow-left-alt',
       
   381 					),
       
   382 					'center center' => array(
       
   383 						'label' => __( 'Center' ),
       
   384 						'icon'  => 'background-position-center-icon',
       
   385 					),
       
   386 					'right center'  => array(
       
   387 						'label' => __( 'Right' ),
       
   388 						'icon'  => 'dashicons dashicons-arrow-right-alt',
       
   389 					),
       
   390 				),
       
   391 				array(
       
   392 					'left bottom'   => array(
       
   393 						'label' => __( 'Bottom Left' ),
       
   394 						'icon'  => 'dashicons dashicons-arrow-left-alt',
       
   395 					),
       
   396 					'center bottom' => array(
       
   397 						'label' => __( 'Bottom' ),
       
   398 						'icon'  => 'dashicons dashicons-arrow-down-alt',
       
   399 					),
       
   400 					'right bottom'  => array(
       
   401 						'label' => __( 'Bottom Right' ),
       
   402 						'icon'  => 'dashicons dashicons-arrow-right-alt',
       
   403 					),
       
   404 				),
       
   405 			);
       
   406 			?>
       
   407 <tr>
       
   408 <th scope="row"><?php _e( 'Image Position' ); ?></th>
       
   409 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
       
   410 <div class="background-position-control">
       
   411 			<?php foreach ( $background_position_options as $group ) : ?>
       
   412 	<div class="button-group">
       
   413 				<?php foreach ( $group as $value => $input ) : ?>
       
   414 		<label>
       
   415 			<input class="screen-reader-text" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>>
       
   416 			<span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
       
   417 			<span class="screen-reader-text"><?php echo $input['label']; ?></span>
       
   418 		</label>
       
   419 	<?php endforeach; ?>
       
   420 	</div>
       
   421 <?php endforeach; ?>
       
   422 </div>
       
   423 </fieldset></td>
       
   424 </tr>
       
   425 
       
   426 <tr>
       
   427 <th scope="row"><label for="background-size"><?php _e( 'Image Size' ); ?></label></th>
       
   428 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Size' ); ?></span></legend>
       
   429 <select id="background-size" name="background-size">
       
   430 <option value="auto"<?php selected( 'auto', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _ex( 'Original', 'Original Size' ); ?></option>
       
   431 <option value="contain"<?php selected( 'contain', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fit to Screen' ); ?></option>
       
   432 <option value="cover"<?php selected( 'cover', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fill Screen' ); ?></option>
       
   433 </select>
       
   434 </fieldset></td>
       
   435 </tr>
       
   436 
       
   437 <tr>
       
   438 <th scope="row"><?php _ex( 'Repeat', 'Background Repeat' ); ?></th>
       
   439 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Repeat', 'Background Repeat' ); ?></span></legend>
       
   440 <input name="background-repeat" type="hidden" value="no-repeat">
       
   441 <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>
       
   442 </fieldset></td>
       
   443 </tr>
       
   444 
       
   445 <tr>
       
   446 <th scope="row"><?php _ex( 'Scroll', 'Background Scroll' ); ?></th>
       
   447 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Scroll', 'Background Scroll' ); ?></span></legend>
       
   448 <input name="background-attachment" type="hidden" value="fixed">
       
   449 <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>
       
   450 </fieldset></td>
       
   451 </tr>
       
   452 <?php endif; // get_background_image() ?>
       
   453 <tr>
       
   454 <th scope="row"><?php _e( 'Background Color' ); ?></th>
       
   455 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
       
   456 		<?php
       
   457 		$default_color = '';
       
   458 		if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
       
   459 			$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
       
   460 		}
       
   461 		?>
       
   462 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color; ?>>
       
   463 </fieldset></td>
       
   464 </tr>
       
   465 </tbody>
       
   466 </table>
       
   467 
       
   468 		<?php wp_nonce_field( 'custom-background' ); ?>
       
   469 		<?php submit_button( null, 'primary', 'save-background-options' ); ?>
       
   470 </form>
       
   471 
       
   472 </div>
       
   473 		<?php
       
   474 	}
       
   475 
       
   476 	/**
       
   477 	 * Handle an Image upload for the background image.
       
   478 	 *
       
   479 	 * @since 3.0.0
       
   480 	 */
       
   481 	public function handle_upload() {
       
   482 		if ( empty( $_FILES ) ) {
       
   483 			return;
       
   484 		}
       
   485 
       
   486 		check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );
       
   487 		$overrides = array( 'test_form' => false );
       
   488 
       
   489 		$uploaded_file = $_FILES['import'];
       
   490 		$wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
       
   491 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
       
   492 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
       
   493 		}
       
   494 
       
   495 		$file = wp_handle_upload( $uploaded_file, $overrides );
       
   496 
       
   497 		if ( isset( $file['error'] ) ) {
       
   498 			wp_die( $file['error'] );
       
   499 		}
       
   500 
       
   501 		$url      = $file['url'];
       
   502 		$type     = $file['type'];
       
   503 		$file     = $file['file'];
       
   504 		$filename = wp_basename( $file );
       
   505 
       
   506 		// Construct the object array.
       
   507 		$object = array(
       
   508 			'post_title'     => $filename,
       
   509 			'post_content'   => $url,
       
   510 			'post_mime_type' => $type,
       
   511 			'guid'           => $url,
       
   512 			'context'        => 'custom-background',
       
   513 		);
       
   514 
       
   515 		// Save the data.
       
   516 		$id = wp_insert_attachment( $object, $file );
       
   517 
       
   518 		// Add the metadata.
       
   519 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
       
   520 		update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
       
   521 
       
   522 		set_theme_mod( 'background_image', esc_url_raw( $url ) );
       
   523 
       
   524 		$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
       
   525 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
       
   526 
       
   527 		/** This action is documented in wp-admin/includes/class-custom-image-header.php */
       
   528 		do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication.
       
   529 		$this->updated = true;
       
   530 	}
       
   531 
       
   532 	/**
       
   533 	 * Ajax handler for adding custom background context to an attachment.
       
   534 	 *
       
   535 	 * Triggers when the user adds a new background image from the
       
   536 	 * Media Manager.
       
   537 	 *
       
   538 	 * @since 4.1.0
       
   539 	 */
       
   540 	public function ajax_background_add() {
       
   541 		check_ajax_referer( 'background-add', 'nonce' );
       
   542 
       
   543 		if ( ! current_user_can( 'edit_theme_options' ) ) {
       
   544 			wp_send_json_error();
       
   545 		}
       
   546 
       
   547 		$attachment_id = absint( $_POST['attachment_id'] );
       
   548 		if ( $attachment_id < 1 ) {
       
   549 			wp_send_json_error();
       
   550 		}
       
   551 
       
   552 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
       
   553 
       
   554 		wp_send_json_success();
       
   555 	}
       
   556 
       
   557 	/**
       
   558 	 * @since 3.4.0
       
   559 	 * @deprecated 3.5.0
       
   560 	 *
       
   561 	 * @param array $form_fields
       
   562 	 * @return array $form_fields
       
   563 	 */
       
   564 	public function attachment_fields_to_edit( $form_fields ) {
       
   565 		return $form_fields;
       
   566 	}
       
   567 
       
   568 	/**
       
   569 	 * @since 3.4.0
       
   570 	 * @deprecated 3.5.0
       
   571 	 *
       
   572 	 * @param array $tabs
       
   573 	 * @return array $tabs
       
   574 	 */
       
   575 	public function filter_upload_tabs( $tabs ) {
       
   576 		return $tabs;
       
   577 	}
       
   578 
       
   579 	/**
       
   580 	 * @since 3.4.0
       
   581 	 * @deprecated 3.5.0
       
   582 	 */
       
   583 	public function wp_set_background_image() {
       
   584 		if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
       
   585 			exit;
       
   586 		}
       
   587 
       
   588 		$attachment_id = absint( $_POST['attachment_id'] );
       
   589 
       
   590 		$sizes = array_keys(
       
   591 			/** This filter is documented in wp-admin/includes/media.php */
       
   592 			apply_filters(
       
   593 				'image_size_names_choose',
       
   594 				array(
       
   595 					'thumbnail' => __( 'Thumbnail' ),
       
   596 					'medium'    => __( 'Medium' ),
       
   597 					'large'     => __( 'Large' ),
       
   598 					'full'      => __( 'Full Size' ),
       
   599 				)
       
   600 			)
       
   601 		);
       
   602 
       
   603 		$size = 'thumbnail';
       
   604 		if ( in_array( $_POST['size'], $sizes, true ) ) {
       
   605 			$size = esc_attr( $_POST['size'] );
       
   606 		}
       
   607 
       
   608 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
       
   609 
       
   610 		$url       = wp_get_attachment_image_src( $attachment_id, $size );
       
   611 		$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
       
   612 		set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
       
   613 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
       
   614 		exit;
       
   615 	}
       
   616 }