Wordpress 3.3
[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         /**
47          * Holds custom headers uploaded by the user
48          *
49          * @var array
50          * @since 3.2.0
51          * @access private
52          */
53         var $uploaded_headers = array();
54
55         /**
56          * Holds the page menu hook.
57          *
58          * @var string
59          * @since 3.0.0
60          * @access private
61          */
62         var $page = '';
63
64         /**
65          * Constructor - Register administration header callback.
66          *
67          * @since 2.1.0
68          * @param callback $admin_header_callback
69          * @param callback $admin_image_div_callback Optional custom image div output callback.
70          * @return Custom_Image_Header
71          */
72         function __construct($admin_header_callback, $admin_image_div_callback = '') {
73                 $this->admin_header_callback = $admin_header_callback;
74                 $this->admin_image_div_callback = $admin_image_div_callback;
75         }
76
77         /**
78          * Set up the hooks for the Custom Header admin page.
79          *
80          * @since 2.1.0
81          */
82         function init() {
83                 if ( ! current_user_can('edit_theme_options') )
84                         return;
85
86                 $this->page = $page = add_theme_page(__('Header'), __('Header'), 'edit_theme_options', 'custom-header', array(&$this, 'admin_page'));
87
88                 add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
89                 add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
90                 add_action("admin_head-$page", array(&$this, 'help') );
91                 add_action("admin_head-$page", array(&$this, 'take_action'), 50);
92                 add_action("admin_head-$page", array(&$this, 'js'), 50);
93                 add_action("admin_head-$page", $this->admin_header_callback, 51);
94         }
95
96         /**
97          * Adds contextual help.
98          *
99          * @since 3.0.0
100          */
101         function help() {
102                 get_current_screen()->add_help_tab( array(
103                         'id'      => 'overview',
104                         'title'   => __('Overview'),
105                         'content' =>
106                                 '<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.' ) . '</p>' .
107                                 '<p>' . __( 'If you want to discard your custom header and go back to the default included in your theme, click on the buttons to remove the custom image and restore the original header image.' ) . '</p>' .
108                                 '<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 Save Changes button.' ) . '</p>'
109                 ) );
110
111                 get_current_screen()->set_help_sidebar(
112                         '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
113                         '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Header_Screen" target="_blank">Documentation on Custom Header</a>' ) . '</p>' .
114                         '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
115                 );
116         }
117
118         /**
119          * Get the current step.
120          *
121          * @since 2.6.0
122          *
123          * @return int Current step
124          */
125         function step() {
126                 if ( ! isset( $_GET['step'] ) )
127                         return 1;
128
129                 $step = (int) $_GET['step'];
130                 if ( $step < 1 || 3 < $step )
131                         $step = 1;
132
133                 return $step;
134         }
135
136         /**
137          * Set up the enqueue for the JavaScript files.
138          *
139          * @since 2.1.0
140          */
141         function js_includes() {
142                 $step = $this->step();
143
144                 if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
145                         wp_enqueue_script('farbtastic');
146                 elseif ( 2 == $step )
147                         wp_enqueue_script('imgareaselect');
148         }
149
150         /**
151          * Set up the enqueue for the CSS files
152          *
153          * @since 2.7
154          */
155         function css_includes() {
156                 $step = $this->step();
157
158                 if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
159                         wp_enqueue_style('farbtastic');
160                 elseif ( 2 == $step )
161                         wp_enqueue_style('imgareaselect');
162         }
163
164         /**
165          * Check if header text is allowed
166          *
167          * @since 3.0.0
168          */
169         function header_text() {
170                 if ( defined( 'NO_HEADER_TEXT' ) && NO_HEADER_TEXT )
171                         return false;
172
173                 return true;
174         }
175
176         /**
177          * Execute custom header modification.
178          *
179          * @since 2.6.0
180          */
181         function take_action() {
182                 if ( ! current_user_can('edit_theme_options') )
183                         return;
184
185                 if ( empty( $_POST ) )
186                         return;
187
188                 $this->updated = true;
189
190                 if ( isset( $_POST['resetheader'] ) ) {
191                         check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
192                         remove_theme_mod( 'header_image' );
193                         return;
194                 }
195
196                 if ( isset( $_POST['resettext'] ) ) {
197                         check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
198                         remove_theme_mod('header_textcolor');
199                         return;
200                 }
201
202                 if ( isset( $_POST['removeheader'] ) ) {
203                         check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
204                         set_theme_mod( 'header_image', 'remove-header' );
205                         return;
206                 }
207
208                 if ( isset( $_POST['text-color'] ) ) {
209                         check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
210                         $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
211                         if ( 'blank' == $_POST['text-color'] ) {
212                                 set_theme_mod( 'header_textcolor', 'blank' );
213                         } else {
214                                 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
215                                 if ( strlen($color) == 6 || strlen($color) == 3 )
216                                         set_theme_mod('header_textcolor', $color);
217                         }
218                 }
219
220                 if ( isset( $_POST['default-header'] ) ) {
221                         check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
222                         if ( 'random-default-image' == $_POST['default-header'] ) {
223                                 set_theme_mod( 'header_image', 'random-default-image' );
224                         } elseif ( 'random-uploaded-image' == $_POST['default-header'] ) {
225                                 set_theme_mod( 'header_image', 'random-uploaded-image' );
226                         } else {
227                                 $this->process_default_headers();
228                                 $uploaded = get_uploaded_header_images();
229                                 if ( isset( $uploaded[$_POST['default-header']] ) )
230                                         set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) );
231                                 elseif ( isset( $this->default_headers[$_POST['default-header']] ) )
232                                         set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
233                         }
234                 }
235         }
236
237         /**
238          * Process the default headers
239          *
240          * @since 3.0.0
241          */
242         function process_default_headers() {
243                 global $_wp_default_headers;
244
245                 if ( !empty($this->headers) )
246                         return;
247
248                 if ( !isset($_wp_default_headers) )
249                         return;
250
251                 $this->default_headers = $_wp_default_headers;
252                 $template_directory_uri = get_template_directory_uri();
253                 $stylesheet_directory_uri = get_stylesheet_directory_uri();
254                 foreach ( array_keys($this->default_headers) as $header ) {
255                         $this->default_headers[$header]['url'] =  sprintf( $this->default_headers[$header]['url'], $template_directory_uri, $stylesheet_directory_uri );
256                         $this->default_headers[$header]['thumbnail_url'] =  sprintf( $this->default_headers[$header]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
257                 }
258
259         }
260
261         /**
262          * Display UI for selecting one of several default headers.
263          *
264          * Show the random image option if this theme has multiple header images.
265          * Random image option is on by default if no header has been set.
266          *
267          * @since 3.0.0
268          */
269         function show_header_selector( $type = 'default' ) {
270                 if ( 'default' == $type ) {
271                         $headers = $this->default_headers;
272                 } else {
273                         $headers = get_uploaded_header_images();
274                         $type = 'uploaded';
275                 }
276
277                 if ( 1 < count( $headers ) ) {
278                         echo '<div class="random-header">';
279                         echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
280                         echo __( '<strong>Random:</strong> Show a different image on each page.' );
281                         echo '</label>';
282                         echo '</div>';
283                 }
284
285                 echo '<div class="available-headers">';
286                 foreach ( $headers as $header_key => $header ) {
287                         $header_thumbnail = $header['thumbnail_url'];
288                         $header_url = $header['url'];
289                         $header_desc = empty( $header['description'] ) ? '' : $header['description'];
290                         echo '<div class="default-header">';
291                         echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
292                         $width = '';
293                         if ( !empty( $header['uploaded'] ) )
294                                 $width = ' width="230"';
295                         echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>';
296                         echo '</div>';
297                 }
298                 echo '<div class="clear"></div></div>';
299         }
300
301         /**
302          * Execute Javascript depending on step.
303          *
304          * @since 2.1.0
305          */
306         function js() {
307                 $step = $this->step();
308                 if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
309                         $this->js_1();
310                 elseif ( 2 == $step )
311                         $this->js_2();
312         }
313
314         /**
315          * Display Javascript based on Step 1 and 3.
316          *
317          * @since 2.6.0
318          */
319         function js_1() { ?>
320 <script type="text/javascript">
321 /* <![CDATA[ */
322         var text_objects = ['#name', '#desc', '#text-color-row'];
323         var farbtastic;
324         var default_color = '#<?php echo HEADER_TEXTCOLOR; ?>';
325         var old_color = null;
326
327         function pickColor(color) {
328                 jQuery('#name').css('color', color);
329                 jQuery('#desc').css('color', color);
330                 jQuery('#text-color').val(color);
331                 farbtastic.setColor(color);
332         }
333
334         function toggle_text(s) {
335                 if (jQuery(s).attr('id') == 'showtext' && jQuery('#text-color').val() != 'blank')
336                         return;
337
338                 if (jQuery(s).attr('id') == 'hidetext' && jQuery('#text-color').val() == 'blank')
339                         return;
340
341                 if (jQuery('#text-color').val() == 'blank') {
342                         //Show text
343                         if (old_color == '#blank')
344                                 old_color = default_color;
345
346                         jQuery( text_objects.toString() ).show();
347                         jQuery('#text-color').val(old_color);
348                         jQuery('#name').css('color', old_color);
349                         jQuery('#desc').css('color', old_color);
350                         pickColor(old_color);
351                 } else {
352                         //Hide text
353                         jQuery( text_objects.toString() ).hide();
354                         old_color = jQuery('#text-color').val();
355                         jQuery('#text-color').val('blank');
356                 }
357         }
358
359         jQuery(document).ready(function() {
360                 jQuery('#pickcolor').click(function() {
361                         jQuery('#color-picker').show();
362                 });
363
364                 jQuery('input[name="hidetext"]').click(function() {
365                         toggle_text(this);
366                 });
367
368                 jQuery('#defaultcolor').click(function() {
369                         pickColor(default_color);
370                         jQuery('#text-color').val(default_color)
371                 });
372
373                 jQuery('#text-color').keyup(function() {
374                         var _hex = jQuery('#text-color').val();
375                         var hex = _hex;
376                         if ( hex[0] != '#' )
377                                 hex = '#' + hex;
378                         hex = hex.replace(/[^#a-fA-F0-9]+/, '');
379                         if ( hex != _hex )
380                                 jQuery('#text-color').val(hex);
381                         if ( hex.length == 4 || hex.length == 7 )
382                                 pickColor( hex );
383                 });
384
385                 jQuery(document).mousedown(function(){
386                         jQuery('#color-picker').each( function() {
387                                 var display = jQuery(this).css('display');
388                                 if (display == 'block')
389                                         jQuery(this).fadeOut(2);
390                         });
391                 });
392
393                 farbtastic = jQuery.farbtastic('#color-picker', function(color) { pickColor(color); });
394                 <?php if ( $color = get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
395                 pickColor('#<?php echo $color; ?>');
396                 <?php } ?>
397
398                 <?php if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() ) { ?>
399                 toggle_text();
400                 <?php } ?>
401         });
402 /* ]]> */
403 </script>
404 <?php
405         }
406
407         /**
408          * Display Javascript based on Step 2.
409          *
410          * @since 2.6.0
411          */
412         function js_2() { ?>
413 <script type="text/javascript">
414 /* <![CDATA[ */
415         function onEndCrop( coords ) {
416                 jQuery( '#x1' ).val(coords.x);
417                 jQuery( '#y1' ).val(coords.y);
418                 jQuery( '#width' ).val(coords.w);
419                 jQuery( '#height' ).val(coords.h);
420         }
421
422         jQuery(document).ready(function() {
423                 var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
424                 var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
425                 var ratio = xinit / yinit;
426                 var ximg = jQuery('img#upload').width();
427                 var yimg = jQuery('img#upload').height();
428
429                 if ( yimg < yinit || ximg < xinit ) {
430                         if ( ximg / yimg > ratio ) {
431                                 yinit = yimg;
432                                 xinit = yinit * ratio;
433                         } else {
434                                 xinit = ximg;
435                                 yinit = xinit / ratio;
436                         }
437                 }
438
439                 jQuery('img#upload').imgAreaSelect({
440                         handles: true,
441                         keys: true,
442                         aspectRatio: xinit + ':' + yinit,
443                         show: true,
444                         x1: 0,
445                         y1: 0,
446                         x2: xinit,
447                         y2: yinit,
448                         maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
449                         maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
450                         onInit: function () {
451                                 jQuery('#width').val(xinit);
452                                 jQuery('#height').val(yinit);
453                         },
454                         onSelectChange: function(img, c) {
455                                 jQuery('#x1').val(c.x1);
456                                 jQuery('#y1').val(c.y1);
457                                 jQuery('#width').val(c.width);
458                                 jQuery('#height').val(c.height);
459                         }
460                 });
461         });
462 /* ]]> */
463 </script>
464 <?php
465         }
466
467         /**
468          * Display first step of custom header image page.
469          *
470          * @since 2.1.0
471          */
472         function step_1() {
473                 $this->process_default_headers();
474 ?>
475
476 <div class="wrap">
477 <?php screen_icon(); ?>
478 <h2><?php _e('Custom Header'); ?></h2>
479
480 <?php if ( ! empty( $this->updated ) ) { ?>
481 <div id="message" class="updated">
482 <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
483 </div>
484 <?php } ?>
485
486 <table class="form-table">
487 <tbody>
488
489
490 <tr valign="top">
491 <th scope="row"><?php _e( 'Preview' ); ?></th>
492 <td >
493         <?php if ( $this->admin_image_div_callback ) {
494           call_user_func( $this->admin_image_div_callback );
495         } else {
496         ?>
497         <div id="headimg" style="max-width:<?php echo HEADER_IMAGE_WIDTH; ?>px;height:<?php echo HEADER_IMAGE_HEIGHT; ?>px;background-image:url(<?php esc_url ( header_image() ) ?>);">
498                 <?php
499                 if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() )
500                         $style = ' style="display:none;"';
501                 else
502                         $style = ' style="color:#' . get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) . ';"';
503                 ?>
504                 <h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>"><?php bloginfo( 'name' ); ?></a></h1>
505                 <div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
506         </div>
507         <?php } ?>
508 </td>
509 </tr>
510 <?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?>
511 <tr valign="top">
512 <th scope="row"><?php _e( 'Upload Image' ); ?></th>
513 <td>
514         <p><?php _e( 'You can upload a custom header image to be shown at the top of your site instead of the default one. On the next screen you will be able to crop the image.' ); ?><br />
515         <?php printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT ); ?></p>
516         <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
517         <p>
518                 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
519                 <input type="file" id="upload" name="import" />
520                 <input type="hidden" name="action" value="save" />
521                 <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ) ?>
522                 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
523         </p>
524         </form>
525 </td>
526 </tr>
527 <?php endif; ?>
528 </tbody>
529 </table>
530
531 <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>">
532 <table class="form-table">
533 <tbody>
534         <?php if ( get_uploaded_header_images() ) : ?>
535 <tr valign="top">
536 <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
537 <td>
538         <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
539         <?php
540                 $this->show_header_selector( 'uploaded' );
541         ?>
542 </td>
543 </tr>
544         <?php endif;
545         if ( ! empty( $this->default_headers ) ) : ?>
546 <tr valign="top">
547 <th scope="row"><?php _e( 'Default Images' ); ?></th>
548 <td>
549 <?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?>
550         <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>
551 <?php else: ?>
552         <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
553 <?php endif; ?>
554         <?php
555                 $this->show_header_selector( 'default' );
556         ?>
557 </td>
558 </tr>
559         <?php endif;
560         if ( get_header_image() ) : ?>
561 <tr valign="top">
562 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
563 <td>
564         <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
565         <?php submit_button( __( 'Remove Header Image' ), 'button', 'removeheader', false ); ?>
566 </td>
567 </tr>
568         <?php endif;
569
570         if ( defined( 'HEADER_IMAGE' ) && '' != HEADER_IMAGE ) : ?>
571 <tr valign="top">
572 <th scope="row"><?php _e( 'Reset Image' ); ?></th>
573 <td>
574         <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
575         <?php submit_button( __( 'Restore Original Header Image' ), 'button', 'resetheader', false ); ?>
576 </td>
577 </tr>
578         <?php endif; ?>
579 </tbody>
580 </table>
581
582         <?php if ( $this->header_text() ) : ?>
583 <table class="form-table">
584 <tbody>
585 <tr valign="top" class="hide-if-no-js">
586 <th scope="row"><?php _e( 'Display Text' ); ?></th>
587 <td>
588         <p>
589         <?php $hidetext = get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>
590         <label><input type="radio" value="1" name="hidetext" id="hidetext"<?php checked( ( 'blank' == $hidetext || empty( $hidetext ) )  ? true : false ); ?> /> <?php _e( 'No' ); ?></label>
591         <label><input type="radio" value="0" name="hidetext" id="showtext"<?php checked( ( 'blank' == $hidetext || empty( $hidetext ) ) ? false : true ); ?> /> <?php _e( 'Yes' ); ?></label>
592         </p>
593 </td>
594 </tr>
595
596 <tr valign="top" id="text-color-row">
597 <th scope="row"><?php _e( 'Text Color' ); ?></th>
598 <td>
599         <p>
600                 <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) ); ?>" />
601                 <span class="description hide-if-js"><?php _e( 'If you want to hide header text, add <strong>#blank</strong> as text color.' );?></span>
602                 <input type="button" class="button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color' ); ?>" id="pickcolor" />
603         </p>
604         <div id="color-picker" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
605 </td>
606 </tr>
607
608         <?php if ( defined('HEADER_TEXTCOLOR') && get_theme_mod('header_textcolor') ) { ?>
609 <tr valign="top">
610 <th scope="row"><?php _e('Reset Text Color'); ?></th>
611 <td>
612         <p><?php _e( 'This will restore the original header text. You will not be able to restore any customizations.' ) ?></p>
613         <?php submit_button( __( 'Restore Original Header Text' ), 'button', 'resettext', false ); ?>
614 </td>
615 </tr>
616         <?php } ?>
617
618 </tbody>
619 </table>
620         <?php endif;
621
622 do_action( 'custom_header_options' );
623
624 wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
625
626 <?php submit_button( null, 'primary', 'save-header-options' ); ?>
627 </form>
628 </div>
629
630 <?php }
631
632         /**
633          * Display second step of custom header image page.
634          *
635          * @since 2.1.0
636          */
637         function step_2() {
638                 check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
639                 if ( ! current_theme_supports( 'custom-header-uploads' ) )
640                         wp_die( __( 'Cheatin&#8217; uh?' ) );
641
642                 $overrides = array('test_form' => false);
643                 $file = wp_handle_upload($_FILES['import'], $overrides);
644
645                 if ( isset($file['error']) )
646                         wp_die( $file['error'],  __( 'Image Upload Error' ) );
647
648                 $url = $file['url'];
649                 $type = $file['type'];
650                 $file = $file['file'];
651                 $filename = basename($file);
652
653                 // Construct the object array
654                 $object = array(
655                 'post_title' => $filename,
656                 'post_content' => $url,
657                 'post_mime_type' => $type,
658                 'guid' => $url,
659                 'context' => 'custom-header'
660                 );
661
662                 // Save the data
663                 $id = wp_insert_attachment($object, $file);
664
665                 list($width, $height, $type, $attr) = getimagesize( $file );
666
667                 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
668                         // Add the meta-data
669                         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
670                         update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
671
672                         set_theme_mod('header_image', esc_url($url));
673                         do_action('wp_create_file_in_uploads', $file, $id); // For replication
674                         return $this->finished();
675                 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
676                         $oitar = $width / HEADER_IMAGE_WIDTH;
677                         $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
678                         if ( is_wp_error( $image ) )
679                                 wp_die( __( 'Image could not be processed.  Please go back and try again.' ), __( 'Image Processing Error' ) );
680
681                         $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
682
683                         $url = str_replace(basename($url), basename($image), $url);
684                         $width = $width / $oitar;
685                         $height = $height / $oitar;
686                 } else {
687                         $oitar = 1;
688                 }
689                 ?>
690
691 <div class="wrap">
692 <?php screen_icon(); ?>
693 <h2><?php _e( 'Crop Header Image' ); ?></h2>
694
695 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 3)); ?>">
696         <p class="hide-if-no-js"><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
697         <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
698
699         <div id="crop_image" style="position: relative">
700                 <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
701         </div>
702
703         <input type="hidden" name="x1" id="x1" value="0"/>
704         <input type="hidden" name="y1" id="y1" value="0"/>
705         <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
706         <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
707         <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
708         <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
709         <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
710
711         <?php submit_button( __( 'Crop and Publish' ) ); ?>
712         </p>
713 </form>
714 </div>
715                 <?php
716         }
717
718         /**
719          * Display third step of custom header image page.
720          *
721          * @since 2.1.0
722          */
723         function step_3() {
724                 check_admin_referer('custom-header-crop-image');
725                 if ( ! current_theme_supports( 'custom-header-uploads' ) )
726                         wp_die( __( 'Cheatin&#8217; uh?' ) );
727
728                 if ( $_POST['oitar'] > 1 ) {
729                         $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
730                         $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
731                         $_POST['width'] = $_POST['width'] * $_POST['oitar'];
732                         $_POST['height'] = $_POST['height'] * $_POST['oitar'];
733                 }
734
735                 $attachment_id = absint( $_POST['attachment_id'] );
736                 $original = get_attached_file($attachment_id);
737
738                 $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
739                 if ( is_wp_error( $cropped ) )
740                         wp_die( __( 'Image could not be processed.  Please go back and try again.' ), __( 'Image Processing Error' ) );
741
742                 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication
743
744                 $parent = get_post($attachment_id);
745                 $parent_url = $parent->guid;
746                 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
747
748                 // Construct the object array
749                 $object = array(
750                         'ID' => $attachment_id,
751                         'post_title' => basename($cropped),
752                         'post_content' => $url,
753                         'post_mime_type' => 'image/jpeg',
754                         'guid' => $url,
755                         'context' => 'custom-header'
756                 );
757
758                 // Update the attachment
759                 wp_insert_attachment($object, $cropped);
760                 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
761                 update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
762
763                 set_theme_mod('header_image', $url);
764
765                 // cleanup
766                 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
767                 @unlink( apply_filters( 'wp_delete_file', $medium ) );
768                 @unlink( apply_filters( 'wp_delete_file', $original ) );
769
770                 return $this->finished();
771         }
772
773         /**
774          * Display last step of custom header image page.
775          *
776          * @since 2.1.0
777          */
778         function finished() {
779                 $this->updated = true;
780                 $this->step_1();
781         }
782
783         /**
784          * Display the page based on the current step.
785          *
786          * @since 2.1.0
787          */
788         function admin_page() {
789                 if ( ! current_user_can('edit_theme_options') )
790                         wp_die(__('You do not have permission to customize headers.'));
791                 $step = $this->step();
792                 if ( 1 == $step )
793                         $this->step_1();
794                 elseif ( 2 == $step )
795                         $this->step_2();
796                 elseif ( 3 == $step )
797                         $this->step_3();
798         }
799
800 }
801 ?>