Make WordPress Core

Changeset 20588


Ignore:
Timestamp:
04/25/2012 05:31:39 PM (15 years ago)
Author:
nacin
Message:

Make WP_Theme::get_files() a general method for retrieving any file types, at any depth, optionally to include parent files. see #20546.

File:
1 edited

Legend:

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

    r20586 r20588  
    402402                                return $this->get_stylesheet();
    403403                        case 'Template Files' :
    404                                 $files = $this->get_files( 'php' );
    405                                 if ( $this->parent() )
    406                                         $files = array_merge( $this->parent()->get_files( 'php' ), $files );
    407                                 return $files;
     404                                return $this->get_files( 'php', 1, true );
    408405                        case 'Stylesheet Files' :
    409                                 $files = $this->get_files( 'css' );
    410                                 if ( $this->parent() )
    411                                         $files = array_merge( $this->parent()->get_files( 'css' ), $files );
    412                                 return $files;
     406                                return $this->get_files( 'css', 0, false );
    413407                        case 'Template Dir' :
    414408                                return $this->get_template_directory();
    … …  
    507501         */
    508502        public function cache_delete() {
    509                 foreach ( array( 'theme', 'screenshot', 'screenshot_count', 'files', 'headers', 'page_templates' ) as $key )
     503                foreach ( array( 'theme', 'screenshot', 'screenshot_count', 'headers', 'page_templates' ) as $key )
    510504                        wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' );
    511505                $this->template = $this->textdomain_loaded = $this->theme_root_uri = $this->parent = $this->errors = $this->headers_sanitized = $this->name_translated = null;
    … …  
    967961
    968962        /**
    969          * Return files in the theme's directory. Does not return files found in the parent theme.
    970          *
    971          * @since 3.4.0
    972          * @access public
    973          *
    974          * @param string|null $type Optional. Type of files to return, either 'php' or 'css'. Defaults to null, for both.
    975          * @return array If a specific $type is requested, returns an array of PHP files. If no $type is requested,
    976          *      returns an array, with the keys being the file types, and the values being an array of files for those type.
    977          */
    978         public function get_files( $type ) {
    979                 $files = $this->cache_get( 'files' );
    980                 if ( ! is_array( $files ) ) {
    981                         $files = (array) self::scandir( $this->get_stylesheet_directory(), array( 'php', 'css' ), 1 );
    982                         foreach ( $files as &$group )
    983                                 ksort( $group );
    984                         $this->cache_add( 'files', $files );
    985                 }
    986 
    987                 if ( null === $type )
    988                         return $files;
    989                 elseif ( isset( $files[ $type ] ) )
    990                         return $files[ $type ];
    991 
    992                 return array();
     963         * Return files in the theme's directory.
     964         *
     965         * @since 3.4.0
     966         * @access public
     967         *
     968         * @param mixed $type Optional. Array of extensions to return. Defaults to all files (null).
     969         * @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite.
     970         * @param bool $search_parent Optional. Whether to return parent files. Defaults to false.
     971         * @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values
     972         *      being absolute paths.
     973         */
     974        public function get_files( $type = null, $depth = 0, $search_parent = false ) {
     975                $files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth );
     976
     977                if ( $search_parent && $this->parent() )
     978                        $files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
     979
     980                return $files;
    993981        }
    994982
    … …  
    1011999                        $page_templates = array();
    10121000
    1013                         $files = (array) self::scandir( $this->get_stylesheet_directory(), 'php', 1 );
    1014 
    1015                         foreach ( $files['php'] as $file => $full_path ) {
     1001                        $files = (array) $this->get_files( 'php', 1 );
     1002
     1003                        foreach ( $files as $file => $full_path ) {
    10161004                                $headers = get_file_data( $full_path, array( 'Template Name' => 'Template Name' ) );
    10171005                                if ( empty( $headers['Template Name'] ) )
    … …  
    10471035         *      for the found files, particularly when this function recurses to lower depths.
    10481036         */
    1049         private static function scandir( $path, $extensions, $depth = 0, $relative_path = '' ) {
     1037        private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
    10501038                if ( ! is_dir( $path ) )
    10511039                        return false;
    10521040
    1053                 $results = scandir( $path );
    1054 
    1055                 $extensions = (array) $extensions;
    1056                 $files = array_fill_keys( $extensions, array() );
    1057                 $_extensions = implode( '|', $extensions );
     1041                if ( $extensions ) {
     1042                        $extensions = (array) $extensions;
     1043                        $_extensions = implode( '|', $extensions );
     1044                }
    10581045
    10591046                $relative_path = trailingslashit( $relative_path );
    10601047                if ( '/' == $relative_path )
    10611048                        $relative_path = '';
     1049
     1050                $results = scandir( $path );
     1051                $files = array();
    10621052
    10631053                foreach ( $results as $result ) {
    … …  
    10691059                                $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result );
    10701060                                $files = array_merge_recursive( $files, $found );
    1071                         } elseif ( preg_match( '~\.(' . $_extensions . ')$~', $result, $match ) ) {
    1072                                 $files[ $match[1] ][ $relative_path . $result ] = $path . '/' . $result;
     1061                        } elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
     1062                                $files[ $relative_path . $result ] = $path . '/' . $result;
    10731063                        }
    10741064                }
Note: See TracChangeset for help on using the changeset viewer.