Make WordPress Core


Ignore:
Timestamp:
02/02/2012 11:35:37 PM (13 years ago)
Author:
ryan
Message:

Allow flexible sizes for custom header uploads. Round 1. Props aaroncampbell, sabreuse. see #17242

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/theme.php

    r19687 r19815  
    13521352        return apply_filters( "theme_mod_$name", $mods[ $name ] );
    13531353
    1354     return apply_filters( "theme_mod_$name", sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ) );
     1354    if ( is_string( $default ) )
     1355        $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1356
     1357    return apply_filters( "theme_mod_$name", $default );
    13551358}
    13561359
     
    14581461
    14591462/**
    1460  * Get random header image from registered images in theme.
     1463 * Get random header image data from registered images in theme.
     1464 *
     1465 * @since 3.4.0
     1466 *
     1467 * @access private
     1468 *
     1469 * @return string Path to header image
     1470 */
     1471
     1472function _get_random_header_data() {
     1473    static $_wp_random_header;
     1474
     1475    if ( empty( $_wp_random_header ) ) {
     1476        global $_wp_default_headers;
     1477        $header_image_mod = get_theme_mod( 'header_image', '' );
     1478        $headers = array();
     1479
     1480        if ( 'random-uploaded-image' == $header_image_mod )
     1481            $headers = get_uploaded_header_images();
     1482        elseif ( ! empty( $_wp_default_headers ) ) {
     1483            if ( 'random-default-image' == $header_image_mod ) {
     1484                $headers = $_wp_default_headers;
     1485            } else {
     1486                $is_random = get_theme_support( 'custom-header' );
     1487                if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
     1488                    $headers = $_wp_default_headers;
     1489            }
     1490        }
     1491
     1492        if ( empty( $headers ) )
     1493            return stdClass();
     1494
     1495        $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
     1496
     1497        $_wp_random_header->url =  sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1498        $_wp_random_header->thumbnail_url =  sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1499    }
     1500    return $_wp_random_header;
     1501}
     1502
     1503/**
     1504 * Get random header image url from registered images in theme.
    14611505 *
    14621506 * @since 3.2.0
     
    14641508 * @return string Path to header image
    14651509 */
     1510
    14661511function get_random_header_image() {
    1467     global $_wp_default_headers;
    1468 
    1469     $header_image_mod = get_theme_mod( 'header_image', '' );
    1470     $headers = array();
    1471 
    1472     if ( 'random-uploaded-image' == $header_image_mod )
    1473         $headers = get_uploaded_header_images();
    1474     elseif ( ! empty( $_wp_default_headers ) ) {
    1475         if ( 'random-default-image' == $header_image_mod ) {
    1476             $headers = $_wp_default_headers;
    1477         } else {
    1478             $is_random = get_theme_support( 'custom-header' );
    1479             if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
    1480                 $headers = $_wp_default_headers;
    1481         }
    1482     }
    1483 
    1484     if ( empty( $headers ) )
     1512    $random_image = _get_random_header_data();
     1513    if ( empty( $random_image->url ) )
    14851514        return '';
    1486 
    1487     $random_image = array_rand( $headers );
    1488     $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
    1489 
    1490     return $header_url;
     1515    return $random_image->url;
    14911516}
    14921517
     
    15481573    foreach ( (array) $headers as $header ) {
    15491574        $url = esc_url_raw( $header->guid );
    1550         $header = basename($url);
    1551         $header_images[$header] = array();
    1552         $header_images[$header]['url'] =  $url;
    1553         $header_images[$header]['thumbnail_url'] =  $url;
    1554         $header_images[$header]['uploaded'] = true;
     1575        $header_data = wp_get_attachment_metadata( $header->ID );
     1576        $header_index = basename($url);
     1577        $header_images[$header_index] = array();
     1578        $header_images[$header_index]['attachment_id'] =  $header->ID;
     1579        $header_images[$header_index]['url'] =  $url;
     1580        $header_images[$header_index]['thumbnail_url'] =  $url;
     1581        $header_images[$header_index]['width'] = $header_data['width'];
     1582        $header_images[$header_index]['height'] = $header_data['height'];
    15551583    }
    15561584
    15571585    return $header_images;
     1586}
     1587
     1588/**
     1589 * Get the header image data.
     1590 *
     1591 * @since 3.4.0
     1592 *
     1593 * @return object
     1594 */
     1595function get_current_header_data() {
     1596    $data = is_random_header_image()? _get_random_header_data() : get_theme_mod( 'header_image_data' );
     1597    $default = array(
     1598        'url'           => '',
     1599        'thumbnail_url' => '',
     1600        'width'         => '',
     1601        'height'        => '',
     1602    );
     1603    return (object) wp_parse_args( $data, $default );
     1604}
     1605
     1606/**
     1607 * Get the header image width.
     1608 *
     1609 * @since 3.4.0
     1610 *
     1611 * @return int
     1612 */
     1613function get_header_image_width() {
     1614    return empty( get_current_header_data()->width )? HEADER_IMAGE_WIDTH : get_current_header_data()->width;
     1615}
     1616
     1617/**
     1618 * Get the header image height.
     1619 *
     1620 * @since 3.4.0
     1621 *
     1622 * @return int
     1623 */
     1624function get_header_image_height() {
     1625    return empty( get_current_header_data()->height )? HEADER_IMAGE_HEIGHT : get_current_header_data()->height;
    15581626}
    15591627
     
    19562024            return in_array( $post_format, $_wp_theme_features[$feature][0] );
    19572025            break;
     2026
     2027        case 'custom-header':
     2028            // specific custom header capabilities can be registered by passing
     2029            // an array to add_theme_support()
     2030            $header_support = $args[0];
     2031            return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
     2032            break;
    19582033    }
    19592034
Note: See TracChangeset for help on using the changeset viewer.