1<?php
2/**
3 * Upgrader API: Bulk_Upgrader_Skin class
4 *
5 * @package WordPress
6 * @subpackage Upgrader
7 * @since 4.6.0
8 */
9
10/**
11 * Generic Bulk Upgrader Skin for WordPress Upgrades.
12 *
13 * @since 3.0.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 Bulk_Upgrader_Skin extends WP_Upgrader_Skin {
19
20 /**
21 * Whether the bulk update process has started.
22 *
23 * @since 3.0.0
24 * @var bool
25 */
26 public $in_loop = false;
27
28 /**
29 * Stores an error message about the update.
30 *
31 * @since 3.0.0
32 * @var string|false
33 */
34 public $error = false;
35
36 /**
37 * Constructor.
38 *
39 * Sets up the generic skin for the Bulk Upgrader classes.
40 *
41 * @since 3.0.0
42 *
43 * @param array $args
44 */
45 public function __construct( $args = array() ) {
46 $defaults = array(
47 'url' => '',
48 'nonce' => '',
49 );
50 $args = wp_parse_args( $args, $defaults );
51
52 parent::__construct( $args );
53 }
54
55 /**
56 * Sets up the strings used in the update process.
57 *
58 * @since 3.0.0
59 */
60 public function add_strings() {
61 $this->upgrader->strings['skin_upgrade_start'] = __( 'The update process is starting. This process may take a while on some hosts, so please be patient.' );
62 /* translators: 1: Title of an update, 2: Error message. */
63 $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while updating %1$s: %2$s' );
64 /* translators: %s: Title of an update. */
65 $this->upgrader->strings['skin_update_failed'] = __( 'The update of %s failed.' );
66 /* translators: %s: Title of an update. */
67 $this->upgrader->strings['skin_update_successful'] = __( '%s updated successfully.' );
68 $this->upgrader->strings['skin_upgrade_end'] = __( 'All updates have been completed.' );
69 }
70
71 /**
72 * Displays a message about the update.
73 *
74 * @since 3.0.0
75 * @since 5.9.0 Renamed `$string` (a PHP reserved keyword) to `$feedback` for PHP 8 named parameter support.
76 *
77 * @param string $feedback Message data.
78 * @param mixed ...$args Optional text replacements.
79 */
80 public function feedback( $feedback, ...$args ) {
81 if ( isset( $this->upgrader->strings[ $feedback ] ) ) {
82 $feedback = $this->upgrader->strings[ $feedback ];
83 }
84
85 if ( str_contains( $feedback, '%' ) ) {
86 if ( $args ) {
87 $args = array_map( 'strip_tags', $args );
88 $args = array_map( 'esc_html', $args );
89 $feedback = vsprintf( $feedback, $args );
90 }
91 }
92 if ( empty( $feedback ) ) {
93 return;
94 }
95 if ( $this->in_loop ) {
96 echo "$feedback<br />\n";
97 } else {
98 echo "<p>$feedback</p>\n";
99 }
100 }
101
102 /**
103 * Displays the header before the update process.
104 *
105 * @since 3.0.0
106 */
107 public function header() {
108 // Nothing. This will be displayed within an iframe.
109 }
110
111 /**
112 * Displays the footer following the update process.
113 *
114 * @since 3.0.0
115 */
116 public function footer() {
117 // Nothing. This will be displayed within an iframe.
118 }
119
120 /**
121 * Displays an error message about the update.
122 *
123 * @since 3.0.0
124 * @since 5.9.0 Renamed `$error` to `$errors` for PHP 8 named parameter support.
125 *
126 * @param string|WP_Error $errors Errors.
127 */
128 public function error( $errors ) {
129 if ( is_string( $errors ) && isset( $this->upgrader->strings[ $errors ] ) ) {
130 $this->error = $this->upgrader->strings[ $errors ];
131 }
132
133 if ( is_wp_error( $errors ) ) {
134 $messages = array();
135 foreach ( $errors->get_error_messages() as $emessage ) {
136 if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
137 $messages[] = $emessage . ' ' . esc_html( strip_tags( $errors->get_error_data() ) );
138 } else {
139 $messages[] = $emessage;
140 }
141 }
142 $this->error = implode( ', ', $messages );
143 }
144 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').hide();</script>';
145 }
146
147 /**
148 * Displays the header before the bulk update process.
149 *
150 * @since 3.0.0
151 */
152 public function bulk_header() {
153 $this->feedback( 'skin_upgrade_start' );
154 }
155
156 /**
157 * Displays the footer following the bulk update process.
158 *
159 * @since 3.0.0
160 */
161 public function bulk_footer() {
162 $this->feedback( 'skin_upgrade_end' );
163 }
164
165 /**
166 * Performs an action before a bulk update.
167 *
168 * @since 3.0.0
169 *
170 * @param string $title
171 */
172 public function before( $title = '' ) {
173 $this->in_loop = true;
174 printf( '<h2>' . $this->upgrader->strings['skin_before_update_header'] . ' <span class="spinner waiting-' . $this->upgrader->update_current . '"></span></h2>', $title, $this->upgrader->update_current, $this->upgrader->update_count );
175 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').css("display", "inline-block");</script>';
176 // This progress messages div gets moved via JavaScript when clicking on "More details.".
177 echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr( $this->upgrader->update_current ) . '"><p>';
178 $this->flush_output();
179 }
180
181 /**
182 * Performs an action following a bulk update.
183 *
184 * @since 3.0.0
185 *
186 * @param string $title
187 */
188 public function after( $title = '' ) {
189 echo '</p></div>';
190 if ( $this->error || ! $this->result ) {
191 if ( $this->error ) {
192 $after_error_message = sprintf( $this->upgrader->strings['skin_update_failed_error'], $title, '<strong>' . $this->error . '</strong>' );
193 } else {
194 $after_error_message = sprintf( $this->upgrader->strings['skin_update_failed'], $title );
195 }
196 wp_admin_notice(
197 $after_error_message,
198 array(
199 'additional_classes' => array( 'error' ),
200 )
201 );
202
203 echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js( $this->upgrader->update_current ) . '\').show();</script>';
204 }
205 if ( $this->result && ! is_wp_error( $this->result ) ) {
206 if ( ! $this->error ) {
207 echo '<div class="updated js-update-details" data-update-details="progress-' . esc_attr( $this->upgrader->update_current ) . '">' .
208 '<p>' . sprintf( $this->upgrader->strings['skin_update_successful'], $title ) .
209 ' <button type="button" class="hide-if-no-js button-link js-update-details-toggle" aria-expanded="false">' . __( 'More details.' ) . '<span class="dashicons dashicons-arrow-down" aria-hidden="true"></span></button>' .
210 '</p></div>';
211 }
212
213 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').hide();</script>';
214 }
215
216 $this->reset();
217 $this->flush_output();
218 }
219
220 /**
221 * Resets the properties used in the update process.
222 *
223 * @since 3.0.0
224 */
225 public function reset() {
226 $this->in_loop = false;
227 $this->error = false;
228 }
229
230 /**
231 * Flushes all output buffers.
232 *
233 * @since 3.0.0
234 */
235 public function flush_output() {
236 wp_ob_end_flush_all();
237 flush();
238 }
239}
240