[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-custom-background.php (source)

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


Generated : Wed Oct 28 08:20:35 2026 Cross-referenced by PHPXref