[ Index ]

PHP Cross Reference of WordPress

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  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  
 121              remove_theme_mod( 'background_image' );
 122              remove_theme_mod( 'background_image_thumb' );
 123  
 124              $this->updated = true;
 125              return;
 126          }
 127  
 128          if ( isset( $_POST['remove-background'] ) ) {
 129              // @todo Uploaded files are not removed here.
 130              check_admin_referer( 'custom-background-remove', '_wpnonce-custom-background-remove' );
 131  
 132              set_theme_mod( 'background_image', '' );
 133              set_theme_mod( 'background_image_thumb', '' );
 134  
 135              $this->updated = true;
 136              wp_safe_redirect( $_POST['_wp_http_referer'] );
 137              return;
 138          }
 139  
 140          if ( isset( $_POST['background-preset'] ) ) {
 141              check_admin_referer( 'custom-background' );
 142  
 143              if ( in_array( $_POST['background-preset'], array( 'default', 'fill', 'fit', 'repeat', 'custom' ), true ) ) {
 144                  $preset = $_POST['background-preset'];
 145              } else {
 146                  $preset = 'default';
 147              }
 148  
 149              set_theme_mod( 'background_preset', $preset );
 150          }
 151  
 152          if ( isset( $_POST['background-position'] ) ) {
 153              check_admin_referer( 'custom-background' );
 154  
 155              $position = explode( ' ', $_POST['background-position'] );
 156  
 157              if ( in_array( $position[0], array( 'left', 'center', 'right' ), true ) ) {
 158                  $position_x = $position[0];
 159              } else {
 160                  $position_x = 'left';
 161              }
 162  
 163              if ( in_array( $position[1], array( 'top', 'center', 'bottom' ), true ) ) {
 164                  $position_y = $position[1];
 165              } else {
 166                  $position_y = 'top';
 167              }
 168  
 169              set_theme_mod( 'background_position_x', $position_x );
 170              set_theme_mod( 'background_position_y', $position_y );
 171          }
 172  
 173          if ( isset( $_POST['background-size'] ) ) {
 174              check_admin_referer( 'custom-background' );
 175  
 176              if ( in_array( $_POST['background-size'], array( 'auto', 'contain', 'cover' ), true ) ) {
 177                  $size = $_POST['background-size'];
 178              } else {
 179                  $size = 'auto';
 180              }
 181  
 182              set_theme_mod( 'background_size', $size );
 183          }
 184  
 185          if ( isset( $_POST['background-repeat'] ) ) {
 186              check_admin_referer( 'custom-background' );
 187  
 188              $repeat = $_POST['background-repeat'];
 189  
 190              if ( 'no-repeat' !== $repeat ) {
 191                  $repeat = 'repeat';
 192              }
 193  
 194              set_theme_mod( 'background_repeat', $repeat );
 195          }
 196  
 197          if ( isset( $_POST['background-attachment'] ) ) {
 198              check_admin_referer( 'custom-background' );
 199  
 200              $attachment = $_POST['background-attachment'];
 201  
 202              if ( 'fixed' !== $attachment ) {
 203                  $attachment = 'scroll';
 204              }
 205  
 206              set_theme_mod( 'background_attachment', $attachment );
 207          }
 208  
 209          if ( isset( $_POST['background-color'] ) ) {
 210              check_admin_referer( 'custom-background' );
 211  
 212              $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['background-color'] );
 213  
 214              if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) {
 215                  set_theme_mod( 'background_color', $color );
 216              } else {
 217                  set_theme_mod( 'background_color', '' );
 218              }
 219          }
 220  
 221          $this->updated = true;
 222      }
 223  
 224      /**
 225       * Display the custom background page.
 226       *
 227       * @since 3.0.0
 228       */
 229  	public function admin_page() {
 230          ?>
 231  <div class="wrap" id="custom-background">
 232  <h1><?php _e( 'Custom Background' ); ?></h1>
 233  
 234          <?php if ( current_user_can( 'customize' ) ) { ?>
 235  <div class="notice notice-info hide-if-no-customize">
 236      <p>
 237              <?php
 238              printf(
 239                  /* 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>.' ),
 241                  admin_url( 'customize.php?autofocus[control]=background_image' )
 242              );
 243              ?>
 244      </p>
 245  </div>
 246          <?php } ?>
 247  
 248          <?php if ( ! empty( $this->updated ) ) { ?>
 249  <div id="message" class="updated">
 250      <p>
 251              <?php
 252              /* translators: %s: Home URL. */
 253              printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) );
 254              ?>
 255      </p>
 256  </div>
 257          <?php } ?>
 258  
 259  <h2><?php _e( 'Background Image' ); ?></h2>
 260  
 261  <table class="form-table" role="presentation">
 262  <tbody>
 263  <tr>
 264  <th scope="row"><?php _e( 'Preview' ); ?></th>
 265  <td>
 266          <?php
 267          if ( $this->admin_image_div_callback ) {
 268              call_user_func( $this->admin_image_div_callback );
 269          } else {
 270              $background_styles = '';
 271              $bgcolor           = get_background_color();
 272              if ( $bgcolor ) {
 273                  $background_styles .= 'background-color: #' . $bgcolor . ';';
 274              }
 275  
 276              $background_image_thumb = get_background_image();
 277              if ( $background_image_thumb ) {
 278                  $background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
 279                  $background_position_x  = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
 280                  $background_position_y  = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
 281                  $background_size        = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
 282                  $background_repeat      = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
 283                  $background_attachment  = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
 284  
 285                  // Background-image URL must be single quote, see below.
 286                  $background_styles .= " background-image: url('$background_image_thumb');"
 287                  . " background-size: $background_size;"
 288                  . " background-position: $background_position_x $background_position_y;"
 289                  . " background-repeat: $background_repeat;"
 290                  . " background-attachment: $background_attachment;";
 291              }
 292              ?>
 293      <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // Must be double quote, see above. ?>
 294              <?php if ( $background_image_thumb ) { ?>
 295          <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
 296          <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
 297          <?php } ?>
 298      </div>
 299      <?php } ?>
 300  </td>
 301  </tr>
 302  
 303          <?php if ( get_background_image() ) : ?>
 304  <tr>
 305  <th scope="row"><?php _e( 'Remove Image' ); ?></th>
 306  <td>
 307  <form method="post">
 308              <?php wp_nonce_field( 'custom-background-remove', '_wpnonce-custom-background-remove' ); ?>
 309              <?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.' ); ?>
 311  </form>
 312  </td>
 313  </tr>
 314          <?php endif; ?>
 315  
 316          <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
 317          <?php if ( $default_image && get_background_image() !== $default_image ) : ?>
 318  <tr>
 319  <th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
 320  <td>
 321  <form method="post">
 322              <?php wp_nonce_field( 'custom-background-reset', '_wpnonce-custom-background-reset' ); ?>
 323              <?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.' ); ?>
 325  </form>
 326  </td>
 327  </tr>
 328          <?php endif; ?>
 329  
 330          <?php if ( current_user_can( 'upload_files' ) ) : ?>
 331  <tr>
 332  <th scope="row"><?php _e( 'Select Image' ); ?></th>
 333  <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
 334      <p>
 335          <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
 336          <input type="file" id="upload" name="import" />
 337          <input type="hidden" name="action" value="save" />
 338              <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
 339              <?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
 340      </p>
 341      <p>
 342          <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
 343          <button id="choose-from-library-link" class="button"
 344              data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
 345              data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button>
 346      </p>
 347      </form>
 348  </td>
 349  </tr>
 350          <?php endif; ?>
 351  </tbody>
 352  </table>
 353  
 354  <h2><?php _e( 'Display Options' ); ?></h2>
 355  <form method="post">
 356  <table class="form-table" role="presentation">
 357  <tbody>
 358          <?php if ( get_background_image() ) : ?>
 359  <input name="background-preset" type="hidden" value="custom">
 360  
 361              <?php
 362              $background_position = sprintf(
 363                  '%s %s',
 364                  get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ),
 365                  get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) )
 366              );
 367  
 368              $background_position_options = array(
 369                  array(
 370                      'left top'   => array(
 371                          'label' => __( 'Top Left' ),
 372                          'icon'  => 'dashicons dashicons-arrow-left-alt',
 373                      ),
 374                      'center top' => array(
 375                          'label' => __( 'Top' ),
 376                          'icon'  => 'dashicons dashicons-arrow-up-alt',
 377                      ),
 378                      'right top'  => array(
 379                          'label' => __( 'Top Right' ),
 380                          'icon'  => 'dashicons dashicons-arrow-right-alt',
 381                      ),
 382                  ),
 383                  array(
 384                      'left center'   => array(
 385                          'label' => __( 'Left' ),
 386                          'icon'  => 'dashicons dashicons-arrow-left-alt',
 387                      ),
 388                      'center center' => array(
 389                          'label' => __( 'Center' ),
 390                          'icon'  => 'background-position-center-icon',
 391                      ),
 392                      'right center'  => array(
 393                          'label' => __( 'Right' ),
 394                          'icon'  => 'dashicons dashicons-arrow-right-alt',
 395                      ),
 396                  ),
 397                  array(
 398                      'left bottom'   => array(
 399                          'label' => __( 'Bottom Left' ),
 400                          'icon'  => 'dashicons dashicons-arrow-left-alt',
 401                      ),
 402                      'center bottom' => array(
 403                          'label' => __( 'Bottom' ),
 404                          'icon'  => 'dashicons dashicons-arrow-down-alt',
 405                      ),
 406                      'right bottom'  => array(
 407                          'label' => __( 'Bottom Right' ),
 408                          'icon'  => 'dashicons dashicons-arrow-right-alt',
 409                      ),
 410                  ),
 411              );
 412              ?>
 413  <tr>
 414  <th scope="row"><?php _e( 'Image Position' ); ?></th>
 415  <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
 416  <div class="background-position-control">
 417              <?php foreach ( $background_position_options as $group ) : ?>
 418      <div class="button-group">
 419                  <?php foreach ( $group as $value => $input ) : ?>
 420          <label>
 421              <input class="ui-helper-hidden-accessible" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>>
 422              <span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
 423              <span class="screen-reader-text"><?php echo $input['label']; ?></span>
 424          </label>
 425      <?php endforeach; ?>
 426      </div>
 427  <?php endforeach; ?>
 428  </div>
 429  </fieldset></td>
 430  </tr>
 431  
 432  <tr>
 433  <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>
 435  <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>
 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>
 438  <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>
 440  </fieldset></td>
 441  </tr>
 442  
 443  <tr>
 444  <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>
 446  <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>
 448  </fieldset></td>
 449  </tr>
 450  
 451  <tr>
 452  <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>
 454  <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>
 456  </fieldset></td>
 457  </tr>
 458  <?php endif; // get_background_image() ?>
 459  <tr>
 460  <th scope="row"><?php _e( 'Background Color' ); ?></th>
 461  <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
 462          <?php
 463          $default_color = '';
 464          if ( current_theme_supports( 'custom-background', 'default-color' ) ) {
 465              $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
 466          }
 467          ?>
 468  <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color; ?>>
 469  </fieldset></td>
 470  </tr>
 471  </tbody>
 472  </table>
 473  
 474          <?php wp_nonce_field( 'custom-background' ); ?>
 475          <?php submit_button( null, 'primary', 'save-background-options' ); ?>
 476  </form>
 477  
 478  </div>
 479          <?php
 480      }
 481  
 482      /**
 483       * Handle an Image upload for the background image.
 484       *
 485       * @since 3.0.0
 486       */
 487  	public function handle_upload() {
 488          if ( empty( $_FILES ) ) {
 489              return;
 490          }
 491  
 492          check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );
 493  
 494          $overrides = array( 'test_form' => false );
 495  
 496          $uploaded_file = $_FILES['import'];
 497          $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
 498          if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
 499              wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
 500          }
 501  
 502          $file = wp_handle_upload( $uploaded_file, $overrides );
 503  
 504          if ( isset( $file['error'] ) ) {
 505              wp_die( $file['error'] );
 506          }
 507  
 508          $url      = $file['url'];
 509          $type     = $file['type'];
 510          $file     = $file['file'];
 511          $filename = wp_basename( $file );
 512  
 513          // Construct the object array.
 514          $object = array(
 515              'post_title'     => $filename,
 516              'post_content'   => $url,
 517              'post_mime_type' => $type,
 518              'guid'           => $url,
 519              'context'        => 'custom-background',
 520          );
 521  
 522          // Save the data.
 523          $id = wp_insert_attachment( $object, $file );
 524  
 525          // Add the metadata.
 526          wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
 527          update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
 528  
 529          set_theme_mod( 'background_image', esc_url_raw( $url ) );
 530  
 531          $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
 532          set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
 533  
 534          /** This action is documented in wp-admin/includes/class-custom-image-header.php */
 535          do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication.
 536          $this->updated = true;
 537      }
 538  
 539      /**
 540       * Ajax handler for adding custom background context to an attachment.
 541       *
 542       * Triggers when the user adds a new background image from the
 543       * Media Manager.
 544       *
 545       * @since 4.1.0
 546       */
 547  	public function ajax_background_add() {
 548          check_ajax_referer( 'background-add', 'nonce' );
 549  
 550          if ( ! current_user_can( 'edit_theme_options' ) ) {
 551              wp_send_json_error();
 552          }
 553  
 554          $attachment_id = absint( $_POST['attachment_id'] );
 555          if ( $attachment_id < 1 ) {
 556              wp_send_json_error();
 557          }
 558  
 559          update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
 560  
 561          wp_send_json_success();
 562      }
 563  
 564      /**
 565       * @since 3.4.0
 566       * @deprecated 3.5.0
 567       *
 568       * @param array $form_fields
 569       * @return array $form_fields
 570       */
 571  	public function attachment_fields_to_edit( $form_fields ) {
 572          return $form_fields;
 573      }
 574  
 575      /**
 576       * @since 3.4.0
 577       * @deprecated 3.5.0
 578       *
 579       * @param array $tabs
 580       * @return array $tabs
 581       */
 582  	public function filter_upload_tabs( $tabs ) {
 583          return $tabs;
 584      }
 585  
 586      /**
 587       * @since 3.4.0
 588       * @deprecated 3.5.0
 589       */
 590  	public function wp_set_background_image() {
 591          check_ajax_referer( 'custom-background' );
 592  
 593          if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
 594              exit;
 595          }
 596  
 597          $attachment_id = absint( $_POST['attachment_id'] );
 598  
 599          $sizes = array_keys(
 600              /** This filter is documented in wp-admin/includes/media.php */
 601              apply_filters(
 602                  'image_size_names_choose',
 603                  array(
 604                      'thumbnail' => __( 'Thumbnail' ),
 605                      'medium'    => __( 'Medium' ),
 606                      'large'     => __( 'Large' ),
 607                      'full'      => __( 'Full Size' ),
 608                  )
 609              )
 610          );
 611  
 612          $size = 'thumbnail';
 613          if ( in_array( $_POST['size'], $sizes, true ) ) {
 614              $size = esc_attr( $_POST['size'] );
 615          }
 616  
 617          update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
 618  
 619          $url       = wp_get_attachment_image_src( $attachment_id, $size );
 620          $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
 621          set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
 622          set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
 623          exit;
 624      }
 625  }


Generated: Sat Sep 18 01:00:04 2021 Cross-referenced by PHPXref 0.7.1