+/**
+ * Register a directory that contains themes.
+ *
+ * @since 2.9.0
+ *
+ * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
+ * @return bool
+ */
+function register_theme_directory( $directory) {
+ global $wp_theme_directories;
+
+ /* If this folder does not exist, return and do not register */
+ if ( !file_exists( $directory ) )
+ /* Try prepending as the theme directory could be relative to the content directory */
+ $registered_directory = WP_CONTENT_DIR . '/' . $directory;
+ else
+ $registered_directory = $directory;
+
+ /* If this folder does not exist, return and do not register */
+ if ( !file_exists( $registered_directory ) )
+ return false;
+
+ $wp_theme_directories[] = $registered_directory;
+
+ return true;
+}
+
+/**
+ * Search all registered theme directories for complete and valid themes.
+ *
+ * @since 2.9.0
+ *
+ * @return array Valid themes found
+ */
+function search_theme_directories() {
+ global $wp_theme_directories, $wp_broken_themes;
+ if ( empty( $wp_theme_directories ) )
+ return false;
+
+ $theme_files = array();
+ $wp_broken_themes = array();
+
+ /* Loop the registered theme directories and extract all themes */
+ foreach ( (array) $wp_theme_directories as $theme_root ) {
+ $theme_loc = $theme_root;
+
+ /* 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);
+
+ /* Files in the root of the current theme directory and one subdir down */
+ $themes_dir = @ opendir($theme_root);
+
+ if ( !$themes_dir )
+ return false;
+
+ while ( ($theme_dir = readdir($themes_dir)) !== false ) {
+ if ( is_dir($theme_root . '/' . $theme_dir) && 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' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
+ $found_stylesheet = true;
+ break;
+ }
+ }
+ @closedir($stylish_dir);
+
+ if ( !$found_stylesheet ) { // look for themes in that dir
+ $subdir = "$theme_root/$theme_dir";
+ $subdir_name = $theme_dir;
+ $theme_subdirs = @opendir( $subdir );
+
+ $found_subdir_themes = false;
+ while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
+ if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
+ if ( $theme_subdir{0} == '.' || $theme_subdir == 'CVS' )
+ continue;
+
+ $stylish_dir = @opendir($subdir . '/' . $theme_subdir);
+ $found_stylesheet = false;
+
+ while ( ($theme_file = readdir($stylish_dir)) !== false ) {
+ if ( $theme_file == 'style.css' ) {
+ $theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
+ $found_stylesheet = true;
+ $found_subdir_themes = true;
+ break;
+ }
+ }
+ @closedir($stylish_dir);
+ }
+ }
+ @closedir($theme_subdir);
+ if ( !$found_subdir_themes )
+ $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
+ }
+ }
+ }
+ if ( is_dir( $theme_dir ) )
+ @closedir( $theme_dir );
+ }
+ return $theme_files;
+}
+