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