Ticket #13231: refactoring_custom_header.2.patch
File refactoring_custom_header.2.patch, 33.7 KB (added by , 15 years ago) |
---|
-
wp-admin/css/wp-admin.dev.css
3706 3706 } 3707 3707 3708 3708 /* Custom Header */ 3709 .appearance_page_custom-header #headimg { 3710 position: relative; 3711 } 3709 3712 3710 table#available-headers td { 3711 padding: 0 20px 10px 0; 3713 .appearance_page_custom-header #headimg img { 3714 z-index: -1; 3715 width: 100%; 3716 height: 100%; 3717 top: 0; 3718 left: 0; 3719 position: absolute; 3712 3720 } 3713 table#available-headers label input { 3714 margin-right: 8px; 3721 3722 .appearance_page_custom-header #headimg h1 { 3723 font-family: 'Trebuchet MS', 'Lucida Grande', Verdana,Arial, Sans-Serif; 3724 font-size: 18px; 3725 font-weight: bold; 3726 margin: 0; 3727 padding-top: 5%; 3728 text-align: center; 3715 3729 } 3716 table#available-headers label img { 3730 3731 .appearance_page_custom-header #headimg #desc { 3732 font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; 3733 font-size: 10px; 3734 text-align: center; 3735 } 3736 3737 .appearance_page_custom-header #uploadForm p label { 3738 font-size: 11px; 3739 } 3740 3741 .appearance_page_custom-header #available-headers .default-header { 3742 float: left; 3743 margin: 0 20px 20px 0; 3744 } 3745 3746 .appearance_page_custom-header #available-headers label input { 3747 margin-right: 10px; 3748 } 3749 3750 .appearance_page_custom-header #available-headers label img { 3717 3751 vertical-align: middle; 3718 3752 } 3719 3753 -
wp-admin/custom-header.php
50 50 * @param callback $admin_image_div_callback Optional custom image div output callback. 51 51 * @return Custom_Image_Header 52 52 */ 53 function Custom_Image_Header( $admin_header_callback, $admin_image_div_callback = '') {53 function Custom_Image_Header( $admin_header_callback, $admin_image_div_callback = '' ) { 54 54 $this->admin_header_callback = $admin_header_callback; 55 55 $this->admin_image_div_callback = $admin_image_div_callback; 56 56 } … … 61 61 * @since 2.1.0 62 62 */ 63 63 function init() { 64 if ( ! current_user_can( 'switch_themes') )64 if ( ! current_user_can( 'edit_theme_options' ) ) 65 65 return; 66 66 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' ) ); 68 68 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 ) ; 74 74 } 75 75 76 76 /** … … 99 99 function js_includes() { 100 100 $step = $this->step(); 101 101 102 if ( 1 == $step)103 wp_enqueue_script( 'farbtastic');102 if ( ( 1 == $step || 3 == $step ) && !defined( 'NO_HEADER_TEXT' ) ) 103 wp_enqueue_script( 'farbtastic' ); 104 104 elseif ( 2 == $step ) 105 wp_enqueue_script( 'jcrop');105 wp_enqueue_script( 'imgareaselect' ); 106 106 } 107 107 108 108 /** … … 113 113 function css_includes() { 114 114 $step = $this->step(); 115 115 116 if ( 1 == $step)117 wp_enqueue_style( 'farbtastic');116 if ( ( 1 == $step || 3 == $step ) && !defined( 'NO_HEADER_TEXT' ) ) 117 wp_enqueue_style( 'farbtastic' ); 118 118 elseif ( 2 == $step ) 119 wp_enqueue_style( 'jcrop');119 wp_enqueue_style( 'imgareaselect' ); 120 120 } 121 121 122 122 /** … … 125 125 * @since 2.6.0 126 126 */ 127 127 function take_action() { 128 if ( ! current_user_can( 'switch_themes') )128 if ( ! current_user_can( 'edit_theme_options' ) ) 129 129 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 } 130 145 131 if ( isset( $_POST['textcolor'] ) ) { 132 check_admin_referer('custom-header'); 133 if ( 'blank' == $_POST['textcolor'] ) { 134 set_theme_mod('header_textcolor', 'blank'); 146 if ( isset( $_POST['text-color'] ) ) { 147 check_admin_referer( 'custom-header' ); 148 $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] ); 149 if ( 'blank' == $_POST['text-color'] ) { 150 set_theme_mod( 'header_textcolor', 'blank' ); 135 151 } else { 136 $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['textcolor']);137 if ( strlen( $color) == 6 || strlen($color) == 3 )138 set_theme_mod( 'header_textcolor', $color);152 $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] ); 153 if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) 154 set_theme_mod( 'header_textcolor', $color ); 139 155 } 140 156 } 141 157 142 if ( isset($_POST['resetheader']) ) { 143 check_admin_referer('custom-header'); 144 remove_theme_mods(); 145 } 146 147 if ( isset($_POST['default-header']) ) { 148 check_admin_referer('custom-header'); 158 if ( isset( $_POST['default-header'] ) ) { 159 check_admin_referer( 'custom-header' ); 149 160 $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']));161 if ( isset( $this->default_headers[$_POST['default-header']] ) ) 162 set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) ); 152 163 } 164 165 $this->updated = true; 153 166 } 154 167 155 168 /** … … 179 192 * @since 3.0.0 180 193 */ 181 194 function show_default_header_selector() { 182 echo '< table id="available-headers" cellspacing="0" cellpadding="0">';195 echo '<div id="available-headers">'; 183 196 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 } 197 foreach ( $this->default_headers as $header_key => $header ) { 198 $header_thumbnail = $header['thumbnail_url']; 199 $header_url = $header['url']; 200 $header_desc = $header['description']; 201 echo '<div class="default-header">'; 202 echo '<label><input name="default-header" type="radio" value="' . esc_attr($header_key) . '" ' . checked($header_url, get_theme_mod( 'header_image' ), false) . ' />'; 203 echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr($header_desc) .'" title="' . esc_attr($header_desc) .'" /></label>'; 204 echo '</div>'; 191 205 } 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>'; 206 207 echo '<div class="clear"></div></div>'; 215 208 } 216 209 217 210 /** … … 221 214 */ 222 215 function js() { 223 216 $step = $this->step(); 224 if ( 1 == $step)217 if ( ( 1 == $step || 3 == $step ) && !defined( 'NO_HEADER_TEXT' ) ) 225 218 $this->js_1(); 226 219 elseif ( 2 == $step ) 227 220 $this->js_2(); 228 221 } 229 222 230 223 /** 231 * Display Javascript based on Step 1 .224 * Display Javascript based on Step 1 and 3. 232 225 * 233 226 * @since 2.6.0 234 227 */ 235 228 function js_1() { ?> 236 229 <script type="text/javascript"> 237 var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];230 var text_objects = ['#name', '#desc', '#text-color-row']; 238 231 var farbtastic; 232 var default_color = '#<?php echo HEADER_TEXTCOLOR; ?>'; 233 var old_color = null; 239 234 240 235 function pickColor(color) { 241 236 jQuery('#name').css('color', color); 242 237 jQuery('#desc').css('color', color); 243 jQuery('#text color').val(color);238 jQuery('#text-color').val(color); 244 239 farbtastic.setColor(color); 245 240 } 241 242 function toggle_text(s) { 243 if (jQuery(s).attr('id') == 'showtext' && jQuery('#text-color').val() != 'blank') 244 return; 245 246 if (jQuery(s).attr('id') == 'hidetext' && jQuery('#text-color').val() == 'blank') 247 return; 246 248 249 if (jQuery('#text-color').val() == 'blank') { 250 //Show text 251 if (old_color == '#blank') 252 old_color = default_color; 253 254 jQuery( text_objects.toString() ).show(); 255 jQuery('#text-color').val(old_color); 256 jQuery('#name').css('color', old_color); 257 jQuery('#desc').css('color', old_color); 258 pickColor(old_color); 259 } else { 260 //Hide text 261 jQuery( text_objects.toString() ).hide(); 262 old_color = jQuery('#text-color').val(); 263 jQuery('#text-color').val('blank'); 264 } 265 } 266 247 267 jQuery(document).ready(function() { 248 268 jQuery('#pickcolor').click(function() { 249 269 jQuery('#colorPickerDiv').show(); 250 270 }); 251 271 252 jQuery(' #hidetext').click(function() {253 toggle_text( );272 jQuery('input[name="hidetext"]').click(function() { 273 toggle_text(this); 254 274 }); 275 276 jQuery('#defaultcolor').click(function() { 277 pickColor(default_color); 278 jQuery('#text-color').val(default_color) 279 }); 280 281 jQuery('#text-color').keyup(function() { 282 var _hex = jQuery('#text-color').val(); 283 var hex = _hex; 284 if ( hex[0] != '#' ) 285 hex = '#' + hex; 286 hex = hex.replace(/[^#a-fA-F0-9]+/, ''); 287 if ( hex != _hex ) 288 jQuery('#text-color').val(hex); 289 if ( hex.length == 4 || hex.length == 7 ) 290 pickColor( hex ); 291 }); 292 293 jQuery(document).mousedown(function(){ 294 jQuery('#colorPickerDiv').each( function() { 295 var display = jQuery(this).css('display'); 296 if (display == 'block') 297 jQuery(this).fadeOut(2); 298 }); 299 }); 255 300 256 farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); }); 257 pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>'); 301 farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { 302 pickColor(color); 303 }); 304 pickColor('#<?php echo get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>'); 258 305 259 <?php if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR) ) { ?>306 <?php if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || defined( 'NO_HEADER_TEXT' ) ) { ?> 260 307 toggle_text(); 261 308 <?php } ?> 262 });263 264 jQuery(document).mousedown(function(){265 // Make the picker disappear, since we're using it in an independant div266 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 }284 309 }); 285 }286 287 function toggle_text(force) {288 if (jQuery('#textcolor').val() == 'blank') {289 //Show text290 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 text296 jQuery( buttons.toString() ).hide();297 jQuery('#textcolor').val('blank');298 jQuery('#hidetext').val('<?php _e('Show Text'); ?>');299 }300 }301 302 303 304 310 </script> 305 311 <?php 306 312 } … … 313 319 function js_2() { ?> 314 320 <script type="text/javascript"> 315 321 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); 322 jQuery('#x1').val(coords.x); 323 jQuery('#y1').val(coords.y); 324 jQuery('#width').val(coords.w); 325 jQuery('#height').val(coords.h); 322 326 } 323 327 324 // with a supplied ratio325 328 jQuery(document).ready(function() { 326 329 var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>; 327 330 var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>; 328 var ratio = xinit /yinit;331 var ratio = xinit + ':' + yinit; 329 332 var ximg = jQuery('#upload').width(); 330 333 var yimg = jQuery('#upload').height(); 331 334 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({ 335 jQuery('#upload').imgAreaSelect({ 336 handles: true, 337 keys: true, 351 338 aspectRatio: ratio, 352 setSelect: [ 0, 0, xinit, yinit ], 353 onSelect: onEndCrop 339 maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>, 340 maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>, 341 onSelectChange: function(img, c) { 342 jQuery('#x1').val(c.x1); 343 jQuery('#y1').val(c.y1); 344 jQuery('#width').val(c.width); 345 jQuery('#height').val(c.height); 346 }, 354 347 }); 355 348 }); 356 349 </script> … … 368 361 369 362 <div class="wrap"> 370 363 <?php screen_icon(); ?> 371 <h2><?php _e( 'Your Header Image'); ?></h2>364 <h2><?php _e( 'Custom Header' ); ?></h2> 372 365 373 <?php 374 if ( isset($_GET['updated']) && $_GET['updated'] ) { ?> 366 <?php if ( ! empty( $this->updated ) ) { ?> 375 367 <div id="message" class="updated"> 376 368 <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p> 377 369 </div> 378 <?php } 370 <?php } ?> 379 371 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 372 <h3><?php _e( 'Header Image' ) ?></h3> 373 <table class="form-table"> 374 <tbody> 384 375 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 } ?> 376 <?php if ( get_header_image() ) { ?> 377 <tr valign="top"> 378 <th scope="row"><?php _e( 'Current Image' ); ?></th> 379 <td > 380 <?php if ( $this->admin_image_div_callback ) { 381 call_user_func( $this->admin_image_div_callback ); 382 } else { 383 ?> 384 <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"> 385 <img id="image" src="<?php esc_url ( header_image() ) ?>" /> 386 <?php 387 $style = ''; 388 if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || defined( 'NO_HEADER_TEXT' ) ) 389 $style = " style='display:none'"; 390 ?> 391 <h1 id="name"<?php echo $style; ?>><?php bloginfo( 'name' ); ?></h1> 392 <div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div> 393 </div> 394 <?php } ?> 395 </td> 396 </tr> 397 <?php } ?> 394 398 395 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?> 396 <form method="post" action="<?php echo admin_url('themes.php?page=custom-header&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 } ?> 399 <tr valign="top"> 400 <th scope="row"><?php _e( 'Upload Image' ); ?></th> 401 <td> 402 <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 /> 403 <?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> 404 <form enctype="multipart/form-data" id="uploadForm" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>"> 405 <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> 406 <p> 407 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br /> 408 <input type="file" id="upload" name="import" /> 409 <input type="hidden" name="action" value="save" /> 410 <?php wp_nonce_field( 'custom-header' ) ?> 411 <input type="submit" class="button" value="<?php esc_attr_e( 'Upload' ); ?>" /> 412 </p> 413 </form> 414 </td> 415 </tr> 416 <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>"> 417 <?php if ( ! empty( $this->default_headers ) ) { ?> 418 <tr valign="top"> 419 <th scope="row"><?php _e( 'Default Images' ); ?></th> 420 <td> 421 <p><?php _e( 'If you didn‘t want to upload your own image, you can use one of these cool headers.' ) ?></p> 422 <?php 423 $this->show_default_header_selector(); 424 ?> 425 </td> 426 </tr> 427 <?php } 402 428 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&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> 429 if ( get_theme_mod( 'header_image' ) && defined( 'HEADER_IMAGE' ) ) { ?> 430 <tr valign="top"> 431 <th scope="row"><?php _e( 'Reset Header Image' ); ?></th> 432 <td> 433 <p><?php _e( 'This will restore the original header image. You will not be able to retrieve any customizations.' ) ?></p> 434 <input type="submit" class="button" name="resetheader" value="<?php esc_attr_e( 'Restore Original Header Image' ); ?>" /> 435 </td> 436 </tr> 437 <?php } ?> 438 </tbody> 439 </table> 418 440 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> 441 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?> 442 <h3><?php _e( 'Header Text' ) ?></h3> 443 <table class="form-table"> 444 <tbody> 445 <tr valign="top" class="hide-if-no-js"> 446 <th scope="row"><?php _e( 'Display Text' ); ?></th> 447 <td> 448 <p> 449 <?php $hidetext = get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?> 450 <label><input type="radio" value="1" name="hidetext" id="hidetext"<?php checked( 'blank' == $hidetext ? true : false ); ?> /> <?php _e( 'No' ); ?></label> 451 <label><input type="radio" value="0" name="hidetext" id="showtext"<?php checked( 'blank' == $hidetext ? false : true ); ?> /> <?php _e( 'Yes' ); ?></label> 452 </p> 453 </td> 454 </tr> 427 455 428 </div> 456 <tr valign="top" id="text-color-row"> 457 <th scope="row"><?php _e( 'Text Color' ); ?></th> 458 <td> 459 <p> 460 <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) ); ?>" /> 461 <span class="description hide-if-js"><?php _e( 'If you want to hide header text, add <strong>#blank</strong> as text color.' );?></span> 462 <input type="button" class="button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color' ); ?>" id="pickcolor" /> 463 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div> 464 </p> 465 </td> 466 </tr> 429 467 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 } ?> 468 <?php if ( defined( 'HEADER_TEXTCOLOR' ) && get_theme_mod( 'header_textcolor' ) ) { ?> 469 <tr valign="top"> 470 <th scope="row"><?php _e( 'Reset Header Text' ); ?></th> 471 <td> 472 <p><?php _e( 'This will restore the original header text. You will not be able to retrieve any customizations.' ) ?></p> 473 <input type="submit" class="button" name="resettext" value="<?php esc_attr_e( 'Restore Original Header Text' ); ?>" /> 474 </td> 475 </tr> 476 <?php } ?> 477 478 </tbody> 479 </table> 480 <?php } 481 482 wp_nonce_field( 'custom-header' ); ?> 483 <p class="submit"><input type="submit" class="button-primary" name="save-header-options" value="<?php esc_attr_e( 'Save Changes' ); ?>" /></p> 449 484 </form> 450 485 </div> 451 <?php endif;452 486 453 487 <?php } 454 488 455 489 /** 456 490 * Display second step of custom header image page. … … 458 492 * @since 2.1.0 459 493 */ 460 494 function step_2() { 461 check_admin_referer('custom-header'); 462 $overrides = array('test_form' => false); 463 $file = wp_handle_upload($_FILES['import'], $overrides); 495 check_admin_referer( 'custom-header' ); 464 496 465 if ( isset($file['error']) )466 die( $file['error']);497 $overrides = array( 'test_form' => false ); 498 $file = wp_handle_upload( $_FILES['import'], $overrides ); 467 499 500 if ( isset( $file['error'] ) ) 501 wp_die( $file['error'], __( 'Image Upload Error' ) ); 502 468 503 $url = $file['url']; 469 504 $type = $file['type']; 470 505 $file = $file['file']; … … 472 507 473 508 // Construct the object array 474 509 $object = array( 475 'post_title' => $filename, 476 'post_content' => $url, 477 'post_mime_type' => $type, 478 'guid' => $url); 510 'post_title' => $filename, 511 'post_content' => __( 'This is your header image, if you delete it, you will lost your header image.' ), 512 'post_mime_type' => $type, 513 'guid' => $url 514 ); 479 515 480 516 // Save the data 481 $id = wp_insert_attachment( $object, $file);517 $id = wp_insert_attachment( $object, $file ); 482 518 483 list( $width, $height, $type, $attr) = getimagesize( $file );519 list( $width, $height, $type, $attr ) = getimagesize( $file ); 484 520 485 521 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) { 486 522 // Add the meta-data 487 523 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 488 524 489 set_theme_mod( 'header_image', esc_url($url));490 do_action( 'wp_create_file_in_uploads', $file, $id); // For replication525 set_theme_mod( 'header_image', esc_url( $url ) ); 526 do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication 491 527 return $this->finished(); 492 528 } elseif ( $width > HEADER_IMAGE_WIDTH ) { 493 529 $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)); 530 $transparency = ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] ) ? true : false; 531 $image = wp_crop_image( $file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace( basename( $file ), 'midsize-'.basename( $file ), $file ), $transparency ); 495 532 if ( is_wp_error( $image ) ) 496 533 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 497 534 498 $image = apply_filters( 'wp_create_file_in_uploads', $image, $id); // For replication535 $image = apply_filters( 'wp_create_file_in_uploads', $image, $id ); // For replication 499 536 500 $url = str_replace( basename($url), basename($image), $url);537 $url = str_replace( basename( $url ), basename( $image ), $url ); 501 538 $width = $width / $oitar; 502 539 $height = $height / $oitar; 503 540 } else { 504 541 $oitar = 1; 505 542 } 543 544 // Check for transparency 545 $transparency = 0; 546 if ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] ) 547 $transparency = 1; 506 548 ?> 507 549 508 550 <div class="wrap"> 551 <?php screen_icon(); ?> 552 <h2><?php _e( 'Crop Header Image' ); ?></h2> 509 553 510 <form method="POST" action="<?php echo esc_attr(add_query_arg('step', 3)) ?>"> 554 <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 3 ) ); ?>"> 555 <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p> 556 <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.' ); ?></strong></p> 557 <p><?php printf( __( 'Images smaller then <strong>%1$d x %2$d pixels</strong> will be zoomed.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT ); ?></p> 558 <div id="crop_image" style="position: relative"> 559 <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /> 560 </div> 511 561 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 562 <input type="hidden" name="x1" id="x1" value="0"/> 563 <input type="hidden" name="y1" id="y1" value="0"/> 564 <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/> 565 <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/> 566 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" /> 567 <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" /> 568 <input type="hidden" name="transparency" id="transparency" value="<?php echo esc_attr( $transparency ); ?>"/> 569 <?php wp_nonce_field( 'custom-header' ) ?> 570 <p class="submit"><input type="submit" value="<?php esc_attr_e( 'Crop Header' ); ?>" /></p> 530 571 </form> 531 572 </div> 532 573 <?php … … 538 579 * @since 2.1.0 539 580 */ 540 581 function step_3() { 541 check_admin_referer('custom-header'); 582 check_admin_referer( 'custom-header' ); 583 542 584 if ( $_POST['oitar'] > 1 ) { 543 585 $_POST['x1'] = $_POST['x1'] * $_POST['oitar']; 544 586 $_POST['y1'] = $_POST['y1'] * $_POST['oitar']; … … 546 588 $_POST['height'] = $_POST['height'] * $_POST['oitar']; 547 589 } 548 590 591 $transparency = ( isset( $_POST['transparency'] ) && 1 == $_POST['transparency'] ) ? true : false; 592 549 593 $original = get_attached_file( $_POST['attachment_id'] ); 550 594 551 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT); 595 // Crop the image 596 $cropped = wp_crop_image( $_POST['attachment_id'], $x1, $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, false, false, $transparency ); 552 597 if ( is_wp_error( $cropped ) ) 553 wp_die( __( 'Image could not be processed. 598 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 554 599 555 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication600 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $_POST['attachment_id'] ); // For replication 556 601 557 $parent = get_post( $_POST['attachment_id']);602 $parent = get_post( $_POST['attachment_id'] ); 558 603 $parent_url = $parent->guid; 559 $url = str_replace( basename($parent_url), basename($cropped), $parent_url);604 $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); 560 605 561 606 // Construct the object array 562 607 $object = array( 563 608 'ID' => $_POST['attachment_id'], 564 'post_title' => basename( $cropped),565 'post_content' => $url,609 'post_title' => basename( $cropped ), 610 'post_content' => __( 'This is your header image, if you delete it, you will lost your header image.' ), 566 611 'post_mime_type' => 'image/jpeg', 567 612 'guid' => $url 568 613 ); 569 614 570 615 // Update the attachment 571 wp_insert_attachment( $object, $cropped);616 wp_insert_attachment( $object, $cropped ); 572 617 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) ); 573 618 574 set_theme_mod( 'header_image', $url);619 set_theme_mod( 'header_image', $url ); 575 620 576 621 // cleanup 577 $medium = str_replace( basename($original), 'midsize-'.basename($original), $original);622 $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original ); 578 623 @unlink( apply_filters( 'wp_delete_file', $medium ) ); 579 624 @unlink( apply_filters( 'wp_delete_file', $original ) ); 580 625 … … 587 632 * @since 2.1.0 588 633 */ 589 634 function finished() { 590 $ _GET['updated'] = 1;591 635 $this->updated = true; 636 $this->step_1(); 592 637 } 593 638 594 639 /** … … 597 642 * @since 2.1.0 598 643 */ 599 644 function admin_page() { 600 if ( ! current_user_can('switch_themes') ) 601 wp_die(__('You do not have permission to customize headers.')); 645 if ( ! current_user_can( 'edit_theme_options' ) ) 646 wp_die( __( 'You do not have permission to customize headers.' ) ); 647 602 648 $step = $this->step(); 603 649 if ( 1 == $step ) 604 650 $this->step_1(); -
wp-admin/includes/image.php
41 41 * @param int $dst_h The destination height. 42 42 * @param int $src_abs Optional. If the source crop points are absolute. 43 43 * @param string $dst_file Optional. The destination file to write to. 44 * @param bool $dst_transparency Optional. The destination file type. Transparency = PNG. 44 45 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure. 45 46 */ 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 ) {47 function 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 ) { 47 48 if ( is_numeric( $src_file ) ) // Handle int as attachment ID 48 49 $src_file = get_attached_file( $src_file ); 49 50 … … 63 64 imageantialias( $dst, true ); 64 65 65 66 imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 66 67 67 imagedestroy( $src ); // Free up memory 68 68 69 69 if ( ! $dst_file ) 70 70 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); 71 71 72 $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); 72 $type = '.jpeg'; 73 if ( $dst_transparency ) 74 $type = '.png'; 73 75 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) ) 75 79 return $dst_file; 80 elseif ( imagejpeg( $dst, $dst_file) ) 81 return $dst_file; 76 82 else 77 83 return false; 78 84 } -
wp-content/themes/twentyten/functions.php
112 112 set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true ); 113 113 114 114 // Don't support text inside the header image. 115 define( 'NO_HEADER_TEXT', true );115 //define( 'NO_HEADER_TEXT', true ); 116 116 117 117 // Add a way for the custom header to be styled in the admin panel that controls 118 118 // custom headers. See twentyten_admin_header_style(), below. … … 177 177 function twentyten_admin_header_style() { 178 178 ?> 179 179 <style type="text/css"> 180 #headimg { 181 height: <?php echo HEADER_IMAGE_HEIGHT; ?>px; 182 width: <?php echo HEADER_IMAGE_WIDTH; ?>px; 180 /* Shows the same border as on front page */ 181 .appearance_page_custom-header #headimg { 182 border-bottom: 1px solid #000000; 183 border-top: 4px solid #000000; 183 184 } 184 #headimg h1, #headimg #desc {185 display: none;186 }187 185 </style> 188 186 <?php 189 187 }