PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 3253 lines | 1651 code | 480 blank | 1122 comment | 373 complexity | c87fca030240b6d7113f3adcfb04fee3 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * A File upgrader class for WordPress.
  4. *
  5. * This set of classes are designed to be used to upgrade/install a local set of files on the filesystem via the Filesystem Abstraction classes.
  6. *
  7. * @link https://core.trac.wordpress.org/ticket/7875 consolidate plugin/theme/core upgrade/install functions
  8. *
  9. * @package WordPress
  10. * @subpackage Upgrader
  11. * @since 2.8.0
  12. */
  13. require ABSPATH . 'wp-admin/includes/class-wp-upgrader-skins.php';
  14. /**
  15. * WordPress Upgrader class for Upgrading/Installing a local set of files via the Filesystem Abstraction classes from a Zip file.
  16. *
  17. * @package WordPress
  18. * @subpackage Upgrader
  19. * @since 2.8.0
  20. */
  21. class WP_Upgrader {
  22. /**
  23. * The error/notification strings used to update the user on the progress.
  24. *
  25. * @since 2.8.0
  26. * @var string $strings
  27. */
  28. public $strings = array();
  29. /**
  30. * The upgrader skin being used.
  31. *
  32. * @since 2.8.0
  33. * @var WP_Upgrader_Skin $skin
  34. */
  35. public $skin = null;
  36. /**
  37. * The result of the installation.
  38. *
  39. * This is set by {@see WP_Upgrader::install_package()}, only when the package is installed
  40. * successfully. It will then be an array, unless a {@see WP_Error} is returned by the
  41. * {@see 'upgrader_post_install'} filter. In that case, the `WP_Error` will be assigned to
  42. * it.
  43. *
  44. * @since 2.8.0
  45. * @var WP_Error|array $result {
  46. * @type string $source The full path to the source the files were installed from.
  47. * @type string $source_files List of all the files in the source directory.
  48. * @type string $destination The full path to the install destination folder.
  49. * @type string $destination_name The name of the destination folder, or empty if `$destination`
  50. * and `$local_destination` are the same.
  51. * @type string $local_destination The full local path to the destination folder. This is usually
  52. * the same as `$destination`.
  53. * @type string $remote_destination The full remote path to the destination folder
  54. * (i.e., from `$wp_filesystem`).
  55. * @type bool $clear_destination Whether the destination folder was cleared.
  56. * }
  57. */
  58. public $result = array();
  59. /**
  60. * The total number of updates being performed.
  61. *
  62. * Set by the bulk update methods.
  63. *
  64. * @since 3.0.0
  65. * @var int $update_count
  66. */
  67. public $update_count = 0;
  68. /**
  69. * The current update if multiple updates are being performed.
  70. *
  71. * Used by the bulk update methods, and incremented for each update.
  72. *
  73. * @since 3.0.0
  74. * @var int
  75. */
  76. public $update_current = 0;
  77. /**
  78. * Construct the upgrader with a skin.
  79. *
  80. * @since 2.8.0
  81. *
  82. * @param WP_Upgrader_Skin $skin The upgrader skin to use. Default is a {@see WP_Upgrader_Skin}
  83. * instance.
  84. */
  85. public function __construct( $skin = null ) {
  86. if ( null == $skin )
  87. $this->skin = new WP_Upgrader_Skin();
  88. else
  89. $this->skin = $skin;
  90. }
  91. /**
  92. * Initialize the upgrader.
  93. *
  94. * This will set the relationship between the skin being used and this upgrader,
  95. * and also add the generic strings to `WP_Upgrader::$strings`.
  96. *
  97. * @since 2.8.0
  98. */
  99. public function init() {
  100. $this->skin->set_upgrader($this);
  101. $this->generic_strings();
  102. }
  103. /**
  104. * Add the generic strings to WP_Upgrader::$strings.
  105. *
  106. * @since 2.8.0
  107. */
  108. public function generic_strings() {
  109. $this->strings['bad_request'] = __('Invalid Data provided.');
  110. $this->strings['fs_unavailable'] = __('Could not access filesystem.');
  111. $this->strings['fs_error'] = __('Filesystem error.');
  112. $this->strings['fs_no_root_dir'] = __('Unable to locate WordPress Root directory.');
  113. $this->strings['fs_no_content_dir'] = __('Unable to locate WordPress Content directory (wp-content).');
  114. $this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress Plugin directory.');
  115. $this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress Theme directory.');
  116. /* translators: %s: directory name */
  117. $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
  118. $this->strings['download_failed'] = __('Download failed.');
  119. $this->strings['installing_package'] = __('Installing the latest version&#8230;');
  120. $this->strings['no_files'] = __('The package contains no files.');
  121. $this->strings['folder_exists'] = __('Destination folder already exists.');
  122. $this->strings['mkdir_failed'] = __('Could not create directory.');
  123. $this->strings['incompatible_archive'] = __('The package could not be installed.');
  124. $this->strings['maintenance_start'] = __('Enabling Maintenance mode&#8230;');
  125. $this->strings['maintenance_end'] = __('Disabling Maintenance mode&#8230;');
  126. }
  127. /**
  128. * Connect to the filesystem.
  129. *
  130. * @since 2.8.0
  131. *
  132. * @param array $directories Optional. A list of directories. If any of these do
  133. * not exist, a {@see WP_Error} object will be returned.
  134. * Default empty array.
  135. * @param bool $allow_relaxed_file_ownership Whether to allow relaxed file ownership.
  136. * Default false.
  137. * @return bool|WP_Error True if able to connect, false or a {@see WP_Error} otherwise.
  138. */
  139. public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
  140. global $wp_filesystem;
  141. if ( false === ( $credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership ) ) ) {
  142. return false;
  143. }
  144. if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
  145. $error = true;
  146. if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
  147. $error = $wp_filesystem->errors;
  148. // Failed to connect, Error and request again
  149. $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
  150. return false;
  151. }
  152. if ( ! is_object($wp_filesystem) )
  153. return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
  154. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  155. return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
  156. foreach ( (array)$directories as $dir ) {
  157. switch ( $dir ) {
  158. case ABSPATH:
  159. if ( ! $wp_filesystem->abspath() )
  160. return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
  161. break;
  162. case WP_CONTENT_DIR:
  163. if ( ! $wp_filesystem->wp_content_dir() )
  164. return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
  165. break;
  166. case WP_PLUGIN_DIR:
  167. if ( ! $wp_filesystem->wp_plugins_dir() )
  168. return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
  169. break;
  170. case get_theme_root():
  171. if ( ! $wp_filesystem->wp_themes_dir() )
  172. return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
  173. break;
  174. default:
  175. if ( ! $wp_filesystem->find_folder($dir) )
  176. return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
  177. break;
  178. }
  179. }
  180. return true;
  181. } //end fs_connect();
  182. /**
  183. * Download a package.
  184. *
  185. * @since 2.8.0
  186. *
  187. * @param string $package The URI of the package. If this is the full path to an
  188. * existing local file, it will be returned untouched.
  189. * @return string|WP_Error The full path to the downloaded package file, or a {@see WP_Error} object.
  190. */
  191. public function download_package( $package ) {
  192. /**
  193. * Filter whether to return the package.
  194. *
  195. * @since 3.7.0
  196. *
  197. * @param bool $reply Whether to bail without returning the package.
  198. * Default false.
  199. * @param string $package The package file name.
  200. * @param WP_Upgrader $this The WP_Upgrader instance.
  201. */
  202. $reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
  203. if ( false !== $reply )
  204. return $reply;
  205. if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
  206. return $package; //must be a local file..
  207. if ( empty($package) )
  208. return new WP_Error('no_package', $this->strings['no_package']);
  209. $this->skin->feedback('downloading_package', $package);
  210. $download_file = download_url($package);
  211. if ( is_wp_error($download_file) )
  212. return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
  213. return $download_file;
  214. }
  215. /**
  216. * Unpack a compressed package file.
  217. *
  218. * @since 2.8.0
  219. *
  220. * @param string $package Full path to the package file.
  221. * @param bool $delete_package Optional. Whether to delete the package file after attempting
  222. * to unpack it. Default true.
  223. * @return string|WP_Error The path to the unpacked contents, or a {@see WP_Error} on failure.
  224. */
  225. public function unpack_package( $package, $delete_package = true ) {
  226. global $wp_filesystem;
  227. $this->skin->feedback('unpack_package');
  228. $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
  229. //Clean up contents of upgrade directory beforehand.
  230. $upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
  231. if ( !empty($upgrade_files) ) {
  232. foreach ( $upgrade_files as $file )
  233. $wp_filesystem->delete($upgrade_folder . $file['name'], true);
  234. }
  235. //We need a working directory
  236. $working_dir = $upgrade_folder . basename($package, '.zip');
  237. // Clean up working directory
  238. if ( $wp_filesystem->is_dir($working_dir) )
  239. $wp_filesystem->delete($working_dir, true);
  240. // Unzip package to working directory
  241. $result = unzip_file( $package, $working_dir );
  242. // Once extracted, delete the package if required.
  243. if ( $delete_package )
  244. unlink($package);
  245. if ( is_wp_error($result) ) {
  246. $wp_filesystem->delete($working_dir, true);
  247. if ( 'incompatible_archive' == $result->get_error_code() ) {
  248. return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
  249. }
  250. return $result;
  251. }
  252. return $working_dir;
  253. }
  254. /**
  255. * Install a package.
  256. *
  257. * Copies the contents of a package form a source directory, and installs them in
  258. * a destination directory. Optionally removes the source. It can also optionally
  259. * clear out the destination folder if it already exists.
  260. *
  261. * @since 2.8.0
  262. *
  263. * @param array|string $args {
  264. * Optional. Array or string of arguments for installing a package. Default empty array.
  265. *
  266. * @type string $source Required path to the package source. Default empty.
  267. * @type string $destination Required path to a folder to install the package in.
  268. * Default empty.
  269. * @type bool $clear_destination Whether to delete any files already in the destination
  270. * folder. Default false.
  271. * @type bool $clear_working Whether to delete the files form the working directory
  272. * after copying to the destination. Default false.
  273. * @type bool $abort_if_destination_exists Whether to abort the installation if
  274. * the destination folder already exists. Default true.
  275. * @type array $hook_extra Extra arguments to pass to the filter hooks called by
  276. * {@see WP_Upgrader::install_package()}. Default empty array.
  277. * }
  278. *
  279. * @return array|WP_Error The result (also stored in `WP_Upgrader:$result`), or a {@see WP_Error} on failure.
  280. */
  281. public function install_package( $args = array() ) {
  282. global $wp_filesystem, $wp_theme_directories;
  283. $defaults = array(
  284. 'source' => '', // Please always pass this
  285. 'destination' => '', // and this
  286. 'clear_destination' => false,
  287. 'clear_working' => false,
  288. 'abort_if_destination_exists' => true,
  289. 'hook_extra' => array()
  290. );
  291. $args = wp_parse_args($args, $defaults);
  292. // These were previously extract()'d.
  293. $source = $args['source'];
  294. $destination = $args['destination'];
  295. $clear_destination = $args['clear_destination'];
  296. @set_time_limit( 300 );
  297. if ( empty( $source ) || empty( $destination ) ) {
  298. return new WP_Error( 'bad_request', $this->strings['bad_request'] );
  299. }
  300. $this->skin->feedback( 'installing_package' );
  301. /**
  302. * Filter the install response before the installation has started.
  303. *
  304. * Returning a truthy value, or one that could be evaluated as a WP_Error
  305. * will effectively short-circuit the installation, returning that value
  306. * instead.
  307. *
  308. * @since 2.8.0
  309. *
  310. * @param bool|WP_Error $response Response.
  311. * @param array $hook_extra Extra arguments passed to hooked filters.
  312. */
  313. $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
  314. if ( is_wp_error( $res ) ) {
  315. return $res;
  316. }
  317. //Retain the Original source and destinations
  318. $remote_source = $args['source'];
  319. $local_destination = $destination;
  320. $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
  321. $remote_destination = $wp_filesystem->find_folder( $local_destination );
  322. //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
  323. if ( 1 == count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) { //Only one folder? Then we want its contents.
  324. $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
  325. } elseif ( count( $source_files ) == 0 ) {
  326. return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
  327. } else { //It's only a single file, the upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
  328. $source = trailingslashit( $args['source'] );
  329. }
  330. /**
  331. * Filter the source file location for the upgrade package.
  332. *
  333. * @since 2.8.0
  334. *
  335. * @param string $source File source location.
  336. * @param string $remote_source Remove file source location.
  337. * @param WP_Upgrader $this WP_Upgrader instance.
  338. */
  339. $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this );
  340. if ( is_wp_error( $source ) ) {
  341. return $source;
  342. }
  343. // Has the source location changed? If so, we need a new source_files list.
  344. if ( $source !== $remote_source ) {
  345. $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
  346. }
  347. /*
  348. * Protection against deleting files in any important base directories.
  349. * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
  350. * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
  351. * to copy the directory into the directory, whilst they pass the source
  352. * as the actual files to copy.
  353. */
  354. $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
  355. if ( is_array( $wp_theme_directories ) ) {
  356. $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
  357. }
  358. if ( in_array( $destination, $protected_directories ) ) {
  359. $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
  360. $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
  361. }
  362. if ( $clear_destination ) {
  363. //We're going to clear the destination if there's something there
  364. $this->skin->feedback('remove_old');
  365. $removed = true;
  366. if ( $wp_filesystem->exists( $remote_destination ) ) {
  367. $removed = $wp_filesystem->delete( $remote_destination, true );
  368. }
  369. /**
  370. * Filter whether the upgrader cleared the destination.
  371. *
  372. * @since 2.8.0
  373. *
  374. * @param bool $removed Whether the destination was cleared.
  375. * @param string $local_destination The local package destination.
  376. * @param string $remote_destination The remote package destination.
  377. * @param array $hook_extra Extra arguments passed to hooked filters.
  378. */
  379. $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
  380. if ( is_wp_error($removed) ) {
  381. return $removed;
  382. } else if ( ! $removed ) {
  383. return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
  384. }
  385. } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists($remote_destination) ) {
  386. //If we're not clearing the destination folder and something exists there already, Bail.
  387. //But first check to see if there are actually any files in the folder.
  388. $_files = $wp_filesystem->dirlist($remote_destination);
  389. if ( ! empty($_files) ) {
  390. $wp_filesystem->delete($remote_source, true); //Clear out the source files.
  391. return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
  392. }
  393. }
  394. //Create destination if needed
  395. if ( ! $wp_filesystem->exists( $remote_destination ) ) {
  396. if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
  397. return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
  398. }
  399. }
  400. // Copy new version of item into place.
  401. $result = copy_dir($source, $remote_destination);
  402. if ( is_wp_error($result) ) {
  403. if ( $args['clear_working'] ) {
  404. $wp_filesystem->delete( $remote_source, true );
  405. }
  406. return $result;
  407. }
  408. //Clear the Working folder?
  409. if ( $args['clear_working'] ) {
  410. $wp_filesystem->delete( $remote_source, true );
  411. }
  412. $destination_name = basename( str_replace($local_destination, '', $destination) );
  413. if ( '.' == $destination_name ) {
  414. $destination_name = '';
  415. }
  416. $this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
  417. /**
  418. * Filter the install response after the installation has finished.
  419. *
  420. * @since 2.8.0
  421. *
  422. * @param bool $response Install response.
  423. * @param array $hook_extra Extra arguments passed to hooked filters.
  424. * @param array $result Installation result data.
  425. */
  426. $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
  427. if ( is_wp_error($res) ) {
  428. $this->result = $res;
  429. return $res;
  430. }
  431. //Bombard the calling function will all the info which we've just used.
  432. return $this->result;
  433. }
  434. /**
  435. * Run an upgrade/install.
  436. *
  437. * Attempts to download the package (if it is not a local file), unpack it, and
  438. * install it in the destination folder.
  439. *
  440. * @since 2.8.0
  441. *
  442. * @param array $options {
  443. * Array or string of arguments for upgrading/installing a package.
  444. *
  445. * @type string $package The full path or URI of the package to install.
  446. * Default empty.
  447. * @type string $destination The full path to the destination folder.
  448. * Default empty.
  449. * @type bool $clear_destination Whether to delete any files already in the
  450. * destination folder. Default false.
  451. * @type bool $clear_working Whether to delete the files form the working
  452. * directory after copying to the destination.
  453. * Default false.
  454. * @type bool $abort_if_destination_exists Whether to abort the installation if the destination
  455. * folder already exists. When true, `$clear_destination`
  456. * should be false. Default true.
  457. * @type bool $is_multi Whether this run is one of multiple upgrade/install
  458. * actions being performed in bulk. When true, the skin
  459. * {@see WP_Upgrader::header()} and {@see WP_Upgrader::footer()}
  460. * aren't called. Default false.
  461. * @type array $hook_extra Extra arguments to pass to the filter hooks called by
  462. * {@see WP_Upgrader::run()}.
  463. * }
  464. *
  465. * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error,
  466. * or false if unable to connect to the filesystem.
  467. */
  468. public function run( $options ) {
  469. $defaults = array(
  470. 'package' => '', // Please always pass this.
  471. 'destination' => '', // And this
  472. 'clear_destination' => false,
  473. 'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please
  474. 'clear_working' => true,
  475. 'is_multi' => false,
  476. 'hook_extra' => array() // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
  477. );
  478. $options = wp_parse_args( $options, $defaults );
  479. if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times
  480. $this->skin->header();
  481. }
  482. // Connect to the Filesystem first.
  483. $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
  484. // Mainly for non-connected filesystem.
  485. if ( ! $res ) {
  486. if ( ! $options['is_multi'] ) {
  487. $this->skin->footer();
  488. }
  489. return false;
  490. }
  491. $this->skin->before();
  492. if ( is_wp_error($res) ) {
  493. $this->skin->error($res);
  494. $this->skin->after();
  495. if ( ! $options['is_multi'] ) {
  496. $this->skin->footer();
  497. }
  498. return $res;
  499. }
  500. //Download the package (Note, This just returns the filename of the file if the package is a local file)
  501. $download = $this->download_package( $options['package'] );
  502. if ( is_wp_error($download) ) {
  503. $this->skin->error($download);
  504. $this->skin->after();
  505. if ( ! $options['is_multi'] ) {
  506. $this->skin->footer();
  507. }
  508. return $download;
  509. }
  510. $delete_package = ( $download != $options['package'] ); // Do not delete a "local" file
  511. //Unzips the file into a temporary directory
  512. $working_dir = $this->unpack_package( $download, $delete_package );
  513. if ( is_wp_error($working_dir) ) {
  514. $this->skin->error($working_dir);
  515. $this->skin->after();
  516. if ( ! $options['is_multi'] ) {
  517. $this->skin->footer();
  518. }
  519. return $working_dir;
  520. }
  521. //With the given options, this installs it to the destination directory.
  522. $result = $this->install_package( array(
  523. 'source' => $working_dir,
  524. 'destination' => $options['destination'],
  525. 'clear_destination' => $options['clear_destination'],
  526. 'abort_if_destination_exists' => $options['abort_if_destination_exists'],
  527. 'clear_working' => $options['clear_working'],
  528. 'hook_extra' => $options['hook_extra']
  529. ) );
  530. $this->skin->set_result($result);
  531. if ( is_wp_error($result) ) {
  532. $this->skin->error($result);
  533. $this->skin->feedback('process_failed');
  534. } else {
  535. //Install Succeeded
  536. $this->skin->feedback('process_success');
  537. }
  538. $this->skin->after();
  539. if ( ! $options['is_multi'] ) {
  540. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
  541. do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
  542. $this->skin->footer();
  543. }
  544. return $result;
  545. }
  546. /**
  547. * Toggle maintenance mode for the site.
  548. *
  549. * Creates/deletes the maintenance file to enable/disable maintenance mode.
  550. *
  551. * @since 2.8.0
  552. *
  553. * @param bool $enable True to enable maintenance mode, false to disable.
  554. */
  555. public function maintenance_mode( $enable = false ) {
  556. global $wp_filesystem;
  557. $file = $wp_filesystem->abspath() . '.maintenance';
  558. if ( $enable ) {
  559. $this->skin->feedback('maintenance_start');
  560. // Create maintenance file to signal that we are upgrading
  561. $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
  562. $wp_filesystem->delete($file);
  563. $wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
  564. } else if ( !$enable && $wp_filesystem->exists($file) ) {
  565. $this->skin->feedback('maintenance_end');
  566. $wp_filesystem->delete($file);
  567. }
  568. }
  569. }
  570. /**
  571. * Plugin Upgrader class for WordPress Plugins, It is designed to upgrade/install plugins from a local zip, remote zip URL, or uploaded zip file.
  572. *
  573. * @package WordPress
  574. * @subpackage Upgrader
  575. * @since 2.8.0
  576. */
  577. class Plugin_Upgrader extends WP_Upgrader {
  578. /**
  579. * Plugin upgrade result.
  580. *
  581. * @since 2.8.0
  582. * @var array|WP_Error $result
  583. * @see WP_Upgrader::$result
  584. */
  585. public $result;
  586. /**
  587. * Whether a bulk upgrade/install is being performed.
  588. *
  589. * @since 2.9.0
  590. * @var bool $bulk
  591. */
  592. public $bulk = false;
  593. /**
  594. * Initialize the upgrade strings.
  595. *
  596. * @since 2.8.0
  597. */
  598. public function upgrade_strings() {
  599. $this->strings['up_to_date'] = __('The plugin is at the latest version.');
  600. $this->strings['no_package'] = __('Update package not available.');
  601. $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
  602. $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
  603. $this->strings['remove_old'] = __('Removing the old version of the plugin&#8230;');
  604. $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
  605. $this->strings['process_failed'] = __('Plugin update failed.');
  606. $this->strings['process_success'] = __('Plugin updated successfully.');
  607. }
  608. /**
  609. * Initialize the install strings.
  610. *
  611. * @since 2.8.0
  612. */
  613. public function install_strings() {
  614. $this->strings['no_package'] = __('Install package not available.');
  615. $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
  616. $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
  617. $this->strings['installing_package'] = __('Installing the plugin&#8230;');
  618. $this->strings['no_files'] = __('The plugin contains no files.');
  619. $this->strings['process_failed'] = __('Plugin install failed.');
  620. $this->strings['process_success'] = __('Plugin installed successfully.');
  621. }
  622. /**
  623. * Install a plugin package.
  624. *
  625. * @since 2.8.0
  626. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  627. *
  628. * @param string $package The full local path or URI of the package.
  629. * @param array $args {
  630. * Optional. Other arguments for installing a plugin package. Default empty array.
  631. *
  632. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  633. * Default true.
  634. * }
  635. *
  636. * @return bool|WP_Error True if the install was successful, false or a WP_Error otherwise.
  637. */
  638. public function install( $package, $args = array() ) {
  639. $defaults = array(
  640. 'clear_update_cache' => true,
  641. );
  642. $parsed_args = wp_parse_args( $args, $defaults );
  643. $this->init();
  644. $this->install_strings();
  645. add_filter('upgrader_source_selection', array($this, 'check_package') );
  646. $this->run( array(
  647. 'package' => $package,
  648. 'destination' => WP_PLUGIN_DIR,
  649. 'clear_destination' => false, // Do not overwrite files.
  650. 'clear_working' => true,
  651. 'hook_extra' => array(
  652. 'type' => 'plugin',
  653. 'action' => 'install',
  654. )
  655. ) );
  656. remove_filter('upgrader_source_selection', array($this, 'check_package') );
  657. if ( ! $this->result || is_wp_error($this->result) )
  658. return $this->result;
  659. // Force refresh of plugin update information
  660. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  661. return true;
  662. }
  663. /**
  664. * Upgrade a plugin.
  665. *
  666. * @since 2.8.0
  667. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  668. *
  669. * @param string $plugin The basename path to the main plugin file.
  670. * @param array $args {
  671. * Optional. Other arguments for upgrading a plugin package. Defualt empty array.
  672. *
  673. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  674. * Default true.
  675. * }
  676. * @return bool|WP_Error True if the upgrade was successful, false or a {@see WP_Error} object otherwise.
  677. */
  678. public function upgrade( $plugin, $args = array() ) {
  679. $defaults = array(
  680. 'clear_update_cache' => true,
  681. );
  682. $parsed_args = wp_parse_args( $args, $defaults );
  683. $this->init();
  684. $this->upgrade_strings();
  685. $current = get_site_transient( 'update_plugins' );
  686. if ( !isset( $current->response[ $plugin ] ) ) {
  687. $this->skin->before();
  688. $this->skin->set_result(false);
  689. $this->skin->error('up_to_date');
  690. $this->skin->after();
  691. return false;
  692. }
  693. // Get the URL to the zip file
  694. $r = $current->response[ $plugin ];
  695. add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
  696. add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
  697. //'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.
  698. $this->run( array(
  699. 'package' => $r->package,
  700. 'destination' => WP_PLUGIN_DIR,
  701. 'clear_destination' => true,
  702. 'clear_working' => true,
  703. 'hook_extra' => array(
  704. 'plugin' => $plugin,
  705. 'type' => 'plugin',
  706. 'action' => 'update',
  707. ),
  708. ) );
  709. // Cleanup our hooks, in case something else does a upgrade on this connection.
  710. remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
  711. remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
  712. if ( ! $this->result || is_wp_error($this->result) )
  713. return $this->result;
  714. // Force refresh of plugin update information
  715. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  716. return true;
  717. }
  718. /**
  719. * Bulk upgrade several plugins at once.
  720. *
  721. * @since 2.8.0
  722. * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
  723. *
  724. * @param string $plugins Array of the basename paths of the plugins' main files.
  725. * @param array $args {
  726. * Optional. Other arguments for upgrading several plugins at once. Default empty array.
  727. *
  728. * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
  729. * Default true.
  730. * }
  731. *
  732. * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
  733. */
  734. public function bulk_upgrade( $plugins, $args = array() ) {
  735. $defaults = array(
  736. 'clear_update_cache' => true,
  737. );
  738. $parsed_args = wp_parse_args( $args, $defaults );
  739. $this->init();
  740. $this->bulk = true;
  741. $this->upgrade_strings();
  742. $current = get_site_transient( 'update_plugins' );
  743. add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
  744. $this->skin->header();
  745. // Connect to the Filesystem first.
  746. $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
  747. if ( ! $res ) {
  748. $this->skin->footer();
  749. return false;
  750. }
  751. $this->skin->bulk_header();
  752. // Only start maintenance mode if:
  753. // - running Multisite and there are one or more plugins specified, OR
  754. // - a plugin with an update available is currently active.
  755. // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
  756. $maintenance = ( is_multisite() && ! empty( $plugins ) );
  757. foreach ( $plugins as $plugin )
  758. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
  759. if ( $maintenance )
  760. $this->maintenance_mode(true);
  761. $results = array();
  762. $this->update_count = count($plugins);
  763. $this->update_current = 0;
  764. foreach ( $plugins as $plugin ) {
  765. $this->update_current++;
  766. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
  767. if ( !isset( $current->response[ $plugin ] ) ) {
  768. $this->skin->set_result('up_to_date');
  769. $this->skin->before();
  770. $this->skin->feedback('up_to_date');
  771. $this->skin->after();
  772. $results[$plugin] = true;
  773. continue;
  774. }
  775. // Get the URL to the zip file
  776. $r = $current->response[ $plugin ];
  777. $this->skin->plugin_active = is_plugin_active($plugin);
  778. $result = $this->run( array(
  779. 'package' => $r->package,
  780. 'destination' => WP_PLUGIN_DIR,
  781. 'clear_destination' => true,
  782. 'clear_working' => true,
  783. 'is_multi' => true,
  784. 'hook_extra' => array(
  785. 'plugin' => $plugin
  786. )
  787. ) );
  788. $results[$plugin] = $this->result;
  789. // Prevent credentials auth screen from displaying multiple times
  790. if ( false === $result )
  791. break;
  792. } //end foreach $plugins
  793. $this->maintenance_mode(false);
  794. /**
  795. * Fires when the bulk upgrader process is complete.
  796. *
  797. * @since 3.6.0
  798. *
  799. * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
  800. * be a Theme_Upgrader or Core_Upgrade instance.
  801. * @param array $data {
  802. * Array of bulk item update data.
  803. *
  804. * @type string $action Type of action. Default 'update'.
  805. * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'.
  806. * @type bool $bulk Whether the update process is a bulk update. Default true.
  807. * @type array $packages Array of plugin, theme, or core packages to update.
  808. * }
  809. */
  810. do_action( 'upgrader_process_complete', $this, array(
  811. 'action' => 'update',
  812. 'type' => 'plugin',
  813. 'bulk' => true,
  814. 'plugins' => $plugins,
  815. ) );
  816. $this->skin->bulk_footer();
  817. $this->skin->footer();
  818. // Cleanup our hooks, in case something else does a upgrade on this connection.
  819. remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
  820. // Force refresh of plugin update information
  821. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  822. return $results;
  823. }
  824. /**
  825. * Check a source package to be sure it contains a plugin.
  826. *
  827. * This function is added to the {@see 'upgrader_source_selection'} filter by
  828. * {@see Plugin_Upgrader::install()}.
  829. *
  830. * @since 3.3.0
  831. *
  832. * @param string $source The path to the downloaded package source.
  833. * @return string|WP_Error The source as passed, or a {@see WP_Error} object if no plugins were found.
  834. */
  835. public function check_package($source) {
  836. global $wp_filesystem;
  837. if ( is_wp_error($source) )
  838. return $source;
  839. $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
  840. if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
  841. return $source;
  842. // Check the folder contains at least 1 valid plugin.
  843. $plugins_found = false;
  844. foreach ( glob( $working_directory . '*.php' ) as $file ) {
  845. $info = get_plugin_data($file, false, false);
  846. if ( !empty( $info['Name'] ) ) {
  847. $plugins_found = true;
  848. break;
  849. }
  850. }
  851. if ( ! $plugins_found )
  852. return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
  853. return $source;
  854. }
  855. /**
  856. * Retrieve the path to the file that contains the plugin info.
  857. *
  858. * This isn't used internally in the class, but is called by the skins.
  859. *
  860. * @since 2.8.0
  861. *
  862. * @return string|false The full path to the main plugin file, or false.
  863. */
  864. public function plugin_info() {
  865. if ( ! is_array($this->result) )
  866. return false;
  867. if ( empty($this->result['destination_name']) )
  868. return false;
  869. $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
  870. if ( empty($plugin) )
  871. return false;
  872. $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
  873. return $this->result['destination_name'] . '/' . $pluginfiles[0];
  874. }
  875. /**
  876. * Deactivates a plugin before it is upgraded.
  877. *
  878. * Hooked to the {@see 'upgrader_pre_install'} filter by {@see Plugin_Upgrader::upgrade()}.
  879. *
  880. * @since 2.8.0
  881. * @since 4.1.0 Added a return value.
  882. *
  883. * @param bool|WP_Error $return Upgrade offer return.
  884. * @param array $plugin Plugin package arguments.
  885. * @return bool|WP_Error The passed in $return param or {@see WP_Error}.
  886. */
  887. public function deactivate_plugin_before_upgrade($return, $plugin) {
  888. if ( is_wp_error($return) ) //Bypass.
  889. return $return;
  890. // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
  891. if ( defined( 'DOING_CRON' ) && DOING_CRON )
  892. return $return;
  893. $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
  894. if ( empty($plugin) )
  895. return new WP_Error('bad_request', $this->strings['bad_request']);
  896. if ( is_plugin_active($plugin) ) {
  897. //Deactivate the plugin silently, Prevent deactivation hooks from running.
  898. deactivate_plugins($plugin, true);
  899. }
  900. return $return;
  901. }
  902. /**
  903. * Delete the old plugin during an upgrade.
  904. *
  905. * Hooked to the {@see 'upgrader_clear_destination'} filter by
  906. * {@see Plugin_Upgrader::upgrade()} and {@see Plugin_Upgrader::bulk_upgrade()}.
  907. *
  908. * @since 2.8.0
  909. */
  910. public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
  911. global $wp_filesystem;
  912. if ( is_wp_error($removed) )
  913. return $removed; //Pass errors through.
  914. $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
  915. if ( empty($plugin) )
  916. return new WP_Error('bad_request', $this->strings['bad_request']);
  917. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  918. $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
  919. if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
  920. return $removed;
  921. // If plugin is in its own directory, recursively delete the directory.
  922. 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
  923. $deleted = $wp_filesystem->delete($this_plugin_dir, true);
  924. else
  925. $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
  926. if ( ! $deleted )
  927. return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
  928. return true;
  929. }
  930. }
  931. /**
  932. * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
  933. *
  934. * @package WordPress
  935. * @subpackage Upgrader
  936. * @since 2.8.0
  937. */
  938. class Theme_Upgrader extends WP_Upgrader {
  939. /**
  940. * Result of the theme upgrade offer.
  941. *
  942. * @since 2.8.0
  943. * @var array|WP_Erorr $result
  944. * @see WP_Upgrader::$result
  945. */
  946. public $result;
  947. /**
  948. * Whether multiple plugins are being upgraded/installed in bulk.
  949. *
  950. * @since 2.9.0
  951. * @var bool $bulk
  952. */
  953. public $bulk = false;
  954. /**
  955. * Initialize the upgrade strings.
  956. *
  957. * @since 2.8.0
  958. */
  959. public function upgrade_strings() {
  960. $this->strings['up_to_date'] = __('The theme is at the latest version.');
  961. $this->strings['no_package'] = __('Update package not available.');
  962. $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
  963. $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
  964. $this->strings['remove_old'] = __('Removing the old version of the theme&#8230;');
  965. $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
  966. $this->strings['process_failed'] = __('Theme update failed.');
  967. $this->strings['process_success'] = __('Theme updated successfully.');
  968. }
  969. /**
  970. * Initialize the install strings.
  971. *
  972. * @since 2.8.0
  973. */
  974. public function install_strings() {
  975. $this->strings['no_package'] = __('Install package not available.');
  976. $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
  977. $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
  978. $this->strings['installing_package'] = __('Installing the theme&#8230;');
  979. $this->strings['no_files'] = __('The theme contains no files.');
  980. $this->strings['process_failed'] = __('Theme install failed.');
  981. $this->strings['process_success'] = __('Theme installed successfully.');
  982. /* translators: 1: theme name, 2: version */
  983. $this->strings['process_success_specific'] = __('Successfully installed the theme <strong>%1$s %2$s</strong>.');
  984. $this->strings['parent_theme_search'] = __('This theme requires a parent theme. Checking if it is installed&#8230;');
  985. /* translators: 1: theme name, 2: version */
  986. $this->strings['parent_theme_prepare_install'] = __('Preparing to install <strong>%1$s %2$s</strong>&#8230;');
  987. /* translators: 1: theme name, 2: version */
  988. $this->strings['parent_theme_currently_installed'] = __('The parent theme, <strong>%1$s %2$s</strong>, is currently installed.');
  989. /* translators: 1: theme name, 2: version */
  990. $this->strings['parent_theme_install_success'] = __('Successfully installed the parent theme, <strong>%1$s %2$s</strong>.');
  991. $this->strings['parent_theme_not_found'] = __('<strong>The parent theme could not be found.</strong> You will need to install the parent theme, <strong>%s</strong>, before you can use this child theme.');
  992. }
  993. /**
  994. * Check if a child theme is being installed and we need to install its parent.
  995. *
  996. * Hooked to the {@see 'upgrader_post_install'} filter by {@see Theme_Upgrader::install()}.
  997. *
  998. * @since 3.4.0
  999. */
  1000. public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
  1001. // Check to see if we need to install a parent theme
  1002. $theme_info = $this->theme_info();
  1003. if ( ! $theme_info->parent() )
  1004. return $install_result;
  1005. $this->skin->feedback( 'parent_theme_search' );
  1006. if ( ! $theme_info->parent()->errors() ) {
  1007. $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display('Name'), $theme_info->parent()->display('Version') );
  1008. // We already have the theme, fall through.
  1009. return $install_result;
  1010. }
  1011. // We don't have the parent theme, let's install it.
  1012. $api = themes_api('theme_information', array('slug' => $theme_info->get('Template'), 'fields' => array('sections' => false, 'tags' => false) ) ); //Save on a bit of bandwidth.
  1013. if ( ! $api || is_wp_error($api) ) {
  1014. $this->skin->feedback( 'parent_theme_not_found', $theme_info->get('Template') );
  1015. // Don't show activate or preview actions after install
  1016. add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
  1017. return $install_result;
  1018. }
  1019. // Backup required data we're going to override:
  1020. $child_api = $this->skin->api;
  1021. $child_success_message = $this->strings['process_success'];
  1022. // Override them
  1023. $this->skin->api = $api;
  1024. $this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];//, $api->name, $api->version);
  1025. $this->skin->feedback('parent_theme_prepare_install', $api->name, $api->version);
  1026. add_filter('install_theme_complete_actions', '__return_false', 999); // Don't show any actions after installing the theme.
  1027. // Install the parent theme
  1028. $parent_result = $this->run( array(
  1029. 'package' => $api->download_link,
  1030. 'destination' => get_theme_root(),
  1031. 'clear_destination' => false, //Do not overwrite files.
  1032. 'clear_working' => true
  1033. ) );
  1034. if ( is_wp_error($parent_result) )
  1035. add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
  1036. // Start cleaning up after the parents installation
  1037. remove_filter('install_theme_complete_actions', '__return_false', 999);
  1038. // Reset child's result and data
  1039. $this->result = $child_result;
  1040. $this->skin->api = $child_api;
  1041. $this->strings['process_success'] = $child_success_message;
  1042. return $install_result;
  1043. }
  1044. /**
  1045. * Don't display the activate and preview actions to the user.
  1046. *
  1047. * Hooked to the {@see 'install_theme_complete_actions'} filter by
  1048. * {@see Theme_Upgrader::check_parent_theme_filter()} when installing
  1049. * a child theme and installing the parent theme fails.
  1050. *
  1051. * @since 3.4.0
  1052. *
  1053. * @param array $actions Preview actions.
  1054. */
  1055. public function hide_activate_preview_actions( $actions ) {
  1056. unset($actions['activate'], $actions['preview']);
  1057. return $actions;
  1058. }
  1059. /**
  1060. * Install a theme package.
  1061. *
  1062. * @since 2.8.0
  1063. * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
  1064. *
  1065. * @param string $package The full local path or URI of the package.
  1066. * @param array $args {
  1067. * Optional. Other arguments for installing a theme package. Default empty array.
  1068. *
  1069. * @type bool $clear_update_cache Whether to clear the updates cache if successful.
  1070. * Default true.
  1071. * }
  1072. *
  1073. * @return bool|WP_Error True if the install was successful, false or a {@see WP_Error} object otherwise.
  1074. */
  1075. public function install( $package, $args = array() ) {
  1076. $defaults = array(
  1077. 'clear_update_cache' => true,
  1078. );
  1079. $parsed_args = wp_parse_args( $args, $defaults );
  1080. $this->init();
  1081. $this->install_strings();
  1082. add_filter('upgrader_source_selection', array($this, 'check_package') );
  1083. add_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'), 10, 3);
  1084. $this->run( array(
  1085. 'package' => $package,
  1086. 'destination' => get_theme_root(),
  1087. 'clear_destination' => false, //Do not overwrite files.
  1088. 'clear_working' => true,
  1089. 'hook_extra' => array(
  1090. 'type' => 'theme',
  1091. 'action' => 'install',
  1092. ),
  1093. ) );
  1094. remove_filter('upgrader_source_selection', array($this, 'check_package') );
  1095. remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
  1096. if ( ! $this->result || is_wp_error($this->result) )
  1097. return $this->result;
  1098. // Refresh the Theme Update information
  1099. wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
  1100. return true;
  1101. }
  1102. /**
  1103. * Upgrade a theme.
  1104. *
  1105. * @since 2.8.0
  1106. * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
  1107. *
  1108. * @param string $theme The theme slug.
  1109. * @param array $args {
  1110. * Optional. Other arguments for upgrading a theme. Default empty array.
  1111. *
  1112. * @type bool $clear_update_cache Whether to clear the update cache if successful.
  1113. * Default true.
  1114. * }
  1115. * @return bool|WP_Error True if the upgrade was successful, false or a {@see WP_Error} object otherwise.
  1116. */
  1117. public function upgrade( $theme, $args = array() ) {
  1118. $defaults = array(
  1119. 'clear_update_cache' => true,
  1120. );
  1121. $parsed_args = wp_parse_args( $args, $defaults );
  1122. $this->init();
  1123. $this->upgrade_strings();
  1124. // Is an update available?
  1125. $current = get_site_transient( 'update_themes' );
  1126. if ( !isset( $current->response[ $theme ] ) ) {
  1127. $this->skin->before();
  1128. $this->skin->set_result(false);
  1129. $this->skin->error( 'up_to_date' );
  1130. $this->skin->after();
  1131. return false;
  1132. }
  1133. $r = $current->response[ $theme ];
  1134. add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
  1135. add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
  1136. add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
  1137. $this->run( array(
  1138. 'package' => $r['package'],
  1139. 'destination' => get_theme_root( $theme ),
  1140. 'clear_destination' => true,
  1141. 'clear_working' => true,
  1142. 'hook_extra' => array(
  1143. 'theme' => $theme,
  1144. 'type' => 'theme',
  1145. 'action' => 'update',
  1146. ),
  1147. ) );
  1148. remove_filter('upgrader_pre_install', array($this, 'current_before'));
  1149. remove_filter('upgrader_post_install', array($this, 'current_after'));
  1150. remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
  1151. if ( ! $this->result || is_wp_error($this->result) )
  1152. return $this->result;
  1153. wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
  1154. return true;
  1155. }
  1156. /**
  1157. * Upgrade several themes at once.
  1158. *
  1159. * @since 3.0.0
  1160. * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
  1161. *
  1162. * @param string $themes The theme slugs.
  1163. * @param array $args {
  1164. * Optional. Other arguments for upgrading several themes at once. Default empty array.
  1165. *
  1166. * @type bool $clear_update_cache Whether to clear the update cache if successful.
  1167. * Default true.
  1168. * }
  1169. * @return array[]|false An array of results, or false if unable to connect to the filesystem.
  1170. */
  1171. public function bulk_upgrade( $themes, $args = array() ) {
  1172. $defaults = array(
  1173. 'clear_update_cache' => true,
  1174. );
  1175. $parsed_args = wp_parse_args( $args, $defaults );
  1176. $this->init();
  1177. $this->bulk = true;
  1178. $this->upgrade_strings();
  1179. $current = get_site_transient( 'update_themes' );
  1180. add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
  1181. add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
  1182. add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
  1183. $this->skin->header();
  1184. // Connect to the Filesystem first.
  1185. $res = $this->fs_connect( array(WP_CONTENT_DIR) );
  1186. if ( ! $res ) {
  1187. $this->skin->footer();
  1188. return false;
  1189. }
  1190. $this->skin->bulk_header();
  1191. // Only start maintenance mode if:
  1192. // - running Multisite and there are one or more themes specified, OR
  1193. // - a theme with an update available is currently in use.
  1194. // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
  1195. $maintenance = ( is_multisite() && ! empty( $themes ) );
  1196. foreach ( $themes as $theme )
  1197. $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
  1198. if ( $maintenance )
  1199. $this->maintenance_mode(true);
  1200. $results = array();
  1201. $this->update_count = count($themes);
  1202. $this->update_current = 0;
  1203. foreach ( $themes as $theme ) {
  1204. $this->update_current++;
  1205. $this->skin->theme_inf

Large files files are truncated, but you can click here to view the full file