Make WordPress Core

Ticket #13231: refactoring_custom_header.patch

File refactoring_custom_header.patch, 30.0 KB (added by ocean90, 15 years ago)
  • wp-admin/css/wp-admin.dev.css

     
    37083708}
    37093709
    37103710/* Custom Header */
     3711.appearance_page_custom-header #headimg, .appearance_page_custom-header img#upload {
     3712        border: 1px solid #000;
     3713}
    37113714
    3712 table#available-headers td {
    3713         padding: 0 20px 10px 0;
     3715.appearance_page_custom-header #headimg h1 {
     3716        font-family: 'Trebuchet MS', 'Lucida Grande', Verdana,Arial, Sans-Serif;
     3717        font-size: 2em;
     3718        font-weight: bold;
     3719        margin: 0;
     3720        padding-top: 70px;
     3721        text-align: center;
    37143722}
    3715 table#available-headers label input {
    3716         margin-right: 8px;
     3723
     3724.appearance_page_custom-header td p label {
     3725        margin-right: 10px;
    37173726}
    3718 table#available-headers label img {
     3727
     3728.appearance_page_custom-header #uploadForm p label {
     3729        font-size: 11px;
     3730}
     3731
     3732.appearance_page_custom-header #headimg #desc {
     3733        font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif;
     3734        font-size: 0.8em;
     3735        text-align: center;
     3736}
     3737
     3738.appearance_page_custom-header table#available-headers td {
     3739        padding: 0 20px 20px 0;
     3740}
     3741.appearance_page_custom-header table#available-headers label input {
     3742        margin-right: 10px;
     3743}
     3744.appearance_page_custom-header table#available-headers label img {
    37193745        vertical-align: middle;
    37203746}
    37213747
  • wp-admin/custom-header.php

     
    5050         * @param callback $admin_image_div_callback Optional custom image div output callback.
    5151         * @return Custom_Image_Header
    5252         */
    53         function Custom_Image_Header($admin_header_callback, $admin_image_div_callback = '') {
     53        function Custom_Image_Header( $admin_header_callback, $admin_image_div_callback = '' ) {
    5454                $this->admin_header_callback = $admin_header_callback;
    5555                $this->admin_image_div_callback = $admin_image_div_callback;
    5656        }
     
    6161         * @since 2.1.0
    6262         */
    6363        function init() {
    64                 if ( ! current_user_can('switch_themes') )
     64                if ( ! current_user_can( 'edit_theme_options' ) )
    6565                        return;
    6666
    67                 $page = add_theme_page(__('Header'), __('Header'), 'switch_themes', 'custom-header', array(&$this, 'admin_page'));
     67                $page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( &$this, 'admin_page' ) );
    6868
    69                 add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
    70                 add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
    71                 add_action("admin_head-$page", array(&$this, 'take_action'), 50);
    72                 add_action("admin_head-$page", array(&$this, 'js'), 50);
    73                 add_action("admin_head-$page", $this->admin_header_callback, 51);
     69                add_action( "admin_print_scripts-$page", array( &$this, 'js_includes' ) );
     70                add_action( "admin_print_styles-$page", array( &$this, 'css_includes' ) );
     71                add_action( "admin_head-$page", array( &$this, 'take_action' ), 50 );
     72                add_action( "admin_head-$page", array( &$this, 'js' ), 50 );
     73                add_action( "admin_head-$page", $this->admin_header_callback, 51) ;
    7474        }
    7575
    7676        /**
     
    9999        function js_includes() {
    100100                $step = $this->step();
    101101
    102                 if ( 1 == $step )
     102                if ( ( 1 == $step || 3 == $step ) && NO_HEADER_TEXT != 1 )
    103103                        wp_enqueue_script('farbtastic');
    104104                elseif ( 2 == $step )
    105                         wp_enqueue_script('jcrop');
     105                        wp_enqueue_script('imgareaselect');
    106106        }
    107107
    108108        /**
     
    113113        function css_includes() {
    114114                $step = $this->step();
    115115
    116                 if ( 1 == $step )
     116                if ( ( 1 == $step || 3 == $step ) && NO_HEADER_TEXT != 1 )
    117117                        wp_enqueue_style('farbtastic');
    118118                elseif ( 2 == $step )
    119                         wp_enqueue_style('jcrop');
     119                        wp_enqueue_style('imgareaselect');
    120120        }
    121121
    122122        /**
     
    125125         * @since 2.6.0
    126126         */
    127127        function take_action() {
    128                 if ( ! current_user_can('switch_themes') )
     128                if ( ! current_user_can('edit_theme_options') )
    129129                        return;
     130                       
     131                if ( empty($_POST) )
     132                        return;
     133                       
     134                if ( isset($_POST['resetheader']) ) {
     135                        check_admin_referer('custom-header');
     136                        remove_theme_mod('header_image');
     137                        return;
     138                }
     139               
     140                if ( isset($_POST['resettext']) ) {
     141                        check_admin_referer('custom-header');
     142                        remove_theme_mod('header_textcolor');
     143                        return;
     144                }
    130145
    131                 if ( isset( $_POST['textcolor'] ) ) {
     146                if ( isset( $_POST['text-color'] ) ) {
    132147                        check_admin_referer('custom-header');
    133                         if ( 'blank' == $_POST['textcolor'] ) {
     148                        if ( 'blank' == $_POST['text-color'] ) {
    134149                                set_theme_mod('header_textcolor', 'blank');
    135150                        } else {
    136                                 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
     151                                $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
    137152                                if ( strlen($color) == 6 || strlen($color) == 3 )
    138153                                        set_theme_mod('header_textcolor', $color);
    139154                        }
    140155                }
    141156
    142                 if ( isset($_POST['resetheader']) ) {
    143                         check_admin_referer('custom-header');
    144                         remove_theme_mods();
    145                 }
    146 
    147157                if ( isset($_POST['default-header']) ) {
    148158                        check_admin_referer('custom-header');
    149159                        $this->process_default_headers();
    150160                        if ( isset($this->default_headers[$_POST['default-header']]) )
    151161                                set_theme_mod('header_image', esc_url($this->default_headers[$_POST['default-header']]['url']));
    152162                }
     163               
     164                $this->updated = true;
    153165        }
    154166
    155167        /**
     
    221233         */
    222234        function js() {
    223235                $step = $this->step();
    224                 if ( 1 == $step )
     236                if ( ( 1 == $step || 3 == $step ) && NO_HEADER_TEXT != 1 )
    225237                        $this->js_1();
    226238                elseif ( 2 == $step )
    227239                        $this->js_2();
    228240        }
    229241
    230242        /**
    231          * Display Javascript based on Step 1.
     243         * Display Javascript based on Step 1 and 3.
    232244         *
    233245         * @since 2.6.0
    234246         */
    235247        function js_1() { ?>
    236248<script type="text/javascript">
    237         var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];
     249        var text_objects = ['#name', '#desc', '#text-color-row'];
    238250        var farbtastic;
     251        var default_color = '#<?php echo HEADER_TEXTCOLOR; ?>';
     252        var old_color = null;
    239253
    240254        function pickColor(color) {
    241255                jQuery('#name').css('color', color);
    242256                jQuery('#desc').css('color', color);
    243                 jQuery('#textcolor').val(color);
     257                jQuery('#text-color').val(color);
    244258                farbtastic.setColor(color);
    245259        }
     260       
     261        function toggle_text(s) {
     262                if (jQuery(s).attr('id') == 'showtext' && jQuery('#text-color').val() != 'blank')
     263                        return;
     264               
     265                if (jQuery(s).attr('id') == 'hidetext' && jQuery('#text-color').val() == 'blank')
     266                        return;
    246267
     268                if (jQuery('#text-color').val() == 'blank') {
     269                        //Show text
     270                        if (old_color == '#blank')
     271                                old_color = default_color;
     272       
     273                        jQuery( text_objects.toString() ).show();
     274                        jQuery('#text-color').val(old_color);
     275                        jQuery('#name').css('color', old_color);
     276                        jQuery('#desc').css('color', old_color);
     277                        pickColor(old_color);
     278                } else {
     279                        //Hide text
     280                        jQuery( text_objects.toString() ).hide();
     281                        old_color = jQuery('#text-color').val();
     282                        jQuery('#text-color').val('blank');
     283                }
     284        }
     285
    247286        jQuery(document).ready(function() {
    248287                jQuery('#pickcolor').click(function() {
    249288                        jQuery('#colorPickerDiv').show();
    250289                });
    251290
    252                 jQuery('#hidetext').click(function() {
    253                         toggle_text();
     291                jQuery('input[name="hidetext"]').click(function() {
     292                        toggle_text(this);
    254293                });
     294               
     295                jQuery('#defaultcolor').click(function() {
     296                        pickColor(default_color);
     297                        jQuery('#text-color').val(default_color)
     298                });
     299               
     300                jQuery('#text-color').keyup(function() {
     301                        var _hex = jQuery('#text-color').val();
     302                        var hex = _hex;
     303                        if ( hex[0] != '#' )
     304                                hex = '#' + hex;
     305                        hex = hex.replace(/[^#a-fA-F0-9]+/, '');
     306                        if ( hex != _hex )
     307                                jQuery('#text-color').val(hex);
     308                        if ( hex.length == 4 || hex.length == 7 )
     309                                pickColor( hex );
     310                });
     311               
     312                jQuery(document).mousedown(function(){
     313                        jQuery('#colorPickerDiv').each( function() {
     314                                var display = jQuery(this).css('display');
     315                                if (display == 'block')
     316                                        jQuery(this).fadeOut(2);
     317                        });
     318                });
    255319
    256                 farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
    257                 pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>');
     320                farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) {
     321                        pickColor(color);
     322                });
     323                pickColor('#<?php echo get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>');
    258324
    259                 <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
     325                <?php if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || defined( 'NO_HEADER_TEXT' ) ) { ?>
    260326                toggle_text();
    261327                <?php } ?>
    262         });
    263 
    264         jQuery(document).mousedown(function(){
    265                 // Make the picker disappear, since we're using it in an independant div
    266                 hide_picker();
    267         });
    268 
    269         function colorDefault() {
    270                 pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
    271         }
    272 
    273         function hide_picker(what) {
    274                 var update = false;
    275                 jQuery('#colorPickerDiv').each(function(){
    276                         var id = jQuery(this).attr('id');
    277                         if (id == what) {
    278                                 return;
    279                         }
    280                         var display = jQuery(this).css('display');
    281                         if (display == 'block') {
    282                                 jQuery(this).fadeOut(2);
    283                         }
    284328                });
    285         }
    286 
    287         function toggle_text(force) {
    288                 if (jQuery('#textcolor').val() == 'blank') {
    289                         //Show text
    290                         jQuery( buttons.toString() ).show();
    291                         jQuery('#textcolor').val('<?php echo HEADER_TEXTCOLOR; ?>');
    292                         jQuery('#hidetext').val('<?php _e('Hide Text'); ?>');
    293                 }
    294                 else {
    295                         //Hide text
    296                         jQuery( buttons.toString() ).hide();
    297                         jQuery('#textcolor').val('blank');
    298                         jQuery('#hidetext').val('<?php _e('Show Text'); ?>');
    299                 }
    300         }
    301 
    302 
    303 
    304329</script>
    305330<?php
    306331        }
     
    313338        function js_2() { ?>
    314339<script type="text/javascript">
    315340        function onEndCrop( coords ) {
    316                 jQuery( '#x1' ).val(coords.x);
    317                 jQuery( '#y1' ).val(coords.y);
    318                 jQuery( '#x2' ).val(coords.x2);
    319                 jQuery( '#y2' ).val(coords.y2);
    320                 jQuery( '#width' ).val(coords.w);
    321                 jQuery( '#height' ).val(coords.h);
     341                jQuery('#x1').val(coords.x);
     342                jQuery('#y1').val(coords.y);
     343                jQuery('#width').val(coords.w);
     344                jQuery('#height').val(coords.h);
    322345        }
    323346
    324         // with a supplied ratio
    325347        jQuery(document).ready(function() {
    326348                var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
    327349                var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
    328                 var ratio = xinit / yinit;
     350                var ratio = xinit +  ':' + yinit;
    329351                var ximg = jQuery('#upload').width();
    330352                var yimg = jQuery('#upload').height();
    331353
    332                 //set up default values
    333                 jQuery( '#x1' ).val(0);
    334                 jQuery( '#y1' ).val(0);
    335                 jQuery( '#x2' ).val(xinit);
    336                 jQuery( '#y2' ).val(yinit);
    337                 jQuery( '#width' ).val(xinit);
    338                 jQuery( '#height' ).val(yinit);
    339 
    340                 if ( yimg < yinit || ximg < xinit ) {
    341                         if ( ximg / yimg > ratio ) {
    342                                 yinit = yimg;
    343                                 xinit = yinit * ratio;
    344                         } else {
    345                                 xinit = ximg;
    346                                 yinit = xinit / ratio;
    347                         }
    348                 }
    349 
    350                 jQuery('#upload').Jcrop({
     354                jQuery('#upload').imgAreaSelect({
     355                        handles: true,
     356                        keys: true,
    351357                        aspectRatio: ratio,
    352                         setSelect: [ 0, 0, xinit, yinit ],
    353                         onSelect: onEndCrop
     358                        maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
     359                        maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
     360                        onSelectChange: function(img, c) {
     361                                jQuery('#x1').val(c.x1);
     362                                jQuery('#y1').val(c.y1);
     363                                jQuery('#width').val(c.width);
     364                                jQuery('#height').val(c.height);
     365                        },
    354366                });
    355367        });
    356368</script>
     
    368380
    369381<div class="wrap">
    370382<?php screen_icon(); ?>
    371 <h2><?php _e('Your Header Image'); ?></h2>
     383<h2><?php _e( 'Custom Header' ); ?></h2>
    372384
    373 <?php
    374 if ( isset($_GET['updated']) && $_GET['updated'] ) { ?>
     385<?php if ( ! empty( $this->updated ) ) { ?>
    375386<div id="message" class="updated">
    376 <p><?php printf(__('Header updated. <a href="%s">Visit your site</a> to see how it looks.'), home_url()); ?></p>
     387        <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url() ); ?></p>
    377388</div>
    378 <?php }
     389<?php } ?>
    379390
    380 if ( get_theme_mod('header_image') || empty($this->default_headers) ) :
    381 ?>
    382 <p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
    383 <?php
     391<h3><?php _e( 'Header Image' ) ?></h3>
     392<table class="form-table">
     393<tbody>
    384394
    385 if ( $this->admin_image_div_callback ) {
    386   call_user_func($this->admin_image_div_callback);
    387 } else {
    388 ?>
    389 <div id="headimg" style="background-image: url(<?php esc_url(header_image()) ?>);">
    390 <h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
    391 <div id="desc"><?php bloginfo('description');?></div>
    392 </div>
    393 <?php } ?>
     395        <?php if ( get_header_image() ) { ?>
     396<tr valign="top">
     397<th scope="row"><?php _e( 'Current Image' ); ?></th>
     398<td>
     399        <?php if ( $this->admin_image_div_callback ) {
     400          call_user_func( $this->admin_image_div_callback );
     401        } else {
     402        ?>
     403        <div id="headimg" style="background-image: url(<?php esc_url ( header_image() ) ?>);color:#<?php echo get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>">
     404                <?php
     405                $style = '';
     406                if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || defined( 'NO_HEADER_TEXT' ) )
     407                        $style = " style='display:none'";
     408                ?>
     409                <h1 id="name"<?php echo $style; ?>><?php bloginfo( 'name' ); ?></h1>
     410                <div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
     411        </div>
     412        <?php } ?>
     413</td>
     414</tr>
     415        <?php } ?>
    394416
    395 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
    396 <form method="post" action="<?php echo admin_url('themes.php?page=custom-header&amp;updated=true') ?>">
    397 <input type="button" class="button" value="<?php esc_attr_e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
    398 <input type="button" class="button" value="<?php esc_attr_e('Select a Text Color'); ?>" id="pickcolor" /><input type="button" class="button" value="<?php esc_attr_e('Use Original Color'); ?>" onclick="colorDefault()" id="defaultcolor" />
    399 <?php wp_nonce_field('custom-header'); ?>
    400 <input type="hidden" name="textcolor" id="textcolor" value="#<?php esc_attr(header_textcolor()) ?>" /><input name="submit" type="submit" class="button" value="<?php esc_attr_e('Save Changes'); ?>" /></form>
    401 <?php } ?>
     417<tr valign="top">
     418<th scope="row"><?php _e( 'Upload Image' ); ?></th>
     419<td>
     420        <p><?php _e( 'Here 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 />
     421        <?php printf( __( 'Images of exactly <strong>%1$d x %2$d pixels</strong> will be used as-is.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT ); ?></p>
     422        <form enctype="multipart/form-data" id="uploadForm" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
     423        <label><input type="checkbox" value="1" name="transparency"/> <?php _e( 'Retain transparency (If you want to upload a transparent image, you should enable this option.)' ); ?></label>
     424        <p>
     425                <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
     426                <input type="file" id="upload" name="import" />
     427                <input type="hidden" name="action" value="save" />
     428                <?php wp_nonce_field( 'custom-header' ) ?>
     429                <input type="submit" class="button" value="<?php esc_attr_e( 'Upload' ); ?>" />
     430        </p>
     431        </form>
     432</td>
     433</tr>
     434<form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>">
     435        <?php if ( ! empty( $this->default_headers ) ) { ?>
     436<tr valign="top">
     437<th scope="row"><?php _e( 'Default Images'); ?></th>
     438<td>
     439        <p><?php _e( 'If you didn&lsquo;t want to upload your own image, you can use one of these cool headers.' ) ?></p>
     440        <?php
     441                $this->show_default_header_selector();
     442        ?>
     443</td>
     444</tr>
     445        <?php }
    402446
    403 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"> </div>
    404 </div>
    405 <?php
    406 else:
    407         echo '<p>' . __('Choose one of these cool headers, or upload your own image below.') . '</p>';
    408         echo '<form method="post" action="' . admin_url('themes.php?page=custom-header&amp;updated=true') . '">';
    409         wp_nonce_field('custom-header');
    410         $this->show_default_header_selector();
    411         echo '<input type="submit" class="button" value="' . esc_attr__('Save Changes') . '"  />';
    412         echo '</form>';
    413         echo '</div>';
    414 endif;
    415 ?>
    416 <div class="wrap">
    417 <h3><?php _e('Upload New Header Image'); ?></h3><p><?php _e('Here 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.'); ?> <?php printf(__('Images of exactly <strong>%1$d x %2$d pixels</strong> will be used as-is.'), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT); ?></p>
     447        if ( get_theme_mod( 'header_image' ) ) { ?>
     448<tr valign="top">
     449<th scope="row"><?php _e( 'Reset Header Image' ); ?></th>
     450<td>
     451        <p><?php _e( 'This will restore the original header image. You will not be able to retrieve any customizations.' ) ?></p>
     452        <input type="submit" class="button" name="resetheader" value="<?php esc_attr_e( 'Restore Original Header Image' ); ?>" />
     453</td>
     454</tr>
     455        <?php } ?>
     456</tbody>
     457</table>
    418458
    419 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo esc_attr(add_query_arg('step', 2)) ?>">
    420 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
    421 <input type="hidden" name="action" value="save" />
    422 <?php wp_nonce_field('custom-header') ?>
    423 <p class="submit">
    424 <input type="submit" value="<?php esc_attr_e('Upload'); ?>" />
    425 </p>
    426 </form>
     459        <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
     460<h3><?php _e( 'Header Text' ) ?></h3>
     461<table class="form-table">
     462<tbody>
     463<tr valign="top" class="hide-if-no-js">
     464<th scope="row"><?php _e( 'Display Text' ); ?></th>
     465<td>
     466        <p>
     467        <?php $hidetext = get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>
     468        <label><input type="radio" value="1" name="hidetext" id="hidetext"<?php checked( 'blank' == $hidetext  ? true : false ); ?> /> <?php _e( 'No' ); ?></label>
     469        <label><input type="radio" value="0" name="hidetext" id="showtext"<?php checked( 'blank' == $hidetext  ? false : true ); ?> /> <?php _e( 'Yes' ); ?></label>
     470        </p>
     471</td>
     472</tr>
    427473
    428 </div>
     474<tr valign="top" id="text-color-row">
     475<th scope="row"><?php _e( 'Text Color' ); ?></th>
     476<td>
     477        <p>
     478                <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) ); ?>" />
     479                <span class="description hide-if-js"><?php _e( 'If you want to hide header text, add <strong>#blank</strong> as text color.' );?></span>
     480                <input type="button" class="button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color' ); ?>" id="pickcolor" />
     481                <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
     482        </p>
     483</td>
     484</tr>
    429485
    430                 <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
    431 <div class="wrap">
    432 <h3><?php _e('Reset Header Image and Color'); ?></h3>
    433 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>">
    434 <?php
    435 wp_nonce_field('custom-header');
    436 if ( !empty($this->default_headers) ) {
    437 ?>
    438 <p><?php _e('Use one of these cool headers.') ?></p>
    439 <?php
    440         $this->show_default_header_selector();
    441 ?>
    442         <input type="submit" class="button" name="resetheader" value="<?php esc_attr_e('Save Changes'); ?>" />
    443 <?php
    444 } else {
    445 ?>
    446 <p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
    447 <input type="submit" class="button" name="resetheader" value="<?php esc_attr_e('Restore Original Header'); ?>" />
    448 <?php } ?>
     486        <?php if ( defined( 'HEADER_TEXTCOLOR' ) && HEADER_TEXTCOLOR && get_theme_mod( 'header_textcolor' ) != HEADER_TEXTCOLOR ) { ?>
     487<tr valign="top">
     488<th scope="row"><?php _e( 'Reset Header Text' ); ?></th>
     489<td>
     490        <p><?php _e( 'This will restore the original header text. You will not be able to retrieve any customizations.' ) ?></p>
     491        <input type="submit" class="button" name="resettext" value="<?php esc_attr_e( 'Restore Original Header Text' ); ?>" />
     492</td>
     493</tr>
     494        <?php } ?>
     495
     496</tbody>
     497</table>
     498        <?php }
     499
     500        wp_nonce_field( 'custom-header' ); ?>
     501<p class="submit"><input type="submit" class="button-primary" name="save-header-options" value="<?php esc_attr_e( 'Save Changes' ); ?>" /></p>
    449502</form>
    450503</div>
    451                 <?php endif;
    452504
    453         }
     505<?php }
    454506
    455507        /**
    456508         * Display second step of custom header image page.
     
    458510         * @since 2.1.0
    459511         */
    460512        function step_2() {
    461                 check_admin_referer('custom-header');
    462                 $overrides = array('test_form' => false);
    463                 $file = wp_handle_upload($_FILES['import'], $overrides);
     513                check_admin_referer( 'custom-header' );
    464514
    465                 if ( isset($file['error']) )
    466                 die( $file['error'] );
     515                $overrides = array( 'test_form' => false );
     516                $file = wp_handle_upload( $_FILES['import'], $overrides );
    467517
     518                if ( isset( $file['error'] ) )
     519                        wp_die( $file['error'],  __( 'Image Upload Error' ) );
     520
    468521                $url = $file['url'];
    469522                $type = $file['type'];
    470523                $file = $file['file'];
     
    472525
    473526                // Construct the object array
    474527                $object = array(
    475                 'post_title' => $filename,
    476                 'post_content' => $url,
    477                 'post_mime_type' => $type,
    478                 'guid' => $url);
     528                        'post_title' => $filename,
     529                        'post_content' => __( 'This is your header image, if you delete it, you will lost your header image.' ),
     530                        'post_mime_type' => $type,
     531                        'guid' => $url
     532                );
    479533
    480534                // Save the data
    481                 $id = wp_insert_attachment($object, $file);
     535                $id = wp_insert_attachment( $object, $file );
    482536
    483                 list($width, $height, $type, $attr) = getimagesize( $file );
     537                list( $width, $height, $type, $attr ) = getimagesize( $file );
    484538
    485539                if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
    486540                        // Add the meta-data
    487541                        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    488542
    489                         set_theme_mod('header_image', esc_url($url));
    490                         do_action('wp_create_file_in_uploads', $file, $id); // For replication
     543                        set_theme_mod( 'header_image', esc_url( $url ) );
     544                        do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
    491545                        return $this->finished();
    492546                } elseif ( $width > HEADER_IMAGE_WIDTH ) {
    493547                        $oitar = $width / HEADER_IMAGE_WIDTH;
    494                         $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
     548                        $transparency = ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] ) ? true : false;
     549                        $image = wp_crop_image( $file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace( basename( $file ), 'midsize-'.basename( $file ), $file ), $transparency );
    495550                        if ( is_wp_error( $image ) )
    496551                                wp_die( __( 'Image could not be processed.  Please go back and try again.' ), __( 'Image Processing Error' ) );
    497552
    498                         $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
     553                        $image = apply_filters( 'wp_create_file_in_uploads', $image, $id ); // For replication
    499554
    500                         $url = str_replace(basename($url), basename($image), $url);
     555                        $url = str_replace( basename( $url ), basename( $image ), $url );
    501556                        $width = $width / $oitar;
    502557                        $height = $height / $oitar;
    503558                } else {
    504559                        $oitar = 1;
    505560                }
     561
     562                // Check for transparency
     563                $transparency = 0;
     564                if ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] )
     565                        $transparency = 1;
    506566                ?>
    507567
    508568<div class="wrap">
     569<?php screen_icon(); ?>
     570<h2><?php _e( 'Crop Header Image' ); ?></h2>
    509571
    510 <form method="POST" action="<?php echo esc_attr(add_query_arg('step', 3)) ?>">
     572<form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 3 ) ); ?>">
     573        <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p>
     574        <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.' ); ?></strong></p>
     575        <p><?php printf( __( 'Images smaller then <strong>%1$d x %2$d pixels</strong> will be zoomed.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT ); ?></p>
     576        <div id="crop_image" style="position: relative">
     577                <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
     578        </div>
    511579
    512 <p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
    513 <div id="testWrap" style="position: relative">
    514 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
    515 </div>
    516 
    517 <p class="submit">
    518 <input type="hidden" name="x1" id="x1" />
    519 <input type="hidden" name="y1" id="y1" />
    520 <input type="hidden" name="x2" id="x2" />
    521 <input type="hidden" name="y2" id="y2" />
    522 <input type="hidden" name="width" id="width" />
    523 <input type="hidden" name="height" id="height" />
    524 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr($id); ?>" />
    525 <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr($oitar); ?>" />
    526 <?php wp_nonce_field('custom-header') ?>
    527 <input type="submit" value="<?php esc_attr_e('Crop Header'); ?>" />
    528 </p>
    529 
     580        <input type="hidden" name="x1" id="x1" value="0"/>
     581        <input type="hidden" name="y1" id="y1" value="0"/>
     582        <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
     583        <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
     584        <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
     585        <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
     586        <input type="hidden" name="transparency" id="transparency" value="<?php echo esc_attr( $transparency ); ?>"/>
     587        <?php wp_nonce_field( 'custom-header' ) ?>
     588        <p class="submit"><input type="submit" value="<?php esc_attr_e( 'Crop Header' ); ?>" /></p>
    530589</form>
    531590</div>
    532591                <?php
     
    538597         * @since 2.1.0
    539598         */
    540599        function step_3() {
    541                 check_admin_referer('custom-header');
     600                check_admin_referer( 'custom-header' );
     601
    542602                if ( $_POST['oitar'] > 1 ) {
    543603                        $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
    544604                        $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
     
    546606                        $_POST['height'] = $_POST['height'] * $_POST['oitar'];
    547607                }
    548608
     609                $transparency = ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] ) ? true : false;
     610
    549611                $original = get_attached_file( $_POST['attachment_id'] );
    550612
    551                 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
     613                // Crop the image
     614                $cropped = wp_crop_image( $_POST['attachment_id'], $x1, $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, false, false, $transparency );
    552615                if ( is_wp_error( $cropped ) )
    553                         wp_die( __( 'Image could not be processed.  Please go back and try again.' ), __( 'Image Processing Error' ) );
     616                        wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    554617
    555                 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
     618                $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $_POST['attachment_id'] ); // For replication
    556619
    557                 $parent = get_post($_POST['attachment_id']);
     620                $parent = get_post( $_POST['attachment_id'] );
    558621                $parent_url = $parent->guid;
    559                 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
     622                $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
    560623
    561624                // Construct the object array
    562625                $object = array(
    563626                        'ID' => $_POST['attachment_id'],
    564                         'post_title' => basename($cropped),
    565                         'post_content' => $url,
     627                        'post_title' => basename( $cropped ),
     628                        'post_content' => __( 'This is your header image, if you delete it, you will lost your header image.' ),
    566629                        'post_mime_type' => 'image/jpeg',
    567630                        'guid' => $url
    568631                );
    569632
    570633                // Update the attachment
    571                 wp_insert_attachment($object, $cropped);
     634                wp_insert_attachment( $object, $cropped );
    572635                wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
    573636
    574                 set_theme_mod('header_image', $url);
     637                set_theme_mod( 'header_image', $url );
    575638
    576639                // cleanup
    577                 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
     640                $medium = str_replace( basename( $original ), 'midsize-'.basename( $original ), $original );
    578641                @unlink( apply_filters( 'wp_delete_file', $medium ) );
    579642                @unlink( apply_filters( 'wp_delete_file', $original ) );
    580643
     
    587650         * @since 2.1.0
    588651         */
    589652        function finished() {
    590                 $_GET['updated'] = 1;
    591           $this->step_1();
     653                $this->updated = true;
     654                $this->step_1();
    592655        }
    593656
    594657        /**
     
    597660         * @since 2.1.0
    598661         */
    599662        function admin_page() {
    600                 if ( ! current_user_can('switch_themes') )
    601                         wp_die(__('You do not have permission to customize headers.'));
     663                if ( ! current_user_can( 'edit_theme_options' ) )
     664                        wp_die( __( 'You do not have permission to customize headers.' ) );
     665
    602666                $step = $this->step();
    603667                if ( 1 == $step )
    604668                        $this->step_1();
  • wp-admin/includes/image.php

     
    4141 * @param int $dst_h The destination height.
    4242 * @param int $src_abs Optional. If the source crop points are absolute.
    4343 * @param string $dst_file Optional. The destination file to write to.
     44 * @param bool $dst_transparency Optional. The destination file type. Transparency = PNG.
    4445 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
    4546 */
    46 function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
     47function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false, $dst_transparency = false ) {
    4748        if ( is_numeric( $src_file ) ) // Handle int as attachment ID
    4849                $src_file = get_attached_file( $src_file );
    4950
     
    6364                imageantialias( $dst, true );
    6465
    6566        imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
    66 
    6767        imagedestroy( $src ); // Free up memory
    6868
    6969        if ( ! $dst_file )
    7070                $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
    7171
    72         $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
     72        $type = '.jpeg';
     73        if ( $dst_transparency )
     74                $type = '.png';
    7375
    74         if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
     76        $dst_file = preg_replace( '/\\.[^\\.]+$/', $type, $dst_file );
     77
     78        if ( $dst_transparency && imagepng( $dst, $dst_file) )
    7579                return $dst_file;
     80        elseif ( imagejpeg( $dst, $dst_file) )
     81                return $dst_file;
    7682        else
    7783                return false;
    7884}