<!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>[20312] trunk/wp-includes: Introduce WP_Theme-&gt;exists() to check if the queried theme actually exists.</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/20312">20312</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-03-29 02:59:48 +0000 (Thu, 29 Mar 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduce WP_Theme-&gt;exists() to check if the queried theme actually exists. WP_Theme-&gt;exists() is a subset of errors() -- a theme with errors may still exist, but a theme that does not exist has an error of theme_not_found. wp_get_theme() now returns false if the theme does not exist. Improve scandir() and get_files() logic. see <a href="http://core.trac.wordpress.org/ticket/20103">#20103</a>.</pre>

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclasswpthemephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-theme.php (20311 => 20312)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-theme.php        2012-03-29 02:15:06 UTC (rev 20311)
+++ trunk/wp-includes/class-wp-theme.php        2012-03-29 02:59:48 UTC (rev 20312)
</span><span class="lines">@@ -195,7 +195,10 @@
</span><span class="cx">                                 $theme_root_template = $cache['theme_root_template'];
</span><span class="cx">                 } elseif ( ! file_exists( $this-&gt;theme_root . '/' . $theme_file ) ) {
</span><span class="cx">                         $this-&gt;headers['Name'] = $this-&gt;stylesheet;
</span><del>-                        $this-&gt;errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
</del><ins>+                        if ( ! file_exists( $this-&gt;theme_root . '/' . $this-&gt;stylesheet ) )
+                                $this-&gt;errors = new WP_Error( 'theme_not_found', __( 'The theme directory does not exist.' ) );
+                        else
+                                $this-&gt;errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
</ins><span class="cx">                         $this-&gt;cache_add( 'theme', array( 'headers' =&gt; $this-&gt;headers, 'errors' =&gt; $this-&gt;errors, 'stylesheet' =&gt; $this-&gt;stylesheet ) );
</span><span class="cx">                         if ( ! file_exists( $this-&gt;theme_root ) ) // Don't cache this one.
</span><span class="cx">                                 $this-&gt;errors-&gt;add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn&amp;#8217;t exist. Please check your installation.' ) );
</span><span class="lines">@@ -384,14 +387,14 @@
</span><span class="cx">                         case 'Stylesheet' :
</span><span class="cx">                                 return $this-&gt;get_stylesheet();
</span><span class="cx">                         case 'Template Files' :
</span><del>-                                $files = $this-&gt;get_files('php', true);
-                                foreach ( $files as &amp;$file )
-                                        $file = $this-&gt;theme_root . '/' . $file;
</del><ins>+                                $files = $this-&gt;get_files( 'php', 1 );
+                                if ( $this-&gt;parent() )
+                                        $files = array_merge( $files, $this-&gt;parent()-&gt;get_files( 'php', 1 ) );
</ins><span class="cx">                                 return $files;
</span><span class="cx">                         case 'Stylesheet Files' :
</span><del>-                                $files = $this-&gt;get_files('css', true);
-                                foreach ( $files as &amp;$file )
-                                        $file = $this-&gt;theme_root . '/' . $file;
</del><ins>+                                $files = $this-&gt;get_files( 'css' );
+                                if ( $this-&gt;parent() )
+                                        $files = array_merge( $files, $this-&gt;parent()-&gt;get_files( 'css' ) );
</ins><span class="cx">                                 return $files;
</span><span class="cx">                         case 'Template Dir' :
</span><span class="cx">                                 return $this-&gt;get_template_directory();
</span><span class="lines">@@ -425,6 +428,21 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Whether the theme exists.
+         *
+         * A theme with errors exists. A theme with the error of 'theme_not_found',
+         * meaning that the theme directory was not found, does not exist.
+         *
+         * @since 3.4.0
+         * @access public
+         *
+         * @return bool Whether the theme exists.
+         */
+        public function exists() {
+                return ! ( is_wp_error( $this-&gt;errors ) &amp;&amp; in_array( 'theme_not_found', $this-&gt;errors-&gt;get_error_codes() ) );
+        }
+
+        /**
</ins><span class="cx">          * Returns reference to the parent theme.
</span><span class="cx">          *
</span><span class="cx">          * @since 3.4.0
</span><span class="lines">@@ -474,7 +492,7 @@
</span><span class="cx">          * @since 3.4.0
</span><span class="cx">          */
</span><span class="cx">         public function cache_delete() {
</span><del>-                foreach ( array( 'theme', 'screenshot', 'screenshot_count', 'files', 'headers' ) as $key )
</del><ins>+                foreach ( array( 'theme', 'screenshot', 'screenshot_count', 'files', 'headers', 'page_templates' ) as $key )
</ins><span class="cx">                         wp_cache_delete( $key . '-' . $this-&gt;cache_hash, 'themes' );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -891,7 +909,7 @@
</span><span class="cx">                         return 0;
</span><span class="cx"> 
</span><span class="cx">                 $prefix = $this-&gt;get_stylesheet() . '/screenshot-';
</span><del>-                $files = self::scandir( $this-&gt;get_stylesheet_directory(), $this-&gt;get_stylesheet(), 'png', 0 );
</del><ins>+                $files = self::scandir( $this-&gt;get_stylesheet_directory(), $this-&gt;get_stylesheet(), 'png' );
</ins><span class="cx"> 
</span><span class="cx">                 $screenshot_count = 1;
</span><span class="cx">                 while ( in_array( $prefix . ( $screenshot_count + 1 ) . '.png', $files['png'] ) )
</span><span class="lines">@@ -925,7 +943,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * Return files in the template and stylesheet directories.
</del><ins>+         * Return files in the theme's directory. Does not return files found in the parent theme.
</ins><span class="cx">          *
</span><span class="cx">          * @since 3.4.0
</span><span class="cx">          * @access public
</span><span class="lines">@@ -934,19 +952,13 @@
</span><span class="cx">          * @return array If a specific $type is requested, returns an array of PHP files. If no $type is requested,
</span><span class="cx">          *         returns an array, with the keys being the file types, and the values being an array of files for those type.
</span><span class="cx">          */
</span><del>-        public function get_files( $type = null, $include_parent_files = false ) {
</del><ins>+        public function get_files( $type = null, $depth = 0 ) {
</ins><span class="cx">                 $files = $this-&gt;cache_get( 'files' );
</span><span class="cx">                 if ( ! is_array( $files ) ) {
</span><del>-                        if ( $include_parent_files || ! $this-&gt;is_child_theme() )
-                                // Template files can be one level down for the purposes of the theme editor, so this should be $depth = 1.
-                                // Todo: We ignore this for now, but this is why the branching is weird.
-                                $files = self::scandir( $this-&gt;get_template_directory(), $this-&gt;get_template(), array( 'php', 'css' ) );
-                        else
-                                $files = array();
-                        if ( $this-&gt;is_child_theme() )
-                                $files = array_merge_recursive( $files, (array) self::scandir( $this-&gt;get_stylesheet_directory(), $this-&gt;get_stylesheet(), array( 'php', 'css' ) ) );
</del><ins>+                        $files = (array) self::scandir( $this-&gt;get_stylesheet_directory(), array( 'php', 'css' ), $depth );
</ins><span class="cx">                         foreach ( $files as &amp;$group )
</span><del>-                                sort( $group );
</del><ins>+                                ksort( $group );
+                        unset( $group );
</ins><span class="cx">                         $this-&gt;cache_add( 'files', $files );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="lines">@@ -958,6 +970,14 @@
</span><span class="cx">                 return array();
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        /**
+         * Returns the theme's page templates.
+         *
+         * @since 3.4.0
+         * @access public
+         *
+         * @return array Array of page templates, keyed by filename, with the value of the translated header name.
+         */
</ins><span class="cx">         public function get_page_templates() {
</span><span class="cx">                 // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
</span><span class="cx">                 if ( $this-&gt;errors() &amp;&amp; $this-&gt;errors()-&gt;get_error_codes() !== array( 'theme_parent_invalid' ) )
</span><span class="lines">@@ -968,9 +988,9 @@
</span><span class="cx">                         return $page_templates;
</span><span class="cx">                 $page_templates = array();
</span><span class="cx"> 
</span><del>-                $files = (array) self::scandir( $this-&gt;get_template_directory(), $this-&gt;get_template_directory(), 'php' );
</del><ins>+                $files = (array) self::scandir( $this-&gt;get_template_directory(), 'php' );
</ins><span class="cx">                 if ( $this-&gt;is_child_theme() )
</span><del>-                        $files = array_merge_recursive( $files, (array) self::scandir( $this-&gt;get_stylesheet_directory(), $this-&gt;get_stylesheet_directory(), 'php' ) );
</del><ins>+                        $files = array_merge_recursive( $files, (array) self::scandir( $this-&gt;get_stylesheet_directory(), 'php' ) );
</ins><span class="cx"> 
</span><span class="cx">                 foreach ( $files['php'] as $file ) {
</span><span class="cx">                         $headers = get_file_data( $file, array( 'Template Name' =&gt; 'Template Name' ) );
</span><span class="lines">@@ -990,12 +1010,12 @@
</span><span class="cx">          * @access public
</span><span class="cx">          *
</span><span class="cx">          * @param string $path Absolute path to search.
</span><ins>+         * @param array|string $extensions Array of extensions to find, or string of a single extension
+         * @param int $depth How deep to search for files. Optional, defaults to a flat scan (0 depth). -1 depth is infinite.
</ins><span class="cx">          * @param string $relative_path The basename of the absolute path. Used to control the returned path
</span><span class="cx">          *         for the found files, particularly when this function recurses to lower depths.
</span><del>-         * @param array|string $extensions Array of extensions to find, or string of a single extension.
-         * @depth int How deep to search for files. Optional, defaults to a flat scan (0 depth).
</del><span class="cx">          */
</span><del>-        private static function scandir( $path, $relative_path, $extensions, $depth = 0 ) {
</del><ins>+        private static function scandir( $path, $extensions, $depth = 1, $relative_path = '' ) {
</ins><span class="cx">                 if ( ! is_dir( $path ) )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -1003,20 +1023,25 @@
</span><span class="cx"> 
</span><span class="cx">                 $extensions = (array) $extensions;
</span><span class="cx">                 $files = array_fill_keys( $extensions, array() );
</span><del>-                $extensions = implode( '|', $extensions );
</del><ins>+                $_extensions = implode( '|', $extensions );
</ins><span class="cx"> 
</span><ins>+                $relative_path = trailingslashit( $relative_path );
+                if ( '/' == $relative_path )
+                        $relative_path = '';
+
</ins><span class="cx">                 foreach ( $results as $result ) {
</span><del>-                        if ( '.' == $result || '..' == $result )
</del><ins>+                        if ( '.' == $result[0] )
</ins><span class="cx">                                 continue;
</span><span class="cx">                         if ( is_dir( $path . '/' . $result ) ) {
</span><del>-                                if ( ! $depth )
</del><ins>+                                if ( ! $depth || 'CVS' == $result )
</ins><span class="cx">                                         continue;
</span><del>-                                $found = self::scandir( $path . '/' . $result, $relative_path . '/' . $result, $extensions, $depth - 1 );
</del><ins>+                                $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result );
</ins><span class="cx">                                 $files = array_merge_recursive( $files, $found );
</span><del>-                        } elseif ( preg_match( '~\.(' . $extensions . ')$~', $result, $match ) ) {
-                                $files[ $match[1] ][] = $relative_path . '/' . $result;
</del><ins>+                        } elseif ( preg_match( '~\.(' . $_extensions . ')$~', $result, $match ) ) {
+                                $files[ $match[1] ][ $relative_path . $result ] = $path . '/' . $result;
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><ins>+
</ins><span class="cx">                 return $files;
</span><span class="cx">         }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesthemephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/theme.php (20311 => 20312)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/theme.php        2012-03-29 02:15:06 UTC (rev 20311)
+++ trunk/wp-includes/theme.php        2012-03-29 02:59:48 UTC (rev 20312)
</span><span class="lines">@@ -87,7 +87,7 @@
</span><span class="cx">  * @param string $stylesheet Directory name for the theme. Optional. Defaults to current theme.
</span><span class="cx">  * @param string $theme_root Absolute path of the theme root to look in. Optional. If not specified, get_raw_theme_root()
</span><span class="cx">  *         is used to calculate the theme root for the $stylesheet provided (or current theme).
</span><del>- * @return WP_Theme
</del><ins>+ * @return WP_Theme|bool WP_Theme object. False if the theme is not found.
</ins><span class="cx">  */
</span><span class="cx"> function wp_get_theme( $stylesheet = null, $theme_root = null ) {
</span><span class="cx">         global $wp_theme_directories;
</span><span class="lines">@@ -97,11 +97,18 @@
</span><span class="cx"> 
</span><span class="cx">         if ( empty( $theme_root ) ) {
</span><span class="cx">                 $theme_root = get_raw_theme_root( $stylesheet );
</span><ins>+                if ( false === $theme_root )
+                        return false;
+
</ins><span class="cx">                 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
</span><span class="cx">                         $theme_root = WP_CONTENT_DIR . $theme_root;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        return new WP_Theme( $stylesheet, $theme_root );
</del><ins>+        $theme = new WP_Theme( $stylesheet, $theme_root );
+        if ( $theme-&gt;exists() )
+                return $theme;
+
+        return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>

</body>
</html>