class-theme-upgrader.php 19 KB

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