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. // Clear cache so wp_update_plugins() knows about the new plugin. 
  75. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 ); 
  76.  
  77. $this->run( array( 
  78. 'package' => $package,  
  79. 'destination' => WP_PLUGIN_DIR,  
  80. 'clear_destination' => false, // Do not overwrite files. 
  81. 'clear_working' => true,  
  82. 'hook_extra' => array( 
  83. 'type' => 'plugin',  
  84. 'action' => 'install',  
  85. ) ); 
  86.  
  87. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 ); 
  88. remove_filter('upgrader_source_selection', array($this, 'check_package') ); 
  89.  
  90. if ( ! $this->result || is_wp_error($this->result) ) 
  91. return $this->result; 
  92.  
  93. // Force refresh of plugin update information 
  94. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] ); 
  95.  
  96. return true; 
  97.  
  98. /** 
  99. * Upgrade a plugin. 
  100. * @since 2.8.0 
  101. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional. 
  102. * @access public 
  103. * @param string $plugin The basename path to the main plugin file. 
  104. * @param array $args { 
  105. * Optional. Other arguments for upgrading a plugin package. Default empty array. 
  106. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. 
  107. * Default true. 
  108. * } 
  109. * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise. 
  110. */ 
  111. public function upgrade( $plugin, $args = array() ) { 
  112.  
  113. $defaults = array( 
  114. 'clear_update_cache' => true,  
  115. ); 
  116. $parsed_args = wp_parse_args( $args, $defaults ); 
  117.  
  118. $this->init(); 
  119. $this->upgrade_strings(); 
  120.  
  121. $current = get_site_transient( 'update_plugins' ); 
  122. if ( !isset( $current->response[ $plugin ] ) ) { 
  123. $this->skin->before(); 
  124. $this->skin->set_result(false); 
  125. $this->skin->error('up_to_date'); 
  126. $this->skin->after(); 
  127. return false; 
  128.  
  129. // Get the URL to the zip file 
  130. $r = $current->response[ $plugin ]; 
  131.  
  132. add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2); 
  133. add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); 
  134. //'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. 
  135. // Clear cache so wp_update_plugins() knows about the new plugin. 
  136. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 ); 
  137.  
  138. $this->run( array( 
  139. 'package' => $r->package,  
  140. 'destination' => WP_PLUGIN_DIR,  
  141. 'clear_destination' => true,  
  142. 'clear_working' => true,  
  143. 'hook_extra' => array( 
  144. 'plugin' => $plugin,  
  145. 'type' => 'plugin',  
  146. 'action' => 'update',  
  147. ),  
  148. ) ); 
  149.  
  150. // Cleanup our hooks, in case something else does a upgrade on this connection. 
  151. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 ); 
  152. remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade')); 
  153. remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); 
  154.  
  155. if ( ! $this->result || is_wp_error($this->result) ) 
  156. return $this->result; 
  157.  
  158. // Force refresh of plugin update information 
  159. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] ); 
  160.  
  161. return true; 
  162.  
  163. /** 
  164. * Bulk upgrade several plugins at once. 
  165. * @since 2.8.0 
  166. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional. 
  167. * @access public 
  168. * @param array $plugins Array of the basename paths of the plugins' main files. 
  169. * @param array $args { 
  170. * Optional. Other arguments for upgrading several plugins at once. Default empty array. 
  171. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. 
  172. * Default true. 
  173. * } 
  174. * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem. 
  175. */ 
  176. public function bulk_upgrade( $plugins, $args = array() ) { 
  177.  
  178. $defaults = array( 
  179. 'clear_update_cache' => true,  
  180. ); 
  181. $parsed_args = wp_parse_args( $args, $defaults ); 
  182.  
  183. $this->init(); 
  184. $this->bulk = true; 
  185. $this->upgrade_strings(); 
  186.  
  187. $current = get_site_transient( 'update_plugins' ); 
  188.  
  189. add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); 
  190. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 ); 
  191.  
  192. $this->skin->header(); 
  193.  
  194. // Connect to the Filesystem first. 
  195. $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) ); 
  196. if ( ! $res ) { 
  197. $this->skin->footer(); 
  198. return false; 
  199.  
  200. $this->skin->bulk_header(); 
  201.  
  202. /** 
  203. * Only start maintenance mode if: 
  204. * - running Multisite and there are one or more plugins specified, OR 
  205. * - a plugin with an update available is currently active. 
  206. * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible. 
  207. */ 
  208. $maintenance = ( is_multisite() && ! empty( $plugins ) ); 
  209. foreach ( $plugins as $plugin ) 
  210. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) ); 
  211. if ( $maintenance ) 
  212. $this->maintenance_mode(true); 
  213.  
  214. $results = array(); 
  215.  
  216. $this->update_count = count($plugins); 
  217. $this->update_current = 0; 
  218. foreach ( $plugins as $plugin ) { 
  219. $this->update_current++; 
  220. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true); 
  221.  
  222. if ( !isset( $current->response[ $plugin ] ) ) { 
  223. $this->skin->set_result('up_to_date'); 
  224. $this->skin->before(); 
  225. $this->skin->feedback('up_to_date'); 
  226. $this->skin->after(); 
  227. $results[$plugin] = true; 
  228. continue; 
  229.  
  230. // Get the URL to the zip file. 
  231. $r = $current->response[ $plugin ]; 
  232.  
  233. $this->skin->plugin_active = is_plugin_active($plugin); 
  234.  
  235. $result = $this->run( array( 
  236. 'package' => $r->package,  
  237. 'destination' => WP_PLUGIN_DIR,  
  238. 'clear_destination' => true,  
  239. 'clear_working' => true,  
  240. 'is_multi' => true,  
  241. 'hook_extra' => array( 
  242. 'plugin' => $plugin 
  243. ) ); 
  244.  
  245. $results[$plugin] = $this->result; 
  246.  
  247. // Prevent credentials auth screen from displaying multiple times 
  248. if ( false === $result ) 
  249. break; 
  250. } //end foreach $plugins 
  251.  
  252. $this->maintenance_mode(false); 
  253.  
  254. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */ 
  255. do_action( 'upgrader_process_complete', $this, array( 
  256. 'action' => 'update',  
  257. 'type' => 'plugin',  
  258. 'bulk' => true,  
  259. 'plugins' => $plugins,  
  260. ) ); 
  261.  
  262. $this->skin->bulk_footer(); 
  263.  
  264. $this->skin->footer(); 
  265.  
  266. // Cleanup our hooks, in case something else does a upgrade on this connection. 
  267. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 ); 
  268. remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); 
  269.  
  270. // Force refresh of plugin update information. 
  271. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] ); 
  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;