From c34a5bc8c997d5b120fc17e8950c9e1859afe79c Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Sat, 18 Mar 2017 03:53:59 +0000 Subject: [PATCH] Themes: Add filter for excluding directories from being scanned for template files. Exclude 'node_modules' directories from paths searched in `WP_Theme::scandir()`. Introduces the `theme_scandir_exclusions` filter to allow sites to exclude any other paths like `bower_components` or `vendor` from being searched for template files. Props lukasbesch, dd32, swisspidy, rachelbaker. Fixes #38292. git-svn-id: https://develop.svn.wordpress.org/trunk@40301 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-theme.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php index 77927d3881..77b5742a49 100644 --- a/src/wp-includes/class-wp-theme.php +++ b/src/wp-includes/class-wp-theme.php @@ -1155,11 +1155,21 @@ final class WP_Theme implements ArrayAccess { $results = scandir( $path ); $files = array(); + /** + * Filters the array of excluded directories and files while scanning theme folder. + * + * @since 4.7.4 + * + * @param array $exclusions Array of excluded directories and files. + */ + $exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules' ) ); + foreach ( $results as $result ) { - if ( '.' == $result[0] ) + if ( '.' == $result[0] || in_array( $result, $exclusions, true ) ) { continue; + } if ( is_dir( $path . '/' . $result ) ) { - if ( ! $depth || 'CVS' == $result ) + if ( ! $depth ) continue; $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result ); $files = array_merge_recursive( $files, $found );