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