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