<!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-&gt;process_default_headers();
-                        if ( isset($this-&gt;default_headers[$_POST['default-header']]) )
-                                set_theme_mod('header_image', esc_url($this-&gt;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-&gt;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-&gt;default_headers[$_POST['default-header']] ) )
+                                        set_theme_mod( 'header_image', esc_url( $this-&gt;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-&gt;default_headers[$header]['url'] =  sprintf( $this-&gt;default_headers[$header]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
</span><span class="cx">                         $this-&gt;default_headers[$header]['thumbnail_url'] =  sprintf( $this-&gt;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 '&lt;div id=&quot;available-headers&quot;&gt;';
</span><del>-                foreach ( $this-&gt;default_headers as $header_key =&gt; $header ) {
</del><ins>+                if ( 'default' == $type ) {
+                        $headers = $this-&gt;default_headers;
+                } else {
+                        $headers = get_uploaded_header_images();
+                        $type = 'uploaded';
+                }
+
+                foreach ( $headers as $header_key =&gt; $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 '&lt;div class=&quot;default-header&quot;&gt;';
</span><del>-                        echo '&lt;label&gt;&lt;input name=&quot;default-header&quot; type=&quot;radio&quot; value=&quot;' . esc_attr($header_key) . '&quot; ' . checked($header_url, get_theme_mod( 'header_image' ), false) . ' /&gt;';
-                        echo '&lt;img src=&quot;' . $header_thumbnail . '&quot; alt=&quot;' . esc_attr($header_desc) .'&quot; title=&quot;' . esc_attr($header_desc) .'&quot; /&gt;&lt;/label&gt;';
</del><ins>+                        echo '&lt;label&gt;&lt;input name=&quot;default-header&quot; type=&quot;radio&quot; value=&quot;' . esc_attr( $header_key ) . '&quot; ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' /&gt;';
+                        $width = '';
+                        if ( !empty( $header['uploaded'] ) )
+                                $width = ' width=&quot;230&quot;';
+                        echo '&lt;img src=&quot;' . $header_thumbnail . '&quot; alt=&quot;' . esc_attr( $header_desc ) .'&quot; title=&quot;' . esc_attr( $header_desc ) . '&quot;' . $width . ' /&gt;&lt;/label&gt;';
</ins><span class="cx">                         echo '&lt;/div&gt;';
</span><span class="cx">                 }
</span><ins>+                if ( 1 &lt; count( $headers ) ) {
+                        echo '&lt;div class=&quot;default-header&quot;&gt;';
+                        echo '&lt;label&gt;&lt;input name=&quot;default-header&quot; type=&quot;radio&quot; value=&quot;random-' . $type . '-image&quot;' . checked( is_random_header_image( $type ), true, false ) . ' /&gt;';
+                        echo __( '&lt;strong&gt;Random:&lt;/strong&gt; Show a different image on each page.' );
+                        echo '&lt;/label&gt;';
+                        echo '&lt;/div&gt;';
+                }
</ins><span class="cx">                 echo '&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;&lt;/div&gt;';
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -480,8 +520,19 @@
</span><span class="cx"> &lt;form method=&quot;post&quot; action=&quot;&lt;?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?&gt;&quot;&gt;
</span><span class="cx"> &lt;table class=&quot;form-table&quot;&gt;
</span><span class="cx"> &lt;tbody&gt;
</span><del>-        &lt;?php if ( ! empty( $this-&gt;default_headers ) ) : ?&gt;
</del><ins>+        &lt;?php if ( get_uploaded_header_images() ) : ?&gt;
</ins><span class="cx"> &lt;tr valign=&quot;top&quot;&gt;
</span><ins>+&lt;th scope=&quot;row&quot;&gt;&lt;?php _e( 'Uploaded Images' ); ?&gt;&lt;/th&gt;
+&lt;td&gt;
+        &lt;p&gt;&lt;?php _e( 'You can use one of your previously uploaded headers.' ) ?&gt;&lt;/p&gt;
+        &lt;?php
+                $this-&gt;show_header_selector( 'uploaded' );
+        ?&gt;
+&lt;/td&gt;
+&lt;/tr&gt;
+        &lt;?php endif;
+        if ( ! empty( $this-&gt;default_headers ) ) : ?&gt;
+&lt;tr valign=&quot;top&quot;&gt;
</ins><span class="cx"> &lt;th scope=&quot;row&quot;&gt;&lt;?php _e( 'Default Images' ); ?&gt;&lt;/th&gt;
</span><span class="cx"> &lt;td&gt;
</span><span class="cx"> &lt;?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?&gt;
</span><span class="lines">@@ -490,13 +541,12 @@
</span><span class="cx">         &lt;p&gt;&lt;?php _e( 'You can use one of these cool headers.' ) ?&gt;
</span><span class="cx"> &lt;?php endif; ?&gt;
</span><span class="cx">         &lt;?php
</span><del>-                $this-&gt;show_default_header_selector();
</del><ins>+                $this-&gt;show_header_selector( 'default' );
</ins><span class="cx">         ?&gt;
</span><span class="cx"> &lt;/td&gt;
</span><span class="cx"> &lt;/tr&gt;
</span><span class="cx">         &lt;?php endif;
</span><del>-
-        if ( get_header_image() ) : ?&gt;
</del><ins>+        if ( get_header_image() &amp;&amp; !is_random_header_image() ) : ?&gt;
</ins><span class="cx"> &lt;tr valign=&quot;top&quot;&gt;
</span><span class="cx"> &lt;th scope=&quot;row&quot;&gt;&lt;?php _e( 'Remove Image' ); ?&gt;&lt;/th&gt;
</span><span class="cx"> &lt;td&gt;
</span><span class="lines">@@ -506,7 +556,7 @@
</span><span class="cx"> &lt;/tr&gt;
</span><span class="cx">         &lt;?php endif;
</span><span class="cx"> 
</span><del>-        if ( defined( 'HEADER_IMAGE' ) ) : ?&gt;
</del><ins>+        if ( defined( 'HEADER_IMAGE' ) &amp;&amp; !is_random_header_image() ) : ?&gt;
</ins><span class="cx"> &lt;tr valign=&quot;top&quot;&gt;
</span><span class="cx"> &lt;th scope=&quot;row&quot;&gt;&lt;?php _e( 'Reset Image' ); ?&gt;&lt;/th&gt;
</span><span class="cx"> &lt;td&gt;
</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 &gt; 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() &amp;&amp; empty( $header_image_mod ) ) )
+                        return true;
+        } else {
+                if ( &quot;random-$type-image&quot; == $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' =&gt; 'attachment', 'meta_key' =&gt; '_wp_attachment_is_custom_header', 'meta_value' =&gt; get_option('stylesheet'), 'orderby' =&gt; 'none', 'nopaging' =&gt; true ) );
+
+        if ( empty( $headers ) )
+                return array();
+
+        foreach ( (array) $headers as $header ) {
+                $url = $header-&gt;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>