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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref