class-theme-upgrader.php 20 KB

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