class-plugin-upgrader.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. /**
  3. * Upgrade API: Plugin_Upgrader class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for upgrading/installing plugins.
  11. *
  12. * It is designed to upgrade/install plugins from a local zip, remote zip URL,
  13. * or uploaded zip file.
  14. *
  15. * @since 2.8.0
  16. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  17. *
  18. * @see WP_Upgrader
  19. */
  20. class Plugin_Upgrader extends WP_Upgrader {
  21. /**
  22. * Plugin upgrade result.
  23. *
  24. * @since 2.8.0
  25. * @var array|WP_Error $result
  26. *
  27. * @see WP_Upgrader::$result
  28. */
  29. public $result;
  30. /**
  31. * Whether a bulk upgrade/installation is being performed.
  32. *
  33. * @since 2.9.0
  34. * @var bool $bulk
  35. */
  36. public $bulk = false;
  37. /**
  38. * Initialize the upgrade strings.
  39. *
  40. * @since 2.8.0
  41. */
  42. public function upgrade_strings() {
  43. $this->strings['up_to_date'] = __( 'The plugin is at the latest version.' );
  44. $this->strings['no_package'] = __( 'Update package not available.' );
  45. /* translators: %s: Package URL. */
  46. $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
  47. $this->strings['unpack_package'] = __( 'Unpacking the update&#8230;' );
  48. $this->strings['remove_old'] = __( 'Removing the old version of the plugin&#8230;' );
  49. $this->strings['remove_old_failed'] = __( 'Could not remove the old plugin.' );
  50. $this->strings['process_failed'] = __( 'Plugin update failed.' );
  51. $this->strings['process_success'] = __( 'Plugin updated successfully.' );
  52. $this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' );
  53. }
  54. /**
  55. * Initialize the installation strings.
  56. *
  57. * @since 2.8.0
  58. */
  59. public function install_strings() {
  60. $this->strings['no_package'] = __( 'Installation package not available.' );
  61. /* translators: %s: Package URL. */
  62. $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
  63. $this->strings['unpack_package'] = __( 'Unpacking the package&#8230;' );
  64. $this->strings['installing_package'] = __( 'Installing the plugin&#8230;' );
  65. $this->strings['no_files'] = __( 'The plugin contains no files.' );
  66. $this->strings['process_failed'] = __( 'Plugin installation failed.' );
  67. $this->strings['process_success'] = __( 'Plugin installed successfully.' );
  68. }
  69. /**
  70. * Install a plugin package.
  71. *
  72. * @since 2.8.0
  73. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  74. *
  75. * @param string $package The full local path or URI of the package.
  76. * @param array $args {
  77. * Optional. Other arguments for installing a plugin package. Default empty array.
  78. *
  79. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  80. * Default true.
  81. * }
  82. * @return bool|WP_Error True if the installation was successful, false or a WP_Error otherwise.
  83. */
  84. public function install( $package, $args = array() ) {
  85. $defaults = array(
  86. 'clear_update_cache' => true,
  87. );
  88. $parsed_args = wp_parse_args( $args, $defaults );
  89. $this->init();
  90. $this->install_strings();
  91. add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
  92. if ( $parsed_args['clear_update_cache'] ) {
  93. // Clear cache so wp_update_plugins() knows about the new plugin.
  94. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
  95. }
  96. $this->run(
  97. array(
  98. 'package' => $package,
  99. 'destination' => WP_PLUGIN_DIR,
  100. 'clear_destination' => false, // Do not overwrite files.
  101. 'clear_working' => true,
  102. 'hook_extra' => array(
  103. 'type' => 'plugin',
  104. 'action' => 'install',
  105. ),
  106. )
  107. );
  108. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
  109. remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
  110. if ( ! $this->result || is_wp_error( $this->result ) ) {
  111. return $this->result;
  112. }
  113. // Force refresh of plugin update information
  114. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  115. return true;
  116. }
  117. /**
  118. * Upgrade a plugin.
  119. *
  120. * @since 2.8.0
  121. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  122. *
  123. * @param string $plugin Path to the plugin file relative to the plugins directory.
  124. * @param array $args {
  125. * Optional. Other arguments for upgrading a plugin package. Default empty array.
  126. *
  127. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  128. * Default true.
  129. * }
  130. * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
  131. */
  132. public function upgrade( $plugin, $args = array() ) {
  133. $defaults = array(
  134. 'clear_update_cache' => true,
  135. );
  136. $parsed_args = wp_parse_args( $args, $defaults );
  137. $this->init();
  138. $this->upgrade_strings();
  139. $current = get_site_transient( 'update_plugins' );
  140. if ( ! isset( $current->response[ $plugin ] ) ) {
  141. $this->skin->before();
  142. $this->skin->set_result( false );
  143. $this->skin->error( 'up_to_date' );
  144. $this->skin->after();
  145. return false;
  146. }
  147. // Get the URL to the zip file
  148. $r = $current->response[ $plugin ];
  149. add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
  150. add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
  151. //'source_selection' => array($this, 'source_selection'), //there's a trac ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
  152. if ( $parsed_args['clear_update_cache'] ) {
  153. // Clear cache so wp_update_plugins() knows about the new plugin.
  154. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
  155. }
  156. $this->run(
  157. array(
  158. 'package' => $r->package,
  159. 'destination' => WP_PLUGIN_DIR,
  160. 'clear_destination' => true,
  161. 'clear_working' => true,
  162. 'hook_extra' => array(
  163. 'plugin' => $plugin,
  164. 'type' => 'plugin',
  165. 'action' => 'update',
  166. ),
  167. )
  168. );
  169. // Cleanup our hooks, in case something else does a upgrade on this connection.
  170. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
  171. remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
  172. remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
  173. if ( ! $this->result || is_wp_error( $this->result ) ) {
  174. return $this->result;
  175. }
  176. // Force refresh of plugin update information
  177. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  178. return true;
  179. }
  180. /**
  181. * Bulk upgrade several plugins at once.
  182. *
  183. * @since 2.8.0
  184. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  185. *
  186. * @param string[] $plugins Array of paths to plugin files relative to the plugins directory.
  187. * @param array $args {
  188. * Optional. Other arguments for upgrading several plugins at once.
  189. *
  190. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. Default true.
  191. * }
  192. * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
  193. */
  194. public function bulk_upgrade( $plugins, $args = array() ) {
  195. $defaults = array(
  196. 'clear_update_cache' => true,
  197. );
  198. $parsed_args = wp_parse_args( $args, $defaults );
  199. $this->init();
  200. $this->bulk = true;
  201. $this->upgrade_strings();
  202. $current = get_site_transient( 'update_plugins' );
  203. add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
  204. $this->skin->header();
  205. // Connect to the Filesystem first.
  206. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
  207. if ( ! $res ) {
  208. $this->skin->footer();
  209. return false;
  210. }
  211. $this->skin->bulk_header();
  212. /*
  213. * Only start maintenance mode if:
  214. * - running Multisite and there are one or more plugins specified, OR
  215. * - a plugin with an update available is currently active.
  216. * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
  217. */
  218. $maintenance = ( is_multisite() && ! empty( $plugins ) );
  219. foreach ( $plugins as $plugin ) {
  220. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) );
  221. }
  222. if ( $maintenance ) {
  223. $this->maintenance_mode( true );
  224. }
  225. $results = array();
  226. $this->update_count = count( $plugins );
  227. $this->update_current = 0;
  228. foreach ( $plugins as $plugin ) {
  229. $this->update_current++;
  230. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );
  231. if ( ! isset( $current->response[ $plugin ] ) ) {
  232. $this->skin->set_result( 'up_to_date' );
  233. $this->skin->before();
  234. $this->skin->feedback( 'up_to_date' );
  235. $this->skin->after();
  236. $results[ $plugin ] = true;
  237. continue;
  238. }
  239. // Get the URL to the zip file.
  240. $r = $current->response[ $plugin ];
  241. $this->skin->plugin_active = is_plugin_active( $plugin );
  242. $result = $this->run(
  243. array(
  244. 'package' => $r->package,
  245. 'destination' => WP_PLUGIN_DIR,
  246. 'clear_destination' => true,
  247. 'clear_working' => true,
  248. 'is_multi' => true,
  249. 'hook_extra' => array(
  250. 'plugin' => $plugin,
  251. ),
  252. )
  253. );
  254. $results[ $plugin ] = $this->result;
  255. // Prevent credentials auth screen from displaying multiple times
  256. if ( false === $result ) {
  257. break;
  258. }
  259. } //end foreach $plugins
  260. $this->maintenance_mode( false );
  261. // Force refresh of plugin update information.
  262. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  263. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
  264. do_action(
  265. 'upgrader_process_complete',
  266. $this,
  267. array(
  268. 'action' => 'update',
  269. 'type' => 'plugin',
  270. 'bulk' => true,
  271. 'plugins' => $plugins,
  272. )
  273. );
  274. $this->skin->bulk_footer();
  275. $this->skin->footer();
  276. // Cleanup our hooks, in case something else does a upgrade on this connection.
  277. remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
  278. return $results;
  279. }
  280. /**
  281. * Check a source package to be sure it contains a plugin.
  282. *
  283. * This function is added to the {@see 'upgrader_source_selection'} filter by
  284. * Plugin_Upgrader::install().
  285. *
  286. * @since 3.3.0
  287. *
  288. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  289. *
  290. * @param string $source The path to the downloaded package source.
  291. * @return string|WP_Error The source as passed, or a WP_Error object
  292. * if no plugins were found.
  293. */
  294. public function check_package( $source ) {
  295. global $wp_filesystem;
  296. if ( is_wp_error( $source ) ) {
  297. return $source;
  298. }
  299. $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
  300. if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
  301. return $source;
  302. }
  303. // Check the folder contains at least 1 valid plugin.
  304. $plugins_found = false;
  305. $files = glob( $working_directory . '*.php' );
  306. if ( $files ) {
  307. foreach ( $files as $file ) {
  308. $info = get_plugin_data( $file, false, false );
  309. if ( ! empty( $info['Name'] ) ) {
  310. $plugins_found = true;
  311. break;
  312. }
  313. }
  314. }
  315. if ( ! $plugins_found ) {
  316. return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
  317. }
  318. return $source;
  319. }
  320. /**
  321. * Retrieve the path to the file that contains the plugin info.
  322. *
  323. * This isn't used internally in the class, but is called by the skins.
  324. *
  325. * @since 2.8.0
  326. *
  327. * @return string|false The full path to the main plugin file, or false.
  328. */
  329. public function plugin_info() {
  330. if ( ! is_array( $this->result ) ) {
  331. return false;
  332. }
  333. if ( empty( $this->result['destination_name'] ) ) {
  334. return false;
  335. }
  336. $plugin = get_plugins( '/' . $this->result['destination_name'] ); //Ensure to pass with leading slash
  337. if ( empty( $plugin ) ) {
  338. return false;
  339. }
  340. $pluginfiles = array_keys( $plugin ); //Assume the requested plugin is the first in the list
  341. return $this->result['destination_name'] . '/' . $pluginfiles[0];
  342. }
  343. /**
  344. * Deactivates a plugin before it is upgraded.
  345. *
  346. * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
  347. *
  348. * @since 2.8.0
  349. * @since 4.1.0 Added a return value.
  350. *
  351. * @param bool|WP_Error $return Upgrade offer return.
  352. * @param array $plugin Plugin package arguments.
  353. * @return bool|WP_Error The passed in $return param or WP_Error.
  354. */
  355. public function deactivate_plugin_before_upgrade( $return, $plugin ) {
  356. if ( is_wp_error( $return ) ) { //Bypass.
  357. return $return;
  358. }
  359. // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
  360. if ( wp_doing_cron() ) {
  361. return $return;
  362. }
  363. $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
  364. if ( empty( $plugin ) ) {
  365. return new WP_Error( 'bad_request', $this->strings['bad_request'] );
  366. }
  367. if ( is_plugin_active( $plugin ) ) {
  368. //Deactivate the plugin silently, Prevent deactivation hooks from running.
  369. deactivate_plugins( $plugin, true );
  370. }
  371. return $return;
  372. }
  373. /**
  374. * Delete the old plugin during an upgrade.
  375. *
  376. * Hooked to the {@see 'upgrader_clear_destination'} filter by
  377. * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
  378. *
  379. * @since 2.8.0
  380. *
  381. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  382. *
  383. * @param bool|WP_Error $removed
  384. * @param string $local_destination
  385. * @param string $remote_destination
  386. * @param array $plugin
  387. * @return WP_Error|bool
  388. */
  389. public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) {
  390. global $wp_filesystem;
  391. if ( is_wp_error( $removed ) ) {
  392. return $removed; //Pass errors through.
  393. }
  394. $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
  395. if ( empty( $plugin ) ) {
  396. return new WP_Error( 'bad_request', $this->strings['bad_request'] );
  397. }
  398. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  399. $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
  400. if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { //If it's already vanished.
  401. return $removed;
  402. }
  403. // If plugin is in its own directory, recursively delete the directory.
  404. if ( strpos( $plugin, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
  405. $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
  406. } else {
  407. $deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
  408. }
  409. if ( ! $deleted ) {
  410. return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
  411. }
  412. return true;
  413. }
  414. }