mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@41806 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -981,13 +981,38 @@ final class WP_Theme implements ArrayAccess {
|
||||
* @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite.
|
||||
* @param bool $search_parent Optional. Whether to return parent files. Defaults to false.
|
||||
* @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values
|
||||
* being absolute paths.
|
||||
* being absolute paths.
|
||||
*/
|
||||
public function get_files( $type = null, $depth = 0, $search_parent = false ) {
|
||||
$files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth );
|
||||
// get and cache all theme files to start with.
|
||||
$label = sanitize_key( 'files_' . $this->cache_hash . '-' . $this->get( 'Version' ) );
|
||||
$transient_key = substr( $label, 0, 29 ) . md5( $label );
|
||||
|
||||
if ( $search_parent && $this->parent() )
|
||||
$files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
|
||||
$all_files = get_transient( $transient_key );
|
||||
if ( false === $all_files ) {
|
||||
$all_files = (array) self::scandir( $this->get_stylesheet_directory(), null, -1 );
|
||||
|
||||
if ( $search_parent && $this->parent() ) {
|
||||
$all_files += (array) self::scandir( $this->get_template_directory(), null, -1 );
|
||||
}
|
||||
|
||||
set_transient( $transient_key, $all_files, HOUR_IN_SECONDS );
|
||||
}
|
||||
|
||||
// Filter $all_files by $type & $depth.
|
||||
$files = array();
|
||||
if ( $type ) {
|
||||
$type = (array) $type;
|
||||
$_extensions = implode( '|', $type );
|
||||
}
|
||||
foreach ( $all_files as $key => $file ) {
|
||||
if ( $depth >= 0 && substr_count( $key, '/' ) > $depth ) {
|
||||
continue; // Filter by depth.
|
||||
}
|
||||
if ( ! $type || preg_match( '~\.(' . $_extensions . ')$~', $file ) ) { // Filter by type.
|
||||
$files[ $key ] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
@@ -1107,8 +1132,9 @@ final class WP_Theme implements ArrayAccess {
|
||||
* with `$relative_path`, with the values being absolute paths. False otherwise.
|
||||
*/
|
||||
private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
|
||||
if ( ! is_dir( $path ) )
|
||||
if ( ! is_dir( $path ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $extensions ) {
|
||||
$extensions = (array) $extensions;
|
||||
@@ -1116,8 +1142,9 @@ final class WP_Theme implements ArrayAccess {
|
||||
}
|
||||
|
||||
$relative_path = trailingslashit( $relative_path );
|
||||
if ( '/' == $relative_path )
|
||||
if ( '/' == $relative_path ) {
|
||||
$relative_path = '';
|
||||
}
|
||||
|
||||
$results = scandir( $path );
|
||||
$files = array();
|
||||
@@ -1125,19 +1152,20 @@ final class WP_Theme implements ArrayAccess {
|
||||
/**
|
||||
* Filters the array of excluded directories and files while scanning theme folder.
|
||||
*
|
||||
* @since 4.7.4
|
||||
* @since 4.7.4
|
||||
*
|
||||
* @param array $exclusions Array of excluded directories and files.
|
||||
*/
|
||||
$exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules' ) );
|
||||
$exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) );
|
||||
|
||||
foreach ( $results as $result ) {
|
||||
if ( '.' == $result[0] || in_array( $result, $exclusions, true ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( is_dir( $path . '/' . $result ) ) {
|
||||
if ( ! $depth )
|
||||
if ( ! $depth ) {
|
||||
continue;
|
||||
}
|
||||
$found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result );
|
||||
$files = array_merge_recursive( $files, $found );
|
||||
} elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
|
||||
|
||||
Reference in New Issue
Block a user