Make WordPress Core

Ticket #11216: 11216.3.diff

File 11216.3.diff, 4.0 KB (added by rzen, 12 years ago)

Creates helper function for recursively searching through entire theme directory

  • wp-includes/theme.php

     
    342342                        $template_directory = trim( $theme_root . '/' . $template );
    343343                }
    344344
    345                 $stylesheet_files = array();
    346                 $template_files = array();
     345                // Loop through the stylesheet (current theme) directory and add all CSS files to an array
     346                $stylesheet_files = _get_theme_files( "$theme_root/$stylesheet/", '|\.css$|' );
     347               
     348                // Loop through the stylesheet (current theme) and template (parent theme) directories and add all PHP files to a single array
     349                $template_files = array_merge( _get_theme_files( "$theme_root/$stylesheet/" ), _get_theme_files( "$template_directory" ) );
    347350
    348                 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
    349                 if ( $stylesheet_dir ) {
    350                         while ( ($file = $stylesheet_dir->read()) !== false ) {
    351                                 if ( !preg_match('|^\.+$|', $file) ) {
    352                                         if ( preg_match('|\.css$|', $file) )
    353                                                 $stylesheet_files[] = "$theme_root/$stylesheet/$file";
    354                                         elseif ( preg_match('|\.php$|', $file) )
    355                                                 $template_files[] = "$theme_root/$stylesheet/$file";
    356                                 }
    357                         }
    358                         @ $stylesheet_dir->close();
    359                 }
    360 
    361                 $template_dir = @ dir("$template_directory");
    362                 if ( $template_dir ) {
    363                         while ( ($file = $template_dir->read()) !== false ) {
    364                                 if ( preg_match('|^\.+$|', $file) )
    365                                         continue;
    366                                 if ( preg_match('|\.php$|', $file) ) {
    367                                         $template_files[] = "$template_directory/$file";
    368                                 } elseif ( is_dir("$template_directory/$file") ) {
    369                                         $template_subdir = @ dir("$template_directory/$file");
    370                                         if ( !$template_subdir )
    371                                                 continue;
    372                                         while ( ($subfile = $template_subdir->read()) !== false ) {
    373                                                 if ( preg_match('|^\.+$|', $subfile) )
    374                                                         continue;
    375                                                 if ( preg_match('|\.php$|', $subfile) )
    376                                                         $template_files[] = "$template_directory/$file/$subfile";
    377                                         }
    378                                         @ $template_subdir->close();
    379                                 }
    380                         }
    381                         @ $template_dir->close();
    382                 }
    383 
    384                 //Make unique and remove duplicates when stylesheet and template are the same i.e. most themes
     351                // Make unique and remove duplicates when stylesheet and template are the same (i.e. most themes)
    385352                $template_files = array_unique($template_files);
    386353                $stylesheet_files = array_unique($stylesheet_files);
    387354
     
    465432}
    466433
    467434/**
     435 * Recursively loop through a theme's subdirectories and add PHP files to an array
     436 *
     437 * @since ...
     438 *
     439 * @param string $dir The current directory to loop through
     440 * @param string $match The regular expression to match for file type(s)
     441 * @param array $ignore A list of files/directories to ignore
     442 * @return array Paths of all theme PHP files.
     443 */
     444function _get_theme_files( $dir, $match = '|\.php$|', $ignore = array( '.', '..', '.svn', '.git' ) ) {
     445       
     446        $the_files = array();
     447       
     448        $the_subdir = @ opendir("$dir");
     449        while ( false !== ( $the_file = readdir( $the_subdir ) ) ) {
     450                // Skip the ignored files
     451                if ( !in_array( $the_file, $ignore ) ) {                       
     452                        // If we're looking at a directory, loop back for a second pass, otherwise handle the file
     453                        if ( is_dir( "$dir/$the_file" ) ) {
     454                                $inner_files = _get_theme_files( "$dir/$the_file", $match );
     455                                if ( is_array($inner_files) ) $the_files = array_merge( $the_files, $inner_files );
     456                        } else {
     457                                // if it's a matching file type, add it to the list, otherwise skip it
     458                                if ( preg_match( $match, $the_file) )
     459                                        $the_files[] = "$dir/$the_file";
     460                                else
     461                                        continue;
     462                    }
     463                }
     464        }
     465        @ closedir($the_subdir);
     466
     467        return $the_files;
     468}
     469
     470/**
    468471 * Retrieve theme roots.
    469472 *
    470473 * @since 2.9.0
  • wp-admin/includes/theme.php

     
    179179                        $basename = str_replace($base, '', $template);
    180180
    181181                        // don't allow template files in subdirectories
    182                         if ( false !== strpos($basename, '/') )
    183                                 continue;
     182                        // if ( false !== strpos($basename, '/') )
     183                        //      continue;
    184184
    185185                        if ( 'functions.php' == $basename )
    186186                                continue;