class-theme-installer-skin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /**
  21. * @param array $args
  22. */
  23. public function __construct( $args = array() ) {
  24. $defaults = array(
  25. 'type' => 'web',
  26. 'url' => '',
  27. 'theme' => '',
  28. 'nonce' => '',
  29. 'title' => '',
  30. );
  31. $args = wp_parse_args( $args, $defaults );
  32. $this->type = $args['type'];
  33. $this->api = isset( $args['api'] ) ? $args['api'] : array();
  34. parent::__construct( $args );
  35. }
  36. /**
  37. */
  38. public function before() {
  39. if ( ! empty( $this->api ) ) {
  40. $this->upgrader->strings['process_success'] = sprintf(
  41. $this->upgrader->strings['process_success_specific'],
  42. $this->api->name,
  43. $this->api->version
  44. );
  45. }
  46. }
  47. /**
  48. */
  49. public function after() {
  50. if ( empty( $this->upgrader->result['destination_name'] ) ) {
  51. return;
  52. }
  53. $theme_info = $this->upgrader->theme_info();
  54. if ( empty( $theme_info ) ) {
  55. return;
  56. }
  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. $install_actions = array();
  70. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
  71. $customize_url = add_query_arg(
  72. array(
  73. 'theme' => urlencode( $stylesheet ),
  74. 'return' => urlencode( admin_url( 'web' === $this->type ? 'theme-install.php' : 'themes.php' ) ),
  75. ),
  76. admin_url( 'customize.php' )
  77. );
  78. $install_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. __( 'Live Preview' ),
  83. /* translators: %s: Theme name. */
  84. sprintf( __( 'Live Preview &#8220;%s&#8221;' ), $name )
  85. );
  86. }
  87. $install_actions['activate'] = sprintf(
  88. '<a href="%s" class="activatelink">' .
  89. '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
  90. esc_url( $activate_link ),
  91. __( 'Activate' ),
  92. /* translators: %s: Theme name. */
  93. sprintf( __( 'Activate &#8220;%s&#8221;' ), $name )
  94. );
  95. if ( is_network_admin() && current_user_can( 'manage_network_themes' ) ) {
  96. $install_actions['network_enable'] = sprintf(
  97. '<a href="%s" target="_parent">%s</a>',
  98. esc_url( wp_nonce_url( 'themes.php?action=enable&amp;theme=' . urlencode( $stylesheet ), 'enable-theme_' . $stylesheet ) ),
  99. __( 'Network Enable' )
  100. );
  101. }
  102. if ( $this->type == 'web' ) {
  103. $install_actions['themes_page'] = sprintf(
  104. '<a href="%s" target="_parent">%s</a>',
  105. self_admin_url( 'theme-install.php' ),
  106. __( 'Return to Theme Installer' )
  107. );
  108. } elseif ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) ) {
  109. $install_actions['themes_page'] = sprintf(
  110. '<a href="%s" target="_parent">%s</a>',
  111. self_admin_url( 'themes.php' ),
  112. __( 'Return to Themes page' )
  113. );
  114. }
  115. if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() || ! current_user_can( 'switch_themes' ) ) {
  116. unset( $install_actions['activate'], $install_actions['preview'] );
  117. }
  118. /**
  119. * Filters the list of action links available following a single theme installation.
  120. *
  121. * @since 2.8.0
  122. *
  123. * @param string[] $install_actions Array of theme action links.
  124. * @param object $api Object containing WordPress.org API theme data.
  125. * @param string $stylesheet Theme directory name.
  126. * @param WP_Theme $theme_info Theme object.
  127. */
  128. $install_actions = apply_filters( 'install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info );
  129. if ( ! empty( $install_actions ) ) {
  130. $this->feedback( implode( ' | ', (array) $install_actions ) );
  131. }
  132. }
  133. }