class-wp-upgrader.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. <?php
  2. /**
  3. * Upgrade API: WP_Upgrader class
  4. *
  5. * Requires skin classes and WP_Upgrader subclasses for backward compatibility.
  6. *
  7. * @package WordPress
  8. * @subpackage Upgrader
  9. * @since 2.8.0
  10. */
  11. /** WP_Upgrader_Skin class */
  12. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
  13. /** Plugin_Upgrader_Skin class */
  14. require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader-skin.php';
  15. /** Theme_Upgrader_Skin class */
  16. require_once ABSPATH . 'wp-admin/includes/class-theme-upgrader-skin.php';
  17. /** Bulk_Upgrader_Skin class */
  18. require_once ABSPATH . 'wp-admin/includes/class-bulk-upgrader-skin.php';
  19. /** Bulk_Plugin_Upgrader_Skin class */
  20. require_once ABSPATH . 'wp-admin/includes/class-bulk-plugin-upgrader-skin.php';
  21. /** Bulk_Theme_Upgrader_Skin class */
  22. require_once ABSPATH . 'wp-admin/includes/class-bulk-theme-upgrader-skin.php';
  23. /** Plugin_Installer_Skin class */
  24. require_once ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
  25. /** Theme_Installer_Skin class */
  26. require_once ABSPATH . 'wp-admin/includes/class-theme-installer-skin.php';
  27. /** Language_Pack_Upgrader_Skin class */
  28. require_once ABSPATH . 'wp-admin/includes/class-language-pack-upgrader-skin.php';
  29. /** Automatic_Upgrader_Skin class */
  30. require_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
  31. /** WP_Ajax_Upgrader_Skin class */
  32. require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
  33. /**
  34. * Core class used for upgrading/installing a local set of files via
  35. * the Filesystem Abstraction classes from a Zip file.
  36. *
  37. * @since 2.8.0
  38. */
  39. class WP_Upgrader {
  40. /**
  41. * The error/notification strings used to update the user on the progress.
  42. *
  43. * @since 2.8.0
  44. * @var array $strings
  45. */
  46. public $strings = array();
  47. /**
  48. * The upgrader skin being used.
  49. *
  50. * @since 2.8.0
  51. * @var Automatic_Upgrader_Skin|WP_Upgrader_Skin $skin
  52. */
  53. public $skin = null;
  54. /**
  55. * The result of the installation.
  56. *
  57. * This is set by WP_Upgrader::install_package(), only when the package is installed
  58. * successfully. It will then be an array, unless a WP_Error is returned by the
  59. * {@see 'upgrader_post_install'} filter. In that case, the WP_Error will be assigned to
  60. * it.
  61. *
  62. * @since 2.8.0
  63. *
  64. * @var WP_Error|array $result {
  65. * @type string $source The full path to the source the files were installed from.
  66. * @type string $source_files List of all the files in the source directory.
  67. * @type string $destination The full path to the installation destination folder.
  68. * @type string $destination_name The name of the destination folder, or empty if `$destination`
  69. * and `$local_destination` are the same.
  70. * @type string $local_destination The full local path to the destination folder. This is usually
  71. * the same as `$destination`.
  72. * @type string $remote_destination The full remote path to the destination folder
  73. * (i.e., from `$wp_filesystem`).
  74. * @type bool $clear_destination Whether the destination folder was cleared.
  75. * }
  76. */
  77. public $result = array();
  78. /**
  79. * The total number of updates being performed.
  80. *
  81. * Set by the bulk update methods.
  82. *
  83. * @since 3.0.0
  84. * @var int $update_count
  85. */
  86. public $update_count = 0;
  87. /**
  88. * The current update if multiple updates are being performed.
  89. *
  90. * Used by the bulk update methods, and incremented for each update.
  91. *
  92. * @since 3.0.0
  93. * @var int
  94. */
  95. public $update_current = 0;
  96. /**
  97. * Construct the upgrader with a skin.
  98. *
  99. * @since 2.8.0
  100. *
  101. * @param WP_Upgrader_Skin $skin The upgrader skin to use. Default is a WP_Upgrader_Skin.
  102. * instance.
  103. */
  104. public function __construct( $skin = null ) {
  105. if ( null == $skin ) {
  106. $this->skin = new WP_Upgrader_Skin();
  107. } else {
  108. $this->skin = $skin;
  109. }
  110. }
  111. /**
  112. * Initialize the upgrader.
  113. *
  114. * This will set the relationship between the skin being used and this upgrader,
  115. * and also add the generic strings to `WP_Upgrader::$strings`.
  116. *
  117. * @since 2.8.0
  118. */
  119. public function init() {
  120. $this->skin->set_upgrader( $this );
  121. $this->generic_strings();
  122. }
  123. /**
  124. * Add the generic strings to WP_Upgrader::$strings.
  125. *
  126. * @since 2.8.0
  127. */
  128. public function generic_strings() {
  129. $this->strings['bad_request'] = __( 'Invalid data provided.' );
  130. $this->strings['fs_unavailable'] = __( 'Could not access filesystem.' );
  131. $this->strings['fs_error'] = __( 'Filesystem error.' );
  132. $this->strings['fs_no_root_dir'] = __( 'Unable to locate WordPress root directory.' );
  133. $this->strings['fs_no_content_dir'] = __( 'Unable to locate WordPress content directory (wp-content).' );
  134. $this->strings['fs_no_plugins_dir'] = __( 'Unable to locate WordPress plugin directory.' );
  135. $this->strings['fs_no_themes_dir'] = __( 'Unable to locate WordPress theme directory.' );
  136. /* translators: %s: Directory name. */
  137. $this->strings['fs_no_folder'] = __( 'Unable to locate needed folder (%s).' );
  138. $this->strings['download_failed'] = __( 'Download failed.' );
  139. $this->strings['installing_package'] = __( 'Installing the latest version&#8230;' );
  140. $this->strings['no_files'] = __( 'The package contains no files.' );
  141. $this->strings['folder_exists'] = __( 'Destination folder already exists.' );
  142. $this->strings['mkdir_failed'] = __( 'Could not create directory.' );
  143. $this->strings['incompatible_archive'] = __( 'The package could not be installed.' );
  144. $this->strings['files_not_writable'] = __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' );
  145. $this->strings['maintenance_start'] = __( 'Enabling Maintenance mode&#8230;' );
  146. $this->strings['maintenance_end'] = __( 'Disabling Maintenance mode&#8230;' );
  147. }
  148. /**
  149. * Connect to the filesystem.
  150. *
  151. * @since 2.8.0
  152. *
  153. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  154. *
  155. * @param array $directories Optional. A list of directories. If any of these do
  156. * not exist, a WP_Error object will be returned.
  157. * Default empty array.
  158. * @param bool $allow_relaxed_file_ownership Whether to allow relaxed file ownership.
  159. * Default false.
  160. * @return bool|WP_Error True if able to connect, false or a WP_Error otherwise.
  161. */
  162. public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
  163. global $wp_filesystem;
  164. $credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership );
  165. if ( false === $credentials ) {
  166. return false;
  167. }
  168. if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
  169. $error = true;
  170. if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
  171. $error = $wp_filesystem->errors;
  172. }
  173. // Failed to connect, Error and request again
  174. $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
  175. return false;
  176. }
  177. if ( ! is_object( $wp_filesystem ) ) {
  178. return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] );
  179. }
  180. if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
  181. return new WP_Error( 'fs_error', $this->strings['fs_error'], $wp_filesystem->errors );
  182. }
  183. foreach ( (array) $directories as $dir ) {
  184. switch ( $dir ) {
  185. case ABSPATH:
  186. if ( ! $wp_filesystem->abspath() ) {
  187. return new WP_Error( 'fs_no_root_dir', $this->strings['fs_no_root_dir'] );
  188. }
  189. break;
  190. case WP_CONTENT_DIR:
  191. if ( ! $wp_filesystem->wp_content_dir() ) {
  192. return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
  193. }
  194. break;
  195. case WP_PLUGIN_DIR:
  196. if ( ! $wp_filesystem->wp_plugins_dir() ) {
  197. return new WP_Error( 'fs_no_plugins_dir', $this->strings['fs_no_plugins_dir'] );
  198. }
  199. break;
  200. case get_theme_root():
  201. if ( ! $wp_filesystem->wp_themes_dir() ) {
  202. return new WP_Error( 'fs_no_themes_dir', $this->strings['fs_no_themes_dir'] );
  203. }
  204. break;
  205. default:
  206. if ( ! $wp_filesystem->find_folder( $dir ) ) {
  207. return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
  208. }
  209. break;
  210. }
  211. }
  212. return true;
  213. } //end fs_connect();
  214. /**
  215. * Download a package.
  216. *
  217. * @since 2.8.0
  218. *
  219. * @param string $package The URI of the package. If this is the full path to an
  220. * existing local file, it will be returned untouched.
  221. * @param bool $check_signatures Whether to validate file signatures. Default false.
  222. * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object.
  223. */
  224. public function download_package( $package, $check_signatures = false ) {
  225. /**
  226. * Filters whether to return the package.
  227. *
  228. * @since 3.7.0
  229. *
  230. * @param bool $reply Whether to bail without returning the package.
  231. * Default false.
  232. * @param string $package The package file name.
  233. * @param WP_Upgrader $this The WP_Upgrader instance.
  234. */
  235. $reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
  236. if ( false !== $reply ) {
  237. return $reply;
  238. }
  239. if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { //Local file or remote?
  240. return $package; //must be a local file..
  241. }
  242. if ( empty( $package ) ) {
  243. return new WP_Error( 'no_package', $this->strings['no_package'] );
  244. }
  245. $this->skin->feedback( 'downloading_package', $package );
  246. $download_file = download_url( $package, 300, $check_signatures );
  247. if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) {
  248. return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() );
  249. }
  250. return $download_file;
  251. }
  252. /**
  253. * Unpack a compressed package file.
  254. *
  255. * @since 2.8.0
  256. *
  257. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  258. *
  259. * @param string $package Full path to the package file.
  260. * @param bool $delete_package Optional. Whether to delete the package file after attempting
  261. * to unpack it. Default true.
  262. * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure.
  263. */
  264. public function unpack_package( $package, $delete_package = true ) {
  265. global $wp_filesystem;
  266. $this->skin->feedback( 'unpack_package' );
  267. $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
  268. //Clean up contents of upgrade directory beforehand.
  269. $upgrade_files = $wp_filesystem->dirlist( $upgrade_folder );
  270. if ( ! empty( $upgrade_files ) ) {
  271. foreach ( $upgrade_files as $file ) {
  272. $wp_filesystem->delete( $upgrade_folder . $file['name'], true );
  273. }
  274. }
  275. // We need a working directory - Strip off any .tmp or .zip suffixes
  276. $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
  277. // Clean up working directory
  278. if ( $wp_filesystem->is_dir( $working_dir ) ) {
  279. $wp_filesystem->delete( $working_dir, true );
  280. }
  281. // Unzip package to working directory
  282. $result = unzip_file( $package, $working_dir );
  283. // Once extracted, delete the package if required.
  284. if ( $delete_package ) {
  285. unlink( $package );
  286. }
  287. if ( is_wp_error( $result ) ) {
  288. $wp_filesystem->delete( $working_dir, true );
  289. if ( 'incompatible_archive' == $result->get_error_code() ) {
  290. return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
  291. }
  292. return $result;
  293. }
  294. return $working_dir;
  295. }
  296. /**
  297. * Flatten the results of WP_Filesystem::dirlist() for iterating over.
  298. *
  299. * @since 4.9.0
  300. * @access protected
  301. *
  302. * @param array $nested_files Array of files as returned by WP_Filesystem::dirlist()
  303. * @param string $path Relative path to prepend to child nodes. Optional.
  304. * @return array $files A flattened array of the $nested_files specified.
  305. */
  306. protected function flatten_dirlist( $nested_files, $path = '' ) {
  307. $files = array();
  308. foreach ( $nested_files as $name => $details ) {
  309. $files[ $path . $name ] = $details;
  310. // Append children recursively
  311. if ( ! empty( $details['files'] ) ) {
  312. $children = $this->flatten_dirlist( $details['files'], $path . $name . '/' );
  313. // Merge keeping possible numeric keys, which array_merge() will reindex from 0..n
  314. $files = $files + $children;
  315. }
  316. }
  317. return $files;
  318. }
  319. /**
  320. * Clears the directory where this item is going to be installed into.
  321. *
  322. * @since 4.3.0
  323. *
  324. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  325. *
  326. * @param string $remote_destination The location on the remote filesystem to be cleared
  327. * @return bool|WP_Error True upon success, WP_Error on failure.
  328. */
  329. public function clear_destination( $remote_destination ) {
  330. global $wp_filesystem;
  331. $files = $wp_filesystem->dirlist( $remote_destination, true, true );
  332. // False indicates that the $remote_destination doesn't exist.
  333. if ( false === $files ) {
  334. return true;
  335. }
  336. // Flatten the file list to iterate over
  337. $files = $this->flatten_dirlist( $files );
  338. // Check all files are writable before attempting to clear the destination.
  339. $unwritable_files = array();
  340. // Check writability.
  341. foreach ( $files as $filename => $file_details ) {
  342. if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
  343. // Attempt to alter permissions to allow writes and try again.
  344. $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' == $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
  345. if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
  346. $unwritable_files[] = $filename;
  347. }
  348. }
  349. }
  350. if ( ! empty( $unwritable_files ) ) {
  351. return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
  352. }
  353. if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
  354. return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
  355. }
  356. return true;
  357. }
  358. /**
  359. * Install a package.
  360. *
  361. * Copies the contents of a package form a source directory, and installs them in
  362. * a destination directory. Optionally removes the source. It can also optionally
  363. * clear out the destination folder if it already exists.
  364. *
  365. * @since 2.8.0
  366. *
  367. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  368. * @global array $wp_theme_directories
  369. *
  370. * @param array|string $args {
  371. * Optional. Array or string of arguments for installing a package. Default empty array.
  372. *
  373. * @type string $source Required path to the package source. Default empty.
  374. * @type string $destination Required path to a folder to install the package in.
  375. * Default empty.
  376. * @type bool $clear_destination Whether to delete any files already in the destination
  377. * folder. Default false.
  378. * @type bool $clear_working Whether to delete the files form the working directory
  379. * after copying to the destination. Default false.
  380. * @type bool $abort_if_destination_exists Whether to abort the installation if
  381. * the destination folder already exists. Default true.
  382. * @type array $hook_extra Extra arguments to pass to the filter hooks called by
  383. * WP_Upgrader::install_package(). Default empty array.
  384. * }
  385. *
  386. * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.
  387. */
  388. public function install_package( $args = array() ) {
  389. global $wp_filesystem, $wp_theme_directories;
  390. $defaults = array(
  391. 'source' => '', // Please always pass this
  392. 'destination' => '', // and this
  393. 'clear_destination' => false,
  394. 'clear_working' => false,
  395. 'abort_if_destination_exists' => true,
  396. 'hook_extra' => array(),
  397. );
  398. $args = wp_parse_args( $args, $defaults );
  399. // These were previously extract()'d.
  400. $source = $args['source'];
  401. $destination = $args['destination'];
  402. $clear_destination = $args['clear_destination'];
  403. set_time_limit( 300 );
  404. if ( empty( $source ) || empty( $destination ) ) {
  405. return new WP_Error( 'bad_request', $this->strings['bad_request'] );
  406. }
  407. $this->skin->feedback( 'installing_package' );
  408. /**
  409. * Filters the install response before the installation has started.
  410. *
  411. * Returning a truthy value, or one that could be evaluated as a WP_Error
  412. * will effectively short-circuit the installation, returning that value
  413. * instead.
  414. *
  415. * @since 2.8.0
  416. *
  417. * @param bool|WP_Error $response Response.
  418. * @param array $hook_extra Extra arguments passed to hooked filters.
  419. */
  420. $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
  421. if ( is_wp_error( $res ) ) {
  422. return $res;
  423. }
  424. //Retain the Original source and destinations
  425. $remote_source = $args['source'];
  426. $local_destination = $destination;
  427. $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
  428. $remote_destination = $wp_filesystem->find_folder( $local_destination );
  429. //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
  430. if ( 1 == count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) { //Only one folder? Then we want its contents.
  431. $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
  432. } elseif ( count( $source_files ) == 0 ) {
  433. return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
  434. } else { // It's only a single file, the upgrader will use the folder name of this file as the destination folder. Folder name is based on zip filename.
  435. $source = trailingslashit( $args['source'] );
  436. }
  437. /**
  438. * Filters the source file location for the upgrade package.
  439. *
  440. * @since 2.8.0
  441. * @since 4.4.0 The $hook_extra parameter became available.
  442. *
  443. * @param string $source File source location.
  444. * @param string $remote_source Remote file source location.
  445. * @param WP_Upgrader $this WP_Upgrader instance.
  446. * @param array $hook_extra Extra arguments passed to hooked filters.
  447. */
  448. $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
  449. if ( is_wp_error( $source ) ) {
  450. return $source;
  451. }
  452. // Has the source location changed? If so, we need a new source_files list.
  453. if ( $source !== $remote_source ) {
  454. $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
  455. }
  456. /*
  457. * Protection against deleting files in any important base directories.
  458. * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
  459. * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
  460. * to copy the directory into the directory, whilst they pass the source
  461. * as the actual files to copy.
  462. */
  463. $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
  464. if ( is_array( $wp_theme_directories ) ) {
  465. $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
  466. }
  467. if ( in_array( $destination, $protected_directories ) ) {
  468. $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
  469. $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
  470. }
  471. if ( $clear_destination ) {
  472. // We're going to clear the destination if there's something there.
  473. $this->skin->feedback( 'remove_old' );
  474. $removed = $this->clear_destination( $remote_destination );
  475. /**
  476. * Filters whether the upgrader cleared the destination.
  477. *
  478. * @since 2.8.0
  479. *
  480. * @param mixed $removed Whether the destination was cleared. true on success, WP_Error on failure
  481. * @param string $local_destination The local package destination.
  482. * @param string $remote_destination The remote package destination.
  483. * @param array $hook_extra Extra arguments passed to hooked filters.
  484. */
  485. $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
  486. if ( is_wp_error( $removed ) ) {
  487. return $removed;
  488. }
  489. } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) {
  490. //If we're not clearing the destination folder and something exists there already, Bail.
  491. //But first check to see if there are actually any files in the folder.
  492. $_files = $wp_filesystem->dirlist( $remote_destination );
  493. if ( ! empty( $_files ) ) {
  494. $wp_filesystem->delete( $remote_source, true ); //Clear out the source files.
  495. return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination );
  496. }
  497. }
  498. //Create destination if needed
  499. if ( ! $wp_filesystem->exists( $remote_destination ) ) {
  500. if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
  501. return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
  502. }
  503. }
  504. // Copy new version of item into place.
  505. $result = copy_dir( $source, $remote_destination );
  506. if ( is_wp_error( $result ) ) {
  507. if ( $args['clear_working'] ) {
  508. $wp_filesystem->delete( $remote_source, true );
  509. }
  510. return $result;
  511. }
  512. //Clear the Working folder?
  513. if ( $args['clear_working'] ) {
  514. $wp_filesystem->delete( $remote_source, true );
  515. }
  516. $destination_name = basename( str_replace( $local_destination, '', $destination ) );
  517. if ( '.' == $destination_name ) {
  518. $destination_name = '';
  519. }
  520. $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
  521. /**
  522. * Filters the installation response after the installation has finished.
  523. *
  524. * @since 2.8.0
  525. *
  526. * @param bool $response Installation response.
  527. * @param array $hook_extra Extra arguments passed to hooked filters.
  528. * @param array $result Installation result data.
  529. */
  530. $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
  531. if ( is_wp_error( $res ) ) {
  532. $this->result = $res;
  533. return $res;
  534. }
  535. //Bombard the calling function will all the info which we've just used.
  536. return $this->result;
  537. }
  538. /**
  539. * Run an upgrade/installation.
  540. *
  541. * Attempts to download the package (if it is not a local file), unpack it, and
  542. * install it in the destination folder.
  543. *
  544. * @since 2.8.0
  545. *
  546. * @param array $options {
  547. * Array or string of arguments for upgrading/installing a package.
  548. *
  549. * @type string $package The full path or URI of the package to install.
  550. * Default empty.
  551. * @type string $destination The full path to the destination folder.
  552. * Default empty.
  553. * @type bool $clear_destination Whether to delete any files already in the
  554. * destination folder. Default false.
  555. * @type bool $clear_working Whether to delete the files form the working
  556. * directory after copying to the destination.
  557. * Default false.
  558. * @type bool $abort_if_destination_exists Whether to abort the installation if the destination
  559. * folder already exists. When true, `$clear_destination`
  560. * should be false. Default true.
  561. * @type bool $is_multi Whether this run is one of multiple upgrade/installation
  562. * actions being performed in bulk. When true, the skin
  563. * WP_Upgrader::header() and WP_Upgrader::footer()
  564. * aren't called. Default false.
  565. * @type array $hook_extra Extra arguments to pass to the filter hooks called by
  566. * WP_Upgrader::run().
  567. * }
  568. * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error,
  569. * or false if unable to connect to the filesystem.
  570. */
  571. public function run( $options ) {
  572. $defaults = array(
  573. 'package' => '', // Please always pass this.
  574. 'destination' => '', // And this
  575. 'clear_destination' => false,
  576. 'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please
  577. 'clear_working' => true,
  578. 'is_multi' => false,
  579. 'hook_extra' => array(), // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
  580. );
  581. $options = wp_parse_args( $options, $defaults );
  582. /**
  583. * Filters the package options before running an update.
  584. *
  585. * See also {@see 'upgrader_process_complete'}.
  586. *
  587. * @since 4.3.0
  588. *
  589. * @param array $options {
  590. * Options used by the upgrader.
  591. *
  592. * @type string $package Package for update.
  593. * @type string $destination Update location.
  594. * @type bool $clear_destination Clear the destination resource.
  595. * @type bool $clear_working Clear the working resource.
  596. * @type bool $abort_if_destination_exists Abort if the Destination directory exists.
  597. * @type bool $is_multi Whether the upgrader is running multiple times.
  598. * @type array $hook_extra {
  599. * Extra hook arguments.
  600. *
  601. * @type string $action Type of action. Default 'update'.
  602. * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'.
  603. * @type bool $bulk Whether the update process is a bulk update. Default true.
  604. * @type string $plugin Path to the plugin file relative to the plugins directory.
  605. * @type string $theme The stylesheet or template name of the theme.
  606. * @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme',
  607. * or 'core'.
  608. * @type object $language_update The language pack update offer.
  609. * }
  610. * }
  611. */
  612. $options = apply_filters( 'upgrader_package_options', $options );
  613. if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times
  614. $this->skin->header();
  615. }
  616. // Connect to the Filesystem first.
  617. $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
  618. // Mainly for non-connected filesystem.
  619. if ( ! $res ) {
  620. if ( ! $options['is_multi'] ) {
  621. $this->skin->footer();
  622. }
  623. return false;
  624. }
  625. $this->skin->before();
  626. if ( is_wp_error( $res ) ) {
  627. $this->skin->error( $res );
  628. $this->skin->after();
  629. if ( ! $options['is_multi'] ) {
  630. $this->skin->footer();
  631. }
  632. return $res;
  633. }
  634. /*
  635. * Download the package (Note, This just returns the filename
  636. * of the file if the package is a local file)
  637. */
  638. $download = $this->download_package( $options['package'], true );
  639. // Allow for signature soft-fail.
  640. // WARNING: This may be removed in the future.
  641. if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
  642. // Don't output the 'no signature could be found' failure message for now.
  643. if ( 'signature_verification_no_signature' != $download->get_error_code() || WP_DEBUG ) {
  644. // Outout the failure error as a normal feedback, and not as an error:
  645. $this->skin->feedback( $download->get_error_message() );
  646. // Report this failure back to WordPress.org for debugging purposes.
  647. wp_version_check(
  648. array(
  649. 'signature_failure_code' => $download->get_error_code(),
  650. 'signature_failure_data' => $download->get_error_data(),
  651. )
  652. );
  653. }
  654. // Pretend this error didn't happen.
  655. $download = $download->get_error_data( 'softfail-filename' );
  656. }
  657. if ( is_wp_error( $download ) ) {
  658. $this->skin->error( $download );
  659. $this->skin->after();
  660. if ( ! $options['is_multi'] ) {
  661. $this->skin->footer();
  662. }
  663. return $download;
  664. }
  665. $delete_package = ( $download != $options['package'] ); // Do not delete a "local" file
  666. // Unzips the file into a temporary directory.
  667. $working_dir = $this->unpack_package( $download, $delete_package );
  668. if ( is_wp_error( $working_dir ) ) {
  669. $this->skin->error( $working_dir );
  670. $this->skin->after();
  671. if ( ! $options['is_multi'] ) {
  672. $this->skin->footer();
  673. }
  674. return $working_dir;
  675. }
  676. // With the given options, this installs it to the destination directory.
  677. $result = $this->install_package(
  678. array(
  679. 'source' => $working_dir,
  680. 'destination' => $options['destination'],
  681. 'clear_destination' => $options['clear_destination'],
  682. 'abort_if_destination_exists' => $options['abort_if_destination_exists'],
  683. 'clear_working' => $options['clear_working'],
  684. 'hook_extra' => $options['hook_extra'],
  685. )
  686. );
  687. $this->skin->set_result( $result );
  688. if ( is_wp_error( $result ) ) {
  689. $this->skin->error( $result );
  690. $this->skin->feedback( 'process_failed' );
  691. } else {
  692. // Installation succeeded.
  693. $this->skin->feedback( 'process_success' );
  694. }
  695. $this->skin->after();
  696. if ( ! $options['is_multi'] ) {
  697. /**
  698. * Fires when the upgrader process is complete.
  699. *
  700. * See also {@see 'upgrader_package_options'}.
  701. *
  702. * @since 3.6.0
  703. * @since 3.7.0 Added to WP_Upgrader::run().
  704. * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
  705. *
  706. * @param WP_Upgrader $this WP_Upgrader instance. In other contexts, $this, might be a
  707. * Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
  708. * @param array $hook_extra {
  709. * Array of bulk item update data.
  710. *
  711. * @type string $action Type of action. Default 'update'.
  712. * @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
  713. * @type bool $bulk Whether the update process is a bulk update. Default true.
  714. * @type array $plugins Array of the basename paths of the plugins' main files.
  715. * @type array $themes The theme slugs.
  716. * @type array $translations {
  717. * Array of translations update data.
  718. *
  719. * @type string $language The locale the translation is for.
  720. * @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
  721. * @type string $slug Text domain the translation is for. The slug of a theme/plugin or
  722. * 'default' for core translations.
  723. * @type string $version The version of a theme, plugin, or core.
  724. * }
  725. * }
  726. */
  727. do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
  728. $this->skin->footer();
  729. }
  730. return $result;
  731. }
  732. /**
  733. * Toggle maintenance mode for the site.
  734. *
  735. * Creates/deletes the maintenance file to enable/disable maintenance mode.
  736. *
  737. * @since 2.8.0
  738. *
  739. * @global WP_Filesystem_Base $wp_filesystem Subclass
  740. *
  741. * @param bool $enable True to enable maintenance mode, false to disable.
  742. */
  743. public function maintenance_mode( $enable = false ) {
  744. global $wp_filesystem;
  745. $file = $wp_filesystem->abspath() . '.maintenance';
  746. if ( $enable ) {
  747. $this->skin->feedback( 'maintenance_start' );
  748. // Create maintenance file to signal that we are upgrading
  749. $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
  750. $wp_filesystem->delete( $file );
  751. $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
  752. } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
  753. $this->skin->feedback( 'maintenance_end' );
  754. $wp_filesystem->delete( $file );
  755. }
  756. }
  757. /**
  758. * Creates a lock using WordPress options.
  759. *
  760. * @since 4.5.0
  761. *
  762. * @param string $lock_name The name of this unique lock.
  763. * @param int $release_timeout Optional. The duration in seconds to respect an existing lock.
  764. * Default: 1 hour.
  765. * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise.
  766. */
  767. public static function create_lock( $lock_name, $release_timeout = null ) {
  768. global $wpdb;
  769. if ( ! $release_timeout ) {
  770. $release_timeout = HOUR_IN_SECONDS;
  771. }
  772. $lock_option = $lock_name . '.lock';
  773. // Try to lock.
  774. $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
  775. if ( ! $lock_result ) {
  776. $lock_result = get_option( $lock_option );
  777. // If a lock couldn't be created, and there isn't a lock, bail.
  778. if ( ! $lock_result ) {
  779. return false;
  780. }
  781. // Check to see if the lock is still valid. If it is, bail.
  782. if ( $lock_result > ( time() - $release_timeout ) ) {
  783. return false;
  784. }
  785. // There must exist an expired lock, clear it and re-gain it.
  786. WP_Upgrader::release_lock( $lock_name );
  787. return WP_Upgrader::create_lock( $lock_name, $release_timeout );
  788. }
  789. // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
  790. update_option( $lock_option, time() );
  791. return true;
  792. }
  793. /**
  794. * Releases an upgrader lock.
  795. *
  796. * @since 4.5.0
  797. *
  798. * @see WP_Upgrader::create_lock()
  799. *
  800. * @param string $lock_name The name of this unique lock.
  801. * @return bool True if the lock was successfully released. False on failure.
  802. */
  803. public static function release_lock( $lock_name ) {
  804. return delete_option( $lock_name . '.lock' );
  805. }
  806. }
  807. /** Plugin_Upgrader class */
  808. require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
  809. /** Theme_Upgrader class */
  810. require_once ABSPATH . 'wp-admin/includes/class-theme-upgrader.php';
  811. /** Language_Pack_Upgrader class */
  812. require_once ABSPATH . 'wp-admin/includes/class-language-pack-upgrader.php';
  813. /** Core_Upgrader class */
  814. require_once ABSPATH . 'wp-admin/includes/class-core-upgrader.php';
  815. /** File_Upload_Upgrader class */
  816. require_once ABSPATH . 'wp-admin/includes/class-file-upload-upgrader.php';
  817. /** WP_Automatic_Updater class */
  818. require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';