WordPress.org

Make WordPress Core

Changeset 17757


Ignore:
Timestamp:
04/28/2011 11:02:16 PM (7 years ago)
Author:
ryan
Message:

Allow selecting previously uploader headers and randomly serving previously uploaded or default headers. Props lancewillett. see #17240

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/custom-header.php

    r17748 r17757  
    4242     */
    4343    var $default_headers = array();
     44
     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();
    4454
    4555    /**
     
    200210        }
    201211
    202         if ( isset($_POST['default-header']) ) {
     212        if ( isset( $_POST['default-header'] ) ) {
    203213            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            }
    207226        }
    208227    }
     
    227246            $this->default_headers[$header]['thumbnail_url'] =  sprintf( $this->default_headers[$header]['thumbnail_url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
    228247        }
     248
    229249    }
    230250
     
    232252     * Display UI for selecting one of several default headers.
    233253     *
     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     *
    234257     * @since 3.0.0
    235258     */
    236     function show_default_header_selector() {
     259    function show_header_selector( $type = 'default' ) {
    237260        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 ) {
    239269            $header_thumbnail = $header['thumbnail_url'];
    240270            $header_url = $header['url'];
    241             $header_desc = $header['description'];
     271            $header_desc = empty( $header['description'] ) ? '' : $header['description'];
    242272            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>';
     278            echo '</div>';
     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>';
    245285            echo '</div>';
    246286        }
     
    481521<table class="form-table">
    482522<tbody>
    483     <?php if ( ! empty( $this->default_headers ) ) : ?>
     523    <?php if ( get_uploaded_header_images() ) : ?>
     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 ) ) : ?>
    484535<tr valign="top">
    485536<th scope="row"><?php _e( 'Default Images' ); ?></th>
     
    491542<?php endif; ?>
    492543    <?php
    493         $this->show_default_header_selector();
     544        $this->show_header_selector( 'default' );
    494545    ?>
    495546</td>
    496547</tr>
    497548    <?php endif;
    498 
    499     if ( get_header_image() ) : ?>
     549    if ( get_header_image() && !is_random_header_image() ) : ?>
    500550<tr valign="top">
    501551<th scope="row"><?php _e( 'Remove Image' ); ?></th>
     
    507557    <?php endif;
    508558
    509     if ( defined( 'HEADER_IMAGE' ) ) : ?>
     559    if ( defined( 'HEADER_IMAGE' ) && !is_random_header_image() ) : ?>
    510560<tr valign="top">
    511561<th scope="row"><?php _e( 'Reset Image' ); ?></th>
     
    694744        wp_insert_attachment($object, $cropped);
    695745        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' ) );
    696747
    697748        set_theme_mod('header_image', $url);
  • trunk/wp-includes/theme.php

    r17316 r17757  
    14281428function get_header_image() {
    14291429    $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
    1430 
    14311430    $url = get_theme_mod( 'header_image', $default );
     1431
     1432    if ( is_random_header_image() )
     1433        $url = get_random_header_image();
    14321434
    14331435    if ( is_ssl() )
     
    14401442
    14411443/**
     1444 * Get random header image from registered images in theme.
     1445 *
     1446 * @since 3.2
     1447 *
     1448 * @return string Path to header image
     1449 */
     1450function get_random_header_image() {
     1451    global $_wp_default_headers;
     1452
     1453    $header_image_mod = get_theme_mod( 'header_image', '' );
     1454    $headers = array();
     1455
     1456    if ( 'random-uploaded-image' == $header_image_mod )
     1457        $headers = get_uploaded_header_images();
     1458    elseif ( ! empty( $_wp_default_headers ) )
     1459        $headers = $_wp_default_headers;
     1460
     1461    if ( empty( $headers ) )
     1462        return '';
     1463
     1464    $random_image = array_rand( $headers );
     1465    $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
     1466
     1467    return $header_url;
     1468}
     1469
     1470/**
     1471 * Check if random header image is in use.
     1472 *
     1473 * Always true if user expressly chooses the option in Appearance > Header.
     1474 * Also true if theme has multiple header images registered and no specific header image is chosen.
     1475 *
     1476 * @since 3.2
     1477 * @uses HEADER_IMAGE
     1478 *
     1479 * @param string $type The random pool to use. any|default|uploaded
     1480 * @return boolean
     1481 */
     1482function is_random_header_image( $type = 'any' ) {
     1483    $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
     1484    $header_image_mod = get_theme_mod( 'header_image', $default );
     1485
     1486    if ( 'any' == $type ) {
     1487        if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
     1488            return true;
     1489    } else {
     1490        if ( "random-$type-image" == $header_image_mod )
     1491            return true;
     1492    }
     1493
     1494    return false;
     1495}
     1496
     1497/**
    14421498 * Display header image path.
    14431499 *
     
    14461502function header_image() {
    14471503    echo get_header_image();
     1504}
     1505
     1506/**
     1507 * Get the header images uploaded for the current theme.
     1508 *
     1509 * @since 3.2.0
     1510 *
     1511 * @return array
     1512 */
     1513function get_uploaded_header_images() {
     1514    $header_images = array();
     1515
     1516    // @todo caching
     1517    $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
     1518
     1519    if ( empty( $headers ) )
     1520        return array();
     1521
     1522    foreach ( (array) $headers as $header ) {
     1523        $url = $header->guid;
     1524        $header = basename($url);
     1525        $header_images[$header] = array();
     1526        $header_images[$header]['url'] =  $url;
     1527        $header_images[$header]['thumbnail_url'] =  $url;
     1528        $header_images[$header]['uploaded'] = true;
     1529    }
     1530
     1531    return $header_images;
    14481532}
    14491533
Note: See TracChangeset for help on using the changeset viewer.