class-theme-upgrader.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. <?php
  2. /**
  3. * Upgrade API: Theme_Upgrader class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for upgrading/installing themes.
  11. *
  12. * It is designed to upgrade/install themes 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 Theme_Upgrader extends WP_Upgrader {
  21. /**
  22. * Result of the theme upgrade offer.
  23. *
  24. * @since 2.8.0
  25. * @var array|WP_Error $result
  26. * @see WP_Upgrader::$result
  27. */
  28. public $result;
  29. /**
  30. * Whether multiple themes are being upgraded/installed in bulk.
  31. *
  32. * @since 2.9.0
  33. * @var bool $bulk
  34. */
  35. public $bulk = false;
  36. /**
  37. * New theme info.
  38. *
  39. * @since 5.5.0
  40. * @var array $new_theme_data
  41. *
  42. * @see check_package()
  43. */
  44. public $new_theme_data = array();
  45. /**
  46. * Initialize the upgrade strings.
  47. *
  48. * @since 2.8.0
  49. */
  50. public function upgrade_strings() {
  51. $this->strings['up_to_date'] = __( 'The theme is at the latest version.' );
  52. $this->strings['no_package'] = __( 'Update package not available.' );
  53. /* translators: %s: Package URL. */
  54. $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
  55. $this->strings['unpack_package'] = __( 'Unpacking the update&#8230;' );
  56. $this->strings['remove_old'] = __( 'Removing the old version of the theme&#8230;' );
  57. $this->strings['remove_old_failed'] = __( 'Could not remove the old theme.' );
  58. $this->strings['process_failed'] = __( 'Theme update failed.' );
  59. $this->strings['process_success'] = __( 'Theme updated successfully.' );
  60. }
  61. /**
  62. * Initialize the installation strings.
  63. *
  64. * @since 2.8.0
  65. */
  66. public function install_strings() {
  67. $this->strings['no_package'] = __( 'Installation package not available.' );
  68. /* translators: %s: Package URL. */
  69. $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
  70. $this->strings['unpack_package'] = __( 'Unpacking the package&#8230;' );
  71. $this->strings['installing_package'] = __( 'Installing the theme&#8230;' );
  72. $this->strings['remove_old'] = __( 'Removing the old version of the theme&#8230;' );
  73. $this->strings['remove_old_failed'] = __( 'Could not remove the old theme.' );
  74. $this->strings['no_files'] = __( 'The theme contains no files.' );
  75. $this->strings['process_failed'] = __( 'Theme installation failed.' );
  76. $this->strings['process_success'] = __( 'Theme installed successfully.' );
  77. /* translators: 1: Theme name, 2: Theme version. */
  78. $this->strings['process_success_specific'] = __( 'Successfully installed the theme <strong>%1$s %2$s</strong>.' );
  79. $this->strings['parent_theme_search'] = __( 'This theme requires a parent theme. Checking if it is installed&#8230;' );
  80. /* translators: 1: Theme name, 2: Theme version. */
  81. $this->strings['parent_theme_prepare_install'] = __( 'Preparing to install <strong>%1$s %2$s</strong>&#8230;' );
  82. /* translators: 1: Theme name, 2: Theme version. */
  83. $this->strings['parent_theme_currently_installed'] = __( 'The parent theme, <strong>%1$s %2$s</strong>, is currently installed.' );
  84. /* translators: 1: Theme name, 2: Theme version. */
  85. $this->strings['parent_theme_install_success'] = __( 'Successfully installed the parent theme, <strong>%1$s %2$s</strong>.' );
  86. /* translators: %s: Theme name. */
  87. $this->strings['parent_theme_not_found'] = sprintf( __( '<strong>The parent theme could not be found.</strong> You will need to install the parent theme, %s, before you can use this child theme.' ), '<strong>%s</strong>' );
  88. /* translators: %s: Theme error. */
  89. $this->strings['current_theme_has_errors'] = __( 'The active theme has the following error: "%s".' );
  90. if ( ! empty( $this->skin->overwrite ) ) {
  91. if ( 'update-theme' === $this->skin->overwrite ) {
  92. $this->strings['installing_package'] = __( 'Updating the theme&#8230;' );
  93. $this->strings['process_failed'] = __( 'Theme update failed.' );
  94. $this->strings['process_success'] = __( 'Theme updated successfully.' );
  95. }
  96. if ( 'downgrade-theme' === $this->skin->overwrite ) {
  97. $this->strings['installing_package'] = __( 'Downgrading the theme&#8230;' );
  98. $this->strings['process_failed'] = __( 'Theme downgrade failed.' );
  99. $this->strings['process_success'] = __( 'Theme downgraded successfully.' );
  100. }
  101. }
  102. }
  103. /**
  104. * Check if a child theme is being installed and we need to install its parent.
  105. *
  106. * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::install().
  107. *
  108. * @since 3.4.0
  109. *
  110. * @param bool $install_result
  111. * @param array $hook_extra
  112. * @param array $child_result
  113. * @return bool
  114. */
  115. public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
  116. // Check to see if we need to install a parent theme.
  117. $theme_info = $this->theme_info();
  118. if ( ! $theme_info->parent() ) {
  119. return $install_result;
  120. }
  121. $this->skin->feedback( 'parent_theme_search' );
  122. if ( ! $theme_info->parent()->errors() ) {
  123. $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display( 'Name' ), $theme_info->parent()->display( 'Version' ) );
  124. // We already have the theme, fall through.
  125. return $install_result;
  126. }
  127. // We don't have the parent theme, let's install it.
  128. $api = themes_api(
  129. 'theme_information',
  130. array(
  131. 'slug' => $theme_info->get( 'Template' ),
  132. 'fields' => array(
  133. 'sections' => false,
  134. 'tags' => false,
  135. ),
  136. )
  137. ); // Save on a bit of bandwidth.
  138. if ( ! $api || is_wp_error( $api ) ) {
  139. $this->skin->feedback( 'parent_theme_not_found', $theme_info->get( 'Template' ) );
  140. // Don't show activate or preview actions after installation.
  141. add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
  142. return $install_result;
  143. }
  144. // Backup required data we're going to override:
  145. $child_api = $this->skin->api;
  146. $child_success_message = $this->strings['process_success'];
  147. // Override them.
  148. $this->skin->api = $api;
  149. $this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];
  150. $this->skin->feedback( 'parent_theme_prepare_install', $api->name, $api->version );
  151. add_filter( 'install_theme_complete_actions', '__return_false', 999 ); // Don't show any actions after installing the theme.
  152. // Install the parent theme.
  153. $parent_result = $this->run(
  154. array(
  155. 'package' => $api->download_link,
  156. 'destination' => get_theme_root(),
  157. 'clear_destination' => false, // Do not overwrite files.
  158. 'clear_working' => true,
  159. )
  160. );
  161. if ( is_wp_error( $parent_result ) ) {
  162. add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
  163. }
  164. // Start cleaning up after the parent's installation.
  165. remove_filter( 'install_theme_complete_actions', '__return_false', 999 );
  166. // Reset child's result and data.
  167. $this->result = $child_result;
  168. $this->skin->api = $child_api;
  169. $this->strings['process_success'] = $child_success_message;
  170. return $install_result;
  171. }
  172. /**
  173. * Don't display the activate and preview actions to the user.
  174. *
  175. * Hooked to the {@see 'install_theme_complete_actions'} filter by
  176. * Theme_Upgrader::check_parent_theme_filter() when installing
  177. * a child theme and installing the parent theme fails.
  178. *
  179. * @since 3.4.0
  180. *
  181. * @param array $actions Preview actions.
  182. * @return array
  183. */
  184. public function hide_activate_preview_actions( $actions ) {
  185. unset( $actions['activate'], $actions['preview'] );
  186. return $actions;
  187. }
  188. /**
  189. * Install a theme package.
  190. *
  191. * @since 2.8.0
  192. * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
  193. *
  194. * @param string $package The full local path or URI of the package.
  195. * @param array $args {
  196. * Optional. Other arguments for installing a theme package. Default empty array.
  197. *
  198. * @type bool $clear_update_cache Whether to clear the updates cache if successful.
  199. * Default true.
  200. * }
  201. *
  202. * @return bool|WP_Error True if the installation was successful, false or a WP_Error object otherwise.
  203. */
  204. public function install( $package, $args = array() ) {
  205. $defaults = array(
  206. 'clear_update_cache' => true,
  207. 'overwrite_package' => false, // Do not overwrite files.
  208. );
  209. $parsed_args = wp_parse_args( $args, $defaults );
  210. $this->init();
  211. $this->install_strings();
  212. add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
  213. add_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ), 10, 3 );
  214. if ( $parsed_args['clear_update_cache'] ) {
  215. // Clear cache so wp_update_themes() knows about the new theme.
  216. add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
  217. }
  218. $this->run(
  219. array(
  220. 'package' => $package,
  221. 'destination' => get_theme_root(),
  222. 'clear_destination' => $parsed_args['overwrite_package'],
  223. 'clear_working' => true,
  224. 'hook_extra' => array(
  225. 'type' => 'theme',
  226. 'action' => 'install',
  227. ),
  228. )
  229. );
  230. remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
  231. remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
  232. remove_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ) );
  233. if ( ! $this->result || is_wp_error( $this->result ) ) {
  234. return $this->result;
  235. }
  236. // Refresh the Theme Update information.
  237. wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
  238. if ( $parsed_args['overwrite_package'] ) {
  239. /** This action is documented in wp-admin/includes/class-plugin-upgrader.php */
  240. do_action( 'upgrader_overwrote_package', $package, $this->new_theme_data, 'theme' );
  241. }
  242. return true;
  243. }
  244. /**
  245. * Upgrade a theme.
  246. *
  247. * @since 2.8.0
  248. * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
  249. *
  250. * @param string $theme The theme slug.
  251. * @param array $args {
  252. * Optional. Other arguments for upgrading a theme. Default empty array.
  253. *
  254. * @type bool $clear_update_cache Whether to clear the update cache if successful.
  255. * Default true.
  256. * }
  257. * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
  258. */
  259. public function upgrade( $theme, $args = array() ) {
  260. $defaults = array(
  261. 'clear_update_cache' => true,
  262. );
  263. $parsed_args = wp_parse_args( $args, $defaults );
  264. $this->init();
  265. $this->upgrade_strings();
  266. // Is an update available?
  267. $current = get_site_transient( 'update_themes' );
  268. if ( ! isset( $current->response[ $theme ] ) ) {
  269. $this->skin->before();
  270. $this->skin->set_result( false );
  271. $this->skin->error( 'up_to_date' );
  272. $this->skin->after();
  273. return false;
  274. }
  275. $r = $current->response[ $theme ];
  276. add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
  277. add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
  278. add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
  279. if ( $parsed_args['clear_update_cache'] ) {
  280. // Clear cache so wp_update_themes() knows about the new theme.
  281. add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
  282. }
  283. $this->run(
  284. array(
  285. 'package' => $r['package'],
  286. 'destination' => get_theme_root( $theme ),
  287. 'clear_destination' => true,
  288. 'clear_working' => true,
  289. 'hook_extra' => array(
  290. 'theme' => $theme,
  291. 'type' => 'theme',
  292. 'action' => 'update',
  293. ),
  294. )
  295. );
  296. remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
  297. remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
  298. remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
  299. remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
  300. if ( ! $this->result || is_wp_error( $this->result ) ) {
  301. return $this->result;
  302. }
  303. wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
  304. // Ensure any future auto-update failures trigger a failure email by removing
  305. // the last failure notification from the list when themes update successfully.
  306. $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
  307. if ( isset( $past_failure_emails[ $theme ] ) ) {
  308. unset( $past_failure_emails[ $theme ] );
  309. update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
  310. }
  311. return true;
  312. }
  313. /**
  314. * Upgrade several themes at once.
  315. *
  316. * @since 3.0.0
  317. * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
  318. *
  319. * @param string[] $themes Array of the theme slugs.
  320. * @param array $args {
  321. * Optional. Other arguments for upgrading several themes at once. Default empty array.
  322. *
  323. * @type bool $clear_update_cache Whether to clear the update cache if successful.
  324. * Default true.
  325. * }
  326. * @return array[]|false An array of results, or false if unable to connect to the filesystem.
  327. */
  328. public function bulk_upgrade( $themes, $args = array() ) {
  329. $defaults = array(
  330. 'clear_update_cache' => true,
  331. );
  332. $parsed_args = wp_parse_args( $args, $defaults );
  333. $this->init();
  334. $this->bulk = true;
  335. $this->upgrade_strings();
  336. $current = get_site_transient( 'update_themes' );
  337. add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
  338. add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
  339. add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
  340. $this->skin->header();
  341. // Connect to the filesystem first.
  342. $res = $this->fs_connect( array( WP_CONTENT_DIR ) );
  343. if ( ! $res ) {
  344. $this->skin->footer();
  345. return false;
  346. }
  347. $this->skin->bulk_header();
  348. /*
  349. * Only start maintenance mode if:
  350. * - running Multisite and there are one or more themes specified, OR
  351. * - a theme with an update available is currently in use.
  352. * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
  353. */
  354. $maintenance = ( is_multisite() && ! empty( $themes ) );
  355. foreach ( $themes as $theme ) {
  356. $maintenance = $maintenance || get_stylesheet() === $theme || get_template() === $theme;
  357. }
  358. if ( $maintenance ) {
  359. $this->maintenance_mode( true );
  360. }
  361. $results = array();
  362. $this->update_count = count( $themes );
  363. $this->update_current = 0;
  364. foreach ( $themes as $theme ) {
  365. $this->update_current++;
  366. $this->skin->theme_info = $this->theme_info( $theme );
  367. if ( ! isset( $current->response[ $theme ] ) ) {
  368. $this->skin->set_result( true );
  369. $this->skin->before();
  370. $this->skin->feedback( 'up_to_date' );
  371. $this->skin->after();
  372. $results[ $theme ] = true;
  373. continue;
  374. }
  375. // Get the URL to the zip file.
  376. $r = $current->response[ $theme ];
  377. $result = $this->run(
  378. array(
  379. 'package' => $r['package'],
  380. 'destination' => get_theme_root( $theme ),
  381. 'clear_destination' => true,
  382. 'clear_working' => true,
  383. 'is_multi' => true,
  384. 'hook_extra' => array(
  385. 'theme' => $theme,
  386. ),
  387. )
  388. );
  389. $results[ $theme ] = $result;
  390. // Prevent credentials auth screen from displaying multiple times.
  391. if ( false === $result ) {
  392. break;
  393. }
  394. } // End foreach $themes.
  395. $this->maintenance_mode( false );
  396. // Refresh the Theme Update information.
  397. wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
  398. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
  399. do_action(
  400. 'upgrader_process_complete',
  401. $this,
  402. array(
  403. 'action' => 'update',
  404. 'type' => 'theme',
  405. 'bulk' => true,
  406. 'themes' => $themes,
  407. )
  408. );
  409. $this->skin->bulk_footer();
  410. $this->skin->footer();
  411. // Cleanup our hooks, in case something else does a upgrade on this connection.
  412. remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
  413. remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
  414. remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
  415. // Ensure any future auto-update failures trigger a failure email by removing
  416. // the last failure notification from the list when themes update successfully.
  417. $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
  418. foreach ( $results as $theme => $result ) {
  419. // Maintain last failure notification when themes failed to update manually.
  420. if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $theme ] ) ) {
  421. continue;
  422. }
  423. unset( $past_failure_emails[ $theme ] );
  424. }
  425. update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
  426. return $results;
  427. }
  428. /**
  429. * Checks that the package source contains a valid theme.
  430. *
  431. * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
  432. *
  433. * @since 3.3.0
  434. *
  435. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  436. * @global string $wp_version The WordPress version string.
  437. *
  438. * @param string $source The path to the downloaded package source.
  439. * @return string|WP_Error The source as passed, or a WP_Error object on failure.
  440. */
  441. public function check_package( $source ) {
  442. global $wp_filesystem, $wp_version;
  443. $this->new_theme_data = array();
  444. if ( is_wp_error( $source ) ) {
  445. return $source;
  446. }
  447. // Check that the folder contains a valid theme.
  448. $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
  449. if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
  450. return $source;
  451. }
  452. // A proper archive should have a style.css file in the single subdirectory.
  453. if ( ! file_exists( $working_directory . 'style.css' ) ) {
  454. return new WP_Error(
  455. 'incompatible_archive_theme_no_style',
  456. $this->strings['incompatible_archive'],
  457. sprintf(
  458. /* translators: %s: style.css */
  459. __( 'The theme is missing the %s stylesheet.' ),
  460. '<code>style.css</code>'
  461. )
  462. );
  463. }
  464. // All these headers are needed on Theme_Installer_Skin::do_overwrite().
  465. $info = get_file_data(
  466. $working_directory . 'style.css',
  467. array(
  468. 'Name' => 'Theme Name',
  469. 'Version' => 'Version',
  470. 'Author' => 'Author',
  471. 'Template' => 'Template',
  472. 'RequiresWP' => 'Requires at least',
  473. 'RequiresPHP' => 'Requires PHP',
  474. )
  475. );
  476. if ( empty( $info['Name'] ) ) {
  477. return new WP_Error(
  478. 'incompatible_archive_theme_no_name',
  479. $this->strings['incompatible_archive'],
  480. sprintf(
  481. /* translators: %s: style.css */
  482. __( 'The %s stylesheet does not contain a valid theme header.' ),
  483. '<code>style.css</code>'
  484. )
  485. );
  486. }
  487. /*
  488. * Parent themes must contain an index file:
  489. * - classic themes require /index.php
  490. * - block themes require /templates/index.html or block-templates/index.html (deprecated 5.9.0).
  491. */
  492. if (
  493. empty( $info['Template'] ) &&
  494. ! file_exists( $working_directory . 'index.php' ) &&
  495. ! file_exists( $working_directory . 'templates/index.html' ) &&
  496. ! file_exists( $working_directory . 'block-templates/index.html' )
  497. ) {
  498. return new WP_Error(
  499. 'incompatible_archive_theme_no_index',
  500. $this->strings['incompatible_archive'],
  501. sprintf(
  502. /* translators: 1: templates/index.html, 2: index.php, 3: Documentation URL, 4: Template, 5: style.css */
  503. __( 'Template is missing. Standalone themes need to have a %1$s or %2$s template file. <a href="%3$s">Child themes</a> need to have a %4$s header in the %5$s stylesheet.' ),
  504. '<code>templates/index.html</code>',
  505. '<code>index.php</code>',
  506. __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
  507. '<code>Template</code>',
  508. '<code>style.css</code>'
  509. )
  510. );
  511. }
  512. $requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
  513. $requires_wp = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
  514. if ( ! is_php_version_compatible( $requires_php ) ) {
  515. $error = sprintf(
  516. /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
  517. __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
  518. PHP_VERSION,
  519. $requires_php
  520. );
  521. return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
  522. }
  523. if ( ! is_wp_version_compatible( $requires_wp ) ) {
  524. $error = sprintf(
  525. /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
  526. __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
  527. $wp_version,
  528. $requires_wp
  529. );
  530. return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
  531. }
  532. $this->new_theme_data = $info;
  533. return $source;
  534. }
  535. /**
  536. * Turn on maintenance mode before attempting to upgrade the active theme.
  537. *
  538. * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
  539. * Theme_Upgrader::bulk_upgrade().
  540. *
  541. * @since 2.8.0
  542. *
  543. * @param bool|WP_Error $response The installation response before the installation has started.
  544. * @param array $theme Theme arguments.
  545. * @return bool|WP_Error The original `$response` parameter or WP_Error.
  546. */
  547. public function current_before( $response, $theme ) {
  548. if ( is_wp_error( $response ) ) {
  549. return $response;
  550. }
  551. $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
  552. // Only run if active theme.
  553. if ( get_stylesheet() !== $theme ) {
  554. return $response;
  555. }
  556. // Change to maintenance mode. Bulk edit handles this separately.
  557. if ( ! $this->bulk ) {
  558. $this->maintenance_mode( true );
  559. }
  560. return $response;
  561. }
  562. /**
  563. * Turn off maintenance mode after upgrading the active theme.
  564. *
  565. * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
  566. * and Theme_Upgrader::bulk_upgrade().
  567. *
  568. * @since 2.8.0
  569. *
  570. * @param bool|WP_Error $response The installation response after the installation has finished.
  571. * @param array $theme Theme arguments.
  572. * @return bool|WP_Error The original `$response` parameter or WP_Error.
  573. */
  574. public function current_after( $response, $theme ) {
  575. if ( is_wp_error( $response ) ) {
  576. return $response;
  577. }
  578. $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
  579. // Only run if active theme.
  580. if ( get_stylesheet() !== $theme ) {
  581. return $response;
  582. }
  583. // Ensure stylesheet name hasn't changed after the upgrade:
  584. if ( get_stylesheet() === $theme && $theme !== $this->result['destination_name'] ) {
  585. wp_clean_themes_cache();
  586. $stylesheet = $this->result['destination_name'];
  587. switch_theme( $stylesheet );
  588. }
  589. // Time to remove maintenance mode. Bulk edit handles this separately.
  590. if ( ! $this->bulk ) {
  591. $this->maintenance_mode( false );
  592. }
  593. return $response;
  594. }
  595. /**
  596. * Delete the old theme during an upgrade.
  597. *
  598. * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
  599. * and Theme_Upgrader::bulk_upgrade().
  600. *
  601. * @since 2.8.0
  602. *
  603. * @global WP_Filesystem_Base $wp_filesystem Subclass
  604. *
  605. * @param bool $removed
  606. * @param string $local_destination
  607. * @param string $remote_destination
  608. * @param array $theme
  609. * @return bool
  610. */
  611. public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
  612. global $wp_filesystem;
  613. if ( is_wp_error( $removed ) ) {
  614. return $removed; // Pass errors through.
  615. }
  616. if ( ! isset( $theme['theme'] ) ) {
  617. return $removed;
  618. }
  619. $theme = $theme['theme'];
  620. $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
  621. if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
  622. if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) ) {
  623. return false;
  624. }
  625. }
  626. return true;
  627. }
  628. /**
  629. * Get the WP_Theme object for a theme.
  630. *
  631. * @since 2.8.0
  632. * @since 3.0.0 The `$theme` argument was added.
  633. *
  634. * @param string $theme The directory name of the theme. This is optional, and if not supplied,
  635. * the directory name from the last result will be used.
  636. * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
  637. * and the last result isn't set.
  638. */
  639. public function theme_info( $theme = null ) {
  640. if ( empty( $theme ) ) {
  641. if ( ! empty( $this->result['destination_name'] ) ) {
  642. $theme = $this->result['destination_name'];
  643. } else {
  644. return false;
  645. }
  646. }
  647. $theme = wp_get_theme( $theme );
  648. $theme->cache_delete();
  649. return $theme;
  650. }
  651. }