3 * The custom header image script.
6 * @subpackage Administration
10 * The custom header image class.
14 * @subpackage Administration
16 class Custom_Image_Header {
19 * Callback for administration header.
25 private $admin_header_callback;
28 * Callback for header div.
34 private $admin_image_div_callback;
37 * Holds default headers.
43 private $default_headers = array();
46 * Holds custom headers uploaded by the user.
52 private $uploaded_headers = array();
55 * Holds the page menu hook.
64 * Constructor - Register administration header callback.
67 * @param callback $admin_header_callback
68 * @param callback $admin_image_div_callback Optional custom image div output callback.
69 * @return Custom_Image_Header
71 public function __construct($admin_header_callback, $admin_image_div_callback = '') {
72 $this->admin_header_callback = $admin_header_callback;
73 $this->admin_image_div_callback = $admin_image_div_callback;
75 add_action( 'admin_menu', array( $this, 'init' ) );
77 add_action( 'customize_save_after', array( $this, 'customize_set_last_used' ) );
78 add_action( 'wp_ajax_custom-header-crop', array( $this, 'ajax_header_crop' ) );
79 add_action( 'wp_ajax_custom-header-add', array( $this, 'ajax_header_add' ) );
80 add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) );
84 * Make private properties readable for backwards compatibility.
89 * @param string $name Property to get.
90 * @return mixed Property.
92 public function __get( $name ) {
97 * Make private properties settable for backwards compatibility.
102 * @param string $name Property to set.
103 * @param mixed $value Property value.
104 * @return mixed Newly-set property.
106 public function __set( $name, $value ) {
107 return $this->$name = $value;
111 * Make private properties checkable for backwards compatibility.
116 * @param string $name Property to check if set.
117 * @return bool Whether the property is set.
119 public function __isset( $name ) {
120 return isset( $this->$name );
124 * Make private properties un-settable for backwards compatibility.
129 * @param string $name Property to unset.
131 public function __unset( $name ) {
132 unset( $this->$name );
136 * Set up the hooks for the Custom Header admin page.
140 public function init() {
141 if ( ! current_user_can('edit_theme_options') )
144 $this->page = $page = add_theme_page(__('Header'), __('Header'), 'edit_theme_options', 'custom-header', array($this, 'admin_page'));
146 add_action("admin_print_scripts-$page", array($this, 'js_includes'));
147 add_action("admin_print_styles-$page", array($this, 'css_includes'));
148 add_action("admin_head-$page", array($this, 'help') );
149 add_action("admin_head-$page", array($this, 'take_action'), 50);
150 add_action("admin_head-$page", array($this, 'js'), 50);
151 if ( $this->admin_header_callback )
152 add_action("admin_head-$page", $this->admin_header_callback, 51);
157 * Adds contextual help.
161 public function help() {
162 get_current_screen()->add_help_tab( array(
164 'title' => __('Overview'),
166 '<p>' . __( 'This screen is used to customize the header section of your theme.') . '</p>' .
167 '<p>' . __( 'You can choose from the theme’s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.') . '<p>'
170 get_current_screen()->add_help_tab( array(
171 'id' => 'set-header-image',
172 'title' => __('Header Image'),
174 '<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 “Choose Image” button.' ) . '</p>' .
175 '<p>' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you’d like and click the “Save Changes” button.' ) . '</p>' .
176 '<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 “Random” radio button next to the Uploaded Images or Default Images section to enable this feature.') . '</p>' .
177 '<p>' . __( 'If you don’t want a header image to be displayed on your site at all, click the “Remove Header Image” 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 “Save Changes”.') . '</p>'
180 get_current_screen()->add_help_tab( array(
181 'id' => 'set-header-text',
182 'title' => __('Header Text'),
184 '<p>' . sprintf( __( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%1$s">General Settings</a> section.' ), admin_url( 'options-general.php' ) ) . '<p>' .
185 '<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. “#ff0000” for red, or by choosing a color using the color picker.' ) . '</p>' .
186 '<p>' . __( 'Don’t forget to click “Save Changes” when you’re done!') . '</p>'
189 get_current_screen()->set_help_sidebar(
190 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
191 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Header_Screen" target="_blank">Documentation on Custom Header</a>' ) . '</p>' .
192 '<p>' . __( '<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
197 * Get the current step.
201 * @return int Current step
203 public function step() {
204 if ( ! isset( $_GET['step'] ) )
207 $step = (int) $_GET['step'];
208 if ( $step < 1 || 3 < $step ||
209 ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
210 ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
218 * Set up the enqueue for the JavaScript files.
222 public function js_includes() {
223 $step = $this->step();
225 if ( ( 1 == $step || 3 == $step ) ) {
227 wp_enqueue_script( 'custom-header' );
228 if ( current_theme_supports( 'custom-header', 'header-text' ) )
229 wp_enqueue_script( 'wp-color-picker' );
230 } elseif ( 2 == $step ) {
231 wp_enqueue_script('imgareaselect');
236 * Set up the enqueue for the CSS files
240 public function css_includes() {
241 $step = $this->step();
243 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
244 wp_enqueue_style( 'wp-color-picker' );
245 elseif ( 2 == $step )
246 wp_enqueue_style('imgareaselect');
250 * Execute custom header modification.
254 public function take_action() {
255 if ( ! current_user_can('edit_theme_options') )
258 if ( empty( $_POST ) )
261 $this->updated = true;
263 if ( isset( $_POST['resetheader'] ) ) {
264 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
265 $this->reset_header_image();
269 if ( isset( $_POST['removeheader'] ) ) {
270 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
271 $this->remove_header_image();
275 if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
276 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
277 set_theme_mod( 'header_textcolor', 'blank' );
278 } elseif ( isset( $_POST['text-color'] ) ) {
279 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
280 $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
281 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
282 if ( strlen($color) == 6 || strlen($color) == 3 )
283 set_theme_mod('header_textcolor', $color);
285 set_theme_mod( 'header_textcolor', 'blank' );
288 if ( isset( $_POST['default-header'] ) ) {
289 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
290 $this->set_header_image( $_POST['default-header'] );
296 * Process the default headers
300 public function process_default_headers() {
301 global $_wp_default_headers;
303 if ( !empty($this->headers) )
306 if ( !isset($_wp_default_headers) )
309 if ( ! empty( $this->default_headers ) ) {
313 $this->default_headers = $_wp_default_headers;
314 $template_directory_uri = get_template_directory_uri();
315 $stylesheet_directory_uri = get_stylesheet_directory_uri();
316 foreach ( array_keys($this->default_headers) as $header ) {
317 $this->default_headers[$header]['url'] = sprintf( $this->default_headers[$header]['url'], $template_directory_uri, $stylesheet_directory_uri );
318 $this->default_headers[$header]['thumbnail_url'] = sprintf( $this->default_headers[$header]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
323 * Display UI for selecting one of several default headers.
325 * Show the random image option if this theme has multiple header images.
326 * Random image option is on by default if no header has been set.
330 public function show_header_selector( $type = 'default' ) {
331 if ( 'default' == $type ) {
332 $headers = $this->default_headers;
334 $headers = get_uploaded_header_images();
338 if ( 1 < count( $headers ) ) {
339 echo '<div class="random-header">';
340 echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
341 echo __( '<strong>Random:</strong> Show a different image on each page.' );
346 echo '<div class="available-headers">';
347 foreach ( $headers as $header_key => $header ) {
348 $header_thumbnail = $header['thumbnail_url'];
349 $header_url = $header['url'];
350 $header_desc = empty( $header['description'] ) ? '' : $header['description'];
351 echo '<div class="default-header">';
352 echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
354 if ( !empty( $header['attachment_id'] ) )
355 $width = ' width="230"';
356 echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>';
359 echo '<div class="clear"></div></div>';
363 * Execute Javascript depending on step.
367 public function js() {
368 $step = $this->step();
369 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
371 elseif ( 2 == $step )
376 * Display Javascript based on Step 1 and 3.
380 public function js_1() {
382 if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
383 $default_color = get_theme_support( 'custom-header', 'default-text-color' );
384 if ( $default_color && false === strpos( $default_color, '#' ) ) {
385 $default_color = '#' . $default_color;
390 <script type="text/javascript">
393 var default_color = '<?php echo $default_color; ?>',
396 function pickColor(color) {
397 $('#name').css('color', color);
398 $('#desc').css('color', color);
399 $('#text-color').val(color);
402 function toggle_text() {
403 var checked = $('#display-header-text').prop('checked'),
405 header_text_fields.toggle( checked );
408 text_color = $('#text-color');
409 if ( '' == text_color.val().replace('#', '') ) {
410 text_color.val( default_color );
411 pickColor( default_color );
413 pickColor( text_color.val() );
417 $(document).ready(function() {
418 var text_color = $('#text-color');
419 header_text_fields = $('.displaying-header-text');
420 text_color.wpColorPicker({
421 change: function( event, ui ) {
422 pickColor( text_color.wpColorPicker('color') );
428 $('#display-header-text').click( toggle_text );
429 <?php if ( ! display_header_text() ) : ?>
440 * Display Javascript based on Step 2.
444 public function js_2() { ?>
445 <script type="text/javascript">
447 function onEndCrop( coords ) {
448 jQuery( '#x1' ).val(coords.x);
449 jQuery( '#y1' ).val(coords.y);
450 jQuery( '#width' ).val(coords.w);
451 jQuery( '#height' ).val(coords.h);
454 jQuery(document).ready(function() {
455 var xinit = <?php echo absint( get_theme_support( 'custom-header', 'width' ) ); ?>;
456 var yinit = <?php echo absint( get_theme_support( 'custom-header', 'height' ) ); ?>;
457 var ratio = xinit / yinit;
458 var ximg = jQuery('img#upload').width();
459 var yimg = jQuery('img#upload').height();
461 if ( yimg < yinit || ximg < xinit ) {
462 if ( ximg / yimg > ratio ) {
464 xinit = yinit * ratio;
467 yinit = xinit / ratio;
471 jQuery('img#upload').imgAreaSelect({
480 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
482 aspectRatio: xinit + ':' + yinit,
485 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
487 maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
490 if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
492 maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
496 onInit: function () {
497 jQuery('#width').val(xinit);
498 jQuery('#height').val(yinit);
500 onSelectChange: function(img, c) {
501 jQuery('#x1').val(c.x1);
502 jQuery('#y1').val(c.y1);
503 jQuery('#width').val(c.width);
504 jQuery('#height').val(c.height);
514 * Display first step of custom header image page.
518 public function step_1() {
519 $this->process_default_headers();
523 <h2><?php _e( 'Custom Header' ); ?></h2>
525 <?php if ( ! empty( $this->updated ) ) { ?>
526 <div id="message" class="updated">
527 <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
531 <h3><?php _e( 'Header Image' ); ?></h3>
533 <table class="form-table">
536 <?php if ( get_custom_header() || display_header_text() ) : ?>
538 <th scope="row"><?php _e( 'Preview' ); ?></th>
541 if ( $this->admin_image_div_callback ) {
542 call_user_func( $this->admin_image_div_callback );
544 $custom_header = get_custom_header();
545 $header_image_style = 'background-image:url(' . esc_url( get_header_image() ) . ');';
546 if ( $custom_header->width )
547 $header_image_style .= 'max-width:' . $custom_header->width . 'px;';
548 if ( $custom_header->height )
549 $header_image_style .= 'height:' . $custom_header->height . 'px;';
551 <div id="headimg" style="<?php echo $header_image_style; ?>">
553 if ( display_header_text() )
554 $style = ' style="color:#' . get_header_textcolor() . ';"';
556 $style = ' style="display:none;"';
558 <h1><a id="name" class="displaying-header-text" <?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>"><?php bloginfo( 'name' ); ?></a></h1>
559 <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
566 <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
568 <th scope="row"><?php _e( 'Select Image' ); ?></th>
570 <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 />
572 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
573 printf( __( 'Images of exactly <strong>%1$d × %2$d pixels</strong> will be used as-is.' ) . '<br />', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) );
574 } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
575 if ( ! current_theme_supports( 'custom-header', 'flex-width' ) )
576 printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
577 } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
578 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
579 printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
581 if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
582 if ( current_theme_supports( 'custom-header', 'width' ) )
583 printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
584 if ( current_theme_supports( 'custom-header', 'height' ) )
585 printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
588 <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ) ?>">
590 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
591 <input type="file" id="upload" name="import" />
592 <input type="hidden" name="action" value="save" />
593 <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?>
594 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
597 $modal_update_href = esc_url( add_query_arg( array(
598 'page' => 'custom-header',
600 '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
601 ), admin_url('themes.php') ) );
604 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
605 <a id="choose-from-library-link" class="button"
606 data-update-link="<?php echo esc_attr( $modal_update_href ); ?>"
607 data-choose="<?php esc_attr_e( 'Choose a Custom Header' ); ?>"
608 data-update="<?php esc_attr_e( 'Set as header' ); ?>"><?php _e( 'Choose Image' ); ?></a>
617 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ) ?>">
618 <table class="form-table">
620 <?php if ( get_uploaded_header_images() ) : ?>
622 <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
624 <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
626 $this->show_header_selector( 'uploaded' );
631 if ( ! empty( $this->default_headers ) ) : ?>
633 <th scope="row"><?php _e( 'Default Images' ); ?></th>
635 <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
636 <p><?php _e( 'If you don‘t want to upload your own image, you can use one of these cool headers, or show a random one.' ) ?></p>
638 <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
641 $this->show_header_selector( 'default' );
646 if ( get_header_image() ) : ?>
648 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
650 <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
651 <?php submit_button( __( 'Remove Header Image' ), 'button', 'removeheader', false ); ?>
656 $default_image = get_theme_support( 'custom-header', 'default-image' );
657 if ( $default_image && get_header_image() != $default_image ) : ?>
659 <th scope="row"><?php _e( 'Reset Image' ); ?></th>
661 <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
662 <?php submit_button( __( 'Restore Original Header Image' ), 'button', 'resetheader', false ); ?>
669 <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
671 <h3><?php _e( 'Header Text' ); ?></h3>
673 <table class="form-table">
676 <th scope="row"><?php _e( 'Header Text' ); ?></th>
679 <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>
684 <tr class="displaying-header-text">
685 <th scope="row"><?php _e( 'Text Color' ); ?></th>
690 if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
691 $default_color = get_theme_support( 'custom-header', 'default-text-color' );
692 if ( $default_color && false === strpos( $default_color, '#' ) ) {
693 $default_color = '#' . $default_color;
697 $default_color_attr = $default_color ? ' data-default-color="' . esc_attr( $default_color ) . '"' : '';
699 $header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' );
700 if ( $header_textcolor && false === strpos( $header_textcolor, '#' ) ) {
701 $header_textcolor = '#' . $header_textcolor;
704 echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr( $header_textcolor ) . '"' . $default_color_attr . ' />';
705 if ( $default_color ) {
706 echo ' <span class="description hide-if-js">' . sprintf( _x( 'Default: %s', 'color' ), esc_html( $default_color ) ) . '</span>';
717 * Fires just before the submit button in the custom header options form.
721 do_action( 'custom_header_options' );
723 wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
725 <?php submit_button( null, 'primary', 'save-header-options' ); ?>
732 * Display second step of custom header image page.
736 public function step_2() {
737 check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
738 if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
739 wp_die( __( 'Cheatin’ uh?' ) );
741 if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
742 $attachment_id = absint( $_GET['file'] );
743 $file = get_attached_file( $attachment_id, true );
744 $url = wp_get_attachment_image_src( $attachment_id, 'full' );
746 } elseif ( isset( $_POST ) ) {
747 $data = $this->step_2_manage_upload();
748 $attachment_id = $data['attachment_id'];
749 $file = $data['file'];
751 $type = $data['type'];
754 if ( file_exists( $file ) ) {
755 list( $width, $height, $type, $attr ) = getimagesize( $file );
757 $data = wp_get_attachment_metadata( $attachment_id );
758 $height = isset( $data[ 'height' ] ) ? $data[ 'height' ] : 0;
759 $width = isset( $data[ 'width' ] ) ? $data[ 'width' ] : 0;
764 // For flex, limit size of image displayed to 1500px unless theme says otherwise
765 if ( current_theme_supports( 'custom-header', 'flex-width' ) )
768 if ( current_theme_supports( 'custom-header', 'max-width' ) )
769 $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
770 $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
772 // If flexible height isn't supported and the image is the exact right size
773 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
774 && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) )
777 if ( file_exists( $file ) )
778 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
780 $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
783 * Fires after the header image is set or an error is returned.
787 * @param string $file Path to the file.
788 * @param int $attachment_id Attachment ID.
790 do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication
792 return $this->finished();
793 } elseif ( $width > $max_width ) {
794 $oitar = $width / $max_width;
795 $image = wp_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
796 if ( ! $image || is_wp_error( $image ) )
797 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
799 /** This filter is documented in wp-admin/custom-header.php */
800 $image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication
802 $url = str_replace(basename($url), basename($image), $url);
803 $width = $width / $oitar;
804 $height = $height / $oitar;
811 <h2><?php _e( 'Crop Header Image' ); ?></h2>
813 <form method="post" action="<?php echo esc_url(add_query_arg('step', 3)); ?>">
814 <p class="hide-if-no-js"><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
815 <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
817 <div id="crop_image" style="position: relative">
818 <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
821 <input type="hidden" name="x1" id="x1" value="0"/>
822 <input type="hidden" name="y1" id="y1" value="0"/>
823 <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
824 <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
825 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
826 <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
827 <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
828 <input type="hidden" name="create-new-attachment" value="true" />
830 <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
833 <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
835 if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) )
836 submit_button( __( 'Skip Cropping, Publish Image as Is' ), 'secondary', 'skip-cropping', false );
846 * Upload the file to be cropped in the second step.
850 public function step_2_manage_upload() {
851 $overrides = array('test_form' => false);
853 $uploaded_file = $_FILES['import'];
854 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'], false );
855 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
856 wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
858 $file = wp_handle_upload($uploaded_file, $overrides);
860 if ( isset($file['error']) )
861 wp_die( $file['error'], __( 'Image Upload Error' ) );
864 $type = $file['type'];
865 $file = $file['file'];
866 $filename = basename($file);
868 // Construct the object array
870 'post_title' => $filename,
871 'post_content' => $url,
872 'post_mime_type' => $type,
874 'context' => 'custom-header'
878 $attachment_id = wp_insert_attachment( $object, $file );
879 return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
883 * Display third step of custom header image page.
887 public function step_3() {
888 check_admin_referer( 'custom-header-crop-image' );
890 if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
891 wp_die( __( 'Cheatin’ uh?' ) );
893 if ( ! empty( $_POST['skip-cropping'] ) && ! ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) )
894 wp_die( __( 'Cheatin’ uh?' ) );
896 if ( $_POST['oitar'] > 1 ) {
897 $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
898 $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
899 $_POST['width'] = $_POST['width'] * $_POST['oitar'];
900 $_POST['height'] = $_POST['height'] * $_POST['oitar'];
903 $attachment_id = absint( $_POST['attachment_id'] );
904 $original = get_attached_file($attachment_id);
906 $dimensions = $this->get_header_dimensions( array(
907 'height' => $_POST['height'],
908 'width' => $_POST['width'],
910 $height = $dimensions['dst_height'];
911 $width = $dimensions['dst_width'];
913 if ( empty( $_POST['skip-cropping'] ) )
914 $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height );
915 elseif ( ! empty( $_POST['create-new-attachment'] ) )
916 $cropped = _copy_image_file( $attachment_id );
918 $cropped = get_attached_file( $attachment_id );
920 if ( ! $cropped || is_wp_error( $cropped ) )
921 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
923 /** This filter is documented in wp-admin/custom-header.php */
924 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
926 $object = $this->create_attachment_object( $cropped, $attachment_id );
928 if ( ! empty( $_POST['create-new-attachment'] ) )
929 unset( $object['ID'] );
931 // Update the attachment
932 $attachment_id = $this->insert_attachment( $object, $cropped );
934 $url = $object['guid'];
935 $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
938 $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
939 if ( file_exists( $medium ) ) {
941 * Filter the path of the file to delete.
945 * @param string $medium Path to the file to delete.
947 @unlink( apply_filters( 'wp_delete_file', $medium ) );
950 if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
951 /** This filter is documented in wp-admin/custom-header.php */
952 @unlink( apply_filters( 'wp_delete_file', $original ) );
955 return $this->finished();
959 * Display last step of custom header image page.
963 public function finished() {
964 $this->updated = true;
969 * Display the page based on the current step.
973 public function admin_page() {
974 if ( ! current_user_can('edit_theme_options') )
975 wp_die(__('You do not have permission to customize headers.'));
976 $step = $this->step();
979 elseif ( 3 == $step )
986 * Unused since 3.5.0.
990 public function attachment_fields_to_edit( $form_fields ) {
995 * Unused since 3.5.0.
999 public function filter_upload_tabs( $tabs ) {
1004 * Choose a header image, selected from existing uploaded and default headers,
1005 * or provide an array of uploaded header data (either new, or from media library).
1007 * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
1008 * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
1009 * among the uploaded images; the key of a default image registered for that theme; and
1010 * the key of an image uploaded for that theme (the basename of the URL).
1011 * Or an array of arguments: attachment_id, url, width, height. All are required.
1015 final public function set_header_image( $choice ) {
1016 if ( is_array( $choice ) || is_object( $choice ) ) {
1017 $choice = (array) $choice;
1018 if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
1021 $choice['url'] = esc_url_raw( $choice['url'] );
1023 $header_image_data = (object) array(
1024 'attachment_id' => $choice['attachment_id'],
1025 'url' => $choice['url'],
1026 'thumbnail_url' => $choice['url'],
1027 'height' => $choice['height'],
1028 'width' => $choice['width'],
1031 update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
1032 set_theme_mod( 'header_image', $choice['url'] );
1033 set_theme_mod( 'header_image_data', $header_image_data );
1037 if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ) ) ) {
1038 set_theme_mod( 'header_image', $choice );
1039 remove_theme_mod( 'header_image_data' );
1043 $uploaded = get_uploaded_header_images();
1044 if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
1045 $header_image_data = $uploaded[ $choice ];
1048 $this->process_default_headers();
1049 if ( isset( $this->default_headers[ $choice ] ) )
1050 $header_image_data = $this->default_headers[ $choice ];
1055 set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
1056 set_theme_mod( 'header_image_data', $header_image_data );
1060 * Remove a header image.
1064 final public function remove_header_image() {
1065 return $this->set_header_image( 'remove-header' );
1069 * Reset a header image to the default image for the theme.
1071 * This method does not do anything if the theme does not have a default header image.
1075 final public function reset_header_image() {
1076 $this->process_default_headers();
1077 $default = get_theme_support( 'custom-header', 'default-image' );
1080 return $this->remove_header_image();
1082 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
1084 $default_data = array();
1085 foreach ( $this->default_headers as $header => $details ) {
1086 if ( $details['url'] == $default ) {
1087 $default_data = $details;
1092 set_theme_mod( 'header_image', $default );
1093 set_theme_mod( 'header_image_data', (object) $default_data );
1097 * Calculate width and height based on what the currently selected theme supports.
1099 * @return array dst_height and dst_width of header image.
1101 final public function get_header_dimensions( $dimensions ) {
1103 $width = absint( $dimensions['width'] );
1104 $height = absint( $dimensions['height'] );
1105 $theme_height = get_theme_support( 'custom-header', 'height' );
1106 $theme_width = get_theme_support( 'custom-header', 'width' );
1107 $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' );
1108 $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
1109 $has_max_width = current_theme_supports( 'custom-header', 'max-width' ) ;
1110 $dst = array( 'dst_height' => null, 'dst_height' => null );
1112 // For flex, limit size of image displayed to 1500px unless theme says otherwise
1113 if ( $has_flex_width ) {
1117 if ( $has_max_width ) {
1118 $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
1120 $max_width = max( $max_width, $theme_width );
1122 if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
1123 $dst['dst_height'] = absint( $height * ( $max_width / $width ) );
1125 elseif ( $has_flex_height && $has_flex_width ) {
1126 $dst['dst_height'] = $height;
1129 $dst['dst_height'] = $theme_height;
1132 if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
1133 $dst['dst_width'] = absint( $width * ( $max_width / $width ) );
1135 elseif ( $has_flex_width && $has_flex_height ) {
1136 $dst['dst_width'] = $width;
1139 $dst['dst_width'] = $theme_width;
1146 * Create an attachment 'object'.
1148 * @param string $cropped Cropped image URL.
1149 * @param int $parent_attachment_id Attachment ID of parent image.
1151 * @return array Attachment object.
1153 final public function create_attachment_object( $cropped, $parent_attachment_id ) {
1154 $parent = get_post( $parent_attachment_id );
1155 $parent_url = $parent->guid;
1156 $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
1158 $size = @getimagesize( $cropped );
1159 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
1162 'ID' => $parent_attachment_id,
1163 'post_title' => basename($cropped),
1164 'post_content' => $url,
1165 'post_mime_type' => $image_type,
1167 'context' => 'custom-header'
1174 * Insert an attachment and its metadata.
1176 * @param array $object Attachment object.
1177 * @param string $cropped Cropped image URL.
1179 * @return int Attachment ID.
1181 final public function insert_attachment( $object, $cropped ) {
1182 $attachment_id = wp_insert_attachment( $object, $cropped );
1183 $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
1185 * Filter the header image attachment metadata.
1189 * @see wp_generate_attachment_metadata()
1191 * @param array $metadata Attachment metadata.
1193 $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata );
1194 wp_update_attachment_metadata( $attachment_id, $metadata );
1195 return $attachment_id;
1199 * Gets attachment uploaded by Media Manager, crops it, then saves it as a
1200 * new object. Returns JSON-encoded object details.
1202 public function ajax_header_crop() {
1203 check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );
1205 if ( ! current_user_can( 'edit_theme_options' ) ) {
1206 wp_send_json_error();
1209 if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
1210 wp_send_json_error();
1213 $crop_details = $_POST['cropDetails'];
1215 $dimensions = $this->get_header_dimensions( array(
1216 'height' => $crop_details['height'],
1217 'width' => $crop_details['width'],
1220 $attachment_id = absint( $_POST['id'] );
1222 $cropped = wp_crop_image(
1224 (int) $crop_details['x1'],
1225 (int) $crop_details['y1'],
1226 (int) $crop_details['width'],
1227 (int) $crop_details['height'],
1228 (int) $dimensions['dst_width'],
1229 (int) $dimensions['dst_height']
1232 if ( ! $cropped || is_wp_error( $cropped ) ) {
1233 wp_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) );
1236 /** This filter is documented in wp-admin/custom-header.php */
1237 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
1239 $object = $this->create_attachment_object( $cropped, $attachment_id );
1241 unset( $object['ID'] );
1243 $new_attachment_id = $this->insert_attachment( $object, $cropped );
1245 $object['attachment_id'] = $new_attachment_id;
1246 $object['width'] = $dimensions['dst_width'];
1247 $object['height'] = $dimensions['dst_height'];
1249 wp_send_json_success( $object );
1253 * Given an attachment ID for a header image, updates its "last used"
1256 * Triggered when the user tries adds a new header image from the
1257 * Media Manager, even if s/he doesn't save that change.
1259 public function ajax_header_add() {
1260 check_ajax_referer( 'header-add', 'nonce' );
1262 if ( ! current_user_can( 'edit_theme_options' ) ) {
1263 wp_send_json_error();
1266 $attachment_id = absint( $_POST['attachment_id'] );
1267 if ( $attachment_id < 1 ) {
1268 wp_send_json_error();
1271 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1272 update_post_meta( $attachment_id, $key, time() );
1273 update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
1275 wp_send_json_success();
1279 * Given an attachment ID for a header image, unsets it as a user-uploaded
1280 * header image for the current theme.
1282 * Triggered when the user clicks the overlay "X" button next to each image
1283 * choice in the Customizer's Header tool.
1285 public function ajax_header_remove() {
1286 check_ajax_referer( 'header-remove', 'nonce' );
1288 if ( ! current_user_can( 'edit_theme_options' ) ) {
1289 wp_send_json_error();
1292 $attachment_id = absint( $_POST['attachment_id'] );
1293 if ( $attachment_id < 1 ) {
1294 wp_send_json_error();
1297 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1298 delete_post_meta( $attachment_id, $key );
1299 delete_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
1301 wp_send_json_success();
1304 public function customize_set_last_used( $wp_customize ) {
1305 $data = $wp_customize->get_setting( 'header_image_data' )->post_value();
1307 if ( ! isset( $data['attachment_id'] ) ) {
1311 $attachment_id = $data['attachment_id'];
1312 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1313 update_post_meta( $attachment_id, $key, time() );
1316 public function get_default_header_images() {
1317 $this->process_default_headers();
1319 // Get the default image if there is one.
1320 $default = get_theme_support( 'custom-header', 'default-image' );
1322 if ( ! $default ) { // If not,
1323 return $this->default_headers; // easy peasy.
1326 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
1327 $already_has_default = false;
1329 foreach ( $this->default_headers as $k => $h ) {
1330 if ( $h['url'] === $default ) {
1331 $already_has_default = true;
1336 if ( $already_has_default ) {
1337 return $this->default_headers;
1340 // If the one true image isn't included in the default set, prepend it.
1341 $header_images = array();
1342 $header_images['default'] = array(
1344 'thumbnail_url' => $default,
1345 'description' => 'Default'
1348 // The rest of the set comes after.
1349 $header_images = array_merge( $header_images, $this->default_headers );
1350 return $header_images;
1353 public function get_uploaded_header_images() {
1354 $header_images = get_uploaded_header_images();
1355 $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
1356 $alt_text_key = '_wp_attachment_image_alt';
1358 foreach ( $header_images as &$header_image ) {
1359 $header_meta = get_post_meta( $header_image['attachment_id'] );
1360 $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
1361 $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
1364 return $header_images;