Plugin_Upgrader

Core class used for upgrading/installing plugins.

Defined (1)

The class is defined in the following location(s).

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