Sin descripción

class-wp-upgrader.php 36KB

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