wp/wp-admin/includes/class-custom-background.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     9 /**
     9 /**
    10  * The custom background class.
    10  * The custom background class.
    11  *
    11  *
    12  * @since 3.0.0
    12  * @since 3.0.0
    13  */
    13  */
       
    14 #[AllowDynamicProperties]
    14 class Custom_Background {
    15 class Custom_Background {
    15 
    16 
    16 	/**
    17 	/**
    17 	 * Callback for administration header.
    18 	 * Callback for administration header.
    18 	 *
    19 	 *
    36 	 * @var bool
    37 	 * @var bool
    37 	 */
    38 	 */
    38 	private $updated;
    39 	private $updated;
    39 
    40 
    40 	/**
    41 	/**
    41 	 * Constructor - Register administration header callback.
    42 	 * Constructor - Registers administration header callback.
    42 	 *
    43 	 *
    43 	 * @since 3.0.0
    44 	 * @since 3.0.0
    44 	 * @param callable $admin_header_callback
    45 	 *
    45 	 * @param callable $admin_image_div_callback Optional custom image div output callback.
    46 	 * @param callable $admin_header_callback    Optional. Administration header callback.
       
    47 	 *                                           Default empty string.
       
    48 	 * @param callable $admin_image_div_callback Optional. Custom image div output callback.
       
    49 	 *                                           Default empty string.
    46 	 */
    50 	 */
    47 	public function __construct( $admin_header_callback = '', $admin_image_div_callback = '' ) {
    51 	public function __construct( $admin_header_callback = '', $admin_image_div_callback = '' ) {
    48 		$this->admin_header_callback    = $admin_header_callback;
    52 		$this->admin_header_callback    = $admin_header_callback;
    49 		$this->admin_image_div_callback = $admin_image_div_callback;
    53 		$this->admin_image_div_callback = $admin_image_div_callback;
    50 
    54 
    55 		// Unused since 3.5.0.
    59 		// Unused since 3.5.0.
    56 		add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
    60 		add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
    57 	}
    61 	}
    58 
    62 
    59 	/**
    63 	/**
    60 	 * Set up the hooks for the Custom Background admin page.
    64 	 * Sets up the hooks for the Custom Background admin page.
    61 	 *
    65 	 *
    62 	 * @since 3.0.0
    66 	 * @since 3.0.0
    63 	 */
    67 	 */
    64 	public function init() {
    68 	public function init() {
    65 		$page = add_theme_page( __( 'Background' ), __( 'Background' ), 'edit_theme_options', 'custom-background', array( $this, 'admin_page' ) );
    69 		$page = add_theme_page(
       
    70 			_x( 'Background', 'custom background' ),
       
    71 			_x( 'Background', 'custom background' ),
       
    72 			'edit_theme_options',
       
    73 			'custom-background',
       
    74 			array( $this, 'admin_page' )
       
    75 		);
       
    76 
    66 		if ( ! $page ) {
    77 		if ( ! $page ) {
    67 			return;
    78 			return;
    68 		}
    79 		}
    69 
    80 
    70 		add_action( "load-{$page}", array( $this, 'admin_load' ) );
    81 		add_action( "load-{$page}", array( $this, 'admin_load' ) );
    75 			add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
    86 			add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
    76 		}
    87 		}
    77 	}
    88 	}
    78 
    89 
    79 	/**
    90 	/**
    80 	 * Set up the enqueue for the CSS & JavaScript files.
    91 	 * Sets up the enqueue for the CSS & JavaScript files.
    81 	 *
    92 	 *
    82 	 * @since 3.0.0
    93 	 * @since 3.0.0
    83 	 */
    94 	 */
    84 	public function admin_load() {
    95 	public function admin_load() {
    85 		get_current_screen()->add_help_tab(
    96 		get_current_screen()->add_help_tab(
    95 		);
   106 		);
    96 
   107 
    97 		get_current_screen()->set_help_sidebar(
   108 		get_current_screen()->set_help_sidebar(
    98 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
   109 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
    99 			'<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Background_Screen">Documentation on Custom Background</a>' ) . '</p>' .
   110 			'<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>'
   111 			'<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
   101 		);
   112 		);
   102 
   113 
   103 		wp_enqueue_media();
   114 		wp_enqueue_media();
   104 		wp_enqueue_script( 'custom-background' );
   115 		wp_enqueue_script( 'custom-background' );
   105 		wp_enqueue_style( 'wp-color-picker' );
   116 		wp_enqueue_style( 'wp-color-picker' );
   106 	}
   117 	}
   107 
   118 
   108 	/**
   119 	/**
   109 	 * Execute custom background modification.
   120 	 * Executes custom background modification.
   110 	 *
   121 	 *
   111 	 * @since 3.0.0
   122 	 * @since 3.0.0
   112 	 */
   123 	 */
   113 	public function take_action() {
   124 	public function take_action() {
   114 		if ( empty( $_POST ) ) {
   125 		if ( empty( $_POST ) ) {
   220 
   231 
   221 		$this->updated = true;
   232 		$this->updated = true;
   222 	}
   233 	}
   223 
   234 
   224 	/**
   235 	/**
   225 	 * Display the custom background page.
   236 	 * Displays the custom background page.
   226 	 *
   237 	 *
   227 	 * @since 3.0.0
   238 	 * @since 3.0.0
   228 	 */
   239 	 */
   229 	public function admin_page() {
   240 	public function admin_page() {
   230 		?>
   241 		?>
   231 <div class="wrap" id="custom-background">
   242 <div class="wrap" id="custom-background">
   232 <h1><?php _e( 'Custom Background' ); ?></h1>
   243 <h1><?php _e( 'Custom Background' ); ?></h1>
   233 
   244 
   234 		<?php if ( current_user_can( 'customize' ) ) { ?>
   245 		<?php
   235 <div class="notice notice-info hide-if-no-customize">
   246 		if ( current_user_can( 'customize' ) ) {
   236 	<p>
   247 			$message = sprintf(
   237 			<?php
       
   238 			printf(
       
   239 				/* translators: %s: URL to background image configuration in Customizer. */
   248 				/* translators: %s: URL to background image configuration in Customizer. */
   240 				__( 'You can now manage and live-preview Custom Backgrounds in the <a href="%s">Customizer</a>.' ),
   249 				__( 'You can now manage and live-preview Custom Backgrounds in the <a href="%s">Customizer</a>.' ),
   241 				admin_url( 'customize.php?autofocus[control]=background_image' )
   250 				admin_url( 'customize.php?autofocus[control]=background_image' )
   242 			);
   251 			);
   243 			?>
   252 			wp_admin_notice(
   244 	</p>
   253 				$message,
   245 </div>
   254 				array(
   246 		<?php } ?>
   255 					'type'               => 'info',
   247 
   256 					'additional_classes' => array( 'hide-if-no-customize' ),
   248 		<?php if ( ! empty( $this->updated ) ) { ?>
   257 				)
   249 <div id="message" class="updated">
   258 			);
   250 	<p>
   259 		}
   251 			<?php
   260 
   252 			/* translators: %s: Home URL. */
   261 		if ( ! empty( $this->updated ) ) {
   253 			printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) );
   262 			$updated_message = sprintf(
   254 			?>
   263 				/* translators: %s: Home URL. */
   255 	</p>
   264 				__( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ),
   256 </div>
   265 				esc_url( home_url( '/' ) )
   257 		<?php } ?>
   266 			);
       
   267 			wp_admin_notice(
       
   268 				$updated_message,
       
   269 				array(
       
   270 					'id'                 => 'message',
       
   271 					'additional_classes' => array( 'updated' ),
       
   272 				)
       
   273 			);
       
   274 		}
       
   275 		?>
   258 
   276 
   259 <h2><?php _e( 'Background Image' ); ?></h2>
   277 <h2><?php _e( 'Background Image' ); ?></h2>
   260 
   278 
   261 <table class="form-table" role="presentation">
   279 <table class="form-table" role="presentation">
   262 <tbody>
   280 <tbody>
   304 <tr>
   322 <tr>
   305 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
   323 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
   306 <td>
   324 <td>
   307 <form method="post">
   325 <form method="post">
   308 			<?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?>
   326 			<?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?>
   309 			<?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
   327 			<?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br />
   310 			<?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ); ?>
   328 			<?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ); ?>
   311 </form>
   329 </form>
   312 </td>
   330 </td>
   313 </tr>
   331 </tr>
   314 		<?php endif; ?>
   332 		<?php endif; ?>
   318 <tr>
   336 <tr>
   319 <th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
   337 <th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
   320 <td>
   338 <td>
   321 <form method="post">
   339 <form method="post">
   322 			<?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?>
   340 			<?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?>
   323 			<?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
   341 			<?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br />
   324 			<?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ); ?>
   342 			<?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ); ?>
   325 </form>
   343 </form>
   326 </td>
   344 </td>
   327 </tr>
   345 </tr>
   328 		<?php endif; ?>
   346 		<?php endif; ?>
   410 				),
   428 				),
   411 			);
   429 			);
   412 			?>
   430 			?>
   413 <tr>
   431 <tr>
   414 <th scope="row"><?php _e( 'Image Position' ); ?></th>
   432 <th scope="row"><?php _e( 'Image Position' ); ?></th>
   415 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
   433 <td><fieldset><legend class="screen-reader-text"><span>
       
   434 			<?php
       
   435 			/* translators: Hidden accessibility text. */
       
   436 			_e( 'Image Position' );
       
   437 			?>
       
   438 </span></legend>
   416 <div class="background-position-control">
   439 <div class="background-position-control">
   417 			<?php foreach ( $background_position_options as $group ) : ?>
   440 			<?php foreach ( $background_position_options as $group ) : ?>
   418 	<div class="button-group">
   441 	<div class="button-group">
   419 				<?php foreach ( $group as $value => $input ) : ?>
   442 				<?php foreach ( $group as $value => $input ) : ?>
   420 		<label>
   443 		<label>
   429 </fieldset></td>
   452 </fieldset></td>
   430 </tr>
   453 </tr>
   431 
   454 
   432 <tr>
   455 <tr>
   433 <th scope="row"><label for="background-size"><?php _e( 'Image Size' ); ?></label></th>
   456 <th scope="row"><label for="background-size"><?php _e( 'Image Size' ); ?></label></th>
   434 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Size' ); ?></span></legend>
   457 <td><fieldset><legend class="screen-reader-text"><span>
       
   458 			<?php
       
   459 			/* translators: Hidden accessibility text. */
       
   460 			_e( 'Image Size' );
       
   461 			?>
       
   462 </span></legend>
   435 <select id="background-size" name="background-size">
   463 <select id="background-size" name="background-size">
   436 <option value="auto"<?php selected( 'auto', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _ex( 'Original', 'Original Size' ); ?></option>
   464 <option value="auto"<?php selected( 'auto', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _ex( 'Original', 'Original Size' ); ?></option>
   437 <option value="contain"<?php selected( 'contain', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fit to Screen' ); ?></option>
   465 <option value="contain"<?php selected( 'contain', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fit to Screen' ); ?></option>
   438 <option value="cover"<?php selected( 'cover', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fill Screen' ); ?></option>
   466 <option value="cover"<?php selected( 'cover', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fill Screen' ); ?></option>
   439 </select>
   467 </select>
   440 </fieldset></td>
   468 </fieldset></td>
   441 </tr>
   469 </tr>
   442 
   470 
   443 <tr>
   471 <tr>
   444 <th scope="row"><?php _ex( 'Repeat', 'Background Repeat' ); ?></th>
   472 <th scope="row"><?php _ex( 'Repeat', 'Background Repeat' ); ?></th>
   445 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Repeat', 'Background Repeat' ); ?></span></legend>
   473 <td><fieldset><legend class="screen-reader-text"><span>
       
   474 			<?php
       
   475 			/* translators: Hidden accessibility text. */
       
   476 			_ex( 'Repeat', 'Background Repeat' );
       
   477 			?>
       
   478 </span></legend>
   446 <input name="background-repeat" type="hidden" value="no-repeat">
   479 <input name="background-repeat" type="hidden" value="no-repeat">
   447 <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>
   480 <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>
   448 </fieldset></td>
   481 </fieldset></td>
   449 </tr>
   482 </tr>
   450 
   483 
   451 <tr>
   484 <tr>
   452 <th scope="row"><?php _ex( 'Scroll', 'Background Scroll' ); ?></th>
   485 <th scope="row"><?php _ex( 'Scroll', 'Background Scroll' ); ?></th>
   453 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Scroll', 'Background Scroll' ); ?></span></legend>
   486 <td><fieldset><legend class="screen-reader-text"><span>
       
   487 			<?php
       
   488 			/* translators: Hidden accessibility text. */
       
   489 			_ex( 'Scroll', 'Background Scroll' );
       
   490 			?>
       
   491 </span></legend>
   454 <input name="background-attachment" type="hidden" value="fixed">
   492 <input name="background-attachment" type="hidden" value="fixed">
   455 <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>
   493 <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>
   456 </fieldset></td>
   494 </fieldset></td>
   457 </tr>
   495 </tr>
   458 <?php endif; // get_background_image() ?>
   496 <?php endif; // get_background_image() ?>
   459 <tr>
   497 <tr>
   460 <th scope="row"><?php _e( 'Background Color' ); ?></th>
   498 <th scope="row"><?php _e( 'Background Color' ); ?></th>
   461 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
   499 <td><fieldset><legend class="screen-reader-text"><span>
       
   500 		<?php
       
   501 		/* translators: Hidden accessibility text. */
       
   502 		_e( 'Background Color' );
       
   503 		?>
       
   504 </span></legend>
   462 		<?php
   505 		<?php
   463 		$default_color = '';
   506 		$default_color = '';
   464 		if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
   507 		if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
   465 			$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
   508 			$default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
   466 		}
   509 		}
   478 </div>
   521 </div>
   479 		<?php
   522 		<?php
   480 	}
   523 	}
   481 
   524 
   482 	/**
   525 	/**
   483 	 * Handle an Image upload for the background image.
   526 	 * Handles an Image upload for the background image.
   484 	 *
   527 	 *
   485 	 * @since 3.0.0
   528 	 * @since 3.0.0
   486 	 */
   529 	 */
   487 	public function handle_upload() {
   530 	public function handle_upload() {
   488 		if ( empty( $_FILES ) ) {
   531 		if ( empty( $_FILES ) ) {
   524 
   567 
   525 		// Add the metadata.
   568 		// Add the metadata.
   526 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
   569 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
   527 		update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
   570 		update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
   528 
   571 
   529 		set_theme_mod( 'background_image', esc_url_raw( $url ) );
   572 		set_theme_mod( 'background_image', sanitize_url( $url ) );
   530 
   573 
   531 		$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
   574 		$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
   532 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
   575 		set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) );
   533 
   576 
   534 		/** This action is documented in wp-admin/includes/class-custom-image-header.php */
   577 		/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
   535 		do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication.
   578 		$file = apply_filters( 'wp_create_file_in_uploads', $file, $id ); // For replication.
       
   579 
   536 		$this->updated = true;
   580 		$this->updated = true;
   537 	}
   581 	}
   538 
   582 
   539 	/**
   583 	/**
   540 	 * Ajax handler for adding custom background context to an attachment.
   584 	 * Handles Ajax request for adding custom background context to an attachment.
   541 	 *
   585 	 *
   542 	 * Triggers when the user adds a new background image from the
   586 	 * Triggers when the user adds a new background image from the
   543 	 * Media Manager.
   587 	 * Media Manager.
   544 	 *
   588 	 *
   545 	 * @since 4.1.0
   589 	 * @since 4.1.0
   616 
   660 
   617 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
   661 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
   618 
   662 
   619 		$url       = wp_get_attachment_image_src( $attachment_id, $size );
   663 		$url       = wp_get_attachment_image_src( $attachment_id, $size );
   620 		$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
   664 		$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
   621 		set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
   665 		set_theme_mod( 'background_image', sanitize_url( $url[0] ) );
   622 		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
   666 		set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) );
   623 		exit;
   667 		exit;
   624 	}
   668 	}
   625 }
   669 }