class-wp-upgrader.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. * @access public
  45. * @var array $strings
  46. */
  47. public $strings = array();
  48. /**
  49. * The upgrader skin being used.
  50. *
  51. * @since 2.8.0
  52. * @access public
  53. * @var Automatic_Upgrader_Skin|WP_Upgrader_Skin $skin
  54. */
  55. public $skin = null;
  56. /**
  57. * The result of the installation.
  58. *
  59. * This is set by WP_Upgrader::install_package(), only when the package is installed
  60. * successfully. It will then be an array, unless a WP_Error is returned by the
  61. * {@see 'upgrader_post_install'} filter. In that case, the WP_Error will be assigned to
  62. * it.
  63. *
  64. * @since 2.8.0
  65. * @access public
  66. *
  67. * @var WP_Error|array $result {
  68. * @type string $source The full path to the source the files were installed from.
  69. * @type string $source_files List of all the files in the source directory.
  70. * @type string $destination The full path to the install destination folder.
  71. * @type string $destination_name The name of the destination folder, or empty if `$destination`
  72. * and `$local_destination` are the same.
  73. * @type string $local_destination The full local path to the destination folder. This is usually
  74. * the same as `$destination`.
  75. * @type string $remote_destination The full remote path to the destination folder
  76. * (i.e., from `$wp_filesystem`).
  77. * @type bool $clear_destination Whether the destination folder was cleared.
  78. * }
  79. */
  80. public $result = array();
  81. /**
  82. * The total number of updates being performed.
  83. *
  84. * Set by the bulk update methods.
  85. *
  86. * @since 3.0.0
  87. * @access public
  88. * @var int $update_count
  89. */
  90. public $update_count = 0;
  91. /**
  92. * The current update if multiple updates are being performed.
  93. *
  94. * Used by the bulk update methods, and incremented for each update.
  95. *
  96. * @since 3.0.0
  97. * @access public
  98. * @var int
  99. */
  100. public $update_current = 0;
  101. /**
  102. * Construct the upgrader with a skin.
  103. *
  104. * @since 2.8.0
  105. * @access public
  106. *
  107. * @param WP_Upgrader_Skin $skin The upgrader skin to use. Default is a WP_Upgrader_Skin.
  108. * instance.
  109. */
  110. public function __construct( $skin = null ) {
  111. if ( null == $skin )
  112. $this->skin = new WP_Upgrader_Skin();
  113. else
  114. $this->skin = $skin;
  115. }
  116. /**
  117. * Initialize the upgrader.
  118. *
  119. * This will set the relationship between the skin being used and this upgrader,
  120. * and also add the generic strings to `WP_Upgrader::$strings`.
  121. *
  122. * @since 2.8.0
  123. * @access public
  124. */
  125. public function init() {
  126. $this->skin->set_upgrader($this);
  127. $this->generic_strings();
  128. }
  129. /**
  130. * Add the generic strings to WP_Upgrader::$strings.
  131. *
  132. * @since 2.8.0
  133. * @access public
  134. */
  135. public function generic_strings() {
  136. $this->strings['bad_request'] = __('Invalid data provided.');
  137. $this->strings['fs_unavailable'] = __('Could not access filesystem.');
  138. $this->strings['fs_error'] = __('Filesystem error.');
  139. $this->strings['fs_no_root_dir'] = __('Unable to locate WordPress root directory.');
  140. $this->strings['fs_no_content_dir'] = __('Unable to locate WordPress content directory (wp-content).');
  141. $this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress plugin directory.');
  142. $this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress theme directory.');
  143. /* translators: %s: directory name */
  144. $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
  145. $this->strings['download_failed'] = __('Download failed.');
  146. $this->strings['installing_package'] = __('Installing the latest version&#8230;');
  147. $this->strings['no_files'] = __('The package contains no files.');
  148. $this->strings['folder_exists'] = __('Destination folder already exists.');
  149. $this->strings['mkdir_failed'] = __('Could not create directory.');
  150. $this->strings['incompatible_archive'] = __('The package could not be installed.');
  151. $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.' );
  152. $this->strings['maintenance_start'] = __('Enabling Maintenance mode&#8230;');
  153. $this->strings['maintenance_end'] = __('Disabling Maintenance mode&#8230;');
  154. }
  155. /**
  156. * Connect to the filesystem.
  157. *
  158. * @since 2.8.0
  159. * @access public
  160. *
  161. * @global WP_Filesystem_Base $wp_filesystem Subclass
  162. *
  163. * @param array $directories Optional. A list of directories. If any of these do
  164. * not exist, a WP_Error object will be returned.
  165. * Default empty array.
  166. * @param bool $allow_relaxed_file_ownership Whether to allow relaxed file ownership.
  167. * Default false.
  168. * @return bool|WP_Error True if able to connect, false or a WP_Error otherwise.
  169. */
  170. public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
  171. global $wp_filesystem;
  172. if ( false === ( $credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership ) ) ) {
  173. return false;
  174. }
  175. if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
  176. $error = true;
  177. if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
  178. $error = $wp_filesystem->errors;
  179. // Failed to connect, Error and request again
  180. $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
  181. return false;
  182. }
  183. if ( ! is_object($wp_filesystem) )
  184. return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
  185. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  186. return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
  187. foreach ( (array)$directories as $dir ) {
  188. switch ( $dir ) {
  189. case ABSPATH:
  190. if ( ! $wp_filesystem->abspath() )
  191. return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
  192. break;
  193. case WP_CONTENT_DIR:
  194. if ( ! $wp_filesystem->wp_content_dir() )
  195. return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
  196. break;
  197. case WP_PLUGIN_DIR:
  198. if ( ! $wp_filesystem->wp_plugins_dir() )
  199. return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
  200. break;
  201. case get_theme_root():
  202. if ( ! $wp_filesystem->wp_themes_dir() )
  203. return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
  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. break;
  209. }
  210. }
  211. return true;
  212. } //end fs_connect();
  213. /**
  214. * Download a package.
  215. *
  216. * @since 2.8.0
  217. * @access public
  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. * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object.
  222. */
  223. public function download_package( $package ) {
  224. /**
  225. * Filters whether to return the package.
  226. *
  227. * @since 3.7.0
  228. * @access public
  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. if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
  239. return $package; //must be a local file..
  240. if ( empty($package) )
  241. return new WP_Error('no_package', $this->strings['no_package']);
  242. $this->skin->feedback('downloading_package', $package);
  243. $download_file = download_url($package);
  244. if ( is_wp_error($download_file) )
  245. return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
  246. return $download_file;
  247. }
  248. /**
  249. * Unpack a compressed package file.
  250. *
  251. * @since 2.8.0
  252. * @access public
  253. *
  254. * @global WP_Filesystem_Base $wp_filesystem Subclass
  255. *
  256. * @param string $package Full path to the package file.
  257. * @param bool $delete_package Optional. Whether to delete the package file after attempting
  258. * to unpack it. Default true.
  259. * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure.
  260. */
  261. public function unpack_package( $package, $delete_package = true ) {
  262. global $wp_filesystem;
  263. $this->skin->feedback('unpack_package');
  264. $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
  265. //Clean up contents of upgrade directory beforehand.
  266. $upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
  267. if ( !empty($upgrade_files) ) {
  268. foreach ( $upgrade_files as $file )
  269. $wp_filesystem->delete($upgrade_folder . $file['name'], true);
  270. }
  271. // We need a working directory - Strip off any .tmp or .zip suffixes
  272. $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
  273. // Clean up working directory
  274. if ( $wp_filesystem->is_dir($working_dir) )
  275. $wp_filesystem->delete($working_dir, true);
  276. // Unzip package to working directory
  277. $result = unzip_file( $package, $working_dir );
  278. // Once extracted, delete the package if required.
  279. if ( $delete_package )
  280. unlink($package);
  281. if ( is_wp_error($result) ) {
  282. $wp_filesystem->delete($working_dir, true);
  283. if ( 'incompatible_archive' == $result->get_error_code() ) {
  284. return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
  285. }
  286. return $result;
  287. }
  288. return $working_dir;
  289. }
  290. /**
  291. * Clears the directory where this item is going to be installed into.
  292. *
  293. * @since 4.3.0
  294. * @access public
  295. *
  296. * @global WP_Filesystem_Base $wp_filesystem Subclass
  297. *
  298. * @param string $remote_destination The location on the remote filesystem to be cleared
  299. * @return bool|WP_Error True upon success, WP_Error on failure.
  300. */
  301. public function clear_destination( $remote_destination ) {
  302. global $wp_filesystem;
  303. if ( ! $wp_filesystem->exists( $remote_destination ) ) {
  304. return true;
  305. }
  306. // Check all files are writable before attempting to clear the destination.
  307. $unwritable_files = array();
  308. $_files = $wp_filesystem->dirlist( $remote_destination, true, true );
  309. // Flatten the resulting array, iterate using each as we append to the array during iteration.
  310. while ( $f = each( $_files ) ) {
  311. $file = $f['value'];
  312. $name = $f['key'];
  313. if ( ! isset( $file['files'] ) ) {
  314. continue;
  315. }
  316. foreach ( $file['files'] as $filename => $details ) {
  317. $_files[ $name . '/' . $filename ] = $details;
  318. }
  319. }
  320. // Check writability.
  321. foreach ( $_files as $filename => $file_details ) {
  322. if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
  323. // Attempt to alter permissions to allow writes and try again.
  324. $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' == $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
  325. if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
  326. $unwritable_files[] = $filename;
  327. }
  328. }
  329. }
  330. if ( ! empty( $unwritable_files ) ) {
  331. return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
  332. }
  333. if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
  334. return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
  335. }
  336. return true;
  337. }
  338. /**
  339. * Install a package.
  340. *
  341. * Copies the contents of a package form a source directory, and installs them in
  342. * a destination directory. Optionally removes the source. It can also optionally
  343. * clear out the destination folder if it already exists.
  344. *
  345. * @since 2.8.0
  346. * @access public
  347. *
  348. * @global WP_Filesystem_Base $wp_filesystem Subclass
  349. * @global array $wp_theme_directories
  350. *
  351. * @param array|string $args {
  352. * Optional. Array or string of arguments for installing a package. Default empty array.
  353. *
  354. * @type string $source Required path to the package source. Default empty.
  355. * @type string $destination Required path to a folder to install the package in.
  356. * Default empty.
  357. * @type bool $clear_destination Whether to delete any files already in the destination
  358. * folder. Default false.
  359. * @type bool $clear_working Whether to delete the files form the working directory
  360. * after copying to the destination. Default false.
  361. * @type bool $abort_if_destination_exists Whether to abort the installation if
  362. * the destination folder already exists. Default true.
  363. * @type array $hook_extra Extra arguments to pass to the filter hooks called by
  364. * WP_Upgrader::install_package(). Default empty array.
  365. * }
  366. *
  367. * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.
  368. */
  369. public function install_package( $args = array() ) {
  370. global $wp_filesystem, $wp_theme_directories;
  371. $defaults = array(
  372. 'source' => '', // Please always pass this
  373. 'destination' => '', // and this
  374. 'clear_destination' => false,
  375. 'clear_working' => false,
  376. 'abort_if_destination_exists' => true,
  377. 'hook_extra' => array()
  378. );
  379. $args = wp_parse_args($args, $defaults);
  380. // These were previously extract()'d.
  381. $source = $args['source'];
  382. $destination = $args['destination'];
  383. $clear_destination = $args['clear_destination'];
  384. @set_time_limit( 300 );
  385. if ( empty( $source ) || empty( $destination ) ) {
  386. return new WP_Error( 'bad_request', $this->strings['bad_request'] );
  387. }
  388. $this->skin->feedback( 'installing_package' );
  389. /**
  390. * Filters the install response before the installation has started.
  391. *
  392. * Returning a truthy value, or one that could be evaluated as a WP_Error
  393. * will effectively short-circuit the installation, returning that value
  394. * instead.
  395. *
  396. * @since 2.8.0
  397. *
  398. * @param bool|WP_Error $response Response.
  399. * @param array $hook_extra Extra arguments passed to hooked filters.
  400. */
  401. $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
  402. if ( is_wp_error( $res ) ) {
  403. return $res;
  404. }
  405. //Retain the Original source and destinations
  406. $remote_source = $args['source'];
  407. $local_destination = $destination;
  408. $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
  409. $remote_destination = $wp_filesystem->find_folder( $local_destination );
  410. //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
  411. if ( 1 == count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) { //Only one folder? Then we want its contents.
  412. $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
  413. } elseif ( count( $source_files ) == 0 ) {
  414. return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
  415. } 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.
  416. $source = trailingslashit( $args['source'] );
  417. }
  418. /**
  419. * Filters the source file location for the upgrade package.
  420. *
  421. * @since 2.8.0
  422. * @since 4.4.0 The $hook_extra parameter became available.
  423. *
  424. * @param string $source File source location.
  425. * @param string $remote_source Remote file source location.
  426. * @param WP_Upgrader $this WP_Upgrader instance.
  427. * @param array $hook_extra Extra arguments passed to hooked filters.
  428. */
  429. $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
  430. if ( is_wp_error( $source ) ) {
  431. return $source;
  432. }
  433. // Has the source location changed? If so, we need a new source_files list.
  434. if ( $source !== $remote_source ) {
  435. $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
  436. }
  437. /*
  438. * Protection against deleting files in any important base directories.
  439. * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
  440. * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
  441. * to copy the directory into the directory, whilst they pass the source
  442. * as the actual files to copy.
  443. */
  444. $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
  445. if ( is_array( $wp_theme_directories ) ) {
  446. $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
  447. }
  448. if ( in_array( $destination, $protected_directories ) ) {
  449. $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
  450. $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
  451. }
  452. if ( $clear_destination ) {
  453. // We're going to clear the destination if there's something there.
  454. $this->skin->feedback('remove_old');
  455. $removed = $this->clear_destination( $remote_destination );
  456. /**
  457. * Filters whether the upgrader cleared the destination.
  458. *
  459. * @since 2.8.0
  460. *
  461. * @param mixed $removed Whether the destination was cleared. true on success, WP_Error on failure
  462. * @param string $local_destination The local package destination.
  463. * @param string $remote_destination The remote package destination.
  464. * @param array $hook_extra Extra arguments passed to hooked filters.
  465. */
  466. $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
  467. if ( is_wp_error( $removed ) ) {
  468. return $removed;
  469. }
  470. } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists($remote_destination) ) {
  471. //If we're not clearing the destination folder and something exists there already, Bail.
  472. //But first check to see if there are actually any files in the folder.
  473. $_files = $wp_filesystem->dirlist($remote_destination);
  474. if ( ! empty($_files) ) {
  475. $wp_filesystem->delete($remote_source, true); //Clear out the source files.
  476. return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
  477. }
  478. }
  479. //Create destination if needed
  480. if ( ! $wp_filesystem->exists( $remote_destination ) ) {
  481. if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
  482. return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
  483. }
  484. }
  485. // Copy new version of item into place.
  486. $result = copy_dir($source, $remote_destination);
  487. if ( is_wp_error($result) ) {
  488. if ( $args['clear_working'] ) {
  489. $wp_filesystem->delete( $remote_source, true );
  490. }
  491. return $result;
  492. }
  493. //Clear the Working folder?
  494. if ( $args['clear_working'] ) {
  495. $wp_filesystem->delete( $remote_source, true );
  496. }
  497. $destination_name = basename( str_replace($local_destination, '', $destination) );
  498. if ( '.' == $destination_name ) {
  499. $destination_name = '';
  500. }
  501. $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
  502. /**
  503. * Filters the install response after the installation has finished.
  504. *
  505. * @since 2.8.0
  506. *
  507. * @param bool $response Install response.
  508. * @param array $hook_extra Extra arguments passed to hooked filters.
  509. * @param array $result Installation result data.
  510. */
  511. $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
  512. if ( is_wp_error($res) ) {
  513. $this->result = $res;
  514. return $res;
  515. }
  516. //Bombard the calling function will all the info which we've just used.
  517. return $this->result;
  518. }
  519. /**
  520. * Run an upgrade/install.
  521. *
  522. * Attempts to download the package (if it is not a local file), unpack it, and
  523. * install it in the destination folder.
  524. *
  525. * @since 2.8.0
  526. * @access public
  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/install
  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. // Install 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. * @access public
  701. *
  702. * @global WP_Filesystem_Base $wp_filesystem Subclass
  703. *
  704. * @param bool $enable True to enable maintenance mode, false to disable.
  705. */
  706. public function maintenance_mode( $enable = false ) {
  707. global $wp_filesystem;
  708. $file = $wp_filesystem->abspath() . '.maintenance';
  709. if ( $enable ) {
  710. $this->skin->feedback('maintenance_start');
  711. // Create maintenance file to signal that we are upgrading
  712. $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
  713. $wp_filesystem->delete($file);
  714. $wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
  715. } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
  716. $this->skin->feedback('maintenance_end');
  717. $wp_filesystem->delete($file);
  718. }
  719. }
  720. /**
  721. * Creates a lock using WordPress options.
  722. *
  723. * @since 4.5.0
  724. * @access public
  725. * @static
  726. *
  727. * @param string $lock_name The name of this unique lock.
  728. * @param int $release_timeout Optional. The duration in seconds to respect an existing lock.
  729. * Default: 1 hour.
  730. * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise.
  731. */
  732. public static function create_lock( $lock_name, $release_timeout = null ) {
  733. global $wpdb;
  734. if ( ! $release_timeout ) {
  735. $release_timeout = HOUR_IN_SECONDS;
  736. }
  737. $lock_option = $lock_name . '.lock';
  738. // Try to lock.
  739. $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
  740. if ( ! $lock_result ) {
  741. $lock_result = get_option( $lock_option );
  742. // If a lock couldn't be created, and there isn't a lock, bail.
  743. if ( ! $lock_result ) {
  744. return false;
  745. }
  746. // Check to see if the lock is still valid. If it is, bail.
  747. if ( $lock_result > ( time() - $release_timeout ) ) {
  748. return false;
  749. }
  750. // There must exist an expired lock, clear it and re-gain it.
  751. WP_Upgrader::release_lock( $lock_name );
  752. return WP_Upgrader::create_lock( $lock_name, $release_timeout );
  753. }
  754. // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
  755. update_option( $lock_option, time() );
  756. return true;
  757. }
  758. /**
  759. * Releases an upgrader lock.
  760. *
  761. * @since 4.5.0
  762. * @access public
  763. * @static
  764. *
  765. * @see WP_Upgrader::create_lock()
  766. *
  767. * @param string $lock_name The name of this unique lock.
  768. * @return bool True if the lock was successfully released. False on failure.
  769. */
  770. public static function release_lock( $lock_name ) {
  771. return delete_option( $lock_name . '.lock' );
  772. }
  773. }
  774. /** Plugin_Upgrader class */
  775. require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
  776. /** Theme_Upgrader class */
  777. require_once ABSPATH . 'wp-admin/includes/class-theme-upgrader.php';
  778. /** Language_Pack_Upgrader class */
  779. require_once ABSPATH . 'wp-admin/includes/class-language-pack-upgrader.php';
  780. /** Core_Upgrader class */
  781. require_once ABSPATH . 'wp-admin/includes/class-core-upgrader.php';
  782. /** File_Upload_Upgrader class */
  783. require_once ABSPATH . 'wp-admin/includes/class-file-upload-upgrader.php';
  784. /** WP_Automatic_Updater class */
  785. require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';