WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/10/17 05:33:57 (4 months ago)
Author:
pento
Message:

File Editor: Add support for more than one sub-directory level.

The theme and plugin editors now list all files in the selected theme or plugin, recursing through subdirectories as necessary.

Props WraithKenny, schlessera, chsxf, MikeHansenMe, Daedalon, valendesigns, westonruter, pento.
Fixes #6531.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme.php

    r41688 r41806  
    982982     * @param bool $search_parent Optional. Whether to return parent files. Defaults to false. 
    983983     * @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values 
    984      *               being absolute paths. 
     984     *               being absolute paths. 
    985985     */ 
    986986    public function get_files( $type = null, $depth = 0, $search_parent = false ) { 
    987         $files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth ); 
    988  
    989         if ( $search_parent && $this->parent() ) 
    990             $files += (array) self::scandir( $this->get_template_directory(), $type, $depth ); 
     987        // get and cache all theme files to start with. 
     988        $label = sanitize_key( 'files_' . $this->cache_hash . '-' . $this->get( 'Version' ) ); 
     989        $transient_key = substr( $label, 0, 29 ) . md5( $label ); 
     990 
     991        $all_files = get_transient( $transient_key ); 
     992        if ( false === $all_files ) { 
     993            $all_files = (array) self::scandir( $this->get_stylesheet_directory(), null, -1 ); 
     994 
     995            if ( $search_parent && $this->parent() ) { 
     996                $all_files += (array) self::scandir( $this->get_template_directory(), null, -1 ); 
     997            } 
     998 
     999            set_transient( $transient_key, $all_files, HOUR_IN_SECONDS ); 
     1000        } 
     1001 
     1002        // Filter $all_files by $type & $depth. 
     1003        $files = array(); 
     1004        if ( $type ) { 
     1005            $type = (array) $type; 
     1006            $_extensions = implode( '|', $type ); 
     1007        } 
     1008        foreach ( $all_files as $key => $file ) { 
     1009            if ( $depth >= 0 && substr_count( $key, '/' ) > $depth ) { 
     1010                continue; // Filter by depth. 
     1011            } 
     1012            if ( ! $type || preg_match( '~\.(' . $_extensions . ')$~', $file ) ) { // Filter by type. 
     1013                $files[ $key ] = $file; 
     1014            } 
     1015        } 
    9911016 
    9921017        return $files; 
     
    11081133     */ 
    11091134    private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) { 
    1110         if ( ! is_dir( $path ) ) 
     1135        if ( ! is_dir( $path ) ) { 
    11111136            return false; 
     1137        } 
    11121138 
    11131139        if ( $extensions ) { 
     
    11171143 
    11181144        $relative_path = trailingslashit( $relative_path ); 
    1119         if ( '/' == $relative_path ) 
     1145        if ( '/' == $relative_path ) { 
    11201146            $relative_path = ''; 
     1147        } 
    11211148 
    11221149        $results = scandir( $path ); 
     
    11261153         * Filters the array of excluded directories and files while scanning theme folder. 
    11271154         * 
    1128          * @since 4.7.4 
     1155         * @since 4.7.4 
    11291156         * 
    11301157         * @param array $exclusions Array of excluded directories and files. 
    11311158         */ 
    1132         $exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules' ) ); 
     1159        $exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) ); 
    11331160 
    11341161        foreach ( $results as $result ) { 
     
    11371164            } 
    11381165            if ( is_dir( $path . '/' . $result ) ) { 
    1139                 if ( ! $depth ) 
     1166                if ( ! $depth ) { 
    11401167                    continue; 
     1168                } 
    11411169                $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result ); 
    11421170                $files = array_merge_recursive( $files, $found ); 
Note: See TracChangeset for help on using the changeset viewer.