Ticket #17240: 17240.2.diff
File 17240.2.diff, 8.6 KB (added by , 14 years ago) |
---|
-
wp-includes/theme.php
1427 1427 */ 1428 1428 function get_header_image() { 1429 1429 $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : ''; 1430 1431 1430 $url = get_theme_mod( 'header_image', $default ); 1432 1431 1432 if ( is_random_header_image() ) { 1433 $url = get_random_header_image(); 1434 } 1435 1433 1436 if ( is_ssl() ) 1434 1437 $url = str_replace( 'http://', 'https://', $url ); 1435 1438 else … … 1439 1442 } 1440 1443 1441 1444 /** 1445 * Get random header image from registered images in theme. 1446 * 1447 * @since 3.2 1448 * 1449 * @return string Path to header image 1450 */ 1451 function get_random_header_image() { 1452 global $_wp_default_headers; 1453 1454 $header_image_mod = get_theme_mod( 'header_image', '' ); 1455 $headers = array(); 1456 1457 if ( 'random-uploaded-image' == $header_image_mod ) { 1458 $headers = get_uploaded_header_images(); 1459 } elseif ( !empty( $_wp_default_headers ) ) { 1460 $headers = $_wp_default_headers; 1461 } 1462 1463 if ( empty( $headers ) ) 1464 return ''; 1465 1466 $random_image = array_rand( $headers ); 1467 $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() ); 1468 1469 return $header_url; 1470 } 1471 1472 /** 1473 * Check if random header image is in use. 1474 * 1475 * Always true if user expressly chooses the option in Appearance > Header. 1476 * Also true if theme has multiple header images registered and no specific header image is chosen. 1477 * 1478 * @since 3.2 1479 * @uses HEADER_IMAGE 1480 * 1481 * @param string $type The random pool to use. any|default|uploaded 1482 * @return boolean 1483 */ 1484 function is_random_header_image( $type = 'any' ) { 1485 $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : ''; 1486 $header_image_mod = get_theme_mod( 'header_image', $default ); 1487 1488 if ( 'any' == $type ) { 1489 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) ) 1490 return true; 1491 } else { 1492 if ( "random-$type-image" == $header_image_mod ) 1493 return true; 1494 } 1495 1496 return false; 1497 } 1498 1499 /** 1442 1500 * Display header image path. 1443 1501 * 1444 1502 * @since 2.1.0 … … 1448 1506 } 1449 1507 1450 1508 /** 1509 * Get the header images uploaded for the current theme. 1510 * 1511 * @since 3.2.0 1512 * 1513 * @return array 1514 */ 1515 function get_uploaded_header_images() { 1516 $header_images = array(); 1517 1518 // @todo caching 1519 $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) ); 1520 1521 if ( empty( $headers ) ) 1522 return array(); 1523 1524 foreach ( (array) $headers as $header ) { 1525 $url = $header->guid; 1526 $header = basename($url); 1527 $header_images[$header] = array(); 1528 $header_images[$header]['url'] = $url; 1529 $header_images[$header]['thumbnail_url'] = $url; 1530 $header_images[$header]['uploaded'] = true; 1531 } 1532 1533 return $header_images; 1534 } 1535 1536 /** 1451 1537 * Add callbacks for image header display. 1452 1538 * 1453 1539 * The parameter $header_callback callback will be required to display the -
wp-admin/custom-header.php
42 42 */ 43 43 var $default_headers = array(); 44 44 45 45 46 /** 47 * Holds custom headers uploaded by the user 48 * 49 * @var array 50 * @since 3.2.0 51 * @access private 52 */ 53 var $uploaded_headers = array(); 54 55 /** 46 56 * Holds the page menu hook. 47 57 * 48 58 * @var string … … 199 209 } 200 210 } 201 211 202 if ( isset( $_POST['default-header']) ) {212 if ( isset( $_POST['default-header'] ) ) { 203 213 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); 204 $this->process_default_headers(); 205 if ( isset($this->default_headers[$_POST['default-header']]) ) 206 set_theme_mod('header_image', esc_url($this->default_headers[$_POST['default-header']]['url'])); 214 if ( 'random-default-image' == $_POST['default-header'] ) { 215 set_theme_mod( 'header_image', 'random-default-image' ); 216 } elseif ( 'random-uploaded-image' == $_POST['default-header'] ) { 217 set_theme_mod( 'header_image', 'random-uploaded-image' ); 218 } else { 219 $this->process_default_headers(); 220 $uploaded = get_uploaded_header_images(); 221 if ( isset( $uploaded[$_POST['default-header']] ) ) 222 set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) ); 223 elseif ( isset( $this->default_headers[$_POST['default-header']] ) ) 224 set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) ); 225 } 207 226 } 208 227 } 209 228 … … 226 245 $this->default_headers[$header]['url'] = sprintf( $this->default_headers[$header]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() ); 227 246 $this->default_headers[$header]['thumbnail_url'] = sprintf( $this->default_headers[$header]['thumbnail_url'], get_template_directory_uri(), get_stylesheet_directory_uri() ); 228 247 } 248 229 249 } 230 250 231 251 /** 232 252 * Display UI for selecting one of several default headers. 233 253 * 254 * Show the random image option if this theme has multiple header images. 255 * Random image option is on by default if no header has been set. 256 * 234 257 * @since 3.0.0 235 258 */ 236 function show_ default_header_selector() {259 function show_header_selector( $type = 'default' ) { 237 260 echo '<div id="available-headers">'; 238 foreach ( $this->default_headers as $header_key => $header ) { 261 if ( 'default' == $type ) { 262 $headers = $this->default_headers; 263 } else { 264 $headers = get_uploaded_header_images(); 265 $type = 'uploaded'; 266 } 267 268 foreach ( $headers as $header_key => $header ) { 239 269 $header_thumbnail = $header['thumbnail_url']; 240 270 $header_url = $header['url']; 241 $header_desc = $header['description'];271 $header_desc = empty( $header['description'] ) ? '' : $header['description']; 242 272 echo '<div class="default-header">'; 243 echo '<label><input name="default-header" type="radio" value="' . esc_attr($header_key) . '" ' . checked($header_url, get_theme_mod( 'header_image' ), false) . ' />'; 244 echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr($header_desc) .'" title="' . esc_attr($header_desc) .'" /></label>'; 273 echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />'; 274 $width = ''; 275 if ( !empty( $header['uploaded'] ) ) 276 $width = ' width="230"'; 277 echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>'; 245 278 echo '</div>'; 246 279 } 280 if ( 1 < count( $headers ) ) { 281 echo '<div class="default-header">'; 282 echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />'; 283 echo __( '<strong>Random:</strong> Show a different image on each page.' ); 284 echo '</label>'; 285 echo '</div>'; 286 } 247 287 echo '<div class="clear"></div></div>'; 248 288 } 249 289 … … 480 520 <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>"> 481 521 <table class="form-table"> 482 522 <tbody> 483 <?php if ( ! empty( $this->default_headers) ) : ?>523 <?php if ( get_uploaded_header_images() ) : ?> 484 524 <tr valign="top"> 525 <th scope="row"><?php _e( 'Uploaded Images' ); ?></th> 526 <td> 527 <p><?php _e( 'You can use one of your previously uploaded headers.' ) ?></p> 528 <?php 529 $this->show_header_selector( 'uploaded' ); 530 ?> 531 </td> 532 </tr> 533 <?php endif; 534 if ( ! empty( $this->default_headers ) ) : ?> 535 <tr valign="top"> 485 536 <th scope="row"><?php _e( 'Default Images' ); ?></th> 486 537 <td> 487 538 <?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?> … … 490 541 <p><?php _e( 'You can use one of these cool headers.' ) ?> 491 542 <?php endif; ?> 492 543 <?php 493 $this->show_ default_header_selector();544 $this->show_header_selector( 'default' ); 494 545 ?> 495 546 </td> 496 547 </tr> 497 548 <?php endif; 498 499 if ( get_header_image() ) : ?> 549 if ( get_header_image() && !is_random_header_image() ) : ?> 500 550 <tr valign="top"> 501 551 <th scope="row"><?php _e( 'Remove Image' ); ?></th> 502 552 <td> … … 506 556 </tr> 507 557 <?php endif; 508 558 509 if ( defined( 'HEADER_IMAGE' ) ) : ?>559 if ( defined( 'HEADER_IMAGE' ) && !is_random_header_image() ) : ?> 510 560 <tr valign="top"> 511 561 <th scope="row"><?php _e( 'Reset Image' ); ?></th> 512 562 <td> … … 693 743 // Update the attachment 694 744 wp_insert_attachment($object, $cropped); 695 745 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) ); 746 update_post_meta( $_POST['attachment_id'], '_wp_attachment_is_custom_header', get_option('stylesheet' ) ); 696 747 697 748 set_theme_mod('header_image', $url); 698 749