class-theme-installer-skin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * Upgrader API: Theme_Installer_Skin class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Theme Installer Skin for the WordPress Theme Installer.
  11. *
  12. * @since 2.8.0
  13. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
  14. *
  15. * @see WP_Upgrader_Skin
  16. */
  17. class Theme_Installer_Skin extends WP_Upgrader_Skin {
  18. public $api;
  19. public $type;
  20. public $url;
  21. public $overwrite;
  22. private $is_downgrading = false;
  23. /**
  24. * @param array $args
  25. */
  26. public function __construct( $args = array() ) {
  27. $defaults = array(
  28. 'type' => 'web',
  29. 'url' => '',
  30. 'theme' => '',
  31. 'nonce' => '',
  32. 'title' => '',
  33. 'overwrite' => '',
  34. );
  35. $args = wp_parse_args( $args, $defaults );
  36. $this->type = $args['type'];
  37. $this->url = $args['url'];
  38. $this->api = isset( $args['api'] ) ? $args['api'] : array();
  39. $this->overwrite = $args['overwrite'];
  40. parent::__construct( $args );
  41. }
  42. /**
  43. * Action to perform before installing a theme.
  44. *
  45. * @since 2.8.0
  46. */
  47. public function before() {
  48. if ( ! empty( $this->api ) ) {
  49. $this->upgrader->strings['process_success'] = sprintf(
  50. $this->upgrader->strings['process_success_specific'],
  51. $this->api->name,
  52. $this->api->version
  53. );
  54. }
  55. }
  56. /**
  57. * Hides the `process_failed` error when updating a theme by uploading a zip file.
  58. *
  59. * @since 5.5.0
  60. *
  61. * @param WP_Error $wp_error WP_Error object.
  62. * @return bool
  63. */
  64. public function hide_process_failed( $wp_error ) {
  65. if (
  66. 'upload' === $this->type &&
  67. '' === $this->overwrite &&
  68. $wp_error->get_error_code() === 'folder_exists'
  69. ) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Action to perform following a single theme install.
  76. *
  77. * @since 2.8.0
  78. */
  79. public function after() {
  80. if ( $this->do_overwrite() ) {
  81. return;
  82. }
  83. if ( empty( $this->upgrader->result['destination_name'] ) ) {
  84. return;
  85. }
  86. $theme_info = $this->upgrader->theme_info();
  87. if ( empty( $theme_info ) ) {
  88. return;
  89. }
  90. $name = $theme_info->display( 'Name' );
  91. $stylesheet = $this->upgrader->result['destination_name'];
  92. $template = $theme_info->get_template();
  93. $activate_link = add_query_arg(
  94. array(
  95. 'action' => 'activate',
  96. 'template' => urlencode( $template ),
  97. 'stylesheet' => urlencode( $stylesheet ),
  98. ),
  99. admin_url( 'themes.php' )
  100. );
  101. $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
  102. $install_actions = array();
  103. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) && ! $theme_info->is_block_theme() ) {
  104. $customize_url = add_query_arg(
  105. array(
  106. 'theme' => urlencode( $stylesheet ),
  107. 'return' => urlencode( admin_url( 'web' === $this->type ? 'theme-install.php' : 'themes.php' ) ),
  108. ),
  109. admin_url( 'customize.php' )
  110. );
  111. $install_actions['preview'] = sprintf(
  112. '<a href="%s" class="hide-if-no-customize load-customize">' .
  113. '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  114. esc_url( $customize_url ),
  115. __( 'Live Preview' ),
  116. /* translators: %s: Theme name. */
  117. sprintf( __( 'Live Preview &#8220;%s&#8221;' ), $name )
  118. );
  119. }
  120. $install_actions['activate'] = sprintf(
  121. '<a href="%s" class="activatelink">' .
  122. '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  123. esc_url( $activate_link ),
  124. __( 'Activate' ),
  125. /* translators: %s: Theme name. */
  126. sprintf( _x( 'Activate &#8220;%s&#8221;', 'theme' ), $name )
  127. );
  128. if ( is_network_admin() && current_user_can( 'manage_network_themes' ) ) {
  129. $install_actions['network_enable'] = sprintf(
  130. '<a href="%s" target="_parent">%s</a>',
  131. esc_url( wp_nonce_url( 'themes.php?action=enable&amp;theme=' . urlencode( $stylesheet ), 'enable-theme_' . $stylesheet ) ),
  132. __( 'Network Enable' )
  133. );
  134. }
  135. if ( 'web' === $this->type ) {
  136. $install_actions['themes_page'] = sprintf(
  137. '<a href="%s" target="_parent">%s</a>',
  138. self_admin_url( 'theme-install.php' ),
  139. __( 'Go to Theme Installer' )
  140. );
  141. } elseif ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) ) {
  142. $install_actions['themes_page'] = sprintf(
  143. '<a href="%s" target="_parent">%s</a>',
  144. self_admin_url( 'themes.php' ),
  145. __( 'Go to Themes page' )
  146. );
  147. }
  148. if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() || ! current_user_can( 'switch_themes' ) ) {
  149. unset( $install_actions['activate'], $install_actions['preview'] );
  150. } elseif ( get_option( 'template' ) === $stylesheet ) {
  151. unset( $install_actions['activate'] );
  152. }
  153. /**
  154. * Filters the list of action links available following a single theme installation.
  155. *
  156. * @since 2.8.0
  157. *
  158. * @param string[] $install_actions Array of theme action links.
  159. * @param object $api Object containing WordPress.org API theme data.
  160. * @param string $stylesheet Theme directory name.
  161. * @param WP_Theme $theme_info Theme object.
  162. */
  163. $install_actions = apply_filters( 'install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info );
  164. if ( ! empty( $install_actions ) ) {
  165. $this->feedback( implode( ' | ', (array) $install_actions ) );
  166. }
  167. }
  168. /**
  169. * Check if the theme can be overwritten and output the HTML for overwriting a theme on upload.
  170. *
  171. * @since 5.5.0
  172. *
  173. * @return bool Whether the theme can be overwritten and HTML was outputted.
  174. */
  175. private function do_overwrite() {
  176. if ( 'upload' !== $this->type || ! is_wp_error( $this->result ) || 'folder_exists' !== $this->result->get_error_code() ) {
  177. return false;
  178. }
  179. $folder = $this->result->get_error_data( 'folder_exists' );
  180. $folder = rtrim( $folder, '/' );
  181. $current_theme_data = false;
  182. $all_themes = wp_get_themes( array( 'errors' => null ) );
  183. foreach ( $all_themes as $theme ) {
  184. $stylesheet_dir = wp_normalize_path( $theme->get_stylesheet_directory() );
  185. if ( rtrim( $stylesheet_dir, '/' ) !== $folder ) {
  186. continue;
  187. }
  188. $current_theme_data = $theme;
  189. }
  190. $new_theme_data = $this->upgrader->new_theme_data;
  191. if ( ! $current_theme_data || ! $new_theme_data ) {
  192. return false;
  193. }
  194. echo '<h2 class="update-from-upload-heading">' . esc_html__( 'This theme is already installed.' ) . '</h2>';
  195. // Check errors for active theme.
  196. if ( is_wp_error( $current_theme_data->errors() ) ) {
  197. $this->feedback( 'current_theme_has_errors', $current_theme_data->errors()->get_error_message() );
  198. }
  199. $this->is_downgrading = version_compare( $current_theme_data['Version'], $new_theme_data['Version'], '>' );
  200. $is_invalid_parent = false;
  201. if ( ! empty( $new_theme_data['Template'] ) ) {
  202. $is_invalid_parent = ! in_array( $new_theme_data['Template'], array_keys( $all_themes ), true );
  203. }
  204. $rows = array(
  205. 'Name' => __( 'Theme name' ),
  206. 'Version' => __( 'Version' ),
  207. 'Author' => __( 'Author' ),
  208. 'RequiresWP' => __( 'Required WordPress version' ),
  209. 'RequiresPHP' => __( 'Required PHP version' ),
  210. 'Template' => __( 'Parent theme' ),
  211. );
  212. $table = '<table class="update-from-upload-comparison"><tbody>';
  213. $table .= '<tr><th></th><th>' . esc_html_x( 'Active', 'theme' ) . '</th><th>' . esc_html_x( 'Uploaded', 'theme' ) . '</th></tr>';
  214. $is_same_theme = true; // Let's consider only these rows.
  215. foreach ( $rows as $field => $label ) {
  216. $old_value = $current_theme_data->display( $field, false );
  217. $old_value = $old_value ? (string) $old_value : '-';
  218. $new_value = ! empty( $new_theme_data[ $field ] ) ? (string) $new_theme_data[ $field ] : '-';
  219. if ( $old_value === $new_value && '-' === $new_value && 'Template' === $field ) {
  220. continue;
  221. }
  222. $is_same_theme = $is_same_theme && ( $old_value === $new_value );
  223. $diff_field = ( 'Version' !== $field && $new_value !== $old_value );
  224. $diff_version = ( 'Version' === $field && $this->is_downgrading );
  225. $invalid_parent = false;
  226. if ( 'Template' === $field && $is_invalid_parent ) {
  227. $invalid_parent = true;
  228. $new_value .= ' ' . __( '(not found)' );
  229. }
  230. $table .= '<tr><td class="name-label">' . $label . '</td><td>' . wp_strip_all_tags( $old_value ) . '</td>';
  231. $table .= ( $diff_field || $diff_version || $invalid_parent ) ? '<td class="warning">' : '<td>';
  232. $table .= wp_strip_all_tags( $new_value ) . '</td></tr>';
  233. }
  234. $table .= '</tbody></table>';
  235. /**
  236. * Filters the compare table output for overwriting a theme package on upload.
  237. *
  238. * @since 5.5.0
  239. *
  240. * @param string $table The output table with Name, Version, Author, RequiresWP, and RequiresPHP info.
  241. * @param WP_Theme $current_theme_data Active theme data.
  242. * @param array $new_theme_data Array with uploaded theme data.
  243. */
  244. echo apply_filters( 'install_theme_overwrite_comparison', $table, $current_theme_data, $new_theme_data );
  245. $install_actions = array();
  246. $can_update = true;
  247. $blocked_message = '<p>' . esc_html__( 'The theme cannot be updated due to the following:' ) . '</p>';
  248. $blocked_message .= '<ul class="ul-disc">';
  249. $requires_php = isset( $new_theme_data['RequiresPHP'] ) ? $new_theme_data['RequiresPHP'] : null;
  250. $requires_wp = isset( $new_theme_data['RequiresWP'] ) ? $new_theme_data['RequiresWP'] : null;
  251. if ( ! is_php_version_compatible( $requires_php ) ) {
  252. $error = sprintf(
  253. /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
  254. __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
  255. PHP_VERSION,
  256. $requires_php
  257. );
  258. $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
  259. $can_update = false;
  260. }
  261. if ( ! is_wp_version_compatible( $requires_wp ) ) {
  262. $error = sprintf(
  263. /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
  264. __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
  265. get_bloginfo( 'version' ),
  266. $requires_wp
  267. );
  268. $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
  269. $can_update = false;
  270. }
  271. $blocked_message .= '</ul>';
  272. if ( $can_update ) {
  273. if ( $this->is_downgrading ) {
  274. $warning = sprintf(
  275. /* translators: %s: Documentation URL. */
  276. __( 'You are uploading an older version of the active theme. You can continue to install the older version, but be sure to <a href="%s">back up your database and files</a> first.' ),
  277. __( 'https://wordpress.org/support/article/wordpress-backups/' )
  278. );
  279. } else {
  280. $warning = sprintf(
  281. /* translators: %s: Documentation URL. */
  282. __( 'You are updating a theme. Be sure to <a href="%s">back up your database and files</a> first.' ),
  283. __( 'https://wordpress.org/support/article/wordpress-backups/' )
  284. );
  285. }
  286. echo '<p class="update-from-upload-notice">' . $warning . '</p>';
  287. $overwrite = $this->is_downgrading ? 'downgrade-theme' : 'update-theme';
  288. $install_actions['overwrite_theme'] = sprintf(
  289. '<a class="button button-primary update-from-upload-overwrite" href="%s" target="_parent">%s</a>',
  290. wp_nonce_url( add_query_arg( 'overwrite', $overwrite, $this->url ), 'theme-upload' ),
  291. _x( 'Replace active with uploaded', 'theme' )
  292. );
  293. } else {
  294. echo $blocked_message;
  295. }
  296. $cancel_url = add_query_arg( 'action', 'upload-theme-cancel-overwrite', $this->url );
  297. $install_actions['themes_page'] = sprintf(
  298. '<a class="button" href="%s" target="_parent">%s</a>',
  299. wp_nonce_url( $cancel_url, 'theme-upload-cancel-overwrite' ),
  300. __( 'Cancel and go back' )
  301. );
  302. /**
  303. * Filters the list of action links available following a single theme installation failure
  304. * when overwriting is allowed.
  305. *
  306. * @since 5.5.0
  307. *
  308. * @param string[] $install_actions Array of theme action links.
  309. * @param object $api Object containing WordPress.org API theme data.
  310. * @param array $new_theme_data Array with uploaded theme data.
  311. */
  312. $install_actions = apply_filters( 'install_theme_overwrite_actions', $install_actions, $this->api, $new_theme_data );
  313. if ( ! empty( $install_actions ) ) {
  314. printf(
  315. '<p class="update-from-upload-expired hidden">%s</p>',
  316. __( 'The uploaded file has expired. Please go back and upload it again.' )
  317. );
  318. echo '<p class="update-from-upload-actions">' . implode( ' ', (array) $install_actions ) . '</p>';
  319. }
  320. return true;
  321. }
  322. }