class-plugin-upgrader.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. * New plugin info.
  39. *
  40. * @since 5.5.0
  41. * @var array $new_plugin_data
  42. *
  43. * @see check_package()
  44. */
  45. public $new_plugin_data = array();
  46. /**
  47. * Initialize the upgrade strings.
  48. *
  49. * @since 2.8.0
  50. */
  51. public function upgrade_strings() {
  52. $this->strings['up_to_date'] = __( 'The plugin is at the latest version.' );
  53. $this->strings['no_package'] = __( 'Update package not available.' );
  54. /* translators: %s: Package URL. */
  55. $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
  56. $this->strings['unpack_package'] = __( 'Unpacking the update&#8230;' );
  57. $this->strings['remove_old'] = __( 'Removing the old version of the plugin&#8230;' );
  58. $this->strings['remove_old_failed'] = __( 'Could not remove the old plugin.' );
  59. $this->strings['process_failed'] = __( 'Plugin update failed.' );
  60. $this->strings['process_success'] = __( 'Plugin updated successfully.' );
  61. $this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' );
  62. }
  63. /**
  64. * Initialize the installation strings.
  65. *
  66. * @since 2.8.0
  67. */
  68. public function install_strings() {
  69. $this->strings['no_package'] = __( 'Installation package not available.' );
  70. /* translators: %s: Package URL. */
  71. $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
  72. $this->strings['unpack_package'] = __( 'Unpacking the package&#8230;' );
  73. $this->strings['installing_package'] = __( 'Installing the plugin&#8230;' );
  74. $this->strings['remove_old'] = __( 'Removing the current plugin&#8230;' );
  75. $this->strings['remove_old_failed'] = __( 'Could not remove the current plugin.' );
  76. $this->strings['no_files'] = __( 'The plugin contains no files.' );
  77. $this->strings['process_failed'] = __( 'Plugin installation failed.' );
  78. $this->strings['process_success'] = __( 'Plugin installed successfully.' );
  79. /* translators: 1: Plugin name, 2: Plugin version. */
  80. $this->strings['process_success_specific'] = __( 'Successfully installed the plugin <strong>%1$s %2$s</strong>.' );
  81. if ( ! empty( $this->skin->overwrite ) ) {
  82. if ( 'update-plugin' === $this->skin->overwrite ) {
  83. $this->strings['installing_package'] = __( 'Updating the plugin&#8230;' );
  84. $this->strings['process_failed'] = __( 'Plugin update failed.' );
  85. $this->strings['process_success'] = __( 'Plugin updated successfully.' );
  86. }
  87. if ( 'downgrade-plugin' === $this->skin->overwrite ) {
  88. $this->strings['installing_package'] = __( 'Downgrading the plugin&#8230;' );
  89. $this->strings['process_failed'] = __( 'Plugin downgrade failed.' );
  90. $this->strings['process_success'] = __( 'Plugin downgraded successfully.' );
  91. }
  92. }
  93. }
  94. /**
  95. * Install a plugin package.
  96. *
  97. * @since 2.8.0
  98. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  99. *
  100. * @param string $package The full local path or URI of the package.
  101. * @param array $args {
  102. * Optional. Other arguments for installing a plugin package. Default empty array.
  103. *
  104. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  105. * Default true.
  106. * }
  107. * @return bool|WP_Error True if the installation was successful, false or a WP_Error otherwise.
  108. */
  109. public function install( $package, $args = array() ) {
  110. $defaults = array(
  111. 'clear_update_cache' => true,
  112. 'overwrite_package' => false, // Do not overwrite files.
  113. );
  114. $parsed_args = wp_parse_args( $args, $defaults );
  115. $this->init();
  116. $this->install_strings();
  117. add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
  118. if ( $parsed_args['clear_update_cache'] ) {
  119. // Clear cache so wp_update_plugins() knows about the new plugin.
  120. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
  121. }
  122. $this->run(
  123. array(
  124. 'package' => $package,
  125. 'destination' => WP_PLUGIN_DIR,
  126. 'clear_destination' => $parsed_args['overwrite_package'],
  127. 'clear_working' => true,
  128. 'hook_extra' => array(
  129. 'type' => 'plugin',
  130. 'action' => 'install',
  131. ),
  132. )
  133. );
  134. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
  135. remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
  136. if ( ! $this->result || is_wp_error( $this->result ) ) {
  137. return $this->result;
  138. }
  139. // Force refresh of plugin update information.
  140. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  141. if ( $parsed_args['overwrite_package'] ) {
  142. /**
  143. * Fires when the upgrader has successfully overwritten a currently installed
  144. * plugin or theme with an uploaded zip package.
  145. *
  146. * @since 5.5.0
  147. *
  148. * @param string $package The package file.
  149. * @param array $data The new plugin or theme data.
  150. * @param string $package_type The package type ('plugin' or 'theme').
  151. */
  152. do_action( 'upgrader_overwrote_package', $package, $this->new_plugin_data, 'plugin' );
  153. }
  154. return true;
  155. }
  156. /**
  157. * Upgrade a plugin.
  158. *
  159. * @since 2.8.0
  160. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  161. *
  162. * @param string $plugin Path to the plugin file relative to the plugins directory.
  163. * @param array $args {
  164. * Optional. Other arguments for upgrading a plugin package. Default empty array.
  165. *
  166. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  167. * Default true.
  168. * }
  169. * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
  170. */
  171. public function upgrade( $plugin, $args = array() ) {
  172. $defaults = array(
  173. 'clear_update_cache' => true,
  174. );
  175. $parsed_args = wp_parse_args( $args, $defaults );
  176. $this->init();
  177. $this->upgrade_strings();
  178. $current = get_site_transient( 'update_plugins' );
  179. if ( ! isset( $current->response[ $plugin ] ) ) {
  180. $this->skin->before();
  181. $this->skin->set_result( false );
  182. $this->skin->error( 'up_to_date' );
  183. $this->skin->after();
  184. return false;
  185. }
  186. // Get the URL to the zip file.
  187. $r = $current->response[ $plugin ];
  188. add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
  189. add_filter( 'upgrader_pre_install', array( $this, 'active_before' ), 10, 2 );
  190. add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
  191. add_filter( 'upgrader_post_install', array( $this, 'active_after' ), 10, 2 );
  192. // There's a Trac ticket to move up the directory for zips which are made a bit differently, useful for non-.org plugins.
  193. // 'source_selection' => array( $this, 'source_selection' ),
  194. if ( $parsed_args['clear_update_cache'] ) {
  195. // Clear cache so wp_update_plugins() knows about the new plugin.
  196. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
  197. }
  198. $this->run(
  199. array(
  200. 'package' => $r->package,
  201. 'destination' => WP_PLUGIN_DIR,
  202. 'clear_destination' => true,
  203. 'clear_working' => true,
  204. 'hook_extra' => array(
  205. 'plugin' => $plugin,
  206. 'type' => 'plugin',
  207. 'action' => 'update',
  208. ),
  209. )
  210. );
  211. // Cleanup our hooks, in case something else does a upgrade on this connection.
  212. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
  213. remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
  214. remove_filter( 'upgrader_pre_install', array( $this, 'active_before' ) );
  215. remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
  216. remove_filter( 'upgrader_post_install', array( $this, 'active_after' ) );
  217. if ( ! $this->result || is_wp_error( $this->result ) ) {
  218. return $this->result;
  219. }
  220. // Force refresh of plugin update information.
  221. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  222. // Ensure any future auto-update failures trigger a failure email by removing
  223. // the last failure notification from the list when plugins update successfully.
  224. $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
  225. if ( isset( $past_failure_emails[ $plugin ] ) ) {
  226. unset( $past_failure_emails[ $plugin ] );
  227. update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
  228. }
  229. return true;
  230. }
  231. /**
  232. * Bulk upgrade several plugins at once.
  233. *
  234. * @since 2.8.0
  235. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  236. *
  237. * @param string[] $plugins Array of paths to plugin files relative to the plugins directory.
  238. * @param array $args {
  239. * Optional. Other arguments for upgrading several plugins at once.
  240. *
  241. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. Default true.
  242. * }
  243. * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
  244. */
  245. public function bulk_upgrade( $plugins, $args = array() ) {
  246. $defaults = array(
  247. 'clear_update_cache' => true,
  248. );
  249. $parsed_args = wp_parse_args( $args, $defaults );
  250. $this->init();
  251. $this->bulk = true;
  252. $this->upgrade_strings();
  253. $current = get_site_transient( 'update_plugins' );
  254. add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
  255. $this->skin->header();
  256. // Connect to the filesystem first.
  257. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
  258. if ( ! $res ) {
  259. $this->skin->footer();
  260. return false;
  261. }
  262. $this->skin->bulk_header();
  263. /*
  264. * Only start maintenance mode if:
  265. * - running Multisite and there are one or more plugins specified, OR
  266. * - a plugin with an update available is currently active.
  267. * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
  268. */
  269. $maintenance = ( is_multisite() && ! empty( $plugins ) );
  270. foreach ( $plugins as $plugin ) {
  271. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) );
  272. }
  273. if ( $maintenance ) {
  274. $this->maintenance_mode( true );
  275. }
  276. $results = array();
  277. $this->update_count = count( $plugins );
  278. $this->update_current = 0;
  279. foreach ( $plugins as $plugin ) {
  280. $this->update_current++;
  281. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );
  282. if ( ! isset( $current->response[ $plugin ] ) ) {
  283. $this->skin->set_result( 'up_to_date' );
  284. $this->skin->before();
  285. $this->skin->feedback( 'up_to_date' );
  286. $this->skin->after();
  287. $results[ $plugin ] = true;
  288. continue;
  289. }
  290. // Get the URL to the zip file.
  291. $r = $current->response[ $plugin ];
  292. $this->skin->plugin_active = is_plugin_active( $plugin );
  293. $result = $this->run(
  294. array(
  295. 'package' => $r->package,
  296. 'destination' => WP_PLUGIN_DIR,
  297. 'clear_destination' => true,
  298. 'clear_working' => true,
  299. 'is_multi' => true,
  300. 'hook_extra' => array(
  301. 'plugin' => $plugin,
  302. ),
  303. )
  304. );
  305. $results[ $plugin ] = $result;
  306. // Prevent credentials auth screen from displaying multiple times.
  307. if ( false === $result ) {
  308. break;
  309. }
  310. } // End foreach $plugins.
  311. $this->maintenance_mode( false );
  312. // Force refresh of plugin update information.
  313. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  314. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
  315. do_action(
  316. 'upgrader_process_complete',
  317. $this,
  318. array(
  319. 'action' => 'update',
  320. 'type' => 'plugin',
  321. 'bulk' => true,
  322. 'plugins' => $plugins,
  323. )
  324. );
  325. $this->skin->bulk_footer();
  326. $this->skin->footer();
  327. // Cleanup our hooks, in case something else does a upgrade on this connection.
  328. remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
  329. // Ensure any future auto-update failures trigger a failure email by removing
  330. // the last failure notification from the list when plugins update successfully.
  331. $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
  332. foreach ( $results as $plugin => $result ) {
  333. // Maintain last failure notification when plugins failed to update manually.
  334. if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $plugin ] ) ) {
  335. continue;
  336. }
  337. unset( $past_failure_emails[ $plugin ] );
  338. }
  339. update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
  340. return $results;
  341. }
  342. /**
  343. * Checks that the source package contains a valid plugin.
  344. *
  345. * Hooked to the {@see 'upgrader_source_selection'} filter by Plugin_Upgrader::install().
  346. *
  347. * @since 3.3.0
  348. *
  349. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  350. * @global string $wp_version The WordPress version string.
  351. *
  352. * @param string $source The path to the downloaded package source.
  353. * @return string|WP_Error The source as passed, or a WP_Error object on failure.
  354. */
  355. public function check_package( $source ) {
  356. global $wp_filesystem, $wp_version;
  357. $this->new_plugin_data = array();
  358. if ( is_wp_error( $source ) ) {
  359. return $source;
  360. }
  361. $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
  362. if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
  363. return $source;
  364. }
  365. // Check that the folder contains at least 1 valid plugin.
  366. $files = glob( $working_directory . '*.php' );
  367. if ( $files ) {
  368. foreach ( $files as $file ) {
  369. $info = get_plugin_data( $file, false, false );
  370. if ( ! empty( $info['Name'] ) ) {
  371. $this->new_plugin_data = $info;
  372. break;
  373. }
  374. }
  375. }
  376. if ( empty( $this->new_plugin_data ) ) {
  377. return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
  378. }
  379. $requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
  380. $requires_wp = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
  381. if ( ! is_php_version_compatible( $requires_php ) ) {
  382. $error = sprintf(
  383. /* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
  384. __( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
  385. PHP_VERSION,
  386. $requires_php
  387. );
  388. return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
  389. }
  390. if ( ! is_wp_version_compatible( $requires_wp ) ) {
  391. $error = sprintf(
  392. /* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
  393. __( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
  394. $wp_version,
  395. $requires_wp
  396. );
  397. return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
  398. }
  399. return $source;
  400. }
  401. /**
  402. * Retrieve the path to the file that contains the plugin info.
  403. *
  404. * This isn't used internally in the class, but is called by the skins.
  405. *
  406. * @since 2.8.0
  407. *
  408. * @return string|false The full path to the main plugin file, or false.
  409. */
  410. public function plugin_info() {
  411. if ( ! is_array( $this->result ) ) {
  412. return false;
  413. }
  414. if ( empty( $this->result['destination_name'] ) ) {
  415. return false;
  416. }
  417. // Ensure to pass with leading slash.
  418. $plugin = get_plugins( '/' . $this->result['destination_name'] );
  419. if ( empty( $plugin ) ) {
  420. return false;
  421. }
  422. // Assume the requested plugin is the first in the list.
  423. $pluginfiles = array_keys( $plugin );
  424. return $this->result['destination_name'] . '/' . $pluginfiles[0];
  425. }
  426. /**
  427. * Deactivates a plugin before it is upgraded.
  428. *
  429. * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
  430. *
  431. * @since 2.8.0
  432. * @since 4.1.0 Added a return value.
  433. *
  434. * @param bool|WP_Error $response The installation response before the installation has started.
  435. * @param array $plugin Plugin package arguments.
  436. * @return bool|WP_Error The original `$response` parameter or WP_Error.
  437. */
  438. public function deactivate_plugin_before_upgrade( $response, $plugin ) {
  439. if ( is_wp_error( $response ) ) { // Bypass.
  440. return $response;
  441. }
  442. // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it.
  443. if ( wp_doing_cron() ) {
  444. return $response;
  445. }
  446. $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
  447. if ( empty( $plugin ) ) {
  448. return new WP_Error( 'bad_request', $this->strings['bad_request'] );
  449. }
  450. if ( is_plugin_active( $plugin ) ) {
  451. // Deactivate the plugin silently, Prevent deactivation hooks from running.
  452. deactivate_plugins( $plugin, true );
  453. }
  454. return $response;
  455. }
  456. /**
  457. * Turns on maintenance mode before attempting to background update an active plugin.
  458. *
  459. * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
  460. *
  461. * @since 5.4.0
  462. *
  463. * @param bool|WP_Error $response The installation response before the installation has started.
  464. * @param array $plugin Plugin package arguments.
  465. * @return bool|WP_Error The original `$response` parameter or WP_Error.
  466. */
  467. public function active_before( $response, $plugin ) {
  468. if ( is_wp_error( $response ) ) {
  469. return $response;
  470. }
  471. // Only enable maintenance mode when in cron (background update).
  472. if ( ! wp_doing_cron() ) {
  473. return $response;
  474. }
  475. $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
  476. // Only run if plugin is active.
  477. if ( ! is_plugin_active( $plugin ) ) {
  478. return $response;
  479. }
  480. // Change to maintenance mode. Bulk edit handles this separately.
  481. if ( ! $this->bulk ) {
  482. $this->maintenance_mode( true );
  483. }
  484. return $response;
  485. }
  486. /**
  487. * Turns off maintenance mode after upgrading an active plugin.
  488. *
  489. * Hooked to the {@see 'upgrader_post_install'} filter by Plugin_Upgrader::upgrade().
  490. *
  491. * @since 5.4.0
  492. *
  493. * @param bool|WP_Error $response The installation response after the installation has finished.
  494. * @param array $plugin Plugin package arguments.
  495. * @return bool|WP_Error The original `$response` parameter or WP_Error.
  496. */
  497. public function active_after( $response, $plugin ) {
  498. if ( is_wp_error( $response ) ) {
  499. return $response;
  500. }
  501. // Only disable maintenance mode when in cron (background update).
  502. if ( ! wp_doing_cron() ) {
  503. return $response;
  504. }
  505. $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
  506. // Only run if plugin is active.
  507. if ( ! is_plugin_active( $plugin ) ) {
  508. return $response;
  509. }
  510. // Time to remove maintenance mode. Bulk edit handles this separately.
  511. if ( ! $this->bulk ) {
  512. $this->maintenance_mode( false );
  513. }
  514. return $response;
  515. }
  516. /**
  517. * Deletes the old plugin during an upgrade.
  518. *
  519. * Hooked to the {@see 'upgrader_clear_destination'} filter by
  520. * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
  521. *
  522. * @since 2.8.0
  523. *
  524. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  525. *
  526. * @param bool|WP_Error $removed Whether the destination was cleared.
  527. * True on success, WP_Error on failure.
  528. * @param string $local_destination The local package destination.
  529. * @param string $remote_destination The remote package destination.
  530. * @param array $plugin Extra arguments passed to hooked filters.
  531. * @return bool|WP_Error
  532. */
  533. public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) {
  534. global $wp_filesystem;
  535. if ( is_wp_error( $removed ) ) {
  536. return $removed; // Pass errors through.
  537. }
  538. $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
  539. if ( empty( $plugin ) ) {
  540. return new WP_Error( 'bad_request', $this->strings['bad_request'] );
  541. }
  542. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  543. $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
  544. if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { // If it's already vanished.
  545. return $removed;
  546. }
  547. // If plugin is in its own directory, recursively delete the directory.
  548. // Base check on if plugin includes directory separator AND that it's not the root plugin folder.
  549. if ( strpos( $plugin, '/' ) && $this_plugin_dir !== $plugins_dir ) {
  550. $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
  551. } else {
  552. $deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
  553. }
  554. if ( ! $deleted ) {
  555. return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
  556. }
  557. return true;
  558. }
  559. }