PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/WPonEB/WPonEB
PHP | 459 lines | 223 code | 73 blank | 163 comment | 33 complexity | 1e51c811e6fa2c3e3af0ec51104bf689 MD5 | raw file
  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( array(
  97. 'package' => $package,
  98. 'destination' => WP_PLUGIN_DIR,
  99. 'clear_destination' => false, // Do not overwrite files.
  100. 'clear_working' => true,
  101. 'hook_extra' => array(
  102. 'type' => 'plugin',
  103. 'action' => 'install',
  104. )
  105. ) );
  106. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
  107. remove_filter('upgrader_source_selection', array($this, 'check_package') );
  108. if ( ! $this->result || is_wp_error($this->result) )
  109. return $this->result;
  110. // Force refresh of plugin update information
  111. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  112. return true;
  113. }
  114. /**
  115. * Upgrade a plugin.
  116. *
  117. * @since 2.8.0
  118. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  119. *
  120. * @param string $plugin The basename path to the main plugin file.
  121. * @param array $args {
  122. * Optional. Other arguments for upgrading a plugin package. Default empty array.
  123. *
  124. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  125. * Default true.
  126. * }
  127. * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
  128. */
  129. public function upgrade( $plugin, $args = array() ) {
  130. $defaults = array(
  131. 'clear_update_cache' => true,
  132. );
  133. $parsed_args = wp_parse_args( $args, $defaults );
  134. $this->init();
  135. $this->upgrade_strings();
  136. $current = get_site_transient( 'update_plugins' );
  137. if ( !isset( $current->response[ $plugin ] ) ) {
  138. $this->skin->before();
  139. $this->skin->set_result(false);
  140. $this->skin->error('up_to_date');
  141. $this->skin->after();
  142. return false;
  143. }
  144. // Get the URL to the zip file
  145. $r = $current->response[ $plugin ];
  146. add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
  147. add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
  148. //'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.
  149. if ( $parsed_args['clear_update_cache'] ) {
  150. // Clear cache so wp_update_plugins() knows about the new plugin.
  151. add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
  152. }
  153. $this->run( array(
  154. 'package' => $r->package,
  155. 'destination' => WP_PLUGIN_DIR,
  156. 'clear_destination' => true,
  157. 'clear_working' => true,
  158. 'hook_extra' => array(
  159. 'plugin' => $plugin,
  160. 'type' => 'plugin',
  161. 'action' => 'update',
  162. ),
  163. ) );
  164. // Cleanup our hooks, in case something else does a upgrade on this connection.
  165. remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
  166. remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
  167. remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
  168. if ( ! $this->result || is_wp_error($this->result) )
  169. return $this->result;
  170. // Force refresh of plugin update information
  171. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  172. return true;
  173. }
  174. /**
  175. * Bulk upgrade several plugins at once.
  176. *
  177. * @since 2.8.0
  178. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  179. *
  180. * @param array $plugins Array of the basename paths of the plugins' main files.
  181. * @param array $args {
  182. * Optional. Other arguments for upgrading several plugins at once. Default empty array.
  183. *
  184. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  185. * Default true.
  186. * }
  187. * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
  188. */
  189. public function bulk_upgrade( $plugins, $args = array() ) {
  190. $defaults = array(
  191. 'clear_update_cache' => true,
  192. );
  193. $parsed_args = wp_parse_args( $args, $defaults );
  194. $this->init();
  195. $this->bulk = true;
  196. $this->upgrade_strings();
  197. $current = get_site_transient( 'update_plugins' );
  198. add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
  199. $this->skin->header();
  200. // Connect to the Filesystem first.
  201. $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
  202. if ( ! $res ) {
  203. $this->skin->footer();
  204. return false;
  205. }
  206. $this->skin->bulk_header();
  207. /*
  208. * Only start maintenance mode if:
  209. * - running Multisite and there are one or more plugins specified, OR
  210. * - a plugin with an update available is currently active.
  211. * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
  212. */
  213. $maintenance = ( is_multisite() && ! empty( $plugins ) );
  214. foreach ( $plugins as $plugin )
  215. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
  216. if ( $maintenance )
  217. $this->maintenance_mode(true);
  218. $results = array();
  219. $this->update_count = count($plugins);
  220. $this->update_current = 0;
  221. foreach ( $plugins as $plugin ) {
  222. $this->update_current++;
  223. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
  224. if ( !isset( $current->response[ $plugin ] ) ) {
  225. $this->skin->set_result('up_to_date');
  226. $this->skin->before();
  227. $this->skin->feedback('up_to_date');
  228. $this->skin->after();
  229. $results[$plugin] = true;
  230. continue;
  231. }
  232. // Get the URL to the zip file.
  233. $r = $current->response[ $plugin ];
  234. $this->skin->plugin_active = is_plugin_active($plugin);
  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. // Prevent credentials auth screen from displaying multiple times
  247. if ( false === $result )
  248. break;
  249. } //end foreach $plugins
  250. $this->maintenance_mode(false);
  251. // Force refresh of plugin update information.
  252. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  253. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
  254. do_action( 'upgrader_process_complete', $this, array(
  255. 'action' => 'update',
  256. 'type' => 'plugin',
  257. 'bulk' => true,
  258. 'plugins' => $plugins,
  259. ) );
  260. $this->skin->bulk_footer();
  261. $this->skin->footer();
  262. // Cleanup our hooks, in case something else does a upgrade on this connection.
  263. remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
  264. return $results;
  265. }
  266. /**
  267. * Check a source package to be sure it contains a plugin.
  268. *
  269. * This function is added to the {@see 'upgrader_source_selection'} filter by
  270. * Plugin_Upgrader::install().
  271. *
  272. * @since 3.3.0
  273. *
  274. * @global WP_Filesystem_Base $wp_filesystem Subclass
  275. *
  276. * @param string $source The path to the downloaded package source.
  277. * @return string|WP_Error The source as passed, or a WP_Error object
  278. * if no plugins were found.
  279. */
  280. public function check_package($source) {
  281. global $wp_filesystem;
  282. if ( is_wp_error($source) )
  283. return $source;
  284. $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
  285. if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
  286. return $source;
  287. // Check the folder contains at least 1 valid plugin.
  288. $plugins_found = false;
  289. $files = glob( $working_directory . '*.php' );
  290. if ( $files ) {
  291. foreach ( $files as $file ) {
  292. $info = get_plugin_data( $file, false, false );
  293. if ( ! empty( $info['Name'] ) ) {
  294. $plugins_found = true;
  295. break;
  296. }
  297. }
  298. }
  299. if ( ! $plugins_found )
  300. return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
  301. return $source;
  302. }
  303. /**
  304. * Retrieve the path to the file that contains the plugin info.
  305. *
  306. * This isn't used internally in the class, but is called by the skins.
  307. *
  308. * @since 2.8.0
  309. *
  310. * @return string|false The full path to the main plugin file, or false.
  311. */
  312. public function plugin_info() {
  313. if ( ! is_array($this->result) )
  314. return false;
  315. if ( empty($this->result['destination_name']) )
  316. return false;
  317. $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
  318. if ( empty($plugin) )
  319. return false;
  320. $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
  321. return $this->result['destination_name'] . '/' . $pluginfiles[0];
  322. }
  323. /**
  324. * Deactivates a plugin before it is upgraded.
  325. *
  326. * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
  327. *
  328. * @since 2.8.0
  329. * @since 4.1.0 Added a return value.
  330. *
  331. * @param bool|WP_Error $return Upgrade offer return.
  332. * @param array $plugin Plugin package arguments.
  333. * @return bool|WP_Error The passed in $return param or WP_Error.
  334. */
  335. public function deactivate_plugin_before_upgrade($return, $plugin) {
  336. if ( is_wp_error($return) ) //Bypass.
  337. return $return;
  338. // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
  339. if ( wp_doing_cron() )
  340. return $return;
  341. $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
  342. if ( empty($plugin) )
  343. return new WP_Error('bad_request', $this->strings['bad_request']);
  344. if ( is_plugin_active($plugin) ) {
  345. //Deactivate the plugin silently, Prevent deactivation hooks from running.
  346. deactivate_plugins($plugin, true);
  347. }
  348. return $return;
  349. }
  350. /**
  351. * Delete the old plugin during an upgrade.
  352. *
  353. * Hooked to the {@see 'upgrader_clear_destination'} filter by
  354. * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
  355. *
  356. * @since 2.8.0
  357. *
  358. * @global WP_Filesystem_Base $wp_filesystem Subclass
  359. *
  360. * @param bool|WP_Error $removed
  361. * @param string $local_destination
  362. * @param string $remote_destination
  363. * @param array $plugin
  364. * @return WP_Error|bool
  365. */
  366. public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
  367. global $wp_filesystem;
  368. if ( is_wp_error($removed) )
  369. return $removed; //Pass errors through.
  370. $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
  371. if ( empty($plugin) )
  372. return new WP_Error('bad_request', $this->strings['bad_request']);
  373. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  374. $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
  375. if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
  376. return $removed;
  377. // If plugin is in its own directory, recursively delete the directory.
  378. 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
  379. $deleted = $wp_filesystem->delete($this_plugin_dir, true);
  380. else
  381. $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
  382. if ( ! $deleted )
  383. return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
  384. return true;
  385. }
  386. }