class-wp-upgrader.php 33 KB

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