[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * The custom header image script.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * The custom header image class.
  11   *
  12   * @since 2.1.0
  13   */
  14  class Custom_Image_Header {
  15  
  16      /**
  17       * Callback for administration header.
  18       *
  19       * @var callable
  20       * @since 2.1.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       * Holds default headers.
  34       *
  35       * @var array
  36       * @since 3.0.0
  37       */
  38      public $default_headers = array();
  39  
  40      /**
  41       * Used to trigger a success message when settings updated and set to true.
  42       *
  43       * @since 3.0.0
  44       * @var bool
  45       */
  46      private $updated;
  47  
  48      /**
  49       * Constructor - Register administration header callback.
  50       *
  51       * @since 2.1.0
  52       * @param callable $admin_header_callback
  53       * @param callable $admin_image_div_callback Optional custom image div output callback.
  54       */
  55  	public function __construct( $admin_header_callback, $admin_image_div_callback = '' ) {
  56          $this->admin_header_callback    = $admin_header_callback;
  57          $this->admin_image_div_callback = $admin_image_div_callback;
  58  
  59          add_action( 'admin_menu', array( $this, 'init' ) );
  60  
  61          add_action( 'customize_save_after', array( $this, 'customize_set_last_used' ) );
  62          add_action( 'wp_ajax_custom-header-crop', array( $this, 'ajax_header_crop' ) );
  63          add_action( 'wp_ajax_custom-header-add', array( $this, 'ajax_header_add' ) );
  64          add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) );
  65      }
  66  
  67      /**
  68       * Set up the hooks for the Custom Header admin page.
  69       *
  70       * @since 2.1.0
  71       */
  72  	public function init() {
  73          $page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( $this, 'admin_page' ) );
  74  
  75          if ( ! $page ) {
  76              return;
  77          }
  78  
  79          add_action( "admin_print_scripts-{$page}", array( $this, 'js_includes' ) );
  80          add_action( "admin_print_styles-{$page}", array( $this, 'css_includes' ) );
  81          add_action( "admin_head-{$page}", array( $this, 'help' ) );
  82          add_action( "admin_head-{$page}", array( $this, 'take_action' ), 50 );
  83          add_action( "admin_head-{$page}", array( $this, 'js' ), 50 );
  84  
  85          if ( $this->admin_header_callback ) {
  86              add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
  87          }
  88      }
  89  
  90      /**
  91       * Adds contextual help.
  92       *
  93       * @since 3.0.0
  94       */
  95  	public function help() {
  96          get_current_screen()->add_help_tab(
  97              array(
  98                  'id'      => 'overview',
  99                  'title'   => __( 'Overview' ),
 100                  'content' =>
 101                      '<p>' . __( 'This screen is used to customize the header section of your theme.' ) . '</p>' .
 102                      '<p>' . __( 'You can choose from the theme&#8217;s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.' ) . '<p>',
 103              )
 104          );
 105  
 106          get_current_screen()->add_help_tab(
 107              array(
 108                  'id'      => 'set-header-image',
 109                  'title'   => __( 'Header Image' ),
 110                  'content' =>
 111                      '<p>' . __( 'You can set a custom image header for your site. Simply upload the image and crop it, and the new header will go live immediately. Alternatively, you can use an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button.' ) . '</p>' .
 112                      '<p>' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you would like and click the &#8220;Save Changes&#8221; button.' ) . '</p>' .
 113                      '<p>' . __( 'If your theme has more than one default header image, or you have uploaded more than one custom header image, you have the option of having WordPress display a randomly different image on each page of your site. Click the &#8220;Random&#8221; radio button next to the Uploaded Images or Default Images section to enable this feature.' ) . '</p>' .
 114                      '<p>' . __( 'If you do not want a header image to be displayed on your site at all, click the &#8220;Remove Header Image&#8221; button at the bottom of the Header Image section of this page. If you want to re-enable the header image later, you just have to select one of the other image options and click &#8220;Save Changes&#8221;.' ) . '</p>',
 115              )
 116          );
 117  
 118          get_current_screen()->add_help_tab(
 119              array(
 120                  'id'      => 'set-header-text',
 121                  'title'   => __( 'Header Text' ),
 122                  'content' =>
 123                      '<p>' . sprintf(
 124                          /* translators: %s: URL to General Settings screen. */
 125                          __( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%s">General Settings</a> section.' ),
 126                          admin_url( 'options-general.php' )
 127                      ) .
 128                      '</p>' .
 129                      '<p>' . __( 'In the Header Text section of this page, you can choose whether to display this text or hide it. You can also choose a color for the text 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>' .
 130                      '<p>' . __( 'Do not forget to click &#8220;Save Changes&#8221; when you are done!' ) . '</p>',
 131              )
 132          );
 133  
 134          get_current_screen()->set_help_sidebar(
 135              '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 136              '<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Header_Screen">Documentation on Custom Header</a>' ) . '</p>' .
 137              '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
 138          );
 139      }
 140  
 141      /**
 142       * Get the current step.
 143       *
 144       * @since 2.6.0
 145       *
 146       * @return int Current step.
 147       */
 148  	public function step() {
 149          if ( ! isset( $_GET['step'] ) ) {
 150              return 1;
 151          }
 152  
 153          $step = (int) $_GET['step'];
 154          if ( $step < 1 || 3 < $step ||
 155              ( 2 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
 156              ( 3 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
 157          ) {
 158              return 1;
 159          }
 160  
 161          return $step;
 162      }
 163  
 164      /**
 165       * Set up the enqueue for the JavaScript files.
 166       *
 167       * @since 2.1.0
 168       */
 169  	public function js_includes() {
 170          $step = $this->step();
 171  
 172          if ( ( 1 === $step || 3 === $step ) ) {
 173              wp_enqueue_media();
 174              wp_enqueue_script( 'custom-header' );
 175              if ( current_theme_supports( 'custom-header', 'header-text' ) ) {
 176                  wp_enqueue_script( 'wp-color-picker' );
 177              }
 178          } elseif ( 2 === $step ) {
 179              wp_enqueue_script( 'imgareaselect' );
 180          }
 181      }
 182  
 183      /**
 184       * Set up the enqueue for the CSS files
 185       *
 186       * @since 2.7.0
 187       */
 188  	public function css_includes() {
 189          $step = $this->step();
 190  
 191          if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
 192              wp_enqueue_style( 'wp-color-picker' );
 193          } elseif ( 2 === $step ) {
 194              wp_enqueue_style( 'imgareaselect' );
 195          }
 196      }
 197  
 198      /**
 199       * Execute custom header modification.
 200       *
 201       * @since 2.6.0
 202       */
 203  	public function take_action() {
 204          if ( ! current_user_can( 'edit_theme_options' ) ) {
 205              return;
 206          }
 207  
 208          if ( empty( $_POST ) ) {
 209              return;
 210          }
 211  
 212          $this->updated = true;
 213  
 214          if ( isset( $_POST['resetheader'] ) ) {
 215              check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
 216  
 217              $this->reset_header_image();
 218  
 219              return;
 220          }
 221  
 222          if ( isset( $_POST['removeheader'] ) ) {
 223              check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
 224  
 225              $this->remove_header_image();
 226  
 227              return;
 228          }
 229  
 230          if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
 231              check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
 232  
 233              set_theme_mod( 'header_textcolor', 'blank' );
 234          } elseif ( isset( $_POST['text-color'] ) ) {
 235              check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
 236  
 237              $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
 238  
 239              $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
 240  
 241              if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) {
 242                  set_theme_mod( 'header_textcolor', $color );
 243              } elseif ( ! $color ) {
 244                  set_theme_mod( 'header_textcolor', 'blank' );
 245              }
 246          }
 247  
 248          if ( isset( $_POST['default-header'] ) ) {
 249              check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
 250  
 251              $this->set_header_image( $_POST['default-header'] );
 252  
 253              return;
 254          }
 255      }
 256  
 257      /**
 258       * Process the default headers
 259       *
 260       * @since 3.0.0
 261       *
 262       * @global array $_wp_default_headers
 263       */
 264  	public function process_default_headers() {
 265          global $_wp_default_headers;
 266  
 267          if ( ! isset( $_wp_default_headers ) ) {
 268              return;
 269          }
 270  
 271          if ( ! empty( $this->default_headers ) ) {
 272              return;
 273          }
 274  
 275          $this->default_headers    = $_wp_default_headers;
 276          $template_directory_uri   = get_template_directory_uri();
 277          $stylesheet_directory_uri = get_stylesheet_directory_uri();
 278  
 279          foreach ( array_keys( $this->default_headers ) as $header ) {
 280              $this->default_headers[ $header ]['url'] = sprintf(
 281                  $this->default_headers[ $header ]['url'],
 282                  $template_directory_uri,
 283                  $stylesheet_directory_uri
 284              );
 285  
 286              $this->default_headers[ $header ]['thumbnail_url'] = sprintf(
 287                  $this->default_headers[ $header ]['thumbnail_url'],
 288                  $template_directory_uri,
 289                  $stylesheet_directory_uri
 290              );
 291          }
 292      }
 293  
 294      /**
 295       * Display UI for selecting one of several default headers.
 296       *
 297       * Show the random image option if this theme has multiple header images.
 298       * Random image option is on by default if no header has been set.
 299       *
 300       * @since 3.0.0
 301       *
 302       * @param string $type The header type. One of 'default' (for the Uploaded Images control)
 303       *                     or 'uploaded' (for the Uploaded Images control).
 304       */
 305  	public function show_header_selector( $type = 'default' ) {
 306          if ( 'default' === $type ) {
 307              $headers = $this->default_headers;
 308          } else {
 309              $headers = get_uploaded_header_images();
 310              $type    = 'uploaded';
 311          }
 312  
 313          if ( 1 < count( $headers ) ) {
 314              echo '<div class="random-header">';
 315              echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
 316              _e( '<strong>Random:</strong> Show a different image on each page.' );
 317              echo '</label>';
 318              echo '</div>';
 319          }
 320  
 321          echo '<div class="available-headers">';
 322  
 323          foreach ( $headers as $header_key => $header ) {
 324              $header_thumbnail = $header['thumbnail_url'];
 325              $header_url       = $header['url'];
 326              $header_alt_text  = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
 327  
 328              echo '<div class="default-header">';
 329              echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
 330              $width = '';
 331              if ( ! empty( $header['attachment_id'] ) ) {
 332                  $width = ' width="230"';
 333              }
 334              echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) . '"' . $width . ' /></label>';
 335              echo '</div>';
 336          }
 337  
 338          echo '<div class="clear"></div></div>';
 339      }
 340  
 341      /**
 342       * Execute JavaScript depending on step.
 343       *
 344       * @since 2.1.0
 345       */
 346      public function js() {
 347          $step = $this->step();
 348  
 349          if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
 350              $this->js_1();
 351          } elseif ( 2 === $step ) {
 352              $this->js_2();
 353          }
 354      }
 355  
 356      /**
 357       * Display JavaScript based on Step 1 and 3.
 358       *
 359       * @since 2.6.0
 360       */
 361  	public function js_1() {
 362          $default_color = '';
 363          if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
 364              $default_color = get_theme_support( 'custom-header', 'default-text-color' );
 365              if ( $default_color && false === strpos( $default_color, '#' ) ) {
 366                  $default_color = '#' . $default_color;
 367              }
 368          }
 369          ?>
 370  <script type="text/javascript">
 371  (function($){
 372      var default_color = '<?php echo esc_js( $default_color ); ?>',
 373          header_text_fields;
 374  
 375  	function pickColor(color) {
 376          $('#name').css('color', color);
 377          $('#desc').css('color', color);
 378          $('#text-color').val(color);
 379      }
 380  
 381  	function toggle_text() {
 382          var checked = $('#display-header-text').prop('checked'),
 383              text_color;
 384          header_text_fields.toggle( checked );
 385          if ( ! checked )
 386              return;
 387          text_color = $('#text-color');
 388          if ( '' === text_color.val().replace('#', '') ) {
 389              text_color.val( default_color );
 390              pickColor( default_color );
 391          } else {
 392              pickColor( text_color.val() );
 393          }
 394      }
 395  
 396      $( function() {
 397          var text_color = $('#text-color');
 398          header_text_fields = $('.displaying-header-text');
 399          text_color.wpColorPicker({
 400              change: function( event, ui ) {
 401                  pickColor( text_color.wpColorPicker('color') );
 402              },
 403              clear: function() {
 404                  pickColor( '' );
 405              }
 406          });
 407          $('#display-header-text').click( toggle_text );
 408          <?php if ( ! display_header_text() ) : ?>
 409          toggle_text();
 410          <?php endif; ?>
 411      } );
 412  })(jQuery);
 413  </script>
 414          <?php
 415      }
 416  
 417      /**
 418       * Display JavaScript based on Step 2.
 419       *
 420       * @since 2.6.0
 421       */
 422  	public function js_2() {
 423  
 424          ?>
 425  <script type="text/javascript">
 426  	function onEndCrop( coords ) {
 427          jQuery( '#x1' ).val(coords.x);
 428          jQuery( '#y1' ).val(coords.y);
 429          jQuery( '#width' ).val(coords.w);
 430          jQuery( '#height' ).val(coords.h);
 431      }
 432  
 433      jQuery( function() {
 434          var xinit = <?php echo absint( get_theme_support( 'custom-header', 'width' ) ); ?>;
 435          var yinit = <?php echo absint( get_theme_support( 'custom-header', 'height' ) ); ?>;
 436          var ratio = xinit / yinit;
 437          var ximg = jQuery('img#upload').width();
 438          var yimg = jQuery('img#upload').height();
 439  
 440          if ( yimg < yinit || ximg < xinit ) {
 441              if ( ximg / yimg > ratio ) {
 442                  yinit = yimg;
 443                  xinit = yinit * ratio;
 444              } else {
 445                  xinit = ximg;
 446                  yinit = xinit / ratio;
 447              }
 448          }
 449  
 450          jQuery('img#upload').imgAreaSelect({
 451              handles: true,
 452              keys: true,
 453              show: true,
 454              x1: 0,
 455              y1: 0,
 456              x2: xinit,
 457              y2: yinit,
 458              <?php
 459              if ( ! current_theme_supports( 'custom-header', 'flex-height' )
 460                  && ! current_theme_supports( 'custom-header', 'flex-width' )
 461              ) {
 462                  ?>
 463              aspectRatio: xinit + ':' + yinit,
 464                  <?php
 465              }
 466              if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
 467                  ?>
 468              maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
 469                  <?php
 470              }
 471              if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
 472                  ?>
 473              maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
 474                  <?php
 475              }
 476              ?>
 477              onInit: function () {
 478                  jQuery('#width').val(xinit);
 479                  jQuery('#height').val(yinit);
 480              },
 481              onSelectChange: function(img, c) {
 482                  jQuery('#x1').val(c.x1);
 483                  jQuery('#y1').val(c.y1);
 484                  jQuery('#width').val(c.width);
 485                  jQuery('#height').val(c.height);
 486              }
 487          });
 488      } );
 489  </script>
 490          <?php
 491      }
 492  
 493      /**
 494       * Display first step of custom header image page.
 495       *
 496       * @since 2.1.0
 497       */
 498  	public function step_1() {
 499          $this->process_default_headers();
 500          ?>
 501  
 502  <div class="wrap">
 503  <h1><?php _e( 'Custom Header' ); ?></h1>
 504  
 505          <?php if ( current_user_can( 'customize' ) ) { ?>
 506  <div class="notice notice-info hide-if-no-customize">
 507      <p>
 508              <?php
 509              printf(
 510                  /* translators: %s: URL to header image configuration in Customizer. */
 511                  __( 'You can now manage and live-preview Custom Header in the <a href="%s">Customizer</a>.' ),
 512                  admin_url( 'customize.php?autofocus[control]=header_image' )
 513              );
 514              ?>
 515      </p>
 516  </div>
 517          <?php } ?>
 518  
 519          <?php if ( ! empty( $this->updated ) ) { ?>
 520  <div id="message" class="updated">
 521      <p>
 522              <?php
 523              /* translators: %s: Home URL. */
 524              printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) );
 525              ?>
 526      </p>
 527  </div>
 528          <?php } ?>
 529  
 530  <h2><?php _e( 'Header Image' ); ?></h2>
 531  
 532  <table class="form-table" role="presentation">
 533  <tbody>
 534  
 535          <?php if ( get_custom_header() || display_header_text() ) : ?>
 536  <tr>
 537  <th scope="row"><?php _e( 'Preview' ); ?></th>
 538  <td>
 539              <?php
 540              if ( $this->admin_image_div_callback ) {
 541                  call_user_func( $this->admin_image_div_callback );
 542              } else {
 543                  $custom_header = get_custom_header();
 544                  $header_image  = get_header_image();
 545  
 546                  if ( $header_image ) {
 547                      $header_image_style = 'background-image:url(' . esc_url( $header_image ) . ');';
 548                  } else {
 549                      $header_image_style = '';
 550                  }
 551  
 552                  if ( $custom_header->width ) {
 553                      $header_image_style .= 'max-width:' . $custom_header->width . 'px;';
 554                  }
 555                  if ( $custom_header->height ) {
 556                      $header_image_style .= 'height:' . $custom_header->height . 'px;';
 557                  }
 558                  ?>
 559      <div id="headimg" style="<?php echo $header_image_style; ?>">
 560                  <?php
 561                  if ( display_header_text() ) {
 562                      $style = ' style="color:#' . get_header_textcolor() . ';"';
 563                  } else {
 564                      $style = ' style="display:none;"';
 565                  }
 566                  ?>
 567          <h1><a id="name" class="displaying-header-text" <?php echo $style; ?> onclick="return false;" href="<?php bloginfo( 'url' ); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1>
 568          <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
 569      </div>
 570              <?php } ?>
 571  </td>
 572  </tr>
 573          <?php endif; ?>
 574  
 575          <?php if ( current_user_can( 'upload_files' ) && current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
 576  <tr>
 577  <th scope="row"><?php _e( 'Select Image' ); ?></th>
 578  <td>
 579      <p><?php _e( 'You can select an image to be shown at the top of your site by uploading from your computer or choosing from your media library. After selecting an image you will be able to crop it.' ); ?><br />
 580              <?php
 581              if ( ! current_theme_supports( 'custom-header', 'flex-height' )
 582                  && ! current_theme_supports( 'custom-header', 'flex-width' )
 583              ) {
 584                  printf(
 585                      /* translators: 1: Image width in pixels, 2: Image height in pixels. */
 586                      __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />',
 587                      get_theme_support( 'custom-header', 'width' ),
 588                      get_theme_support( 'custom-header', 'height' )
 589                  );
 590              } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
 591                  if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
 592                      printf(
 593                          /* translators: %s: Size in pixels. */
 594                          __( 'Images should be at least %s wide.' ) . ' ',
 595                          sprintf(
 596                              /* translators: %d: Custom header width. */
 597                              '<strong>' . __( '%d pixels' ) . '</strong>',
 598                              get_theme_support( 'custom-header', 'width' )
 599                          )
 600                      );
 601                  }
 602              } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
 603                  if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
 604                      printf(
 605                          /* translators: %s: Size in pixels. */
 606                          __( 'Images should be at least %s tall.' ) . ' ',
 607                          sprintf(
 608                              /* translators: %d: Custom header height. */
 609                              '<strong>' . __( '%d pixels' ) . '</strong>',
 610                              get_theme_support( 'custom-header', 'height' )
 611                          )
 612                      );
 613                  }
 614              }
 615  
 616              if ( current_theme_supports( 'custom-header', 'flex-height' )
 617                  || current_theme_supports( 'custom-header', 'flex-width' )
 618              ) {
 619                  if ( current_theme_supports( 'custom-header', 'width' ) ) {
 620                      printf(
 621                          /* translators: %s: Size in pixels. */
 622                          __( 'Suggested width is %s.' ) . ' ',
 623                          sprintf(
 624                              /* translators: %d: Custom header width. */
 625                              '<strong>' . __( '%d pixels' ) . '</strong>',
 626                              get_theme_support( 'custom-header', 'width' )
 627                          )
 628                      );
 629                  }
 630  
 631                  if ( current_theme_supports( 'custom-header', 'height' ) ) {
 632                      printf(
 633                          /* translators: %s: Size in pixels. */
 634                          __( 'Suggested height is %s.' ) . ' ',
 635                          sprintf(
 636                              /* translators: %d: Custom header height. */
 637                              '<strong>' . __( '%d pixels' ) . '</strong>',
 638                              get_theme_support( 'custom-header', 'height' )
 639                          )
 640                      );
 641                  }
 642              }
 643              ?>
 644      </p>
 645      <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ); ?>">
 646      <p>
 647          <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
 648          <input type="file" id="upload" name="import" />
 649          <input type="hidden" name="action" value="save" />
 650              <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?>
 651              <?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
 652      </p>
 653              <?php
 654              $modal_update_href = add_query_arg(
 655                  array(
 656                      'page'                          => 'custom-header',
 657                      'step'                          => 2,
 658                      '_wpnonce-custom-header-upload' => wp_create_nonce( 'custom-header-upload' ),
 659                  ),
 660                  admin_url( 'themes.php' )
 661              );
 662              ?>
 663      <p>
 664          <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
 665          <button id="choose-from-library-link" class="button"
 666              data-update-link="<?php echo esc_url( $modal_update_href ); ?>"
 667              data-choose="<?php esc_attr_e( 'Choose a Custom Header' ); ?>"
 668              data-update="<?php esc_attr_e( 'Set as header' ); ?>"><?php _e( 'Choose Image' ); ?></button>
 669      </p>
 670      </form>
 671  </td>
 672  </tr>
 673          <?php endif; ?>
 674  </tbody>
 675  </table>
 676  
 677  <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>">
 678          <?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?>
 679  <table class="form-table" role="presentation">
 680  <tbody>
 681          <?php if ( get_uploaded_header_images() ) : ?>
 682  <tr>
 683  <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
 684  <td>
 685      <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ); ?></p>
 686              <?php
 687              $this->show_header_selector( 'uploaded' );
 688              ?>
 689  </td>
 690  </tr>
 691              <?php
 692      endif;
 693          if ( ! empty( $this->default_headers ) ) :
 694              ?>
 695  <tr>
 696  <th scope="row"><?php _e( 'Default Images' ); ?></th>
 697  <td>
 698              <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
 699      <p><?php _e( 'If you don&lsquo;t want to upload your own image, you can use one of these cool headers, or show a random one.' ); ?></p>
 700      <?php else : ?>
 701      <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ); ?></p>
 702      <?php endif; ?>
 703              <?php
 704              $this->show_header_selector( 'default' );
 705              ?>
 706  </td>
 707  </tr>
 708              <?php
 709      endif;
 710          if ( get_header_image() ) :
 711              ?>
 712  <tr>
 713  <th scope="row"><?php _e( 'Remove Image' ); ?></th>
 714  <td>
 715      <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ); ?></p>
 716              <?php submit_button( __( 'Remove Header Image' ), '', 'removeheader', false ); ?>
 717  </td>
 718  </tr>
 719              <?php
 720      endif;
 721  
 722          $default_image = sprintf(
 723              get_theme_support( 'custom-header', 'default-image' ),
 724              get_template_directory_uri(),
 725              get_stylesheet_directory_uri()
 726          );
 727  
 728          if ( $default_image && get_header_image() !== $default_image ) :
 729              ?>
 730  <tr>
 731  <th scope="row"><?php _e( 'Reset Image' ); ?></th>
 732  <td>
 733      <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?></p>
 734              <?php submit_button( __( 'Restore Original Header Image' ), '', 'resetheader', false ); ?>
 735  </td>
 736  </tr>
 737      <?php endif; ?>
 738  </tbody>
 739  </table>
 740  
 741          <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
 742  
 743  <h2><?php _e( 'Header Text' ); ?></h2>
 744  
 745  <table class="form-table" role="presentation">
 746  <tbody>
 747  <tr>
 748  <th scope="row"><?php _e( 'Header Text' ); ?></th>
 749  <td>
 750      <p>
 751      <label><input type="checkbox" name="display-header-text" id="display-header-text"<?php checked( display_header_text() ); ?> /> <?php _e( 'Show header text with your image.' ); ?></label>
 752      </p>
 753  </td>
 754  </tr>
 755  
 756  <tr class="displaying-header-text">
 757  <th scope="row"><?php _e( 'Text Color' ); ?></th>
 758  <td>
 759      <p>
 760              <?php
 761              $default_color = '';
 762              if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
 763                  $default_color = get_theme_support( 'custom-header', 'default-text-color' );
 764                  if ( $default_color && false === strpos( $default_color, '#' ) ) {
 765                      $default_color = '#' . $default_color;
 766                  }
 767              }
 768  
 769              $default_color_attr = $default_color ? ' data-default-color="' . esc_attr( $default_color ) . '"' : '';
 770  
 771              $header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' );
 772              if ( $header_textcolor && false === strpos( $header_textcolor, '#' ) ) {
 773                  $header_textcolor = '#' . $header_textcolor;
 774              }
 775  
 776              echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr( $header_textcolor ) . '"' . $default_color_attr . ' />';
 777              if ( $default_color ) {
 778                  /* translators: %s: Default text color. */
 779                  echo ' <span class="description hide-if-js">' . sprintf( _x( 'Default: %s', 'color' ), esc_html( $default_color ) ) . '</span>';
 780              }
 781              ?>
 782      </p>
 783  </td>
 784  </tr>
 785  </tbody>
 786  </table>
 787              <?php
 788  endif;
 789  
 790          /**
 791           * Fires just before the submit button in the custom header options form.
 792           *
 793           * @since 3.1.0
 794           */
 795          do_action( 'custom_header_options' );
 796  
 797          wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' );
 798          ?>
 799  
 800          <?php submit_button( null, 'primary', 'save-header-options' ); ?>
 801  </form>
 802  </div>
 803  
 804          <?php
 805      }
 806  
 807      /**
 808       * Display second step of custom header image page.
 809       *
 810       * @since 2.1.0
 811       */
 812  	public function step_2() {
 813          check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );
 814  
 815          if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
 816              wp_die(
 817                  '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
 818                  '<p>' . __( 'The active theme does not support uploading a custom header image.' ) . '</p>',
 819                  403
 820              );
 821          }
 822  
 823          if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
 824              $attachment_id = absint( $_GET['file'] );
 825              $file          = get_attached_file( $attachment_id, true );
 826              $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
 827              $url           = $url[0];
 828          } elseif ( isset( $_POST ) ) {
 829              $data          = $this->step_2_manage_upload();
 830              $attachment_id = $data['attachment_id'];
 831              $file          = $data['file'];
 832              $url           = $data['url'];
 833          }
 834  
 835          if ( file_exists( $file ) ) {
 836              list( $width, $height, $type, $attr ) = wp_getimagesize( $file );
 837          } else {
 838              $data   = wp_get_attachment_metadata( $attachment_id );
 839              $height = isset( $data['height'] ) ? (int) $data['height'] : 0;
 840              $width  = isset( $data['width'] ) ? (int) $data['width'] : 0;
 841              unset( $data );
 842          }
 843  
 844          $max_width = 0;
 845  
 846          // For flex, limit size of image displayed to 1500px unless theme says otherwise.
 847          if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
 848              $max_width = 1500;
 849          }
 850  
 851          if ( current_theme_supports( 'custom-header', 'max-width' ) ) {
 852              $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
 853          }
 854  
 855          $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
 856  
 857          // If flexible height isn't supported and the image is the exact right size.
 858          if ( ! current_theme_supports( 'custom-header', 'flex-height' )
 859              && ! current_theme_supports( 'custom-header', 'flex-width' )
 860              && (int) get_theme_support( 'custom-header', 'width' ) === $width
 861              && (int) get_theme_support( 'custom-header', 'height' ) === $height
 862          ) {
 863              // Add the metadata.
 864              if ( file_exists( $file ) ) {
 865                  wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
 866              }
 867  
 868              $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
 869  
 870              /**
 871               * Fires after the header image is set or an error is returned.
 872               *
 873               * @since 2.1.0
 874               *
 875               * @param string $file          Path to the file.
 876               * @param int    $attachment_id Attachment ID.
 877               */
 878              do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication.
 879  
 880              return $this->finished();
 881          } elseif ( $width > $max_width ) {
 882              $oitar = $width / $max_width;
 883  
 884              $image = wp_crop_image(
 885                  $attachment_id,
 886                  0,
 887                  0,
 888                  $width,
 889                  $height,
 890                  $max_width,
 891                  $height / $oitar,
 892                  false,
 893                  str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file ), $file )
 894              );
 895  
 896              if ( ! $image || is_wp_error( $image ) ) {
 897                  wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
 898              }
 899  
 900              /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
 901              $image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication.
 902  
 903              $url    = str_replace( wp_basename( $url ), wp_basename( $image ), $url );
 904              $width  = $width / $oitar;
 905              $height = $height / $oitar;
 906          } else {
 907              $oitar = 1;
 908          }
 909          ?>
 910  
 911  <div class="wrap">
 912  <h1><?php _e( 'Crop Header Image' ); ?></h1>
 913  
 914  <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 3 ) ); ?>">
 915      <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p>
 916      <p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.' ); ?></strong></p>
 917  
 918      <div id="crop_image" style="position: relative">
 919          <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="" />
 920      </div>
 921  
 922      <input type="hidden" name="x1" id="x1" value="0" />
 923      <input type="hidden" name="y1" id="y1" value="0" />
 924      <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>" />
 925      <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>" />
 926      <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
 927      <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
 928          <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
 929      <input type="hidden" name="create-new-attachment" value="true" />
 930      <?php } ?>
 931          <?php wp_nonce_field( 'custom-header-crop-image' ); ?>
 932  
 933      <p class="submit">
 934          <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
 935          <?php
 936          if ( isset( $oitar ) && 1 === $oitar
 937              && ( current_theme_supports( 'custom-header', 'flex-height' )
 938                  || current_theme_supports( 'custom-header', 'flex-width' ) )
 939          ) {
 940              submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
 941          }
 942          ?>
 943      </p>
 944  </form>
 945  </div>
 946          <?php
 947      }
 948  
 949  
 950      /**
 951       * Upload the file to be cropped in the second step.
 952       *
 953       * @since 3.4.0
 954       */
 955  	public function step_2_manage_upload() {
 956          $overrides = array( 'test_form' => false );
 957  
 958          $uploaded_file = $_FILES['import'];
 959          $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
 960  
 961          if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
 962              wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
 963          }
 964  
 965          $file = wp_handle_upload( $uploaded_file, $overrides );
 966  
 967          if ( isset( $file['error'] ) ) {
 968              wp_die( $file['error'], __( 'Image Upload Error' ) );
 969          }
 970  
 971          $url      = $file['url'];
 972          $type     = $file['type'];
 973          $file     = $file['file'];
 974          $filename = wp_basename( $file );
 975  
 976          // Construct the attachment array.
 977          $attachment = array(
 978              'post_title'     => $filename,
 979              'post_content'   => $url,
 980              'post_mime_type' => $type,
 981              'guid'           => $url,
 982              'context'        => 'custom-header',
 983          );
 984  
 985          // Save the data.
 986          $attachment_id = wp_insert_attachment( $attachment, $file );
 987  
 988          return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
 989      }
 990  
 991      /**
 992       * Display third step of custom header image page.
 993       *
 994       * @since 2.1.0
 995       * @since 4.4.0 Switched to using wp_get_attachment_url() instead of the guid
 996       *              for retrieving the header image URL.
 997       */
 998  	public function step_3() {
 999          check_admin_referer( 'custom-header-crop-image' );
1000  
1001          if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
1002              wp_die(
1003                  '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
1004                  '<p>' . __( 'The active theme does not support uploading a custom header image.' ) . '</p>',
1005                  403
1006              );
1007          }
1008  
1009          if ( ! empty( $_POST['skip-cropping'] )
1010              && ! current_theme_supports( 'custom-header', 'flex-height' )
1011              && ! current_theme_supports( 'custom-header', 'flex-width' )
1012          ) {
1013              wp_die(
1014                  '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
1015                  '<p>' . __( 'The active theme does not support a flexible sized header image.' ) . '</p>',
1016                  403
1017              );
1018          }
1019  
1020          if ( $_POST['oitar'] > 1 ) {
1021              $_POST['x1']     = $_POST['x1'] * $_POST['oitar'];
1022              $_POST['y1']     = $_POST['y1'] * $_POST['oitar'];
1023              $_POST['width']  = $_POST['width'] * $_POST['oitar'];
1024              $_POST['height'] = $_POST['height'] * $_POST['oitar'];
1025          }
1026  
1027          $attachment_id = absint( $_POST['attachment_id'] );
1028          $original      = get_attached_file( $attachment_id );
1029  
1030          $dimensions = $this->get_header_dimensions(
1031              array(
1032                  'height' => $_POST['height'],
1033                  'width'  => $_POST['width'],
1034              )
1035          );
1036          $height     = $dimensions['dst_height'];
1037          $width      = $dimensions['dst_width'];
1038  
1039          if ( empty( $_POST['skip-cropping'] ) ) {
1040              $cropped = wp_crop_image(
1041                  $attachment_id,
1042                  (int) $_POST['x1'],
1043                  (int) $_POST['y1'],
1044                  (int) $_POST['width'],
1045                  (int) $_POST['height'],
1046                  $width,
1047                  $height
1048              );
1049          } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
1050              $cropped = _copy_image_file( $attachment_id );
1051          } else {
1052              $cropped = get_attached_file( $attachment_id );
1053          }
1054  
1055          if ( ! $cropped || is_wp_error( $cropped ) ) {
1056              wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
1057          }
1058  
1059          /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
1060          $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
1061  
1062          $attachment = $this->create_attachment_object( $cropped, $attachment_id );
1063  
1064          if ( ! empty( $_POST['create-new-attachment'] ) ) {
1065              unset( $attachment['ID'] );
1066          }
1067  
1068          // Update the attachment.
1069          $attachment_id = $this->insert_attachment( $attachment, $cropped );
1070  
1071          $url = wp_get_attachment_url( $attachment_id );
1072          $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
1073  
1074          // Cleanup.
1075          $medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original ), $original );
1076          if ( file_exists( $medium ) ) {
1077              wp_delete_file( $medium );
1078          }
1079  
1080          if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
1081              wp_delete_file( $original );
1082          }
1083  
1084          return $this->finished();
1085      }
1086  
1087      /**
1088       * Display last step of custom header image page.
1089       *
1090       * @since 2.1.0
1091       */
1092  	public function finished() {
1093          $this->updated = true;
1094          $this->step_1();
1095      }
1096  
1097      /**
1098       * Display the page based on the current step.
1099       *
1100       * @since 2.1.0
1101       */
1102  	public function admin_page() {
1103          if ( ! current_user_can( 'edit_theme_options' ) ) {
1104              wp_die( __( 'Sorry, you are not allowed to customize headers.' ) );
1105          }
1106  
1107          $step = $this->step();
1108  
1109          if ( 2 === $step ) {
1110              $this->step_2();
1111          } elseif ( 3 === $step ) {
1112              $this->step_3();
1113          } else {
1114              $this->step_1();
1115          }
1116      }
1117  
1118      /**
1119       * Unused since 3.5.0.
1120       *
1121       * @since 3.4.0
1122       *
1123       * @param array $form_fields
1124       * @return array $form_fields
1125       */
1126  	public function attachment_fields_to_edit( $form_fields ) {
1127          return $form_fields;
1128      }
1129  
1130      /**
1131       * Unused since 3.5.0.
1132       *
1133       * @since 3.4.0
1134       *
1135       * @param array $tabs
1136       * @return array $tabs
1137       */
1138  	public function filter_upload_tabs( $tabs ) {
1139          return $tabs;
1140      }
1141  
1142      /**
1143       * Choose a header image, selected from existing uploaded and default headers,
1144       * or provide an array of uploaded header data (either new, or from media library).
1145       *
1146       * @since 3.4.0
1147       *
1148       * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
1149       *  for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
1150       *  among the uploaded images; the key of a default image registered for that theme; and
1151       *  the key of an image uploaded for that theme (the attachment ID of the image).
1152       *  Or an array of arguments: attachment_id, url, width, height. All are required.
1153       */
1154  	final public function set_header_image( $choice ) {
1155          if ( is_array( $choice ) || is_object( $choice ) ) {
1156              $choice = (array) $choice;
1157  
1158              if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
1159                  return;
1160              }
1161  
1162              $choice['url'] = esc_url_raw( $choice['url'] );
1163  
1164              $header_image_data = (object) array(
1165                  'attachment_id' => $choice['attachment_id'],
1166                  'url'           => $choice['url'],
1167                  'thumbnail_url' => $choice['url'],
1168                  'height'        => $choice['height'],
1169                  'width'         => $choice['width'],
1170              );
1171  
1172              update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
1173  
1174              set_theme_mod( 'header_image', $choice['url'] );
1175              set_theme_mod( 'header_image_data', $header_image_data );
1176  
1177              return;
1178          }
1179  
1180          if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ), true ) ) {
1181              set_theme_mod( 'header_image', $choice );
1182              remove_theme_mod( 'header_image_data' );
1183  
1184              return;
1185          }
1186  
1187          $uploaded = get_uploaded_header_images();
1188  
1189          if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
1190              $header_image_data = $uploaded[ $choice ];
1191          } else {
1192              $this->process_default_headers();
1193              if ( isset( $this->default_headers[ $choice ] ) ) {
1194                  $header_image_data = $this->default_headers[ $choice ];
1195              } else {
1196                  return;
1197              }
1198          }
1199  
1200          set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
1201          set_theme_mod( 'header_image_data', $header_image_data );
1202      }
1203  
1204      /**
1205       * Remove a header image.
1206       *
1207       * @since 3.4.0
1208       */
1209  	final public function remove_header_image() {
1210          $this->set_header_image( 'remove-header' );
1211      }
1212  
1213      /**
1214       * Reset a header image to the default image for the theme.
1215       *
1216       * This method does not do anything if the theme does not have a default header image.
1217       *
1218       * @since 3.4.0
1219       */
1220  	final public function reset_header_image() {
1221          $this->process_default_headers();
1222          $default = get_theme_support( 'custom-header', 'default-image' );
1223  
1224          if ( ! $default ) {
1225              $this->remove_header_image();
1226              return;
1227          }
1228  
1229          $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
1230  
1231          $default_data = array();
1232          foreach ( $this->default_headers as $header => $details ) {
1233              if ( $details['url'] === $default ) {
1234                  $default_data = $details;
1235                  break;
1236              }
1237          }
1238  
1239          set_theme_mod( 'header_image', $default );
1240          set_theme_mod( 'header_image_data', (object) $default_data );
1241      }
1242  
1243      /**
1244       * Calculate width and height based on what the currently selected theme supports.
1245       *
1246       * @since 3.9.0
1247       *
1248       * @param array $dimensions
1249       * @return array dst_height and dst_width of header image.
1250       */
1251  	final public function get_header_dimensions( $dimensions ) {
1252          $max_width       = 0;
1253          $width           = absint( $dimensions['width'] );
1254          $height          = absint( $dimensions['height'] );
1255          $theme_height    = get_theme_support( 'custom-header', 'height' );
1256          $theme_width     = get_theme_support( 'custom-header', 'width' );
1257          $has_flex_width  = current_theme_supports( 'custom-header', 'flex-width' );
1258          $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
1259          $has_max_width   = current_theme_supports( 'custom-header', 'max-width' );
1260          $dst             = array(
1261              'dst_height' => null,
1262              'dst_width'  => null,
1263          );
1264  
1265          // For flex, limit size of image displayed to 1500px unless theme says otherwise.
1266          if ( $has_flex_width ) {
1267              $max_width = 1500;
1268          }
1269  
1270          if ( $has_max_width ) {
1271              $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
1272          }
1273          $max_width = max( $max_width, $theme_width );
1274  
1275          if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
1276              $dst['dst_height'] = absint( $height * ( $max_width / $width ) );
1277          } elseif ( $has_flex_height && $has_flex_width ) {
1278              $dst['dst_height'] = $height;
1279          } else {
1280              $dst['dst_height'] = $theme_height;
1281          }
1282  
1283          if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
1284              $dst['dst_width'] = absint( $width * ( $max_width / $width ) );
1285          } elseif ( $has_flex_width && $has_flex_height ) {
1286              $dst['dst_width'] = $width;
1287          } else {
1288              $dst['dst_width'] = $theme_width;
1289          }
1290  
1291          return $dst;
1292      }
1293  
1294      /**
1295       * Create an attachment 'object'.
1296       *
1297       * @since 3.9.0
1298       *
1299       * @param string $cropped              Cropped image URL.
1300       * @param int    $parent_attachment_id Attachment ID of parent image.
1301       * @return array An array with attachment object data.
1302       */
1303  	final public function create_attachment_object( $cropped, $parent_attachment_id ) {
1304          $parent     = get_post( $parent_attachment_id );
1305          $parent_url = wp_get_attachment_url( $parent->ID );
1306          $url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
1307  
1308          $size       = wp_getimagesize( $cropped );
1309          $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
1310  
1311          $attachment = array(
1312              'ID'             => $parent_attachment_id,
1313              'post_title'     => wp_basename( $cropped ),
1314              'post_mime_type' => $image_type,
1315              'guid'           => $url,
1316              'context'        => 'custom-header',
1317              'post_parent'    => $parent_attachment_id,
1318          );
1319  
1320          return $attachment;
1321      }
1322  
1323      /**
1324       * Insert an attachment and its metadata.
1325       *
1326       * @since 3.9.0
1327       *
1328       * @param array  $attachment An array with attachment object data.
1329       * @param string $cropped    File path to cropped image.
1330       * @return int Attachment ID.
1331       */
1332  	final public function insert_attachment( $attachment, $cropped ) {
1333          $parent_id = isset( $attachment['post_parent'] ) ? $attachment['post_parent'] : null;
1334          unset( $attachment['post_parent'] );
1335  
1336          $attachment_id = wp_insert_attachment( $attachment, $cropped );
1337          $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
1338  
1339          // If this is a crop, save the original attachment ID as metadata.
1340          if ( $parent_id ) {
1341              $metadata['attachment_parent'] = $parent_id;
1342          }
1343  
1344          /**
1345           * Filters the header image attachment metadata.
1346           *
1347           * @since 3.9.0
1348           *
1349           * @see wp_generate_attachment_metadata()
1350           *
1351           * @param array $metadata Attachment metadata.
1352           */
1353          $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata );
1354  
1355          wp_update_attachment_metadata( $attachment_id, $metadata );
1356  
1357          return $attachment_id;
1358      }
1359  
1360      /**
1361       * Gets attachment uploaded by Media Manager, crops it, then saves it as a
1362       * new object. Returns JSON-encoded object details.
1363       *
1364       * @since 3.9.0
1365       */
1366  	public function ajax_header_crop() {
1367          check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );
1368  
1369          if ( ! current_user_can( 'edit_theme_options' ) ) {
1370              wp_send_json_error();
1371          }
1372  
1373          if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
1374              wp_send_json_error();
1375          }
1376  
1377          $crop_details = $_POST['cropDetails'];
1378  
1379          $dimensions = $this->get_header_dimensions(
1380              array(
1381                  'height' => $crop_details['height'],
1382                  'width'  => $crop_details['width'],
1383              )
1384          );
1385  
1386          $attachment_id = absint( $_POST['id'] );
1387  
1388          $cropped = wp_crop_image(
1389              $attachment_id,
1390              (int) $crop_details['x1'],
1391              (int) $crop_details['y1'],
1392              (int) $crop_details['width'],
1393              (int) $crop_details['height'],
1394              (int) $dimensions['dst_width'],
1395              (int) $dimensions['dst_height']
1396          );
1397  
1398          if ( ! $cropped || is_wp_error( $cropped ) ) {
1399              wp_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) );
1400          }
1401  
1402          /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
1403          $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
1404  
1405          $attachment = $this->create_attachment_object( $cropped, $attachment_id );
1406  
1407          $previous = $this->get_previous_crop( $attachment );
1408  
1409          if ( $previous ) {
1410              $attachment['ID'] = $previous;
1411          } else {
1412              unset( $attachment['ID'] );
1413          }
1414  
1415          $new_attachment_id = $this->insert_attachment( $attachment, $cropped );
1416  
1417          $attachment['attachment_id'] = $new_attachment_id;
1418          $attachment['url']           = wp_get_attachment_url( $new_attachment_id );
1419  
1420          $attachment['width']  = $dimensions['dst_width'];
1421          $attachment['height'] = $dimensions['dst_height'];
1422  
1423          wp_send_json_success( $attachment );
1424      }
1425  
1426      /**
1427       * Given an attachment ID for a header image, updates its "last used"
1428       * timestamp to now.
1429       *
1430       * Triggered when the user tries adds a new header image from the
1431       * Media Manager, even if s/he doesn't save that change.
1432       *
1433       * @since 3.9.0
1434       */
1435  	public function ajax_header_add() {
1436          check_ajax_referer( 'header-add', 'nonce' );
1437  
1438          if ( ! current_user_can( 'edit_theme_options' ) ) {
1439              wp_send_json_error();
1440          }
1441  
1442          $attachment_id = absint( $_POST['attachment_id'] );
1443          if ( $attachment_id < 1 ) {
1444              wp_send_json_error();
1445          }
1446  
1447          $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1448          update_post_meta( $attachment_id, $key, time() );
1449          update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
1450  
1451          wp_send_json_success();
1452      }
1453  
1454      /**
1455       * Given an attachment ID for a header image, unsets it as a user-uploaded
1456       * header image for the active theme.
1457       *
1458       * Triggered when the user clicks the overlay "X" button next to each image
1459       * choice in the Customizer's Header tool.
1460       *
1461       * @since 3.9.0
1462       */
1463  	public function ajax_header_remove() {
1464          check_ajax_referer( 'header-remove', 'nonce' );
1465  
1466          if ( ! current_user_can( 'edit_theme_options' ) ) {
1467              wp_send_json_error();
1468          }
1469  
1470          $attachment_id = absint( $_POST['attachment_id'] );
1471          if ( $attachment_id < 1 ) {
1472              wp_send_json_error();
1473          }
1474  
1475          $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1476          delete_post_meta( $attachment_id, $key );
1477          delete_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
1478  
1479          wp_send_json_success();
1480      }
1481  
1482      /**
1483       * Updates the last-used postmeta on a header image attachment after saving a new header image via the Customizer.
1484       *
1485       * @since 3.9.0
1486       *
1487       * @param WP_Customize_Manager $wp_customize Customize manager.
1488       */
1489  	public function customize_set_last_used( $wp_customize ) {
1490  
1491          $header_image_data_setting = $wp_customize->get_setting( 'header_image_data' );
1492  
1493          if ( ! $header_image_data_setting ) {
1494              return;
1495          }
1496  
1497          $data = $header_image_data_setting->post_value();
1498  
1499          if ( ! isset( $data['attachment_id'] ) ) {
1500              return;
1501          }
1502  
1503          $attachment_id = $data['attachment_id'];
1504          $key           = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1505          update_post_meta( $attachment_id, $key, time() );
1506      }
1507  
1508      /**
1509       * Gets the details of default header images if defined.
1510       *
1511       * @since 3.9.0
1512       *
1513       * @return array Default header images.
1514       */
1515  	public function get_default_header_images() {
1516          $this->process_default_headers();
1517  
1518          // Get the default image if there is one.
1519          $default = get_theme_support( 'custom-header', 'default-image' );
1520  
1521          if ( ! $default ) { // If not, easy peasy.
1522              return $this->default_headers;
1523          }
1524  
1525          $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
1526  
1527          $already_has_default = false;
1528  
1529          foreach ( $this->default_headers as $k => $h ) {
1530              if ( $h['url'] === $default ) {
1531                  $already_has_default = true;
1532                  break;
1533              }
1534          }
1535  
1536          if ( $already_has_default ) {
1537              return $this->default_headers;
1538          }
1539  
1540          // If the one true image isn't included in the default set, prepend it.
1541          $header_images            = array();
1542          $header_images['default'] = array(
1543              'url'           => $default,
1544              'thumbnail_url' => $default,
1545              'description'   => 'Default',
1546          );
1547  
1548          // The rest of the set comes after.
1549          return array_merge( $header_images, $this->default_headers );
1550      }
1551  
1552      /**
1553       * Gets the previously uploaded header images.
1554       *
1555       * @since 3.9.0
1556       *
1557       * @return array Uploaded header images.
1558       */
1559  	public function get_uploaded_header_images() {
1560          $header_images = get_uploaded_header_images();
1561          $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1562          $alt_text_key  = '_wp_attachment_image_alt';
1563  
1564          foreach ( $header_images as &$header_image ) {
1565              $header_meta               = get_post_meta( $header_image['attachment_id'] );
1566              $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
1567              $header_image['alt_text']  = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
1568          }
1569  
1570          return $header_images;
1571      }
1572  
1573      /**
1574       * Get the ID of a previous crop from the same base image.
1575       *
1576       * @since 4.9.0
1577       *
1578       * @param array $attachment An array with a cropped attachment object data.
1579       * @return int|false An attachment ID if one exists. False if none.
1580       */
1581  	public function get_previous_crop( $attachment ) {
1582          $header_images = $this->get_uploaded_header_images();
1583  
1584          // Bail early if there are no header images.
1585          if ( empty( $header_images ) ) {
1586              return false;
1587          }
1588  
1589          $previous = false;
1590  
1591          foreach ( $header_images as $image ) {
1592              if ( $image['attachment_parent'] === $attachment['post_parent'] ) {
1593                  $previous = $image['attachment_id'];
1594                  break;
1595              }
1596          }
1597  
1598          return $previous;
1599      }
1600  }


Generated: Thu Mar 28 01:00:02 2024 Cross-referenced by PHPXref 0.7.1