<!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" />
<title>[20020] trunk/wp-includes/theme.php: Rewrite search_theme_directories() -- better performance and allow for caching.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/20020">20020</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-02-28 20:05:00 +0000 (Tue, 28 Feb 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Rewrite search_theme_directories() -- better performance and allow for caching.

 * Update it to use PHP5 scandir().
 * Don't scan individual theme dirs -- we only need to check for style.css. (Solid performance gains.)
 * Improve and simplify branching.
 * Introduce wp_cache_themes_persistently filter to enable persistent caching of the return value, based on the theme_roots transient.

see <a href="http://core.trac.wordpress.org/ticket/20103">#20103</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesthemephp">trunk/wp-includes/theme.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesthemephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/theme.php (20019 => 20020)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/theme.php        2012-02-28 20:02:43 UTC (rev 20019)
+++ trunk/wp-includes/theme.php        2012-02-28 20:05:00 UTC (rev 20020)
</span><span class="lines">@@ -571,78 +571,91 @@
</span><span class="cx">  * @return array Valid themes found
</span><span class="cx">  */
</span><span class="cx"> function search_theme_directories() {
</span><del>-        global $wp_theme_directories, $wp_broken_themes;
</del><ins>+        global $wp_theme_directories;
</ins><span class="cx">         if ( empty( $wp_theme_directories ) )
</span><span class="cx">                 return false;
</span><span class="cx"> 
</span><del>-        $theme_files = array();
-        $wp_broken_themes = array();
</del><ins>+        static $found_themes;
+        if ( isset( $found_themes ) )
+                return $found_themes;
</ins><span class="cx"> 
</span><del>-        /* Loop the registered theme directories and extract all themes */
-        foreach ( (array) $wp_theme_directories as $theme_root ) {
-                $theme_loc = $theme_root;
</del><ins>+        $found_themes = array();
</ins><span class="cx"> 
</span><del>-                /* We don't want to replace all forward slashes, see Trac #4541 */
-                if ( '/' != WP_CONTENT_DIR )
-                        $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
</del><ins>+        if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
+                $cached_roots = get_site_transient( 'theme_roots' );
+                if ( is_array( $cached_roots ) ) {
+                        foreach ( $cached_roots as $theme_dir =&gt; $theme_root ) {
+                                $found_themes[ $theme_dir ] = array(
+                                        'theme_file' =&gt; $theme_dir . '/style.css',
+                                        'theme_root' =&gt; $theme_root,
+                                );
+                        }
+                        return $found_themes;
+                }
+                if ( ! is_int( $cache_expiration ) )
+                        $cache_expiration = 7200;
+        } else {
+                // Two hours is the default.
+                $cache_expiration = 7200;
+        }
</ins><span class="cx"> 
</span><del>-                /* Files in the root of the current theme directory and one subdir down */
-                $themes_dir = @ opendir($theme_root);
</del><ins>+        /* Loop the registered theme directories and extract all themes */
+        foreach ( $wp_theme_directories as $theme_root ) {
</ins><span class="cx"> 
</span><del>-                if ( !$themes_dir )
</del><ins>+                // Start with directories in the root of the current theme directory.
+                $dirs = @ scandir( $theme_root );
+                if ( ! $dirs )
</ins><span class="cx">                         return false;
</span><del>-
-                while ( ($theme_dir = readdir($themes_dir)) !== false ) {
-                        if ( is_dir($theme_root . '/' . $theme_dir) &amp;&amp; is_readable($theme_root . '/' . $theme_dir) ) {
-                                if ( $theme_dir[0] == '.' || $theme_dir == 'CVS' )
-                                        continue;
-
-                                $stylish_dir = @opendir($theme_root . '/' . $theme_dir);
-                                $found_stylesheet = false;
-
-                                while ( ($theme_file = readdir($stylish_dir)) !== false ) {
-                                        if ( $theme_file == 'style.css' ) {
-                                                $theme_files[$theme_dir] = array( 'theme_file' =&gt; $theme_dir . '/' . $theme_file, 'theme_root' =&gt; $theme_root );
-                                                $found_stylesheet = true;
-                                                break;
-                                        }
</del><ins>+                foreach ( $dirs as $dir ) {
+                        if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
+                                continue;
+                        if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
+                                // wp-content/themes/a-single-theme
+                                // wp-content/themes is $theme_root, a-single-theme is $dir
+                                $found_themes[ $dir ] = array(
+                                        'theme_file' =&gt; $dir . '/style.css',
+                                        'theme_root' =&gt; $theme_root,
+                                );
+                        } else {
+                                $found_theme = false;
+                                // wp-content/themes/a-folder-of-themes/*
+                                // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
+                                $sub_dirs = @ scandir( $theme_root . '/' . $dir );
+                                if ( ! $sub_dirs )
+                                        return false;
+                                foreach ( $sub_dirs as $sub_dir ) {
+                                        if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
+                                                continue;
+                                        if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
+                                                continue;
+                                        $found_themes[ $dir . '/' . $sub_dir ] = array(
+                                                'theme_file' =&gt; $dir . '/' . $sub_dir . '/style.css',
+                                                'theme_root' =&gt; $theme_root,
+                                        );
+                                        $found_theme = true;
</ins><span class="cx">                                 }
</span><del>-                                @closedir($stylish_dir);
</del><ins>+                                // Never mind the above, it's just a theme missing a style.css.
+                                // Return it; WP_Theme will catch the error.
+                                if ( ! $found_theme )
+                                        $found_themes[ $dir ] = array(
+                                                'theme_file' =&gt; $dir . '/style.css',
+                                                'theme_root' =&gt; $theme_root,
+                                        );
+                        }
+                }
+        }
</ins><span class="cx"> 
</span><del>-                                if ( !$found_stylesheet ) { // look for themes in that dir
-                                        $subdir = &quot;$theme_root/$theme_dir&quot;;
-                                        $subdir_name = $theme_dir;
-                                        $theme_subdirs = @opendir( $subdir );
</del><ins>+        asort( $found_themes );
</ins><span class="cx"> 
</span><del>-                                        $found_subdir_themes = false;
-                                        while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
-                                                if ( is_dir( $subdir . '/' . $theme_subdir) &amp;&amp; is_readable($subdir . '/' . $theme_subdir) ) {
-                                                        if ( $theme_subdir[0] == '.' || $theme_subdir == 'CVS' )
-                                                                continue;
</del><ins>+        $theme_roots = array();
+        foreach ( $found_themes as $theme_dir =&gt; $theme_data ) {
+                $theme_roots[ $theme_dir ] = $theme_data['theme_root'];
+        }
</ins><span class="cx"> 
</span><del>-                                                        $stylish_dir = @opendir($subdir . '/' . $theme_subdir);
-                                                        $found_stylesheet = false;
</del><ins>+        if ( $theme_roots != get_site_transient( 'theme_roots' ) )
+                set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
</ins><span class="cx"> 
</span><del>-                                                        while ( ($theme_file = readdir($stylish_dir)) !== false ) {
-                                                                if ( $theme_file == 'style.css' ) {
-                                                                        $theme_files[&quot;$theme_dir/$theme_subdir&quot;] = array( 'theme_file' =&gt; $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' =&gt; $theme_root );
-                                                                        $found_stylesheet = true;
-                                                                        $found_subdir_themes = true;
-                                                                        break;
-                                                                }
-                                                        }
-                                                        @closedir($stylish_dir);
-                                                }
-                                        }
-                                        @closedir($theme_subdirs);
-                                        if ( !$found_subdir_themes )
-                                                $wp_broken_themes[$theme_dir] = array('Name' =&gt; $theme_dir, 'Title' =&gt; $theme_dir, 'Description' =&gt; __('Stylesheet is missing.'));
-                                }
-                        }
-                }
-                @closedir( $themes_dir );
-        }
-        return $theme_files;
</del><ins>+        return $found_themes;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>

</body>
</html>