Make WordPress Core

Ticket #6531: 6531.7.diff

File 6531.7.diff, 6.5 KB (added by westonruter, 7 years ago)

Δ https://github.com/xwp/wordpress-develop/pull/271/commits/f80a849

  • src/wp-admin/includes/file.php

    diff --git src/wp-admin/includes/file.php src/wp-admin/includes/file.php
    index 05bfde46a4..6c15bded98 100644
    $wp_file_descriptions = array( 
    7070 * @since 1.5.0
    7171 *
    7272 * @global array $wp_file_descriptions Theme file descriptions.
    73  * @global array $allowed_files        List of allowed files. 
     73 * @global array $allowed_files        List of allowed files.
    7474 * @param string $file Filesystem path or filename
    7575 * @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist.
    7676 *                Appends 'Page Template' to basename of $file if the file is a page template
    function get_home_path() { 
    126126 * @return bool|array False on failure, Else array of files
    127127 */
    128128function list_files( $folder = '', $levels = 100 ) {
    129         if ( empty($folder) )
     129        if ( empty( $folder ) ) {
    130130                return false;
     131        }
    131132
    132         if ( ! $levels )
     133        if ( ! $levels ) {
    133134                return false;
     135        }
     136
     137        /**
     138         * Filters the array of excluded directories and files while scanning the folder.
     139         *
     140         * @since 4.9
     141         *
     142         * @param array $exclusions Array of excluded directories and files.
     143         */
     144        $exclusions = (array) apply_filters( 'list_files_exclusions', array( 'CVS', 'node_modules' ) );
    134145
    135146        $files = array();
    136147        if ( $dir = @opendir( $folder ) ) {
    137                 while (($file = readdir( $dir ) ) !== false ) {
    138                         if ( in_array($file, array('.', '..') ) )
     148                while ( ( $file = readdir( $dir ) ) !== false ) {
     149                        if ( in_array( $file, array( '.', '..' ), true ) ) {
     150                                continue;
     151                        }
     152
     153                        if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
    139154                                continue;
     155                        }
     156
    140157                        if ( is_dir( $folder . '/' . $file ) ) {
    141                                 $files2 = list_files( $folder . '/' . $file, $levels - 1);
    142                                 if ( $files2 )
    143                                         $files = array_merge($files, $files2 );
    144                                 else
     158                                $files2 = list_files( $folder . '/' . $file, $levels - 1 );
     159                                if ( $files2 ) {
     160                                        $files = array_merge( $files, $files2 );
     161                                } else {
    145162                                        $files[] = $folder . '/' . $file . '/';
     163                                }
    146164                        } else {
    147165                                $files[] = $folder . '/' . $file;
    148166                        }
  • src/wp-admin/includes/plugin.php

    diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
    index 97dd767850..802f9884be 100644
    function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup 
    190190 * @param string $plugin Path to the main plugin file from plugins directory.
    191191 * @return array List of files relative to the plugin root.
    192192 */
    193 function get_plugin_files($plugin) {
     193function get_plugin_files( $plugin ) {
    194194        $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
    195         $dir = dirname($plugin_file);
    196         $plugin_files = array($plugin);
    197         if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
    198                 $plugins_dir = @ opendir( $dir );
    199                 if ( $plugins_dir ) {
    200                         while (($file = readdir( $plugins_dir ) ) !== false ) {
    201                                 if ( substr($file, 0, 1) == '.' )
    202                                         continue;
    203                                 if ( is_dir( $dir . '/' . $file ) ) {
    204                                         $plugins_subdir = @ opendir( $dir . '/' . $file );
    205                                         if ( $plugins_subdir ) {
    206                                                 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
    207                                                         if ( substr($subfile, 0, 1) == '.' )
    208                                                                 continue;
    209                                                         $plugin_files[] = plugin_basename("$dir/$file/$subfile");
    210                                                 }
    211                                                 @closedir( $plugins_subdir );
    212                                         }
    213                                 } else {
    214                                         if ( plugin_basename("$dir/$file") != $plugin )
    215                                                 $plugin_files[] = plugin_basename("$dir/$file");
    216                                 }
    217                         }
    218                         @closedir( $plugins_dir );
    219                 }
     195        $dir = dirname( $plugin_file );
     196
     197        $data = get_plugin_data( $plugin_file );
     198        $label = isset( $data['Version'] ) ? 'list_files_cache_' . $dir . '-' . $data['Version'] : 'list_files_cache_' . $dir;
     199
     200        $plugin_files = get_transient( $label );
     201        if ( ! empty( $plugin_files ) ) {
     202                return $plugin_files;
     203        }
     204
     205        $plugin_files = array( $plugin );
     206        if ( is_dir( $dir ) && WP_PLUGIN_DIR !== $dir ) {
     207
     208                $list_files = list_files( $dir );
     209                $list_files = array_map( 'plugin_basename', $list_files );
     210
     211                $plugin_files += $list_files;
     212                $plugin_files = array_unique( $plugin_files );
    220213        }
    221214
     215        set_transient( $label, $plugin_files, HOUR_IN_SECONDS );
     216
    222217        return $plugin_files;
    223218}
    224219
  • src/wp-admin/theme-editor.php

    diff --git src/wp-admin/theme-editor.php src/wp-admin/theme-editor.php
    index 2a593dee64..3247bb7d14 100644
    $file_types = array_unique( array_merge( $file_types, $default_types ) ); 
    120120foreach ( $file_types as $type ) {
    121121        switch ( $type ) {
    122122                case 'php':
    123                         $allowed_files += $theme->get_files( 'php', 1 );
     123                        $allowed_files += $theme->get_files( 'php', -1 );
    124124                        $has_templates = ! empty( $allowed_files );
    125125                        break;
    126126                case 'css':
    127                         $style_files = $theme->get_files( 'css' );
     127                        $style_files = $theme->get_files( 'css', -1 );
    128128                        $allowed_files['style.css'] = $style_files['style.css'];
    129129                        $allowed_files += $style_files;
    130130                        break;
    131131                default:
    132                         $allowed_files += $theme->get_files( $type );
     132                        $allowed_files += $theme->get_files( $type, -1 );
    133133                        break;
    134134        }
    135135}
  • src/wp-includes/class-wp-theme.php

    diff --git src/wp-includes/class-wp-theme.php src/wp-includes/class-wp-theme.php
    index 3e6c62906b..1ae904579e 100644
    final class WP_Theme implements ArrayAccess { 
    984984         *                   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 );
     987                // get and cache all theme files to start with.
     988                $label = 'list_files_cache_' . $this->get( 'Name' ) . '-' . $this->get( 'Version' );
     989                $all_files = get_transient( $label );
     990                if ( empty( $all_files ) ) {
     991                        $all_files = (array) self::scandir( $this->get_stylesheet_directory(), null, -1 );
     992
     993                        if ( $search_parent && $this->parent() ) {
     994                                $all_files += (array) self::scandir( $this->get_template_directory(), null, -1 );
     995                        }
     996
     997                        set_transient( $label, $all_files, HOUR_IN_SECONDS );
     998                }
    988999
    989                 if ( $search_parent && $this->parent() )
    990                         $files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
     1000                // Filter $all_files by $type & $depth.
     1001                $files = array();
     1002                if ( $type ) {
     1003                        $type = (array) $type;
     1004                        $_extensions = implode( '|', $type );
     1005                }
     1006                foreach ( $all_files as $key => $file ) {
     1007                        if ( $depth >= 0 && substr_count( $key, '/' ) > $depth ) {
     1008                                continue; // Filter by depth.
     1009                        }
     1010                        if ( ! $type || preg_match( '~\.(' . $_extensions . ')$~', $file ) ) { // Filter by type.
     1011                                $files[ $key ] = $file;
     1012                        }
     1013                }
    9911014
    9921015                return $files;
    9931016        }