Ticket #7875: 7875.diff

File 7875.diff, 52.5 KB (added by DD32, 3 years ago)
  • wp-admin/includes/class-wp-filesystem-base.php

     
    4646         * @return string The location of the remote path. 
    4747         */ 
    4848        function abspath() { 
    49                 if ( defined('FTP_BASE') && strpos($this->method, 'ftp') !== false ) 
    50                         return FTP_BASE; 
    5149                $folder = $this->find_folder(ABSPATH); 
    5250                //Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare. 
    5351                if ( ! $folder && $this->is_dir('/wp-includes') ) 
     
    6260         * @return string The location of the remote path. 
    6361         */ 
    6462        function wp_content_dir() { 
    65                 if ( defined('FTP_CONTENT_DIR') && strpos($this->method, 'ftp') !== false ) 
    66                         return FTP_CONTENT_DIR; 
    6763                return $this->find_folder(WP_CONTENT_DIR); 
    6864        } 
    6965        /** 
     
    7571         * @return string The location of the remote path. 
    7672         */ 
    7773        function wp_plugins_dir() { 
    78                 if ( defined('FTP_PLUGIN_DIR') && strpos($this->method, 'ftp') !== false ) 
    79                         return FTP_PLUGIN_DIR; 
    8074                return $this->find_folder(WP_PLUGIN_DIR); 
    8175        } 
    8276        /** 
     
    142136         */ 
    143137        function find_folder($folder) { 
    144138 
    145                 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there. 
     139                if ( strpos($this->method, 'ftp') !== false ) { 
     140                        $constant_overrides = array( 'FTP_BASE' => ABSPATH, 'FTP_CONTENT_DIR' => WP_CONTENT_DIR, 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR ); 
     141                        foreach ( $constant_overrides as $constant => $dir ) 
     142                                if ( defined($constant) && $folder === $dir ) 
     143                                        return trailingslashit(constant($constant)); 
     144                } elseif ( 'direct' == $this->method ) { 
     145                        return trailingslashit($folder); 
     146                } 
     147 
     148                $folder = preg_replace('|^([a-z]{1}):|i', '', $the_folder); //Strip out windows driveletter if its there. 
    146149                $folder = str_replace('\\', '/', $folder); //Windows path sanitiation 
    147150 
    148151                if ( isset($this->cache[ $folder ] ) ) 
    149152                        return $this->cache[ $folder ]; 
    150153 
    151154                if ( $this->exists($folder) ) { //Folder exists at that absolute path. 
     155                        $folder = trailingslashit($folder); 
    152156                        $this->cache[ $folder ] = $folder; 
    153157                        return $folder; 
    154158                } 
     
    189193                        // If its found, change into it and follow through looking for it. 
    190194                        // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on. 
    191195                        // If it reaches the end, and still cant find it, it'll return false for the entire function. 
    192                         if( isset($files[ $key ]) ){ 
     196                        if ( isset($files[ $key ]) ){ 
    193197                                //Lets try that folder: 
    194198                                $newdir = trailingslashit(path_join($base, $key)); 
    195                                 if( $this->verbose ) 
     199                                if ( $this->verbose ) 
    196200                                        printf( __('Changing to %s') . '<br/>', $newdir ); 
    197                                 if( $ret = $this->search_for_folder( $folder, $newdir, $loop) ) 
     201                                if ( $ret = $this->search_for_folder( $folder, $newdir, $loop) ) 
    198202                                        return $ret; 
    199203                        } 
    200204                } 
    201205 
    202206                //Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take. 
    203                 if(isset( $files[ $last_path ] ) ) { 
    204                         if( $this->verbose ) 
     207                if (isset( $files[ $last_path ] ) ) { 
     208                        if ( $this->verbose ) 
    205209                                printf( __('Found %s') . '<br/>',  $base . $last_path ); 
    206                         return $base . $last_path; 
     210                        return trailingslashit($base . $last_path); 
    207211                } 
    208                 if( $loop ) 
     212                if ( $loop ) 
    209213                        return false;//Prevent tihs function looping again. 
    210214                //As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups. 
    211215                return $this->search_for_folder($folder, '/', true); 
  • wp-admin/includes/class-wp-filesystem-direct.php

     
    4949                return @chdir($dir); 
    5050        } 
    5151        function chgrp($file, $group, $recursive = false) { 
    52                 if( ! $this->exists($file) ) 
     52                if ( ! $this->exists($file) ) 
    5353                        return false; 
    54                 if( ! $recursive ) 
     54                if ( ! $recursive ) 
    5555                        return @chgrp($file, $group); 
    56                 if( ! $this->is_dir($file) ) 
     56                if ( ! $this->is_dir($file) ) 
    5757                        return @chgrp($file, $group); 
    5858                //Is a directory, and we want recursive 
    5959                $file = trailingslashit($file); 
    6060                $filelist = $this->dirlist($file); 
    61                 foreach($filelist as $filename) 
     61                foreach ($filelist as $filename) 
    6262                        $this->chgrp($file . $filename, $group, $recursive); 
    6363 
    6464                return true; 
    6565        } 
    6666        function chmod($file, $mode = false, $recursive = false) { 
    67                 if( ! $mode ) 
     67                if ( ! $mode ) 
    6868                        $mode = $this->permission; 
    69                 if( ! $this->exists($file) ) 
     69                if ( ! $this->exists($file) ) 
    7070                        return false; 
    71                 if( ! $recursive ) 
     71                if ( ! $recursive ) 
    7272                        return @chmod($file,$mode); 
    73                 if( ! $this->is_dir($file) ) 
     73                if ( ! $this->is_dir($file) ) 
    7474                        return @chmod($file, $mode); 
    7575                //Is a directory, and we want recursive 
    7676                $file = trailingslashit($file); 
    7777                $filelist = $this->dirlist($file); 
    78                 foreach($filelist as $filename) 
     78                foreach ($filelist as $filename) 
    7979                        $this->chmod($file . $filename, $mode, $recursive); 
    8080 
    8181                return true; 
    8282        } 
    8383        function chown($file, $owner, $recursive = false) { 
    84                 if( ! $this->exists($file) ) 
     84                if ( ! $this->exists($file) ) 
    8585                        return false; 
    86                 if( ! $recursive ) 
     86                if ( ! $recursive ) 
    8787                        return @chown($file, $owner); 
    88                 if( ! $this->is_dir($file) ) 
     88                if ( ! $this->is_dir($file) ) 
    8989                        return @chown($file, $owner); 
    9090                //Is a directory, and we want recursive 
    9191                $filelist = $this->dirlist($file); 
    92                 foreach($filelist as $filename){ 
     92                foreach ($filelist as $filename){ 
    9393                        $this->chown($file . '/' . $filename, $owner, $recursive); 
    9494                } 
    9595                return true; 
    9696        } 
    9797        function owner($file) { 
    9898                $owneruid = @fileowner($file); 
    99                 if( ! $owneruid ) 
     99                if ( ! $owneruid ) 
    100100                        return false; 
    101                 if( ! function_exists('posix_getpwuid') ) 
     101                if ( ! function_exists('posix_getpwuid') ) 
    102102                        return $owneruid; 
    103103                $ownerarray = posix_getpwuid($owneruid); 
    104104                return $ownerarray['name']; 
    105105        } 
    106106        function getchmod($file) { 
    107                 return @fileperms($file); 
     107                return substr(decoct(@fileperms($file)),3); 
    108108        } 
    109109        function group($file) { 
    110110                $gid = @filegroup($file); 
    111                 if( ! $gid ) 
     111                if ( ! $gid ) 
    112112                        return false; 
    113                 if( ! function_exists('posix_getgrgid') ) 
     113                if ( ! function_exists('posix_getgrgid') ) 
    114114                        return $gid; 
    115115                $grouparray = posix_getgrgid($gid); 
    116116                return $grouparray['name']; 
    117117        } 
    118118 
    119119        function copy($source, $destination, $overwrite = false) { 
    120                 if( ! $overwrite && $this->exists($destination) ) 
     120                if ( ! $overwrite && $this->exists($destination) ) 
    121121                        return false; 
    122122                return copy($source, $destination); 
    123123        } 
    124124 
    125125        function move($source, $destination, $overwrite = false) { 
    126126                //Possible to use rename()? 
    127                 if( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){ 
     127                if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){ 
    128128                        $this->delete($source); 
    129129                        return true; 
    130130                } else { 
     
    135135        function delete($file, $recursive = false) { 
    136136                $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise 
    137137 
    138                 if( $this->is_file($file) ) 
     138                if ( $this->is_file($file) ) 
    139139                        return @unlink($file); 
    140                 if( ! $recursive && $this->is_dir($file) ) 
     140                if ( ! $recursive && $this->is_dir($file) ) 
    141141                        return @rmdir($file); 
    142142 
    143143                //At this point its a folder, and we're in recursive mode 
     
    145145                $filelist = $this->dirlist($file, true); 
    146146 
    147147                $retval = true; 
    148                 if( is_array($filelist) ) //false if no files, So check first. 
    149                         foreach($filelist as $filename => $fileinfo) 
    150                                 if( ! $this->delete($file . $filename, $recursive) ) 
     148                if ( is_array($filelist) ) //false if no files, So check first. 
     149                        foreach ($filelist as $filename => $fileinfo) 
     150                                if ( ! $this->delete($file . $filename, $recursive) ) 
    151151                                        $retval = false; 
    152152 
    153                 if( ! @rmdir($file) ) 
    154                         return false; 
     153                if ( file_exists($file) && ! @rmdir($file) ) 
     154                        $retval = false; 
    155155                return $retval; 
    156156        } 
    157157 
     
    187187        } 
    188188 
    189189        function touch($file, $time = 0, $atime = 0){ 
    190                 if($time == 0) 
     190                if ($time == 0) 
    191191                        $time = time(); 
    192                 if($atime == 0) 
     192                if ($atime == 0) 
    193193                        $atime = time(); 
    194194                return @touch($file, $time, $atime); 
    195195        } 
    196196 
    197197        function mkdir($path, $chmod = false, $chown = false, $chgrp = false){ 
    198                 if( ! $chmod) 
     198                if ( ! $chmod) 
    199199                        $chmod = $this->permission; 
    200200 
    201                 if( ! @mkdir($path, $chmod) ) 
     201                if ( ! @mkdir($path, $chmod) ) 
    202202                        return false; 
    203                 if( $chown ) 
     203                if ( $chown ) 
    204204                        $this->chown($path, $chown); 
    205                 if( $chgrp ) 
     205                if ( $chgrp ) 
    206206                        $this->chgrp($path, $chgrp); 
    207207                return true; 
    208208        } 
    209209 
    210210        function rmdir($path, $recursive = false) { 
    211211                //Currently unused and untested, Use delete() instead. 
    212                 if( ! $recursive ) 
     212                if ( ! $recursive ) 
    213213                        return @rmdir($path); 
    214214                //recursive: 
    215215                $filelist = $this->dirlist($path); 
    216                 foreach($filelist as $filename => $det) { 
     216                foreach ($filelist as $filename => $det) { 
    217217                        if ( '/' == substr($filename, -1, 1) ) 
    218218                                $this->rmdir($path . '/' . $filename, $recursive); 
    219219                        @rmdir($filename); 
     
    222222        } 
    223223 
    224224        function dirlist($path, $incdot = false, $recursive = false) { 
    225                 if( $this->is_file($path) ) { 
     225                if ( $this->is_file($path) ) { 
    226226                        $limitFile = basename($path); 
    227227                        $path = dirname($path); 
    228228                } else { 
    229229                        $limitFile = false; 
    230230                } 
    231                 if( ! $this->is_dir($path) ) 
     231                if ( ! $this->is_dir($path) ) 
    232232                        return false; 
    233233 
    234234                $ret = array(); 
     
    239239                        $struc = array(); 
    240240                        $struc['name'] = $entry; 
    241241 
    242                         if( '.' == $struc['name'] || '..' == $struc['name'] ) 
     242                        if ( '.' == $struc['name'] || '..' == $struc['name'] ) 
    243243                                continue; //Do not care about these folders. 
    244                         if( '.' == $struc['name'][0] && !$incdot) 
     244                        if ( '.' == $struc['name'][0] && !$incdot) 
    245245                                continue; 
    246                         if( $limitFile && $struc['name'] != $limitFile) 
     246                        if ( $limitFile && $struc['name'] != $limitFile) 
    247247                                continue; 
    248248 
    249249                        $struc['perms']         = $this->gethchmod($path.'/'.$entry); 
     
    258258                        $struc['type']          = $this->is_dir($path.'/'.$entry) ? 'd' : 'f'; 
    259259 
    260260                        if ( 'd' == $struc['type'] ) { 
    261                                 if( $recursive ) 
     261                                if ( $recursive ) 
    262262                                        $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive); 
    263263                                else 
    264264                                        $struc['files'] = array(); 
  • wp-admin/includes/class-wp-filesystem-ftpext.php

     
    169169        } 
    170170        function getchmod($file) { 
    171171                $dir = $this->dirlist($file); 
    172                 return $dir[$file]['permsn']; 
     172                return $this->getnumchmodfromh( $dir[basename($file)]['perms'] ); 
    173173        } 
    174174        function group($file) { 
    175175                $dir = $this->dirlist($file); 
     
    321321        } 
    322322 
    323323        function dirlist($path = '.', $incdot = false, $recursive = false) { 
    324                 if( $this->is_file($path) ) { 
    325                         $limitFile = basename($path); 
    326                         $path = dirname($path) . '/'; 
     324 
     325                if ( substr($path, -1) !== '/') { 
     326                        $limit = basename($path); 
     327                        $path = trailingslashit(dirname($path)); 
    327328                } else { 
    328                         $limitFile = false; 
     329                        $limit = false; 
    329330                } 
    330331 
    331332                $list = @ftp_rawlist($this->link, '-a ' . $path, false); 
     
    339340                        if ( empty($entry) ) 
    340341                                continue; 
    341342 
    342                         if ( '.' == $entry["name"] || '..' == $entry["name"] ) 
     343                        if ( '.' == $entry['name'] || '..' == $entry['name'] ) 
    343344                                continue; 
    344345 
     346                        if ( $limit && $entry['name'] != $limit ) 
     347                                continue; 
     348 
    345349                        $dirlist[ $entry['name'] ] = $entry; 
    346350                } 
    347351 
  • wp-admin/includes/class-wp-filesystem-ftpsockets.php

     
    186186 
    187187        function getchmod($file) { 
    188188                $dir = $this->dirlist($file); 
    189                 return $dir[$file]['permsn']; 
     189                return $this->getnumchmodfromh( $dir[basename($file)]['perms'] ); 
    190190        } 
    191191 
    192192        function group($file) { 
     
    281281        } 
    282282 
    283283        function dirlist($path = '.', $incdot = false, $recursive = false ) { 
    284                 if( $this->is_file($path) ) { 
    285                         $limitFile = basename($path); 
    286                         $path = dirname($path) . '/'; 
     284 
     285                if ( substr($path, -1) !== '/') { 
     286                        $limit = basename($path); 
     287                        $path = trailingslashit(dirname($path)); 
    287288                } else { 
    288                         $limitFile = false; 
     289                        $limit = false; 
    289290                } 
    290291 
    291292                $list = $this->ftp->dirlist($path); 
    292293                if( ! $list ) 
    293294                        return false; 
     295 
    294296                if( empty($list) ) 
    295297                        return array(); 
    296298 
    297299                $ret = array(); 
    298300                foreach ( $list as $struc ) { 
     301                        if ( $limit && $struc['name'] != $limit ) 
     302                                continue; 
    299303 
    300304                        if ( 'd' == $struc['type'] ) { 
    301305                                $struc['files'] = array(); 
  • wp-admin/includes/class-wp-upgrader.php

     
     1<?php 
     2/** 
     3 * This file is an attempt at an abstracted version of the plugin/theme/core installer/upgrader which can be used interchangably for all uses needed within WordPress. 
     4 * It is designed to be as flexible as possible, but some logic may seem rather, crazy to say the least. 
     5 * Yes, this header is designed to be replaced before commiting, Hopefully i'll get some proper documentation in here. 
     6 * 
     7 * Oh yeah, this needs testing with FTP, only tested with Direct class. 
     8 */ 
     9class WP_Upgrader { 
     10        var $strings = array(); //Set in child classes constructor. 
     11         
     12        function generic_strings() { 
     13                $this->strings['bad_request'] = __('Invalid Data provided.'); 
     14                $this->strings['fs_unavailable'] = __('Could not access filesystem.'); 
     15                $this->strings['fs_error'] = __('Filesystem error'); 
     16                $this->strings['fs_no_root_dir'] = __('Unable to locate WordPress Root directory.'); 
     17                $this->strings['fs_no_content_dir'] = __('Unable to locate WordPress Content directory (wp-content).'); 
     18                $this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress Plugin directory.'); 
     19                $this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress Theme directory.'); 
     20                $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).'); 
     21 
     22                $this->strings['download_failed'] = __('Download failed.'); 
     23                $this->strings['installing_package'] = __('Installing the latest version.'); 
     24                $this->strings['folder_exists'] = __('Destination folder already exists.'); 
     25                $this->strings['mkdir_failed'] = __('Could not create directory.'); 
     26                $this->strings['bad_package'] = __('Incompatible Archive'); 
     27        } 
     28         
     29        function feedback($string) { 
     30                //TODO Front end fixing. 
     31                $str = $this->strings[$string]; 
     32                  
     33                $args = func_get_args(); 
     34                $args = array_splice($args, 1); 
     35                if ( !empty($args) ) 
     36                        $str = vsprintf($str, $args); 
     37                 
     38                return apply_filters('update_feedback', $str); 
     39        } 
     40 
     41        function fs_connect( $directories = array() ) { 
     42                global $wp_filesystem; 
     43         
     44                // Is a filesystem accessor setup? 
     45                if ( ! $wp_filesystem || ! is_object($wp_filesystem) ) 
     46                        WP_Filesystem(); 
     47         
     48                if ( ! is_object($wp_filesystem) ) 
     49                        return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] ); 
     50         
     51                if ( $wp_filesystem->errors->get_error_code() ) 
     52                        return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors); 
     53 
     54                foreach ( (array)$directories as $dir ) { 
     55                        if ( ABSPATH == $dir && ! $wp_filesystem->abspath() ) 
     56                                return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']); 
     57 
     58                        elseif ( WP_CONTENT_DIR == $dir && ! $wp_filesystem->wp_content_dir() ) 
     59                                return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']); 
     60 
     61                        elseif ( WP_PLUGIN_DIR == $dir && ! $wp_filesystem->wp_plugins_dir() ) 
     62                                return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']); 
     63 
     64                        elseif ( WP_CONTENT_DIR . '/themes' == $dir && ! $wp_filesystem->find_folder(WP_CONTENT_DIR . '/themes') ) 
     65                                return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']); 
     66 
     67                        elseif ( ! $wp_filesystem->find_folder($dir) ) 
     68                                return new WP_Error('fs_no_folder', sprintf($strings['fs_no_folder'], $dir)); 
     69                } 
     70        } //end fs_connect(); 
     71 
     72        function download_package($package) { 
     73 
     74                if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote? 
     75                        return $package; //must be a local file.. 
     76                 
     77                if ( empty($package) ) 
     78                        return new WP_Error('no_package', $this->strings['no_package']); 
     79 
     80                $this->feedback('downloading_package', $package); 
     81 
     82                $download_file = download_url($package); //TODO, move to class? 
     83         
     84                if ( is_wp_error($download_file) ) 
     85                        return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message()); 
     86                 
     87                return $download_file; 
     88        } 
     89         
     90        function unpack_package($package, $delete_package = true) { 
     91                global $wp_filesystem; 
     92                 
     93                $this->feedback('unpack_package'); 
     94 
     95                //We need a working directory 
     96                $working_dir = $wp_filesystem->wp_content_dir() . 'upgrade/' . basename($package, '.zip'); 
     97 
     98                // Clean up working directory 
     99                if ( $wp_filesystem->is_dir($working_dir) ) 
     100                        $wp_filesystem->delete($working_dir, true); 
     101 
     102                // Unzip package to working directory 
     103                $result = unzip_file($package, $working_dir); //TODO: Move to class? 
     104 
     105                // Once extracted, delete the package 
     106                unlink($package); 
     107 
     108                if ( is_wp_error($result) ) { 
     109                        $wp_filesystem->delete($working_dir, true); 
     110                        return $result; 
     111                } 
     112                 
     113                return $working_dir; 
     114        } 
     115         
     116        //TODO: Better variable naming. 
     117        function install_package($args = array()) { 
     118                global $wp_filesystem; 
     119                $defaults = array( 'source' => '', 'destination' => '', //Please always pass these 
     120                                                'clear_destination' => false, 'clear_working' => false, 
     121                                                'hook_extra' => array()); 
     122 
     123                $r = wp_parse_args($args, $defaults); 
     124                extract($r); 
     125                 
     126                if ( empty($source) || empty($destination) ) 
     127                        return new WP_Error('bad_request', $this->strings['bad_request']); 
     128                 
     129                $this->feedback('installing_package'); 
     130 
     131                $res = apply_filters('upgrader_pre_install', $hook_extra); 
     132                if ( is_wp_error($res) ) 
     133                        return $res; 
     134 
     135                //Retain the Original source and destinations 
     136                $the_source = $source; 
     137                $local_destination = $destination; 
     138                 
     139                $source_files = array_keys( $wp_filesystem->dirlist($the_source) ); 
     140                $remote_destination = $wp_filesystem->find_folder($local_destination); 
     141 
     142                //Locate which directory to copy to the new folder, This is based on the actual folder holding the files. 
     143                if ( count($source_files) > 1 ) //If multiple files, then we want the parent directory as source. //Note, This can be changed via the filter 'upgrader_source_selection' 
     144                        $source = trailingslashit(dirname($source)); 
     145                elseif ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0]) ) //Only one folder? Then we want its contents. 
     146                        $source = trailingslashit($source) . trailingslashit($source_files[0]); 
     147                elseif ( count($source_files) == 0 ) 
     148                        return new WP_Error('bad_package', $this->strings['bad_package']); //There are no files? 
     149                //else //Its only a single file, I guess this can just be dumped into the destination folder hopefully. 
     150                 
     151                //Hook ability to change the source file location.. 
     152                $source = apply_filters('upgrader_source_selection', $source, $the_source, &$this); 
     153                if ( is_wp_error($source) ) 
     154                        return $source; 
     155                 
     156                //Has the source location changed? If so, we need a new source_files list. 
     157                if ( $source !== $the_source ) 
     158                        $source_files = array_keys( $wp_filesystem->dirlist($source) ); 
     159                 
     160                //Protection against deleting files in any important base directories. 
     161                if ( in_array( $destination, array(ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes') ) ) { 
     162                        $remote_destination = trailingslashit($remote_destination) . trailingslashit(basename($source)); 
     163                        $destination = trailingslashit($destination) . trailingslashit(basename($source)); 
     164                } 
     165 
     166                //If we're not clearing the destination folder, and something exists there allready, Bail. 
     167                if ( ! $clear_destination && $wp_filesystem->exists($remote_destination) ) { 
     168                        $wp_filesystem->delete($working_dir, true); 
     169                        return new WP_Error('folder_exists', $this->strings['folder_exists'], $filelist[0] ); 
     170                } else if ( $clear_destination ) { 
     171                        //We're going to clear the destination if theres something there 
     172                        $this->feedback('remove_old'); 
     173 
     174                        $removed = true; 
     175                        if ( $wp_filesystem->exists($remote_destination) ) 
     176                                $removed = $wp_filesystem->delete($remote_destination, true); 
     177 
     178                        $removed = apply_filters('upgrader_clear_destination', $removed, $local_destination, $remote_destination, $hook_extra); 
     179 
     180                        if ( is_wp_error($removed) ) 
     181                                return $removed; 
     182                        elseif ( ! $removed ) 
     183                                return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']); 
     184                } 
     185 
     186                //The folder name of the destination folder. will get put into the destiination folder, ie. WP_PLUGIN_DIR/SOURCE_DIR/ 
     187                $source_folder = basename($source); 
     188                 
     189                //Create destination if needed 
     190                if ( !$wp_filesystem->exists($remote_destination) ) 
     191                        if ( !$wp_filesystem->mkdir($remote_destination) ) 
     192                                return new WP_Error('mkdir_failed', $this->strings['mkdir_failed'], $remote_destination); 
     193                 
     194                // Copy new version of item into place. 
     195                $result = copy_dir($source, $remote_destination); 
     196                if ( is_wp_error($result) ) { 
     197                        if ( $clear_working ) 
     198                                $wp_filesystem->delete($the_source, true); 
     199                        return $result; 
     200                } 
     201                 
     202                //Clear the Working folder?  
     203                if ( $clear_working ) 
     204                        $wp_filesystem->delete($the_source, true); 
     205 
     206                //Bombard the calling function will all the info which we've just used. 
     207                return compact('the_source', 'source', 'source_folder', 'source_files', 'destination', 'remote_destination', 'clear_destination', 'delete_source_dir'); 
     208        } 
     209         
     210        function run($options) { 
     211 
     212                $defaults = array(      'package' => '', //Please always pass this. 
     213                                                        'destination' => '', //And this 
     214                                                        'clear_destination' => false, 
     215                                                        'clear_working' => true, 
     216                                                        'hook_extra' => array() //Pass any extra $hook_extra args here, this will be passed to any hooked filters. 
     217                                                ); 
     218 
     219                $options = wp_parse_args($args, $options); 
     220                extract($options); 
     221         
     222                //Connect to the Filesystem first. 
     223                $res = $this->fs_connect( array(WP_CONTENT_DIR, $destination) ); 
     224                if ( is_wp_error($res) ) 
     225                        return $res; 
     226                 
     227                //Download the package (Note, This just returns the filename of the file if the package is a local file) 
     228                $download = $this->download_package( $package ); 
     229                if ( is_wp_error($download) ) 
     230                        return $download; 
     231                 
     232                //Unzip's the file into a temporary directory 
     233                $working_dir = $this->unpack_package( $download ); 
     234                if ( is_wp_error($working_dir) ) 
     235                        return $working_dir; 
     236 
     237                //With the given options, this installs it to the destination directory. 
     238                return $this->install_package( array(  
     239                                                                                        'source' => $working_dir, 
     240                                                                                        'destination' => $destination, 
     241                                                                                        'clear_destination' => $clear_destination, 
     242                                                                                        'clear_working' => $clear_working, 
     243                                                                                        'hook_extra' => $hook_extra 
     244                                                                                ) ); 
     245        } 
     246         
     247} 
     248 
     249class Plugin_Upgrader extends WP_Upgrader { 
     250 
     251        function upgrade_strings() { 
     252                $this->generic_strings(); 
     253                $this->strings['up_to_date'] = __('The plugin is at the latest version.'); 
     254                $this->strings['no_package'] = __('Upgrade package not available.'); 
     255                $this->strings['downloading_package'] = __('Downloading update from %s.'); 
     256                $this->strings['unpack_package'] = __('Unpacking the update.'); 
     257                $this->strings['deactivate_plugin'] = __('Deactivating the plugin.'); 
     258                $this->strings['remove_old'] = __('Removing the old version of the plugin.'); 
     259                $this->strings['remove_old_failed'] = __('Could not remove the old plugin.'); 
     260        } 
     261 
     262        function install_strings() { 
     263                $this->generic_strings(); 
     264                $this->strings['no_package'] = __('Install package not available.'); 
     265                $this->strings['downloading_package'] = __('Downloading install package from %s.'); 
     266                $this->strings['unpack_package'] = __('Unpacking the package.'); 
     267        } 
     268 
     269        function install($package) { 
     270                 
     271                $this->install_strings(); 
     272 
     273                $options = array( 
     274                                                'package' => $package, 
     275                                                'destination' => WP_PLUGIN_DIR, 
     276                                                'clear_destination' => false, //Do not overwrite files. 
     277                                                'clear_working' => true, 
     278                                                'hook_extra' => array() 
     279                                                ); 
     280                 
     281                $result = $this->run($options); 
     282         
     283                // Force refresh of plugin update information 
     284                delete_transient('update_plugins'); 
     285         
     286                if( empty($result['destination_name']) ) 
     287                        return false;  
     288 
     289                $plugin = get_plugins('/' . $result['destination_name']); //Ensure to pass with leading slash 
     290                $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 
     291         
     292                return $result['destination_name']. '/' . $pluginfiles[0]; 
     293        } 
     294 
     295        function upgrade($plugin) { 
     296                 
     297                $this->upgrade_strings(); 
     298                 
     299                $current = get_transient( 'update_plugins' ); 
     300                if ( !isset( $current->response[ $plugin ] ) ) 
     301                        return new WP_Error('up_to_date', $this->strings['up_to_date']); 
     302 
     303                // Get the URL to the zip file 
     304                $r = $current->response[ $plugin ]; 
     305                 
     306                add_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade')); 
     307                add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4); 
     308                //'source_selection' => array(&$this, 'source_selection'), //theres a track ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins. 
     309                 
     310                $options = array( 
     311                                        'package' => $r->package, 
     312                                        'destination' => WP_PLUGIN_DIR, 
     313                                        'clear_destination' => true, 
     314                                        'clear_working' => true, 
     315                                        'hook_extra' => array( 
     316                                                                'plugin' => $plugin 
     317                                        ) 
     318                                ); 
     319                 
     320                $result = $this->run($options); 
     321 
     322                //Cleanup our hooks, incase something else does a upgrade on this connection. 
     323                remove_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade')); 
     324                remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin')); 
     325 
     326                if ( is_wp_error($result) ) 
     327                        return $result; 
     328 
     329                // Force refresh of plugin update information 
     330                delete_transient('update_plugins'); 
     331 
     332                if( empty($result['source_folder']) ) 
     333                        return false;  
     334 
     335                $plugin = get_plugins('/' . $result['source_folder']); //Ensure to pass with leading slash 
     336                $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 
     337         
     338                return $result['source_folder'] . '/' . $pluginfiles[0]; 
     339        } 
     340 
     341        //Hooked to pre_install 
     342        function deactivate_plugin_before_upgrade($plugin) { 
     343 
     344                $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : ''; 
     345                if ( empty($plugin) ) 
     346                        return new WP_Error('bad_request', $this->strings['bad_request']); 
     347 
     348                if ( is_plugin_active($plugin) ) { 
     349                        $this->feedback('deactivate_plugin'); 
     350                        //Deactivate the plugin silently, Prevent deactivation hooks from running. 
     351                        deactivate_plugins($plugin, true); 
     352                }        
     353        } 
     354 
     355        //Hooked to upgrade_clear_destination 
     356        function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) { 
     357                global $wp_filesystem; 
     358         
     359                if ( is_wp_error($removed) ) 
     360                        return $removed; //Pass errors through. 
     361         
     362                $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : ''; 
     363                if ( empty($plugin) ) 
     364                        return new WP_Error('bad_request', $this->strings['bad_request']); 
     365 
     366                $plugins_dir = $wp_filesystem->wp_plugins_dir(); 
     367                $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) ); 
     368         
     369                // If plugin is in its own directory, recursively delete the directory. 
     370                if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder 
     371                        $deleted = $wp_filesystem->delete($this_plugin_dir, true); 
     372                else 
     373                        $deleted = $wp_filesystem->delete($plugins_dir . $plugin); 
     374 
     375                if ( ! $deleted ) 
     376                        return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']); 
     377                 
     378                return $removed; 
     379        } 
     380} 
     381 
     382 
     383class Theme_Upgrader extends WP_Upgrader { 
     384 
     385        function upgrade_strings() { 
     386                $this->generic_strings(); 
     387                $this->strings['up_to_date'] = __('The theme is at the latest version.'); 
     388                $this->strings['no_package'] = __('Upgrade package not available.'); 
     389                $this->strings['downloading_package'] = __('Downloading update from %s.'); 
     390                $this->strings['unpack_package'] = __('Unpacking the update.'); 
     391//              $this->strings['deactivate_plugin'] = __('Deactivating the plugin.'); // => Maintainence mode? 
     392                $this->strings['remove_old'] = __('Removing the old version of the theme.'); 
     393                $this->strings['remove_old_failed'] = __('Could not remove the old theme.'); 
     394        } 
     395 
     396        function install_strings() { 
     397                $this->generic_strings(); 
     398                $this->strings['no_package'] = __('Install package not available.'); 
     399                $this->strings['downloading_package'] = __('Downloading install package from %s.'); 
     400                $this->strings['unpack_package'] = __('Unpacking the package.'); 
     401        } 
     402 
     403        function install($package) { 
     404                 
     405                $this->install_strings(); 
     406 
     407                $options = array( 
     408                                                'package' => $package, 
     409                                                'destination' => WP_CONTENT_DIR . '/themes', 
     410                                                'clear_destination' => false, //Do not overwrite files. 
     411                                                'clear_working' => true 
     412                                                ); 
     413                 
     414                $result = $this->run($options); 
     415         
     416                // Force refresh of theme update information 
     417                delete_transient('update_themes'); 
     418         
     419                if( empty($result['destination_name']) ) 
     420                        return false;  
     421 
     422                $plugin = get_plugins('/' . $result['destination_name']); //Ensure to pass with leading slash 
     423                $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 
     424         
     425                return $folder . '/' . $pluginfiles[0]; 
     426        } 
     427 
     428        function upgrade($theme) { 
     429                 
     430                $this->upgrade_strings(); 
     431                 
     432                // Is an update available? 
     433                $current = get_transient( 'update_themes' ); 
     434                if ( !isset( $current->response[ $theme ] ) ) 
     435                        return new WP_Error('up_to_date', $this->strings['up_to_date']); 
     436                 
     437                $r = $current->response[ $theme ]; 
     438                 
     439                $themes = get_themes(); 
     440                foreach ( (array) $themes as $this_theme ) { 
     441                        if ( $this_theme['Stylesheet'] == $theme ) { 
     442                                $theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']); 
     443                                break; 
     444                        } 
     445                } 
     446                unset($themes); 
     447 
     448                $options = array( 
     449                                                'package' => $r['package'], 
     450                                                'destination' => WP_CONTENT_DIR . '/themes', 
     451                                                'clear_destination' => true, 
     452                                                'clear_working' => true, 
     453                                                'hook_extra' => array( 
     454                                                                                        'theme' => $theme 
     455                                                                                        ) 
     456                                                ); 
     457                 
     458                $result = $this->run($options); 
     459 
     460                if ( is_wp_error($result) ) 
     461                        return $result; 
     462 
     463                // Force refresh of theme update information 
     464                delete_transient('update_themes'); 
     465 
     466                return true; 
     467        } 
     468 
     469} 
     470 
     471//Untested. 
     472class Core_Upgrader extends WP_Upgrader { 
     473 
     474        function upgrade_strings() { 
     475                $this->generic_strings(); 
     476                $this->strings['up_to_date'] = __('WordPress is at the latest version.'); 
     477                $this->strings['no_package'] = __('Upgrade package not available.'); 
     478                $this->strings['downloading_package'] = __('Downloading update from %s.'); 
     479                $this->strings['unpack_package'] = __('Unpacking the update.'); 
     480                $this->strings['copy_failed'] = __('Could not copy files.'); 
     481        } 
     482 
     483        function upgrade($current) { 
     484                global $wp_filesystem; 
     485                $this->upgrade_strings(); 
     486                 
     487                @set_time_limit( 300 ); 
     488         
     489                if ( !empty($feedback) ) 
     490                        add_filter('update_feedback', $feedback); 
     491         
     492                // Is an update available? 
     493                if ( !isset( $current->response ) || $current->response == 'latest' ) 
     494                        return new WP_Error('up_to_date', $this->strings['up_to_date']); 
     495 
     496                $res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) ); 
     497                if ( is_wp_error($res) ) 
     498                        return $res; 
     499                 
     500                $wp_dir = trailingslashit($wp_filesystem->abspath()); 
     501                 
     502                $download = $this->download_package( $current->package ); 
     503                if ( is_wp_error($download) ) 
     504                        return $download; 
     505                 
     506                $working_dir = $this->unpack_package( $download ); 
     507                if ( is_wp_error($working_dir) ) 
     508                        return $working_dir; 
     509 
     510                // Copy update-core.php from the new version into place. 
     511                if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) { 
     512                        $wp_filesystem->delete($working_dir, true); 
     513                        return new WP_Error('copy_failed', $this->strings['copy_failed']); 
     514                } 
     515                $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE); 
     516         
     517                require(ABSPATH . 'wp-admin/includes/update-core.php'); 
     518         
     519                return update_core($working_dir, $wp_dir); 
     520        } 
     521 
     522} 
     523 No newline at end of file 
  • wp-admin/includes/plugin-install.php

     
    723723 * @return mixed. 
    724724 */ 
    725725function wp_install_plugin($package, $feedback = '') { 
    726         global $wp_filesystem; 
    727  
    728726        if ( !empty($feedback) ) 
    729                 add_filter('install_feedback', $feedback); 
     727                add_filter('update_feedback', $feedback); 
    730728 
    731         // Is a filesystem accessor setup? 
    732         if ( ! $wp_filesystem || ! is_object($wp_filesystem) ) 
    733                 WP_Filesystem(); 
    734  
    735         if ( ! is_object($wp_filesystem) ) 
    736                 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
    737  
    738         if ( $wp_filesystem->errors->get_error_code() ) 
    739                 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors); 
    740  
    741         //Get the base plugin folder 
    742         $plugins_dir = $wp_filesystem->wp_plugins_dir(); 
    743         if ( empty($plugins_dir) ) 
    744                 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.')); 
    745  
    746         //And the same for the Content directory. 
    747         $content_dir = $wp_filesystem->wp_content_dir(); 
    748         if( empty($content_dir) ) 
    749                 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).')); 
    750  
    751         $plugins_dir = trailingslashit( $plugins_dir ); 
    752         $content_dir = trailingslashit( $content_dir ); 
    753  
    754         if ( empty($package) ) 
    755                 return new WP_Error('no_package', __('Install package not available.')); 
    756  
    757         // Download the package 
    758         apply_filters('install_feedback', sprintf(__('Downloading plugin package from %s'), $package)); 
    759         $download_file = download_url($package); 
    760  
    761         if ( is_wp_error($download_file) ) 
    762                 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message()); 
    763  
    764         $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip'); 
    765  
    766         // Clean up working directory 
    767         if ( $wp_filesystem->is_dir($working_dir) ) 
    768                 $wp_filesystem->delete($working_dir, true); 
    769  
    770         apply_filters('install_feedback', __('Unpacking the plugin package')); 
    771         // Unzip package to working directory 
    772         $result = unzip_file($download_file, $working_dir); 
    773  
    774         // Once extracted, delete the package 
    775         @unlink($download_file); 
    776  
    777         if ( is_wp_error($result) ) { 
    778                 $wp_filesystem->delete($working_dir, true); 
    779                 return $result; 
    780         } 
    781  
    782         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin 
    783         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) ); 
    784  
    785         if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) { 
    786                 $wp_filesystem->delete($working_dir, true); 
    787                 return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] ); 
    788         } 
    789  
    790         apply_filters('install_feedback', __('Installing the plugin')); 
    791         // Copy new version of plugin into place. 
    792         $result = copy_dir($working_dir, $plugins_dir); 
    793         if ( is_wp_error($result) ) { 
    794                 $wp_filesystem->delete($working_dir, true); 
    795                 return $result; 
    796         } 
    797  
    798         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin 
    799         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) ); 
    800  
    801         // Remove working directory 
    802         $wp_filesystem->delete($working_dir, true); 
    803  
    804         if( empty($filelist) ) 
    805                 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup. 
    806  
    807         $folder = $filelist[0]; 
    808         $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash 
    809         $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 
    810  
    811         //Return the plugin files name. 
    812         return  $folder . '/' . $pluginfiles[0]; 
     729        include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
     730        $upgrader = new Plugin_Upgrader(); 
     731        return $upgrader->install($package); 
    813732} 
    814733 
    815734/** 
     
    822741 * @return mixed. 
    823742 */ 
    824743function wp_install_plugin_local_package($package, $feedback = '') { 
    825         global $wp_filesystem; 
    826  
    827744        if ( !empty($feedback) ) 
    828                 add_filter('install_feedback', $feedback); 
     745                add_filter('update_feedback', $feedback); 
    829746 
    830         // Is a filesystem accessor setup? 
    831         if ( ! $wp_filesystem || ! is_object($wp_filesystem) ) 
    832                 WP_Filesystem(); 
    833  
    834         if ( ! is_object($wp_filesystem) ) 
    835                 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
    836  
    837         if ( $wp_filesystem->errors->get_error_code() ) 
    838                 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors); 
    839  
    840         //Get the base plugin folder 
    841         $plugins_dir = $wp_filesystem->wp_plugins_dir(); 
    842         if ( empty($plugins_dir) ) 
    843                 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.')); 
    844  
    845         //And the same for the Content directory. 
    846         $content_dir = $wp_filesystem->wp_content_dir(); 
    847         if( empty($content_dir) ) 
    848                 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).')); 
    849  
    850         $plugins_dir = trailingslashit( $plugins_dir ); 
    851         $content_dir = trailingslashit( $content_dir ); 
    852  
    853         if ( empty($package) ) 
    854                 return new WP_Error('no_package', __('Install package not available.')); 
    855  
    856         $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip'); 
    857  
    858         // Clean up working directory 
    859         if ( $wp_filesystem->is_dir($working_dir) ) 
    860                 $wp_filesystem->delete($working_dir, true); 
    861  
    862         apply_filters('install_feedback', __('Unpacking the plugin package')); 
    863         // Unzip package to working directory 
    864         $result = unzip_file($package, $working_dir); 
    865  
    866         // Once extracted, delete the package 
    867         unlink($package); 
    868  
    869         if ( is_wp_error($result) ) { 
    870                 $wp_filesystem->delete($working_dir, true); 
    871                 return $result; 
    872         } 
    873  
    874         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin 
    875         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) ); 
    876  
    877         if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) { 
    878                 $wp_filesystem->delete($working_dir, true); 
    879                 return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] ); 
    880         } 
    881  
    882         apply_filters('install_feedback', __('Installing the plugin')); 
    883         // Copy new version of plugin into place. 
    884         $result = copy_dir($working_dir, $plugins_dir); 
    885         if ( is_wp_error($result) ) { 
    886                 $wp_filesystem->delete($working_dir, true); 
    887                 return $result; 
    888         } 
    889  
    890         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin 
    891         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) ); 
    892  
    893         // Remove working directory 
    894         $wp_filesystem->delete($working_dir, true); 
    895  
    896         if( empty($filelist) ) 
    897                 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup. 
    898  
    899         $folder = $filelist[0]; 
    900         $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash 
    901         $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 
    902  
    903         //Return the plugin files name. 
    904         return  $folder . '/' . $pluginfiles[0]; 
     747        include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
     748        $upgrader = new Plugin_Upgrader(); 
     749        return $upgrader->install($package); 
    905750} 
    906751 
    907752?> 
  • wp-admin/includes/update.php

     
    167167add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 ); 
    168168 
    169169function wp_update_plugin($plugin, $feedback = '') { 
    170         global $wp_filesystem; 
    171170 
    172171        if ( !empty($feedback) ) 
    173172                add_filter('update_feedback', $feedback); 
    174173 
    175         // Is an update available? 
    176         $current = get_transient( 'update_plugins' ); 
    177         if ( !isset( $current->response[ $plugin ] ) ) 
    178                 return new WP_Error('up_to_date', __('The plugin is at the latest version.')); 
    179  
    180         // Is a filesystem accessor setup? 
    181         if ( ! $wp_filesystem || ! is_object($wp_filesystem) ) 
    182                 WP_Filesystem(); 
    183  
    184         if ( ! is_object($wp_filesystem) ) 
    185                 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
    186  
    187         if ( $wp_filesystem->errors->get_error_code() ) 
    188                 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors); 
    189  
    190         //Get the base plugin folder 
    191         $plugins_dir = $wp_filesystem->wp_plugins_dir(); 
    192         if ( empty($plugins_dir) ) 
    193                 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.')); 
    194  
    195         //And the same for the Content directory. 
    196         $content_dir = $wp_filesystem->wp_content_dir(); 
    197         if( empty($content_dir) ) 
    198                 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).')); 
    199  
    200         $plugins_dir = trailingslashit( $plugins_dir ); 
    201         $content_dir = trailingslashit( $content_dir ); 
    202  
    203         // Get the URL to the zip file 
    204         $r = $current->response[ $plugin ]; 
    205  
    206         if ( empty($r->package) ) 
    207                 return new WP_Error('no_package', __('Upgrade package not available.')); 
    208  
    209         // Download the package 
    210         $package = $r->package; 
    211         apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package)); 
    212         $download_file = download_url($package); 
    213  
    214         if ( is_wp_error($download_file) ) 
    215                 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message()); 
    216  
    217         $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php'); 
    218  
    219         // Clean up working directory 
    220         if ( $wp_filesystem->is_dir($working_dir) ) 
    221                 $wp_filesystem->delete($working_dir, true); 
    222  
    223         apply_filters('update_feedback', __('Unpacking the update')); 
    224         // Unzip package to working directory 
    225         $result = unzip_file($download_file, $working_dir); 
    226  
    227         // Once extracted, delete the package 
    228         unlink($download_file); 
    229  
    230         if ( is_wp_error($result) ) { 
    231                 $wp_filesystem->delete($working_dir, true); 
    232                 return $result; 
    233         } 
    234  
    235         if ( is_plugin_active($plugin) ) { 
    236                 //Deactivate the plugin silently, Prevent deactivation hooks from running. 
    237                 apply_filters('update_feedback', __('Deactivating the plugin')); 
    238                 deactivate_plugins($plugin, true); 
    239         } 
    240  
    241         // Remove the existing plugin. 
    242         apply_filters('update_feedback', __('Removing the old version of the plugin')); 
    243         $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) ); 
    244  
    245         // If plugin is in its own directory, recursively delete the directory. 
    246         if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder 
    247                 $deleted = $wp_filesystem->delete($this_plugin_dir, true); 
    248         else 
    249                 $deleted = $wp_filesystem->delete($plugins_dir . $plugin); 
    250  
    251         if ( ! $deleted ) { 
    252                 $wp_filesystem->delete($working_dir, true); 
    253                 return new WP_Error('delete_failed', __('Could not remove the old plugin')); 
    254         } 
    255  
    256         apply_filters('update_feedback', __('Installing the latest version')); 
    257         // Copy new version of plugin into place. 
    258         $result = copy_dir($working_dir, $plugins_dir); 
    259         if ( is_wp_error($result) ) { 
    260                 $wp_filesystem->delete($working_dir, true); 
    261                 return $result; 
    262         } 
    263  
    264         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin 
    265         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) ); 
    266  
    267         // Remove working directory 
    268         $wp_filesystem->delete($working_dir, true); 
    269  
    270         // Force refresh of plugin update information 
    271         delete_transient('update_plugins'); 
    272  
    273         if( empty($filelist) ) 
    274                 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup. 
    275  
    276         $folder = $filelist[0]; 
    277         $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash 
    278         $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 
    279  
    280         return  $folder . '/' . $pluginfiles[0]; 
     174        include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
     175        $upgrader = new Plugin_Upgrader(); 
     176        return $upgrader->upgrade($plugin); 
    281177} 
    282178 
    283179function wp_update_theme($theme, $feedback = '') { 
    284         global $wp_filesystem; 
    285  
     180         
    286181        if ( !empty($feedback) ) 
    287182                add_filter('update_feedback', $feedback); 
    288183 
    289         // Is an update available? 
    290         $current = get_transient( 'update_themes' ); 
    291         if ( !isset( $current->response[ $theme ] ) ) 
    292                 return new WP_Error('up_to_date', __('The theme is at the latest version.')); 
    293  
    294         $r = $current->response[ $theme ]; 
    295  
    296         $themes = get_themes(); 
    297         foreach ( (array) $themes as $this_theme ) { 
    298                 if ( $this_theme['Stylesheet'] == $theme ) { 
    299                         $theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']); 
    300                         break; 
    301                 } 
    302         } 
    303         unset($themes); 
    304  
    305         if ( empty($theme_directory) ) 
    306                 return new WP_Error('theme_non_existant', __('Theme does not exist.')); 
    307  
    308         // Is a filesystem accessor setup? 
    309         if ( ! $wp_filesystem || ! is_object($wp_filesystem) ) 
    310                 WP_Filesystem(); 
    311  
    312         if ( ! is_object($wp_filesystem) ) 
    313                 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
    314  
    315         if ( $wp_filesystem->errors->get_error_code() ) 
    316                 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors); 
    317  
    318         //Get the base plugin folder 
    319         $themes_dir = $wp_filesystem->wp_themes_dir(); 
    320         if ( empty($themes_dir) ) 
    321                 return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress Theme directory.')); 
    322  
    323         //And the same for the Content directory. 
    324         $content_dir = $wp_filesystem->wp_content_dir(); 
    325         if( empty($content_dir) ) 
    326                 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).')); 
    327  
    328         $themes_dir = trailingslashit( $themes_dir ); 
    329         $content_dir = trailingslashit( $content_dir ); 
    330  
    331         if ( empty($r->package) ) 
    332                 return new WP_Error('no_package', __('Upgrade package not available.')); 
    333  
    334         // Download the package 
    335         apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $r['package'])); 
    336         $download_file = download_url($r['package']); 
    337  
    338         if ( is_wp_error($download_file) ) 
    339                 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message()); 
    340  
    341         $working_dir = $content_dir . 'upgrade/' . basename($theme_directory); 
    342  
    343         // Clean up working directory 
    344         if ( $wp_filesystem->is_dir($working_dir) ) 
    345                 $wp_filesystem->delete($working_dir, true); 
    346  
    347         apply_filters('update_feedback', __('Unpacking the update')); 
    348         // Unzip package to working directory 
    349         $result = unzip_file($download_file, $working_dir); 
    350  
    351         // Once extracted, delete the package 
    352         unlink($download_file); 
    353  
    354         if ( is_wp_error($result) ) { 
    355                 $wp_filesystem->delete($working_dir, true); 
    356                 return $result; 
    357         } 
    358  
    359         //TODO: Is theme currently active? If so, set default theme 
    360         /* 
    361         if ( is_plugin_active($plugin) ) { 
    362                 //Deactivate the plugin silently, Prevent deactivation hooks from running. 
    363                 apply_filters('update_feedback', __('Deactivating the plugin')); 
    364                 deactivate_plugins($plugin, true); 
    365         }*/ 
    366  
    367         // Remove the existing plugin. 
    368         apply_filters('update_feedback', __('Removing the old version of the theme')); 
    369         $deleted = $wp_filesystem->delete($themes_dir . $theme_directory, true); 
    370  
    371         if ( ! $deleted ) { 
    372                 $wp_filesystem->delete($working_dir, true); 
    373                 return new WP_Error('delete_failed', __('Could not remove the old plugin')); 
    374         } 
    375  
    376         apply_filters('update_feedback', __('Installing the latest version')); 
    377         // Copy new version of plugin into place. 
    378         $result = copy_dir($working_dir, $themes_dir); 
    379         if ( is_wp_error($result) ) { 
    380                 $wp_filesystem->delete($working_dir, true); 
    381                 return $result; 
    382         } 
    383  
    384         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin 
    385         //$filelist = array_keys( $wp_filesystem->dirlist($working_dir) ); 
    386  
    387         // Remove working directory 
    388         $wp_filesystem->delete($working_dir, true); 
    389  
    390         // Force refresh of plugin update information 
    391         delete_transient('update_themes'); 
    392  
    393         /*if( empty($filelist) ) 
    394                 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup. 
    395  
    396         $folder = $filelist[0]; 
    397         $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash 
    398         $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 
    399  
    400         return  $folder . '/' . $pluginfiles[0];*/ 
     184        include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
     185        $upgrader = new Theme_Upgrader(); 
     186        return $upgrader->upgrade($theme); 
    401187} 
    402188 
    403189 
    404190function wp_update_core($current, $feedback = '') { 
    405         global $wp_filesystem; 
    406  
    407         @set_time_limit( 300 ); 
    408  
     191         
    409192        if ( !empty($feedback) ) 
    410193                add_filter('update_feedback', $feedback); 
    411194 
    412         // Is an update available? 
    413         if ( !isset( $current->response ) || $current->response == 'latest' ) 
    414                 return new WP_Error('up_to_date', __('WordPress is at the latest version.')); 
     195        include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
     196        $upgrader = new Core_Upgrader(); 
     197        return $upgrader->upgrade($current); 
    415198 
    416         // Is a filesystem accessor setup? 
    417         if ( ! $wp_filesystem || ! is_object($wp_filesystem) ) 
    418                 WP_Filesystem(); 
    419  
    420         if ( ! is_object($wp_filesystem) ) 
    421                 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
    422  
    423         if ( $wp_filesystem->errors->get_error_code() ) 
    424                 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors); 
    425  
    426         // Get the base WP folder 
    427         $wp_dir = $wp_filesystem->abspath(); 
    428         if ( empty($wp_dir) ) 
    429                 return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.')); 
    430  
    431         // And the same for the Content directory. 
    432         $content_dir = $wp_filesystem->wp_content_dir(); 
    433         if( empty($content_dir) ) 
    434                 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).')); 
    435  
    436         $wp_dir = trailingslashit( $wp_dir ); 
    437         $content_dir = trailingslashit( $content_dir ); 
    438  
    439         // Get the URL to the zip file 
    440         $package = $current->package; 
    441  
    442         // Download the package 
    443         apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package)); 
    444         $download_file = download_url($package); 
    445  
    446         if ( is_wp_error($download_file) ) 
    447                 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message()); 
    448  
    449         $working_dir = $content_dir . 'upgrade/core'; 
    450         // Clean up working directory 
    451         if ( $wp_filesystem->is_dir($working_dir) ) { 
    452                 $wp_filesystem->delete($working_dir, true); 
    453         } 
    454  
    455         apply_filters('update_feedback', __('Unpacking the core update')); 
    456         // Unzip package to working directory 
    457         $result = unzip_file($download_file, $working_dir); 
    458         // Once extracted, delete the package 
    459         unlink($download_file); 
    460  
    461         if ( is_wp_error($result) ) { 
    462                 $wp_filesystem->delete($working_dir, true); 
    463                 return $result; 
    464         } 
    465  
    466         // Copy update-core.php from the new version into place. 
    467         if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) { 
    468                 $wp_filesystem->delete($working_dir, true); 
    469                 return new WP_Error('copy_failed', __('Could not copy files')); 
    470         } 
    471         $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE); 
    472  
    473         require(ABSPATH . 'wp-admin/includes/update-core.php'); 
    474  
    475         return update_core($working_dir, $wp_dir); 
    476199} 
    477200 
    478201function maintenance_nag() { 
  • wp-includes/update.php

     
    112112 
    113113        $new_option = new stdClass; 
    114114        $new_option->last_checked = time(); 
    115         $timeout = 'load-plugins.php' == current_filter() ? 360 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours 
     115        $timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours 
    116116        $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked ); 
    117117 
    118118        $plugin_changed = false; 
     
    195195 
    196196        $new_option = new stdClass; 
    197197        $new_option->last_checked = time( ); 
    198         $timeout = 'load-themes.php' == current_filter() ? 360 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours 
     198        $timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours 
    199199        $time_not_changed = isset( $current_theme->last_checked ) && $timeout > ( time( ) - $current_theme->last_checked ); 
    200200 
    201201        if( $time_not_changed )