|
1 <?php |
|
2 /** |
|
3 * Upgrade API: Core_Upgrader class |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Upgrader |
|
7 * @since 4.6.0 |
|
8 */ |
|
9 |
|
10 /** |
|
11 * Core class used for updating core. |
|
12 * |
|
13 * It allows for WordPress to upgrade itself in combination with |
|
14 * the wp-admin/includes/update-core.php file. |
|
15 * |
|
16 * @since 2.8.0 |
|
17 * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php. |
|
18 * |
|
19 * @see WP_Upgrader |
|
20 */ |
|
21 class Core_Upgrader extends WP_Upgrader { |
|
22 |
|
23 /** |
|
24 * Initialize the upgrade strings. |
|
25 * |
|
26 * @since 2.8.0 |
|
27 */ |
|
28 public function upgrade_strings() { |
|
29 $this->strings['up_to_date'] = __('WordPress is at the latest version.'); |
|
30 $this->strings['locked'] = __('Another update is currently in progress.'); |
|
31 $this->strings['no_package'] = __('Update package not available.'); |
|
32 /* translators: %s: package URL */ |
|
33 $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '<span class="code">%s</span>' ); |
|
34 $this->strings['unpack_package'] = __('Unpacking the update…'); |
|
35 $this->strings['copy_failed'] = __('Could not copy files.'); |
|
36 $this->strings['copy_failed_space'] = __('Could not copy files. You may have run out of disk space.' ); |
|
37 $this->strings['start_rollback'] = __( 'Attempting to roll back to previous version.' ); |
|
38 $this->strings['rollback_was_required'] = __( 'Due to an error during updating, WordPress has rolled back to your previous version.' ); |
|
39 } |
|
40 |
|
41 /** |
|
42 * Upgrade WordPress core. |
|
43 * |
|
44 * @since 2.8.0 |
|
45 * |
|
46 * @global WP_Filesystem_Base $wp_filesystem Subclass |
|
47 * @global callable $_wp_filesystem_direct_method |
|
48 * |
|
49 * @param object $current Response object for whether WordPress is current. |
|
50 * @param array $args { |
|
51 * Optional. Arguments for upgrading WordPress core. Default empty array. |
|
52 * |
|
53 * @type bool $pre_check_md5 Whether to check the file checksums before |
|
54 * attempting the upgrade. Default true. |
|
55 * @type bool $attempt_rollback Whether to attempt to rollback the chances if |
|
56 * there is a problem. Default false. |
|
57 * @type bool $do_rollback Whether to perform this "upgrade" as a rollback. |
|
58 * Default false. |
|
59 * } |
|
60 * @return null|false|WP_Error False or WP_Error on failure, null on success. |
|
61 */ |
|
62 public function upgrade( $current, $args = array() ) { |
|
63 global $wp_filesystem; |
|
64 |
|
65 include( ABSPATH . WPINC . '/version.php' ); // $wp_version; |
|
66 |
|
67 $start_time = time(); |
|
68 |
|
69 $defaults = array( |
|
70 'pre_check_md5' => true, |
|
71 'attempt_rollback' => false, |
|
72 'do_rollback' => false, |
|
73 'allow_relaxed_file_ownership' => false, |
|
74 ); |
|
75 $parsed_args = wp_parse_args( $args, $defaults ); |
|
76 |
|
77 $this->init(); |
|
78 $this->upgrade_strings(); |
|
79 |
|
80 // Is an update available? |
|
81 if ( !isset( $current->response ) || $current->response == 'latest' ) |
|
82 return new WP_Error('up_to_date', $this->strings['up_to_date']); |
|
83 |
|
84 $res = $this->fs_connect( array( ABSPATH, WP_CONTENT_DIR ), $parsed_args['allow_relaxed_file_ownership'] ); |
|
85 if ( ! $res || is_wp_error( $res ) ) { |
|
86 return $res; |
|
87 } |
|
88 |
|
89 $wp_dir = trailingslashit($wp_filesystem->abspath()); |
|
90 |
|
91 $partial = true; |
|
92 if ( $parsed_args['do_rollback'] ) |
|
93 $partial = false; |
|
94 elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() ) |
|
95 $partial = false; |
|
96 |
|
97 /* |
|
98 * If partial update is returned from the API, use that, unless we're doing |
|
99 * a reinstallation. If we cross the new_bundled version number, then use |
|
100 * the new_bundled zip. Don't though if the constant is set to skip bundled items. |
|
101 * If the API returns a no_content zip, go with it. Finally, default to the full zip. |
|
102 */ |
|
103 if ( $parsed_args['do_rollback'] && $current->packages->rollback ) |
|
104 $to_download = 'rollback'; |
|
105 elseif ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version && $partial ) |
|
106 $to_download = 'partial'; |
|
107 elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' ) |
|
108 && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) ) |
|
109 $to_download = 'new_bundled'; |
|
110 elseif ( $current->packages->no_content ) |
|
111 $to_download = 'no_content'; |
|
112 else |
|
113 $to_download = 'full'; |
|
114 |
|
115 // Lock to prevent multiple Core Updates occurring |
|
116 $lock = WP_Upgrader::create_lock( 'core_updater', 15 * MINUTE_IN_SECONDS ); |
|
117 if ( ! $lock ) { |
|
118 return new WP_Error( 'locked', $this->strings['locked'] ); |
|
119 } |
|
120 |
|
121 $download = $this->download_package( $current->packages->$to_download ); |
|
122 if ( is_wp_error( $download ) ) { |
|
123 WP_Upgrader::release_lock( 'core_updater' ); |
|
124 return $download; |
|
125 } |
|
126 |
|
127 $working_dir = $this->unpack_package( $download ); |
|
128 if ( is_wp_error( $working_dir ) ) { |
|
129 WP_Upgrader::release_lock( 'core_updater' ); |
|
130 return $working_dir; |
|
131 } |
|
132 |
|
133 // Copy update-core.php from the new version into place. |
|
134 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) { |
|
135 $wp_filesystem->delete($working_dir, true); |
|
136 WP_Upgrader::release_lock( 'core_updater' ); |
|
137 return new WP_Error( 'copy_failed_for_update_core_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-admin/includes/update-core.php' ); |
|
138 } |
|
139 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE); |
|
140 |
|
141 require_once( ABSPATH . 'wp-admin/includes/update-core.php' ); |
|
142 |
|
143 if ( ! function_exists( 'update_core' ) ) { |
|
144 WP_Upgrader::release_lock( 'core_updater' ); |
|
145 return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] ); |
|
146 } |
|
147 |
|
148 $result = update_core( $working_dir, $wp_dir ); |
|
149 |
|
150 // In the event of an issue, we may be able to roll back. |
|
151 if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) { |
|
152 $try_rollback = false; |
|
153 if ( is_wp_error( $result ) ) { |
|
154 $error_code = $result->get_error_code(); |
|
155 /* |
|
156 * Not all errors are equal. These codes are critical: copy_failed__copy_dir, |
|
157 * mkdir_failed__copy_dir, copy_failed__copy_dir_retry, and disk_full. |
|
158 * do_rollback allows for update_core() to trigger a rollback if needed. |
|
159 */ |
|
160 if ( false !== strpos( $error_code, 'do_rollback' ) ) |
|
161 $try_rollback = true; |
|
162 elseif ( false !== strpos( $error_code, '__copy_dir' ) ) |
|
163 $try_rollback = true; |
|
164 elseif ( 'disk_full' === $error_code ) |
|
165 $try_rollback = true; |
|
166 } |
|
167 |
|
168 if ( $try_rollback ) { |
|
169 /** This filter is documented in wp-admin/includes/update-core.php */ |
|
170 apply_filters( 'update_feedback', $result ); |
|
171 |
|
172 /** This filter is documented in wp-admin/includes/update-core.php */ |
|
173 apply_filters( 'update_feedback', $this->strings['start_rollback'] ); |
|
174 |
|
175 $rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) ); |
|
176 |
|
177 $original_result = $result; |
|
178 $result = new WP_Error( 'rollback_was_required', $this->strings['rollback_was_required'], (object) array( 'update' => $original_result, 'rollback' => $rollback_result ) ); |
|
179 } |
|
180 } |
|
181 |
|
182 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */ |
|
183 do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'core' ) ); |
|
184 |
|
185 // Clear the current updates |
|
186 delete_site_transient( 'update_core' ); |
|
187 |
|
188 if ( ! $parsed_args['do_rollback'] ) { |
|
189 $stats = array( |
|
190 'update_type' => $current->response, |
|
191 'success' => true, |
|
192 'fs_method' => $wp_filesystem->method, |
|
193 'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ), |
|
194 'fs_method_direct' => !empty( $GLOBALS['_wp_filesystem_direct_method'] ) ? $GLOBALS['_wp_filesystem_direct_method'] : '', |
|
195 'time_taken' => time() - $start_time, |
|
196 'reported' => $wp_version, |
|
197 'attempted' => $current->version, |
|
198 ); |
|
199 |
|
200 if ( is_wp_error( $result ) ) { |
|
201 $stats['success'] = false; |
|
202 // Did a rollback occur? |
|
203 if ( ! empty( $try_rollback ) ) { |
|
204 $stats['error_code'] = $original_result->get_error_code(); |
|
205 $stats['error_data'] = $original_result->get_error_data(); |
|
206 // Was the rollback successful? If not, collect its error too. |
|
207 $stats['rollback'] = ! is_wp_error( $rollback_result ); |
|
208 if ( is_wp_error( $rollback_result ) ) { |
|
209 $stats['rollback_code'] = $rollback_result->get_error_code(); |
|
210 $stats['rollback_data'] = $rollback_result->get_error_data(); |
|
211 } |
|
212 } else { |
|
213 $stats['error_code'] = $result->get_error_code(); |
|
214 $stats['error_data'] = $result->get_error_data(); |
|
215 } |
|
216 } |
|
217 |
|
218 wp_version_check( $stats ); |
|
219 } |
|
220 |
|
221 WP_Upgrader::release_lock( 'core_updater' ); |
|
222 |
|
223 return $result; |
|
224 } |
|
225 |
|
226 /** |
|
227 * Determines if this WordPress Core version should update to an offered version or not. |
|
228 * |
|
229 * @since 3.7.0 |
|
230 * |
|
231 * @static |
|
232 * |
|
233 * @param string $offered_ver The offered version, of the format x.y.z. |
|
234 * @return bool True if we should update to the offered version, otherwise false. |
|
235 */ |
|
236 public static function should_update_to_version( $offered_ver ) { |
|
237 include( ABSPATH . WPINC . '/version.php' ); // $wp_version; // x.y.z |
|
238 |
|
239 $current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y |
|
240 $new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y |
|
241 $current_is_development_version = (bool) strpos( $wp_version, '-' ); |
|
242 |
|
243 // Defaults: |
|
244 $upgrade_dev = true; |
|
245 $upgrade_minor = true; |
|
246 $upgrade_major = false; |
|
247 |
|
248 // WP_AUTO_UPDATE_CORE = true (all), 'minor', false. |
|
249 if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) { |
|
250 if ( false === WP_AUTO_UPDATE_CORE ) { |
|
251 // Defaults to turned off, unless a filter allows it |
|
252 $upgrade_dev = $upgrade_minor = $upgrade_major = false; |
|
253 } elseif ( true === WP_AUTO_UPDATE_CORE ) { |
|
254 // ALL updates for core |
|
255 $upgrade_dev = $upgrade_minor = $upgrade_major = true; |
|
256 } elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) { |
|
257 // Only minor updates for core |
|
258 $upgrade_dev = $upgrade_major = false; |
|
259 $upgrade_minor = true; |
|
260 } |
|
261 } |
|
262 |
|
263 // 1: If we're already on that version, not much point in updating? |
|
264 if ( $offered_ver == $wp_version ) |
|
265 return false; |
|
266 |
|
267 // 2: If we're running a newer version, that's a nope |
|
268 if ( version_compare( $wp_version, $offered_ver, '>' ) ) |
|
269 return false; |
|
270 |
|
271 $failure_data = get_site_option( 'auto_core_update_failed' ); |
|
272 if ( $failure_data ) { |
|
273 // If this was a critical update failure, cannot update. |
|
274 if ( ! empty( $failure_data['critical'] ) ) |
|
275 return false; |
|
276 |
|
277 // Don't claim we can update on update-core.php if we have a non-critical failure logged. |
|
278 if ( $wp_version == $failure_data['current'] && false !== strpos( $offered_ver, '.1.next.minor' ) ) |
|
279 return false; |
|
280 |
|
281 // Cannot update if we're retrying the same A to B update that caused a non-critical failure. |
|
282 // Some non-critical failures do allow retries, like download_failed. |
|
283 // 3.7.1 => 3.7.2 resulted in files_not_writable, if we are still on 3.7.1 and still trying to update to 3.7.2. |
|
284 if ( empty( $failure_data['retry'] ) && $wp_version == $failure_data['current'] && $offered_ver == $failure_data['attempted'] ) |
|
285 return false; |
|
286 } |
|
287 |
|
288 // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2 |
|
289 if ( $current_is_development_version ) { |
|
290 |
|
291 /** |
|
292 * Filters whether to enable automatic core updates for development versions. |
|
293 * |
|
294 * @since 3.7.0 |
|
295 * |
|
296 * @param bool $upgrade_dev Whether to enable automatic updates for |
|
297 * development versions. |
|
298 */ |
|
299 if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) ) |
|
300 return false; |
|
301 // Else fall through to minor + major branches below. |
|
302 } |
|
303 |
|
304 // 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4) |
|
305 if ( $current_branch == $new_branch ) { |
|
306 |
|
307 /** |
|
308 * Filters whether to enable minor automatic core updates. |
|
309 * |
|
310 * @since 3.7.0 |
|
311 * |
|
312 * @param bool $upgrade_minor Whether to enable minor automatic core updates. |
|
313 */ |
|
314 return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor ); |
|
315 } |
|
316 |
|
317 // 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1) |
|
318 if ( version_compare( $new_branch, $current_branch, '>' ) ) { |
|
319 |
|
320 /** |
|
321 * Filters whether to enable major automatic core updates. |
|
322 * |
|
323 * @since 3.7.0 |
|
324 * |
|
325 * @param bool $upgrade_major Whether to enable major automatic core updates. |
|
326 */ |
|
327 return apply_filters( 'allow_major_auto_core_updates', $upgrade_major ); |
|
328 } |
|
329 |
|
330 // If we're not sure, we don't want it |
|
331 return false; |
|
332 } |
|
333 |
|
334 /** |
|
335 * Compare the disk file checksums against the expected checksums. |
|
336 * |
|
337 * @since 3.7.0 |
|
338 * |
|
339 * @global string $wp_version |
|
340 * @global string $wp_local_package |
|
341 * |
|
342 * @return bool True if the checksums match, otherwise false. |
|
343 */ |
|
344 public function check_files() { |
|
345 global $wp_version, $wp_local_package; |
|
346 |
|
347 $checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' ); |
|
348 |
|
349 if ( ! is_array( $checksums ) ) |
|
350 return false; |
|
351 |
|
352 foreach ( $checksums as $file => $checksum ) { |
|
353 // Skip files which get updated |
|
354 if ( 'wp-content' == substr( $file, 0, 10 ) ) |
|
355 continue; |
|
356 if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum ) |
|
357 return false; |
|
358 } |
|
359 |
|
360 return true; |
|
361 } |
|
362 } |