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