<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[17757] trunk:
Allow selecting previously uploader headers and randomly serving previously uploaded or default headers
.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/17757">17757</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2011-04-28 23:02:16 +0000 (Thu, 28 Apr 2011)</dd>
</dl>
<h3>Log Message</h3>
<pre>Allow selecting previously uploader headers and randomly serving previously uploaded or default headers. Props lancewillett. see <a href="http://trac.wordpress.org/ticket/17240">#17240</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmincustomheaderphp">trunk/wp-admin/custom-header.php</a></li>
<li><a href="#trunkwpincludesthemephp">trunk/wp-includes/theme.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmincustomheaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/custom-header.php (17756 => 17757)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/custom-header.php        2011-04-28 22:15:36 UTC (rev 17756)
+++ trunk/wp-admin/custom-header.php        2011-04-28 23:02:16 UTC (rev 17757)
</span><span class="lines">@@ -42,7 +42,17 @@
</span><span class="cx">          */
</span><span class="cx">         var $default_headers = array();
</span><span class="cx">
</span><ins>+
</ins><span class="cx">         /**
</span><ins>+         * Holds custom headers uploaded by the user
+         *
+         * @var array
+         * @since 3.2.0
+         * @access private
+         */
+        var $uploaded_headers = array();
+
+        /**
</ins><span class="cx">          * Holds the page menu hook.
</span><span class="cx">          *
</span><span class="cx">          * @var string
</span><span class="lines">@@ -199,11 +209,20 @@
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                if ( isset($_POST['default-header']) ) {
</del><ins>+                if ( isset( $_POST['default-header'] ) ) {
</ins><span class="cx">                         check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
</span><del>-                        $this->process_default_headers();
-                        if ( isset($this->default_headers[$_POST['default-header']]) )
-                                set_theme_mod('header_image', esc_url($this->default_headers[$_POST['default-header']]['url']));
</del><ins>+                        if ( 'random-default-image' == $_POST['default-header'] ) {
+                                set_theme_mod( 'header_image', 'random-default-image' );
+                        } elseif ( 'random-uploaded-image' == $_POST['default-header'] ) {
+                                set_theme_mod( 'header_image', 'random-uploaded-image' );
+                        } else {
+                                $this->process_default_headers();
+                                $uploaded = get_uploaded_header_images();
+                                if ( isset( $uploaded[$_POST['default-header']] ) )
+                                        set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) );
+                                elseif ( isset( $this->default_headers[$_POST['default-header']] ) )
+                                        set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
+                        }
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -226,24 +245,45 @@
</span><span class="cx">                         $this->default_headers[$header]['url'] = sprintf( $this->default_headers[$header]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
</span><span class="cx">                         $this->default_headers[$header]['thumbnail_url'] = sprintf( $this->default_headers[$header]['thumbnail_url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
</span><span class="cx">                 }
</span><ins>+
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><span class="cx">          * Display UI for selecting one of several default headers.
</span><span class="cx">          *
</span><ins>+         * Show the random image option if this theme has multiple header images.
+         * Random image option is on by default if no header has been set.
+         *
</ins><span class="cx">          * @since 3.0.0
</span><span class="cx">          */
</span><del>-        function show_default_header_selector() {
</del><ins>+        function show_header_selector( $type = 'default' ) {
</ins><span class="cx">                 echo '<div id="available-headers">';
</span><del>-                foreach ( $this->default_headers as $header_key => $header ) {
</del><ins>+                if ( 'default' == $type ) {
+                        $headers = $this->default_headers;
+                } else {
+                        $headers = get_uploaded_header_images();
+                        $type = 'uploaded';
+                }
+
+                foreach ( $headers as $header_key => $header ) {
</ins><span class="cx">                         $header_thumbnail = $header['thumbnail_url'];
</span><span class="cx">                         $header_url = $header['url'];
</span><del>-                        $header_desc = $header['description'];
</del><ins>+                        $header_desc = empty( $header['description'] ) ? '' : $header['description'];
</ins><span class="cx">                         echo '<div class="default-header">';
</span><del>-                        echo '<label><input name="default-header" type="radio" value="' . esc_attr($header_key) . '" ' . checked($header_url, get_theme_mod( 'header_image' ), false) . ' />';
-                        echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr($header_desc) .'" title="' . esc_attr($header_desc) .'" /></label>';
</del><ins>+                        echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
+                        $width = '';
+                        if ( !empty( $header['uploaded'] ) )
+                                $width = ' width="230"';
+                        echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>';
</ins><span class="cx">                         echo '</div>';
</span><span class="cx">                 }
</span><ins>+                if ( 1 < count( $headers ) ) {
+                        echo '<div class="default-header">';
+                        echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
+                        echo __( '<strong>Random:</strong> Show a different image on each page.' );
+                        echo '</label>';
+                        echo '</div>';
+                }
</ins><span class="cx">                 echo '<div class="clear"></div></div>';
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -480,8 +520,19 @@
</span><span class="cx"> <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>">
</span><span class="cx"> <table class="form-table">
</span><span class="cx"> <tbody>
</span><del>-        <?php if ( ! empty( $this->default_headers ) ) : ?>
</del><ins>+        <?php if ( get_uploaded_header_images() ) : ?>
</ins><span class="cx"> <tr valign="top">
</span><ins>+<th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
+<td>
+        <p><?php _e( 'You can use one of your previously uploaded headers.' ) ?></p>
+        <?php
+                $this->show_header_selector( 'uploaded' );
+        ?>
+</td>
+</tr>
+        <?php endif;
+        if ( ! empty( $this->default_headers ) ) : ?>
+<tr valign="top">
</ins><span class="cx"> <th scope="row"><?php _e( 'Default Images' ); ?></th>
</span><span class="cx"> <td>
</span><span class="cx"> <?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?>
</span><span class="lines">@@ -490,13 +541,12 @@
</span><span class="cx">         <p><?php _e( 'You can use one of these cool headers.' ) ?>
</span><span class="cx"> <?php endif; ?>
</span><span class="cx">         <?php
</span><del>-                $this->show_default_header_selector();
</del><ins>+                $this->show_header_selector( 'default' );
</ins><span class="cx">         ?>
</span><span class="cx"> </td>
</span><span class="cx"> </tr>
</span><span class="cx">         <?php endif;
</span><del>-
-        if ( get_header_image() ) : ?>
</del><ins>+        if ( get_header_image() && !is_random_header_image() ) : ?>
</ins><span class="cx"> <tr valign="top">
</span><span class="cx"> <th scope="row"><?php _e( 'Remove Image' ); ?></th>
</span><span class="cx"> <td>
</span><span class="lines">@@ -506,7 +556,7 @@
</span><span class="cx"> </tr>
</span><span class="cx">         <?php endif;
</span><span class="cx">
</span><del>-        if ( defined( 'HEADER_IMAGE' ) ) : ?>
</del><ins>+        if ( defined( 'HEADER_IMAGE' ) && !is_random_header_image() ) : ?>
</ins><span class="cx"> <tr valign="top">
</span><span class="cx"> <th scope="row"><?php _e( 'Reset Image' ); ?></th>
</span><span class="cx"> <td>
</span><span class="lines">@@ -693,6 +743,7 @@
</span><span class="cx">                 // Update the attachment
</span><span class="cx">                 wp_insert_attachment($object, $cropped);
</span><span class="cx">                 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
</span><ins>+                update_post_meta( $_POST['attachment_id'], '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
</ins><span class="cx">
</span><span class="cx">                 set_theme_mod('header_image', $url);
</span><span class="cx">
</span></span></pre></div>
<a id="trunkwpincludesthemephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/theme.php (17756 => 17757)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/theme.php        2011-04-28 22:15:36 UTC (rev 17756)
+++ trunk/wp-includes/theme.php        2011-04-28 23:02:16 UTC (rev 17757)
</span><span class="lines">@@ -1427,9 +1427,11 @@
</span><span class="cx"> */
</span><span class="cx"> function get_header_image() {
</span><span class="cx">         $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
</span><del>-
</del><span class="cx">         $url = get_theme_mod( 'header_image', $default );
</span><span class="cx">
</span><ins>+        if ( is_random_header_image() )
+                $url = get_random_header_image();
+
</ins><span class="cx">         if ( is_ssl() )
</span><span class="cx">                 $url = str_replace( 'http://', 'https://', $url );
</span><span class="cx">         else
</span><span class="lines">@@ -1439,6 +1441,60 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Get random header image from registered images in theme.
+ *
+ * @since 3.2
+ *
+ * @return string Path to header image
+ */
+function get_random_header_image() {
+        global $_wp_default_headers;
+
+        $header_image_mod = get_theme_mod( 'header_image', '' );
+        $headers = array();
+
+        if ( 'random-uploaded-image' == $header_image_mod )
+                $headers = get_uploaded_header_images();
+        elseif ( ! empty( $_wp_default_headers ) )
+                $headers = $_wp_default_headers;
+
+        if ( empty( $headers ) )
+                return '';
+
+        $random_image = array_rand( $headers );
+        $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
+
+        return $header_url;
+}
+
+/**
+ * Check if random header image is in use.
+ *
+ * Always true if user expressly chooses the option in Appearance > Header.
+ * Also true if theme has multiple header images registered and no specific header image is chosen.
+ *
+ * @since 3.2
+ * @uses HEADER_IMAGE
+ *
+ * @param string $type The random pool to use. any|default|uploaded
+ * @return boolean
+ */
+function is_random_header_image( $type = 'any' ) {
+        $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
+        $header_image_mod = get_theme_mod( 'header_image', $default );
+
+        if ( 'any' == $type ) {
+                if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
+                        return true;
+        } else {
+                if ( "random-$type-image" == $header_image_mod )
+                        return true;
+        }
+
+        return false;
+}
+
+/**
</ins><span class="cx"> * Display header image path.
</span><span class="cx"> *
</span><span class="cx"> * @since 2.1.0
</span><span class="lines">@@ -1448,6 +1504,34 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Get the header images uploaded for the current theme.
+ *
+ * @since 3.2.0
+ *
+ * @return array
+ */
+function get_uploaded_header_images() {
+        $header_images = array();
+
+        // @todo caching
+        $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
+
+        if ( empty( $headers ) )
+                return array();
+
+        foreach ( (array) $headers as $header ) {
+                $url = $header->guid;
+                $header = basename($url);
+                $header_images[$header] = array();
+                $header_images[$header]['url'] = $url;
+                $header_images[$header]['thumbnail_url'] = $url;
+                $header_images[$header]['uploaded'] = true;
+        }
+
+        return $header_images;
+}
+
+/**
</ins><span class="cx"> * Add callbacks for image header display.
</span><span class="cx"> *
</span><span class="cx"> * The parameter $header_callback callback will be required to display the
</span></span></pre>
</div>
</div>
</body>
</html>