Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (6 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-filesystem-base.php

    r42228 r42343  
    5454     */
    5555    public function abspath() {
    56         $folder = $this->find_folder(ABSPATH);
     56        $folder = $this->find_folder( ABSPATH );
    5757        // Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
    58         if ( ! $folder && $this->is_dir( '/' . WPINC ) )
     58        if ( ! $folder && $this->is_dir( '/' . WPINC ) ) {
    5959            $folder = '/';
     60        }
    6061        return $folder;
    6162    }
     
    6970     */
    7071    public function wp_content_dir() {
    71         return $this->find_folder(WP_CONTENT_DIR);
     72        return $this->find_folder( WP_CONTENT_DIR );
    7273    }
    7374
     
    8081     */
    8182    public function wp_plugins_dir() {
    82         return $this->find_folder(WP_PLUGIN_DIR);
     83        return $this->find_folder( WP_PLUGIN_DIR );
    8384    }
    8485
     
    9596
    9697        // Account for relative theme roots
    97         if ( '/themes' == $theme_root || ! is_dir( $theme_root ) )
     98        if ( '/themes' == $theme_root || ! is_dir( $theme_root ) ) {
    9899            $theme_root = WP_CONTENT_DIR . $theme_root;
     100        }
    99101
    100102        return $this->find_folder( $theme_root );
     
    109111     */
    110112    public function wp_lang_dir() {
    111         return $this->find_folder(WP_LANG_DIR);
     113        return $this->find_folder( WP_LANG_DIR );
    112114    }
    113115
     
    129131     */
    130132    public function find_base_dir( $base = '.', $echo = false ) {
    131         _deprecated_function(__FUNCTION__, '2.7.0', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
     133        _deprecated_function( __FUNCTION__, '2.7.0', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
    132134        $this->verbose = $echo;
    133135        return $this->abspath();
     
    150152     */
    151153    public function get_base_dir( $base = '.', $echo = false ) {
    152         _deprecated_function(__FUNCTION__, '2.7.0', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
     154        _deprecated_function( __FUNCTION__, '2.7.0', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
    153155        $this->verbose = $echo;
    154156        return $this->abspath();
     
    167169     */
    168170    public function find_folder( $folder ) {
    169         if ( isset( $this->cache[ $folder ] ) )
     171        if ( isset( $this->cache[ $folder ] ) ) {
    170172            return $this->cache[ $folder ];
    171 
    172         if ( stripos($this->method, 'ftp') !== false ) {
     173        }
     174
     175        if ( stripos( $this->method, 'ftp' ) !== false ) {
    173176            $constant_overrides = array(
    174                 'FTP_BASE' => ABSPATH,
     177                'FTP_BASE'        => ABSPATH,
    175178                'FTP_CONTENT_DIR' => WP_CONTENT_DIR,
    176                 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR,
    177                 'FTP_LANG_DIR' => WP_LANG_DIR
     179                'FTP_PLUGIN_DIR'  => WP_PLUGIN_DIR,
     180                'FTP_LANG_DIR'    => WP_LANG_DIR,
    178181            );
    179182
    180183            // Direct matches ( folder = CONSTANT/ )
    181184            foreach ( $constant_overrides as $constant => $dir ) {
    182                 if ( ! defined( $constant ) )
     185                if ( ! defined( $constant ) ) {
    183186                    continue;
    184                 if ( $folder === $dir )
     187                }
     188                if ( $folder === $dir ) {
    185189                    return trailingslashit( constant( $constant ) );
     190                }
    186191            }
    187192
    188193            // Prefix Matches ( folder = CONSTANT/subdir )
    189194            foreach ( $constant_overrides as $constant => $dir ) {
    190                 if ( ! defined( $constant ) )
     195                if ( ! defined( $constant ) ) {
    191196                    continue;
     197                }
    192198                if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir
    193199                    $potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder );
     
    201207            }
    202208        } elseif ( 'direct' == $this->method ) {
    203             $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
    204             return trailingslashit($folder);
    205         }
    206 
    207         $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); // Strip out windows drive letter if it's there.
    208         $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
    209 
    210         if ( isset($this->cache[ $folder ] ) )
     209            $folder = str_replace( '\\', '/', $folder ); // Windows path sanitisation
     210            return trailingslashit( $folder );
     211        }
     212
     213        $folder = preg_replace( '|^([a-z]{1}):|i', '', $folder ); // Strip out windows drive letter if it's there.
     214        $folder = str_replace( '\\', '/', $folder ); // Windows path sanitisation
     215
     216        if ( isset( $this->cache[ $folder ] ) ) {
    211217            return $this->cache[ $folder ];
    212 
    213         if ( $this->exists($folder) ) { // Folder exists at that absolute path.
    214             $folder = trailingslashit($folder);
     218        }
     219
     220        if ( $this->exists( $folder ) ) { // Folder exists at that absolute path.
     221            $folder                 = trailingslashit( $folder );
    215222            $this->cache[ $folder ] = $folder;
    216223            return $folder;
    217224        }
    218         if ( $return = $this->search_for_folder($folder) )
     225        if ( $return = $this->search_for_folder( $folder ) ) {
    219226            $this->cache[ $folder ] = $return;
     227        }
    220228        return $return;
    221229    }
     
    234242     */
    235243    public function search_for_folder( $folder, $base = '.', $loop = false ) {
    236         if ( empty( $base ) || '.' == $base )
    237             $base = trailingslashit($this->cwd());
    238 
    239         $folder = untrailingslashit($folder);
     244        if ( empty( $base ) || '.' == $base ) {
     245            $base = trailingslashit( $this->cwd() );
     246        }
     247
     248        $folder = untrailingslashit( $folder );
    240249
    241250        if ( $this->verbose ) {
     
    244253        }
    245254
    246         $folder_parts = explode('/', $folder);
     255        $folder_parts     = explode( '/', $folder );
    247256        $folder_part_keys = array_keys( $folder_parts );
    248         $last_index = array_pop( $folder_part_keys );
    249         $last_path = $folder_parts[ $last_index ];
     257        $last_index       = array_pop( $folder_part_keys );
     258        $last_path        = $folder_parts[ $last_index ];
    250259
    251260        $files = $this->dirlist( $base );
    252261
    253262        foreach ( $folder_parts as $index => $key ) {
    254             if ( $index == $last_index )
     263            if ( $index == $last_index ) {
    255264                continue; // We want this to be caught by the next code block.
     265            }
    256266
    257267            /*
     
    262272             * cant find it, it'll return false for the entire function.
    263273             */
    264             if ( isset($files[ $key ]) ){
     274            if ( isset( $files[ $key ] ) ) {
    265275
    266276                // Let's try that folder:
    267                 $newdir = trailingslashit(path_join($base, $key));
     277                $newdir = trailingslashit( path_join( $base, $key ) );
    268278                if ( $this->verbose ) {
    269279                    /* translators: %s: directory name */
     
    273283                // Only search for the remaining path tokens in the directory, not the full path again.
    274284                $newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
    275                 if ( $ret = $this->search_for_folder( $newfolder, $newdir, $loop) )
     285                if ( $ret = $this->search_for_folder( $newfolder, $newdir, $loop ) ) {
    276286                    return $ret;
     287                }
    277288            }
    278289        }
     
    280291        // Only check this as a last resort, to prevent locating the incorrect install.
    281292        // All above procedures will fail quickly if this is the right branch to take.
    282         if (isset( $files[ $last_path ] ) ) {
     293        if ( isset( $files[ $last_path ] ) ) {
    283294            if ( $this->verbose ) {
    284295                /* translators: %s: directory name */
    285                 printf( "\n" . __( 'Found %s' ) . "<br/>\n",  $base . $last_path );
     296                printf( "\n" . __( 'Found %s' ) . "<br/>\n", $base . $last_path );
    286297            }
    287             return trailingslashit($base . $last_path);
     298            return trailingslashit( $base . $last_path );
    288299        }
    289300
    290301        // Prevent this function from looping again.
    291302        // No need to proceed if we've just searched in /
    292         if ( $loop || '/' == $base )
     303        if ( $loop || '/' == $base ) {
    293304            return false;
     305        }
    294306
    295307        // As an extra last resort, Change back to / if the folder wasn't found.
     
    311323     * @return string The *nix-style representation of permissions.
    312324     */
    313     public function gethchmod( $file ){
     325    public function gethchmod( $file ) {
    314326        $perms = intval( $this->getchmod( $file ), 8 );
    315         if (($perms & 0xC000) == 0xC000) // Socket
     327        if ( ( $perms & 0xC000 ) == 0xC000 ) { // Socket
    316328            $info = 's';
    317         elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
     329        } elseif ( ( $perms & 0xA000 ) == 0xA000 ) { // Symbolic Link
    318330            $info = 'l';
    319         elseif (($perms & 0x8000) == 0x8000) // Regular
     331        } elseif ( ( $perms & 0x8000 ) == 0x8000 ) { // Regular
    320332            $info = '-';
    321         elseif (($perms & 0x6000) == 0x6000) // Block special
     333        } elseif ( ( $perms & 0x6000 ) == 0x6000 ) { // Block special
    322334            $info = 'b';
    323         elseif (($perms & 0x4000) == 0x4000) // Directory
     335        } elseif ( ( $perms & 0x4000 ) == 0x4000 ) { // Directory
    324336            $info = 'd';
    325         elseif (($perms & 0x2000) == 0x2000) // Character special
     337        } elseif ( ( $perms & 0x2000 ) == 0x2000 ) { // Character special
    326338            $info = 'c';
    327         elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
     339        } elseif ( ( $perms & 0x1000 ) == 0x1000 ) { // FIFO pipe
    328340            $info = 'p';
    329         else // Unknown
     341        } else { // Unknown
    330342            $info = 'u';
     343        }
    331344
    332345        // Owner
    333         $info .= (($perms & 0x0100) ? 'r' : '-');
    334         $info .= (($perms & 0x0080) ? 'w' : '-');
    335         $info .= (($perms & 0x0040) ?
    336                     (($perms & 0x0800) ? 's' : 'x' ) :
    337                     (($perms & 0x0800) ? 'S' : '-'));
     346        $info .= ( ( $perms & 0x0100 ) ? 'r' : '-' );
     347        $info .= ( ( $perms & 0x0080 ) ? 'w' : '-' );
     348        $info .= ( ( $perms & 0x0040 ) ?
     349                    ( ( $perms & 0x0800 ) ? 's' : 'x' ) :
     350                    ( ( $perms & 0x0800 ) ? 'S' : '-' ) );
    338351
    339352        // Group
    340         $info .= (($perms & 0x0020) ? 'r' : '-');
    341         $info .= (($perms & 0x0010) ? 'w' : '-');
    342         $info .= (($perms & 0x0008) ?
    343                     (($perms & 0x0400) ? 's' : 'x' ) :
    344                     (($perms & 0x0400) ? 'S' : '-'));
     353        $info .= ( ( $perms & 0x0020 ) ? 'r' : '-' );
     354        $info .= ( ( $perms & 0x0010 ) ? 'w' : '-' );
     355        $info .= ( ( $perms & 0x0008 ) ?
     356                    ( ( $perms & 0x0400 ) ? 's' : 'x' ) :
     357                    ( ( $perms & 0x0400 ) ? 'S' : '-' ) );
    345358
    346359        // World
    347         $info .= (($perms & 0x0004) ? 'r' : '-');
    348         $info .= (($perms & 0x0002) ? 'w' : '-');
    349         $info .= (($perms & 0x0001) ?
    350                     (($perms & 0x0200) ? 't' : 'x' ) :
    351                     (($perms & 0x0200) ? 'T' : '-'));
     360        $info .= ( ( $perms & 0x0004 ) ? 'r' : '-' );
     361        $info .= ( ( $perms & 0x0002 ) ? 'w' : '-' );
     362        $info .= ( ( $perms & 0x0001 ) ?
     363                    ( ( $perms & 0x0200 ) ? 't' : 'x' ) :
     364                    ( ( $perms & 0x0200 ) ? 'T' : '-' ) );
    352365        return $info;
    353366    }
     
    369382     * Converts '-rw-r--r--' to 0644
    370383     * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
    371      *
     384     *
    372385     * @link https://secure.php.net/manual/en/function.chmod.php#49614
    373386     *
     
    379392    public function getnumchmodfromh( $mode ) {
    380393        $realmode = '';
    381         $legal =  array('', 'w', 'r', 'x', '-');
    382         $attarray = preg_split('//', $mode);
     394        $legal    = array( '', 'w', 'r', 'x', '-' );
     395        $attarray = preg_split( '//', $mode );
    383396
    384397        for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
    385             if ($key = array_search($attarray[$i], $legal)) {
    386                 $realmode .= $legal[$key];
     398            if ( $key = array_search( $attarray[ $i ], $legal ) ) {
     399                $realmode .= $legal[ $key ];
    387400            }
    388401        }
    389402
    390         $mode = str_pad($realmode, 10, '-', STR_PAD_LEFT);
    391         $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
    392         $mode = strtr($mode,$trans);
    393 
    394         $newmode = $mode[0];
     403        $mode  = str_pad( $realmode, 10, '-', STR_PAD_LEFT );
     404        $trans = array(
     405            '-' => '0',
     406            'r' => '4',
     407            'w' => '2',
     408            'x' => '1',
     409        );
     410        $mode  = strtr( $mode, $trans );
     411
     412        $newmode  = $mode[0];
    395413        $newmode .= $mode[1] + $mode[2] + $mode[3];
    396414        $newmode .= $mode[4] + $mode[5] + $mode[6];
     
    540558     * @since 2.5.0
    541559     * @abstract
    542      * 
     560     *
    543561     * @param string $file Path to the file.
    544562     * @return string|bool Username of the user or false on error.
Note: See TracChangeset for help on using the changeset viewer.