class-theme-upgrader-skin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Upgrader API: Theme_Upgrader_Skin class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Theme Upgrader Skin for WordPress Theme Upgrades.
  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_Upgrader_Skin extends WP_Upgrader_Skin {
  18. /**
  19. * Holds the theme slug in the Theme Directory.
  20. *
  21. * @since 2.8.0
  22. *
  23. * @var string
  24. */
  25. public $theme = '';
  26. /**
  27. * Constructor.
  28. *
  29. * Sets up the theme upgrader skin.
  30. *
  31. * @since 2.8.0
  32. *
  33. * @param array $args Optional. The theme upgrader skin arguments to
  34. * override default options. Default empty array.
  35. */
  36. public function __construct( $args = array() ) {
  37. $defaults = array(
  38. 'url' => '',
  39. 'theme' => '',
  40. 'nonce' => '',
  41. 'title' => __( 'Update Theme' ),
  42. );
  43. $args = wp_parse_args( $args, $defaults );
  44. $this->theme = $args['theme'];
  45. parent::__construct( $args );
  46. }
  47. /**
  48. * Action to perform following a single theme update.
  49. *
  50. * @since 2.8.0
  51. */
  52. public function after() {
  53. $this->decrement_update_count( 'theme' );
  54. $update_actions = array();
  55. $theme_info = $this->upgrader->theme_info();
  56. if ( $theme_info ) {
  57. $name = $theme_info->display( 'Name' );
  58. $stylesheet = $this->upgrader->result['destination_name'];
  59. $template = $theme_info->get_template();
  60. $activate_link = add_query_arg(
  61. array(
  62. 'action' => 'activate',
  63. 'template' => urlencode( $template ),
  64. 'stylesheet' => urlencode( $stylesheet ),
  65. ),
  66. admin_url( 'themes.php' )
  67. );
  68. $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
  69. $customize_url = add_query_arg(
  70. array(
  71. 'theme' => urlencode( $stylesheet ),
  72. 'return' => urlencode( admin_url( 'themes.php' ) ),
  73. ),
  74. admin_url( 'customize.php' )
  75. );
  76. if ( get_stylesheet() === $stylesheet ) {
  77. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
  78. $update_actions['preview'] = sprintf(
  79. '<a href="%s" class="hide-if-no-customize load-customize">' .
  80. '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  81. esc_url( $customize_url ),
  82. __( 'Customize' ),
  83. /* translators: %s: Theme name. */
  84. sprintf( __( 'Customize &#8220;%s&#8221;' ), $name )
  85. );
  86. }
  87. } elseif ( current_user_can( 'switch_themes' ) ) {
  88. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
  89. $update_actions['preview'] = sprintf(
  90. '<a href="%s" class="hide-if-no-customize load-customize">' .
  91. '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  92. esc_url( $customize_url ),
  93. __( 'Live Preview' ),
  94. /* translators: %s: Theme name. */
  95. sprintf( __( 'Live Preview &#8220;%s&#8221;' ), $name )
  96. );
  97. }
  98. $update_actions['activate'] = sprintf(
  99. '<a href="%s" class="activatelink">' .
  100. '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  101. esc_url( $activate_link ),
  102. __( 'Activate' ),
  103. /* translators: %s: Theme name. */
  104. sprintf( _x( 'Activate &#8220;%s&#8221;', 'theme' ), $name )
  105. );
  106. }
  107. if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() ) {
  108. unset( $update_actions['preview'], $update_actions['activate'] );
  109. }
  110. }
  111. $update_actions['themes_page'] = sprintf(
  112. '<a href="%s" target="_parent">%s</a>',
  113. self_admin_url( 'themes.php' ),
  114. __( 'Go to Themes page' )
  115. );
  116. /**
  117. * Filters the list of action links available following a single theme update.
  118. *
  119. * @since 2.8.0
  120. *
  121. * @param string[] $update_actions Array of theme action links.
  122. * @param string $theme Theme directory name.
  123. */
  124. $update_actions = apply_filters( 'update_theme_complete_actions', $update_actions, $this->theme );
  125. if ( ! empty( $update_actions ) ) {
  126. $this->feedback( implode( ' | ', (array) $update_actions ) );
  127. }
  128. }
  129. }