/wp-admin/includes/class-plugin-upgrader.php

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