Make WordPress Core

Ticket #13231: refactoring_custom_header.6.patch

File refactoring_custom_header.6.patch, 34.3 KB (added by ocean90, 15 years ago)

Updated to trunk, Valid HTML

  • wp-admin/css/wp-admin.dev.css

     
    36973697}
    36983698
    36993699/* Custom Header */
     3700.appearance_page_custom-header #headimg  {
     3701        position: relative;
     3702}
    37003703
    3701 table#available-headers td {
    3702         padding: 0 20px 10px 0;
     3704.appearance_page_custom-header #headimg img {
     3705        z-index: -1;
     3706        width: 100%;
     3707        height: 100%;
     3708        top: 0;
     3709        left: 0;
     3710        position: absolute;
    37033711}
    3704 table#available-headers label input {
    3705         margin-right: 8px;
     3712
     3713.appearance_page_custom-header #headimg h1 {
     3714        font-family: 'Trebuchet MS', 'Lucida Grande', Verdana,Arial, Sans-Serif;
     3715        font-size: 18px;
     3716        font-weight: bold;
     3717        margin: 0;
     3718        padding-top: 5%;
     3719        text-align: center;
    37063720}
    3707 table#available-headers label img {
     3721
     3722.appearance_page_custom-header #headimg #desc {
     3723        font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif;
     3724        font-size: 10px;
     3725        text-align: center;
     3726}
     3727
     3728.appearance_page_custom-header #upload-form p label {
     3729        font-size: 11px;
     3730}
     3731
     3732.appearance_page_custom-header  #available-headers .default-header {
     3733        float: left;
     3734        margin: 0 20px 20px 0;
     3735}
     3736
     3737.appearance_page_custom-header #available-headers label input {
     3738        margin-right: 10px;
     3739}
     3740
     3741.appearance_page_custom-header #available-headers label img {
    37083742        vertical-align: middle;
    37093743}
    37103744
  • 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('edit_theme_options') )
     64                if ( ! current_user_can( 'edit_theme_options' ) )
    6565                        return;
    6666
    67                 $page = add_theme_page(__('Header'), __('Header'), 'edit_theme_options', '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 )
    103                         wp_enqueue_script('farbtastic');
     102                if ( ( 1 == $step || 3 == $step ) && !defined( 'NO_HEADER_TEXT' ) )
     103                        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 )
    117                         wp_enqueue_style('farbtastic');
     116                if ( ( 1 == $step || 3 == $step ) && !defined( 'NO_HEADER_TEXT' ) )
     117                        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('edit_theme_options') )
     128                if ( ! current_user_can( 'edit_theme_options' ) )
    129129                        return;
     130                       
     131                if ( empty( $_POST ) )
     132                        return;
     133                       
     134                $this->updated = true;
     135                       
     136                if ( isset( $_POST['resetheader'] ) ) {
     137                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     138                        remove_theme_mod( 'header_image' );
     139                        return;
     140                }
     141               
     142                if ( isset( $_POST['resettext'] ) ) {
     143                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     144                        remove_theme_mod('header_textcolor');
     145                        return;
     146                }
     147               
     148                if ( isset( $_POST['removeheader'] ) ) {
     149                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     150                        set_theme_mod( 'header_image', '' );
     151                        return;
     152                }
    130153
    131                 if ( isset( $_POST['textcolor'] ) ) {
    132                         check_admin_referer('custom-header');
    133                         if ( 'blank' == $_POST['textcolor'] ) {
    134                                 set_theme_mod('header_textcolor', 'blank');
     154                if ( isset( $_POST['text-color'] ) ) {
     155                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     156                        $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
     157                        if ( 'blank' == $_POST['text-color'] ) {
     158                                set_theme_mod( 'header_textcolor', 'blank' );
    135159                        } else {
    136                                 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
     160                                $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
    137161                                if ( strlen($color) == 6 || strlen($color) == 3 )
    138                                         set_theme_mod('header_textcolor', $color);
     162                                        set_theme_mod( 'header_textcolor', $color );
    139163                        }
    140164                }
    141165
    142                 if ( isset($_POST['resetheader']) ) {
    143                         check_admin_referer('custom-header');
    144                         remove_theme_mods();
     166                if ( isset( $_POST['default-header'] ) ) {
     167                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     168                        $this->process_default_headers();
     169                        if ( isset( $this->default_headers[$_POST['default-header']] ) )
     170                                set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
    145171                }
    146172
    147                 if ( isset($_POST['default-header']) ) {
    148                         check_admin_referer('custom-header');
    149                         $this->process_default_headers();
    150                         if ( isset($this->default_headers[$_POST['default-header']]) )
    151                                 set_theme_mod('header_image', esc_url($this->default_headers[$_POST['default-header']]['url']));
    152                 }
    153173        }
    154174
    155175        /**
     
    160180        function process_default_headers() {
    161181                global $_wp_default_headers;
    162182
    163                 if ( !empty($this->headers) )
     183                if ( ! empty( $this->headers ) )
    164184                        return;
    165185
    166                 if ( !isset($_wp_default_headers) )
     186                if ( ! isset( $_wp_default_headers ) )
    167187                        return;
    168188
    169189                $this->default_headers = $_wp_default_headers;
     
    179199         * @since 3.0.0
    180200         */
    181201        function show_default_header_selector() {
    182                 echo '<table id="available-headers" cellspacing="0" cellpadding="0">';
     202                echo '<div id="available-headers">';
    183203
    184                 $headers = array_keys($this->default_headers);
    185                 $table = array();
    186                 $rows = ceil(count($headers) / 3);
    187                 for ( $row = 1; $row <= $rows; $row++ ) {
    188                         for ( $col = 1; $col <= 3; $col++ ) {
    189                                 $table[$row][$col] = array_shift($headers);
    190                         }
     204                foreach ( $this->default_headers as $header_key => $header ) {
     205                        $header_thumbnail = $header['thumbnail_url'];
     206                        $header_url = $header['url'];
     207                        $header_desc = $header['description'];
     208                        echo '<div class="default-header">';
     209                        echo '<label><input name="default-header" type="radio" value="' . esc_attr($header_key) . '" ' . checked($header_url, get_theme_mod( 'header_image' ), false) . ' />';
     210                        echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr($header_desc) .'" title="' . esc_attr($header_desc) .'" /></label>';
     211                        echo '</div>';
    191212                }
    192 
    193                 foreach ( $table as $row => $cols ) {
    194                         echo '<tr>';
    195                         foreach ( $cols as $col => $header_key ) {
    196                                 if ( !$header_key )
    197                                         continue;
    198                                 $class = array('available-header');
    199                                 if ( $row == 1 ) $class[] = 'top';
    200                                 if ( $col == 1 ) $class[] = 'left';
    201                                 if ( $row == $rows ) $class[] = 'bottom';
    202                                 if ( $col == 3 ) $class[] = 'right';
    203                                 if ( !isset($this->headers[$header_key]))
    204                                 echo '<td class="' . join(' ', $class) . '">';
    205                                 $header_thumbnail = $this->default_headers[$header_key]['thumbnail_url'];
    206                                 $header_url = $this->default_headers[$header_key]['url'];
    207                                 $header_desc = $this->default_headers[$header_key]['description'];
    208                                 echo '<label><input name="default-header" type="radio" value="' . esc_attr($header_key) . '" ' . checked($header_url, get_header_image(), false) . ' />';
    209                                 echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr($header_desc) .'" /></label>';
    210                                 echo  '</td>';
    211                         }
    212                         echo '</tr>';
    213                 }
    214                 echo '</table>';
     213               
     214                echo '<div class="clear"></div></div>';
    215215        }
    216216
    217217        /**
     
    221221         */
    222222        function js() {
    223223                $step = $this->step();
    224                 if ( 1 == $step )
     224                if ( ( 1 == $step || 3 == $step ) && !defined( 'NO_HEADER_TEXT' ) )
    225225                        $this->js_1();
    226226                elseif ( 2 == $step )
    227227                        $this->js_2();
    228228        }
    229229
    230230        /**
    231          * Display Javascript based on Step 1.
     231         * Display Javascript based on Step 1 and 3.
    232232         *
    233233         * @since 2.6.0
    234234         */
    235235        function js_1() { ?>
    236236<script type="text/javascript">
    237         var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];
     237//<![CDATA[
     238        var text_objects = ['#name', '#desc', '#text-color-row'];
    238239        var farbtastic;
     240        var default_color = '#<?php echo HEADER_TEXTCOLOR; ?>';
     241        var old_color = null;
    239242
    240243        function pickColor(color) {
    241244                jQuery('#name').css('color', color);
    242245                jQuery('#desc').css('color', color);
    243                 jQuery('#textcolor').val(color);
     246                jQuery('#text-color').val(color);
    244247                farbtastic.setColor(color);
    245248        }
     249       
     250        function toggle_text(s) {
     251                if (jQuery(s).attr('id') == 'showtext' && jQuery('#text-color').val() != 'blank')
     252                        return;
     253               
     254                if (jQuery(s).attr('id') == 'hidetext' && jQuery('#text-color').val() == 'blank')
     255                        return;
    246256
     257                if (jQuery('#text-color').val() == 'blank') {
     258                        //Show text
     259                        if (old_color == '#blank')
     260                                old_color = default_color;
     261       
     262                        jQuery( text_objects.toString() ).show();
     263                        jQuery('#text-color').val(old_color);
     264                        jQuery('#name').css('color', old_color);
     265                        jQuery('#desc').css('color', old_color);
     266                        pickColor(old_color);
     267                } else {
     268                        //Hide text
     269                        jQuery( text_objects.toString() ).hide();
     270                        old_color = jQuery('#text-color').val();
     271                        jQuery('#text-color').val('blank');
     272                }
     273        }
     274
    247275        jQuery(document).ready(function() {
    248276                jQuery('#pickcolor').click(function() {
    249277                        jQuery('#colorPickerDiv').show();
    250278                });
    251279
    252                 jQuery('#hidetext').click(function() {
    253                         toggle_text();
     280                jQuery('input[name="hidetext"]').click(function() {
     281                        toggle_text(this);
    254282                });
     283               
     284                jQuery('#defaultcolor').click(function() {
     285                        pickColor(default_color);
     286                        jQuery('#text-color').val(default_color)
     287                });
     288               
     289                jQuery('#text-color').keyup(function() {
     290                        var _hex = jQuery('#text-color').val();
     291                        var hex = _hex;
     292                        if ( hex[0] != '#' )
     293                                hex = '#' + hex;
     294                        hex = hex.replace(/[^#a-fA-F0-9]+/, '');
     295                        if ( hex != _hex )
     296                                jQuery('#text-color').val(hex);
     297                        if ( hex.length == 4 || hex.length == 7 )
     298                                pickColor( hex );
     299                });
     300               
     301                jQuery(document).mousedown(function(){
     302                        jQuery('#colorPickerDiv').each( function() {
     303                                var display = jQuery(this).css('display');
     304                                if (display == 'block')
     305                                        jQuery(this).fadeOut(2);
     306                        });
     307                });
    255308
    256309                farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
    257310                pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>');
    258311
    259                 <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
     312                <?php if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || defined( 'NO_HEADER_TEXT' ) ) { ?>
    260313                toggle_text();
    261314                <?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                         }
    284315                });
    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 
     316//]]>
    304317</script>
    305318<?php
    306319        }
     
    312325         */
    313326        function js_2() { ?>
    314327<script type="text/javascript">
     328//<![CDATA[
    315329        function onEndCrop( coords ) {
    316330                jQuery( '#x1' ).val(coords.x);
    317331                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);
     332                jQuery('#width').val(coords.w);
     333                jQuery('#height').val(coords.h);
    322334        }
    323335
    324         // with a supplied ratio
    325336        jQuery(document).ready(function() {
    326337                var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
    327338                var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
    328                 var ratio = xinit / yinit;
     339                var ratio = xinit +  ':' + yinit;
    329340                var ximg = jQuery('#upload').width();
    330341                var yimg = jQuery('#upload').height();
    331342
    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({
     343                jQuery('#upload').imgAreaSelect({
     344                        handles: true,
     345                        keys: true,
    351346                        aspectRatio: ratio,
    352                         setSelect: [ 0, 0, xinit, yinit ],
    353                         onSelect: onEndCrop
     347                        maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
     348                        maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
     349                        onSelectChange: function(img, c) {
     350                                jQuery('#x1').val(c.x1);
     351                                jQuery('#y1').val(c.y1);
     352                                jQuery('#width').val(c.width);
     353                                jQuery('#height').val(c.height);
     354                        },
    354355                });
    355356        });
     357//]]>
    356358</script>
    357359<?php
    358360        }
     
    368370
    369371<div class="wrap">
    370372<?php screen_icon(); ?>
    371 <h2><?php _e('Your Header Image'); ?></h2>
     373<h2><?php _e( 'Custom Header' ); ?></h2>
    372374
    373 <?php
    374 if ( isset($_GET['updated']) && $_GET['updated'] ) { ?>
     375<?php if ( ! empty( $this->updated ) ) { ?>
    375376<div id="message" class="updated">
    376377<p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
    377378</div>
    378 <?php }
     379<?php } ?>
    379380
    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
     381<h3><?php _e( 'Header Image' ) ?></h3>
     382<table class="form-table">
     383<tbody>
    384384
    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 } ?>
     385        <?php if ( get_header_image() ) : ?>
     386<tr valign="top">
     387<th scope="row"><?php _e( 'Current Image' ); ?></th>
     388<td >
     389        <?php if ( $this->admin_image_div_callback ) {
     390          call_user_func( $this->admin_image_div_callback );
     391        } else {
     392        ?>
     393        <div id="headimg" style="color:#<?php echo get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>;max-width:<?php echo HEADER_IMAGE_WIDTH; ?>px;;height:<?php echo HEADER_IMAGE_HEIGHT; ?>px">
     394                <img id="image" src="<?php esc_url ( header_image() ) ?>" alt="" />
     395                <?php
     396                $style = '';
     397                if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || defined( 'NO_HEADER_TEXT' ) )
     398                        $style = " style='display:none'";
     399                ?>
     400                <h1 id="name"<?php echo $style; ?>><?php bloginfo( 'name' ); ?></h1>
     401                <div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
     402        </div>
     403        <?php } ?>
     404</td>
     405</tr>
     406        <?php endif; ?>
    394407
    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 } ?>
     408<tr valign="top">
     409<th scope="row"><?php _e( 'Upload Image' ); ?></th>
     410<td>
     411        <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 />
     412        <?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>
     413        <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
     414        <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>
     415        <p>
     416                <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
     417                <input type="file" id="upload" name="import" />
     418                <input type="hidden" name="action" value="save" />
     419                <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ) ?>
     420                <input type="submit" class="button" value="<?php esc_attr_e( 'Upload' ); ?>" />
     421        </p>
     422        </form>
     423</td>
     424</tr>
     425</tbody>
     426</table>
     427<form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>">
     428<table class="form-table">
     429<tbody>
     430        <?php if ( ! empty( $this->default_headers ) ) : ?>
     431<tr valign="top">
     432<th scope="row"><?php _e( 'Default Images' ); ?></th>
     433<td>
     434        <p><?php _e( 'If you didn&lsquo;t want to upload your own image, you can use one of these cool headers.' ) ?></p>
     435        <?php
     436                $this->show_default_header_selector();
     437        ?>
     438</td>
     439</tr>
     440        <?php endif;
    402441
    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>
     442        if ( get_header_image() ) : ?>
     443<tr valign="top">
     444<th scope="row"><?php _e( 'Remove Image' ); ?></th>
     445<td>
     446        <p><?php _e( 'This will remove the header image. You will not be able to retrieve any customizations.' ) ?></p>
     447        <input type="submit" class="button" name="removeheader" value="<?php esc_attr_e( 'Remove Header Image' ); ?>" />
     448</td>
     449</tr>
     450        <?php endif;
     451       
     452        if ( get_theme_mod( 'header_image' ) && defined( 'HEADER_IMAGE' ) ) : ?>
     453<tr valign="top">
     454<th scope="row"><?php _e( 'Reset Image' ); ?></th>
     455<td>
     456        <p><?php _e( 'This will restore the original header image. You will not be able to retrieve any customizations.' ) ?></p>
     457        <input type="submit" class="button" name="resetheader" value="<?php esc_attr_e( 'Restore Original Header Image' ); ?>" />
     458</td>
     459</tr>
     460        <?php endif; ?>
     461</tbody>
     462</table>
    418463
    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>
     464        <?php if ( !defined( 'NO_HEADER_TEXT' ) ) : ?>
     465<h3><?php _e( 'Header Text' ) ?></h3>
     466<table class="form-table">
     467<tbody>
     468<tr valign="top" class="hide-if-no-js">
     469<th scope="row"><?php _e( 'Display Text' ); ?></th>
     470<td>
     471        <p>
     472        <?php $hidetext = get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>
     473        <label><input type="radio" value="1" name="hidetext" id="hidetext"<?php checked( 'blank' == $hidetext  ? true : false ); ?> /> <?php _e( 'No' ); ?></label>
     474        <label><input type="radio" value="0" name="hidetext" id="showtext"<?php checked( 'blank' == $hidetext  ? false : true ); ?> /> <?php _e( 'Yes' ); ?></label>
     475        </p>
     476</td>
     477</tr>
    427478
    428 </div>
     479<tr valign="top" id="text-color-row">
     480<th scope="row"><?php _e( 'Text Color' ); ?></th>
     481<td>
     482        <p>
     483                <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) ); ?>" />
     484                <span class="description hide-if-js"><?php _e( 'If you want to hide header text, add <strong>#blank</strong> as text color.' );?></span>
     485                <input type="button" class="button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color' ); ?>" id="pickcolor" />
     486        </p>
     487        <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
     488</td>
     489</tr>
    429490
    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 } ?>
     491        <?php if ( defined( 'HEADER_TEXTCOLOR' ) && get_theme_mod( 'header_textcolor' ) ) { ?>
     492<tr valign="top">
     493<th scope="row"><?php _e( 'Reset Header Text' ); ?></th>
     494<td>
     495        <p><?php _e( 'This will restore the original header text. You will not be able to retrieve any customizations.' ) ?></p>
     496        <input type="submit" class="button" name="resettext" value="<?php esc_attr_e( 'Restore Original Header Text' ); ?>" />
     497</td>
     498</tr>
     499        <?php } ?>
     500
     501</tbody>
     502</table>
     503        <?php endif;
     504
     505wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
     506<p class="submit"><input type="submit" class="button-primary" name="save-header-options" value="<?php esc_attr_e( 'Save Changes' ); ?>" /></p>
    449507</form>
    450508</div>
    451                 <?php endif;
    452509
    453         }
     510<?php }
    454511
    455512        /**
    456513         * Display second step of custom header image page.
     
    458515         * @since 2.1.0
    459516         */
    460517        function step_2() {
    461                 check_admin_referer('custom-header');
    462                 $overrides = array('test_form' => false);
    463                 $file = wp_handle_upload($_FILES['import'], $overrides);
     518                check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );
    464519
    465                 if ( isset($file['error']) )
    466                 die( $file['error'] );
     520                $overrides = array( 'test_form' => false );
     521                $file = wp_handle_upload( $_FILES['import'], $overrides );
    467522
     523                if ( isset( $file['error'] ) )
     524                        wp_die( $file['error'],  __( 'Image Upload Error' ) );
     525
    468526                $url = $file['url'];
    469527                $type = $file['type'];
    470528                $file = $file['file'];
     
    472530
    473531                // Construct the object array
    474532                $object = array(
    475                 'post_title' => $filename,
    476                 'post_content' => $url,
    477                 'post_mime_type' => $type,
    478                 'guid' => $url);
     533                        'post_title' => $filename,
     534                        'post_content' => __( 'This is your header image, if you delete it, you will lost your header image.' ),
     535                        'post_mime_type' => $type,
     536                        'guid' => $url
     537                );
    479538
    480539                // Save the data
    481                 $id = wp_insert_attachment($object, $file);
     540                $id = wp_insert_attachment( $object, $file );
    482541
    483                 list($width, $height, $type, $attr) = getimagesize( $file );
     542                list( $width, $height, $type, $attr) = getimagesize( $file );
    484543
    485544                if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
    486545                        // Add the meta-data
    487546                        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    488547
    489                         set_theme_mod('header_image', esc_url($url));
    490                         do_action('wp_create_file_in_uploads', $file, $id); // For replication
     548                        set_theme_mod( 'header_image', esc_url( $url ) );
     549                        do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
    491550                        return $this->finished();
    492551                } elseif ( $width > HEADER_IMAGE_WIDTH ) {
    493552                        $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));
     553                        $transparency = ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] ) ? true : false;
     554                        $image = wp_crop_image( $file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace( basename( $file ), 'midsize-'.basename( $file ), $file ), $transparency );
    495555                        if ( is_wp_error( $image ) )
    496556                                wp_die( __( 'Image could not be processed.  Please go back and try again.' ), __( 'Image Processing Error' ) );
    497557
    498                         $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
     558                        $image = apply_filters( 'wp_create_file_in_uploads', $image, $id ); // For replication
    499559
    500                         $url = str_replace(basename($url), basename($image), $url);
     560                        $url = str_replace( basename( $url ), basename( $image ), $url );
    501561                        $width = $width / $oitar;
    502562                        $height = $height / $oitar;
    503563                } else {
    504564                        $oitar = 1;
    505565                }
     566
     567                // Check for transparency
     568                $transparency = 0;
     569                if ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] )
     570                        $transparency = 1;
    506571                ?>
    507572
    508573<div class="wrap">
     574<?php screen_icon(); ?>
     575<h2><?php _e( 'Crop Header Image' ); ?></h2>
    509576
    510 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 3)) ?>">
     577<form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 3 ) ); ?>">
     578        <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p>
     579        <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.' ); ?></strong></p>
     580        <p><?php printf( __( 'Images smaller then <strong>%1$d x %2$d pixels</strong> will be zoomed.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT ); ?></p>
     581        <div id="crop_image" style="position: relative">
     582                <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
     583        </div>
    511584
    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 
     585        <input type="hidden" name="x1" id="x1" value="0"/>
     586        <input type="hidden" name="y1" id="y1" value="0"/>
     587        <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
     588        <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
     589        <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
     590        <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
     591        <input type="hidden" name="transparency" id="transparency" value="<?php echo esc_attr( $transparency ); ?>"/>
     592        <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
     593        <p class="submit"><input type="submit" value="<?php esc_attr_e( 'Crop Header' ); ?>" /></p>
    530594</form>
    531595</div>
    532596                <?php
     
    538602         * @since 2.1.0
    539603         */
    540604        function step_3() {
    541                 check_admin_referer('custom-header');
     605                check_admin_referer( 'custom-header-crop-image' );
     606
    542607                if ( $_POST['oitar'] > 1 ) {
    543608                        $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
    544609                        $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
     
    546611                        $_POST['height'] = $_POST['height'] * $_POST['oitar'];
    547612                }
    548613
     614                $transparency = ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] ) ? true : false;
     615
    549616                $original = get_attached_file( $_POST['attachment_id'] );
    550617
    551                 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
     618                // Crop the image
     619                $cropped = wp_crop_image( $_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, false, false, $transparency );
    552620                if ( is_wp_error( $cropped ) )
    553                         wp_die( __( 'Image could not be processed.  Please go back and try again.' ), __( 'Image Processing Error' ) );
     621                        wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    554622
    555                 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
     623                $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $_POST['attachment_id'] ); // For replication
    556624
    557                 $parent = get_post($_POST['attachment_id']);
     625                $parent = get_post( $_POST['attachment_id'] );
    558626                $parent_url = $parent->guid;
    559                 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
     627                $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
    560628
    561629                // Construct the object array
    562630                $object = array(
    563631                        'ID' => $_POST['attachment_id'],
    564                         'post_title' => basename($cropped),
    565                         'post_content' => $url,
     632                        'post_title' => basename( $cropped ),
     633                        'post_content' => __( 'This is your header image, if you delete it, you will lost your header image.' ),
    566634                        'post_mime_type' => 'image/jpeg',
    567635                        'guid' => $url
    568636                );
     
    571639                wp_insert_attachment($object, $cropped);
    572640                wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
    573641
    574                 set_theme_mod('header_image', $url);
     642                set_theme_mod( 'header_image', $url );
    575643
    576644                // cleanup
    577                 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
     645                $medium = str_replace( basename( $original ), 'midsize-'.basename( $original ), $original );
    578646                @unlink( apply_filters( 'wp_delete_file', $medium ) );
    579647                @unlink( apply_filters( 'wp_delete_file', $original ) );
    580648
     
    587655         * @since 2.1.0
    588656         */
    589657        function finished() {
    590                 $_GET['updated'] = 1;
    591           $this->step_1();
     658                $this->updated = true;
     659                $this->step_1();
    592660        }
    593661
    594662        /**
     
    597665         * @since 2.1.0
    598666         */
    599667        function admin_page() {
    600                 if ( ! current_user_can('edit_theme_options') )
    601                         wp_die(__('You do not have permission to customize headers.'));
     668                if ( ! current_user_can( 'edit_theme_options' ) )
     669                        wp_die( __( 'You do not have permission to customize headers.' ) );
     670
    602671                $step = $this->step();
    603672                if ( 1 == $step )
    604673                        $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}
  • wp-content/themes/twentyten/functions.php

     
    187187function twentyten_admin_header_style() {
    188188?>
    189189<style type="text/css">
    190 #headimg {
    191         height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
    192         width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
     190/* Shows the same border as on front page */
     191.appearance_page_custom-header #headimg {
     192        border-bottom: 1px solid #000000;
     193        border-top: 4px solid #000000;
    193194}
    194 #headimg h1, #headimg #desc {
    195         display: none;
    196 }
    197195</style>
    198196<?php
    199197}