|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Core_Upgrader extends WP_Upgrader { |
|
|
|
|
|
|
|
|
|
|
|
|
|
public function upgrade_strings() { |
|
$this->strings['up_to_date'] = __( 'WordPress is at the latest version.' ); |
|
$this->strings['locked'] = __( 'Another update is currently in progress.' ); |
|
$this->strings['no_package'] = __( 'Update package not available.' ); |
|
|
|
$this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '<span class="code pre">%s</span>' ); |
|
$this->strings['unpack_package'] = __( 'Unpacking the update…' ); |
|
$this->strings['copy_failed'] = __( 'Could not copy files.' ); |
|
$this->strings['copy_failed_space'] = __( 'Could not copy files. You may have run out of disk space.' ); |
|
$this->strings['start_rollback'] = __( 'Attempting to restore the previous version.' ); |
|
$this->strings['rollback_was_required'] = __( 'Due to an error during updating, WordPress has been restored to your previous version.' ); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function upgrade( $current, $args = array() ) { |
|
global $wp_filesystem; |
|
|
|
require ABSPATH . WPINC . '/version.php'; |
|
|
|
$start_time = time(); |
|
|
|
$defaults = array( |
|
'pre_check_md5' => true, |
|
'attempt_rollback' => false, |
|
'do_rollback' => false, |
|
'allow_relaxed_file_ownership' => false, |
|
); |
|
$parsed_args = wp_parse_args( $args, $defaults ); |
|
|
|
$this->init(); |
|
$this->upgrade_strings(); |
|
|
|
|
|
if ( ! isset( $current->response ) || 'latest' === $current->response ) { |
|
return new WP_Error( 'up_to_date', $this->strings['up_to_date'] ); |
|
} |
|
|
|
$res = $this->fs_connect( array( ABSPATH, WP_CONTENT_DIR ), $parsed_args['allow_relaxed_file_ownership'] ); |
|
if ( ! $res || is_wp_error( $res ) ) { |
|
return $res; |
|
} |
|
|
|
$wp_dir = trailingslashit( $wp_filesystem->abspath() ); |
|
|
|
$partial = true; |
|
if ( $parsed_args['do_rollback'] ) { |
|
$partial = false; |
|
} elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() ) { |
|
$partial = false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( $parsed_args['do_rollback'] && $current->packages->rollback ) { |
|
$to_download = 'rollback'; |
|
} elseif ( $current->packages->partial && 'reinstall' !== $current->response && $wp_version === $current->partial_version && $partial ) { |
|
$to_download = 'partial'; |
|
} elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' ) |
|
&& ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) ) { |
|
$to_download = 'new_bundled'; |
|
} elseif ( $current->packages->no_content ) { |
|
$to_download = 'no_content'; |
|
} else { |
|
$to_download = 'full'; |
|
} |
|
|
|
|
|
$lock = WP_Upgrader::create_lock( 'core_updater', 15 * MINUTE_IN_SECONDS ); |
|
if ( ! $lock ) { |
|
return new WP_Error( 'locked', $this->strings['locked'] ); |
|
} |
|
|
|
$download = $this->download_package( $current->packages->$to_download, false ); |
|
|
|
|
|
|
|
|
|
|
|
if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) { |
|
|
|
|
|
apply_filters( 'update_feedback', $download->get_error_message() ); |
|
|
|
|
|
wp_version_check( |
|
array( |
|
'signature_failure_code' => $download->get_error_code(), |
|
'signature_failure_data' => $download->get_error_data(), |
|
) |
|
); |
|
|
|
|
|
$download = $download->get_error_data( 'softfail-filename' ); |
|
} |
|
|
|
if ( is_wp_error( $download ) ) { |
|
WP_Upgrader::release_lock( 'core_updater' ); |
|
return $download; |
|
} |
|
|
|
$working_dir = $this->unpack_package( $download ); |
|
if ( is_wp_error( $working_dir ) ) { |
|
WP_Upgrader::release_lock( 'core_updater' ); |
|
return $working_dir; |
|
} |
|
|
|
|
|
if ( ! $wp_filesystem->copy( $working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true ) ) { |
|
$wp_filesystem->delete( $working_dir, true ); |
|
WP_Upgrader::release_lock( 'core_updater' ); |
|
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' ); |
|
} |
|
$wp_filesystem->chmod( $wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE ); |
|
|
|
wp_opcache_invalidate( ABSPATH . 'wp-admin/includes/update-core.php' ); |
|
require_once ABSPATH . 'wp-admin/includes/update-core.php'; |
|
|
|
if ( ! function_exists( 'update_core' ) ) { |
|
WP_Upgrader::release_lock( 'core_updater' ); |
|
return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] ); |
|
} |
|
|
|
$result = update_core( $working_dir, $wp_dir ); |
|
|
|
|
|
if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) { |
|
$try_rollback = false; |
|
if ( is_wp_error( $result ) ) { |
|
$error_code = $result->get_error_code(); |
|
|
|
|
|
|
|
|
|
|
|
if ( str_contains( $error_code, 'do_rollback' ) ) { |
|
$try_rollback = true; |
|
} elseif ( str_contains( $error_code, '__copy_dir' ) ) { |
|
$try_rollback = true; |
|
} elseif ( 'disk_full' === $error_code ) { |
|
$try_rollback = true; |
|
} |
|
} |
|
|
|
if ( $try_rollback ) { |
|
|
|
apply_filters( 'update_feedback', $result ); |
|
|
|
|
|
apply_filters( 'update_feedback', $this->strings['start_rollback'] ); |
|
|
|
$rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) ); |
|
|
|
$original_result = $result; |
|
$result = new WP_Error( |
|
'rollback_was_required', |
|
$this->strings['rollback_was_required'], |
|
(object) array( |
|
'update' => $original_result, |
|
'rollback' => $rollback_result, |
|
) |
|
); |
|
} |
|
} |
|
|
|
|
|
do_action( |
|
'upgrader_process_complete', |
|
$this, |
|
array( |
|
'action' => 'update', |
|
'type' => 'core', |
|
) |
|
); |
|
|
|
|
|
delete_site_transient( 'update_core' ); |
|
|
|
if ( ! $parsed_args['do_rollback'] ) { |
|
$stats = array( |
|
'update_type' => $current->response, |
|
'success' => true, |
|
'fs_method' => $wp_filesystem->method, |
|
'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ), |
|
'fs_method_direct' => ! empty( $GLOBALS['_wp_filesystem_direct_method'] ) ? $GLOBALS['_wp_filesystem_direct_method'] : '', |
|
'time_taken' => time() - $start_time, |
|
'reported' => $wp_version, |
|
'attempted' => $current->version, |
|
); |
|
|
|
if ( is_wp_error( $result ) ) { |
|
$stats['success'] = false; |
|
|
|
if ( ! empty( $try_rollback ) ) { |
|
$stats['error_code'] = $original_result->get_error_code(); |
|
$stats['error_data'] = $original_result->get_error_data(); |
|
|
|
$stats['rollback'] = ! is_wp_error( $rollback_result ); |
|
if ( is_wp_error( $rollback_result ) ) { |
|
$stats['rollback_code'] = $rollback_result->get_error_code(); |
|
$stats['rollback_data'] = $rollback_result->get_error_data(); |
|
} |
|
} else { |
|
$stats['error_code'] = $result->get_error_code(); |
|
$stats['error_data'] = $result->get_error_data(); |
|
} |
|
} |
|
|
|
wp_version_check( $stats ); |
|
} |
|
|
|
WP_Upgrader::release_lock( 'core_updater' ); |
|
|
|
return $result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function should_update_to_version( $offered_ver ) { |
|
require ABSPATH . WPINC . '/version.php'; |
|
|
|
$current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); |
|
$new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); |
|
|
|
$current_is_development_version = (bool) strpos( $wp_version, '-' ); |
|
|
|
|
|
$upgrade_dev = get_site_option( 'auto_update_core_dev', 'enabled' ) === 'enabled'; |
|
$upgrade_minor = get_site_option( 'auto_update_core_minor', 'enabled' ) === 'enabled'; |
|
$upgrade_major = get_site_option( 'auto_update_core_major', 'unset' ) === 'enabled'; |
|
|
|
|
|
if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) { |
|
if ( false === WP_AUTO_UPDATE_CORE ) { |
|
|
|
$upgrade_dev = false; |
|
$upgrade_minor = false; |
|
$upgrade_major = false; |
|
} elseif ( true === WP_AUTO_UPDATE_CORE |
|
|| in_array( WP_AUTO_UPDATE_CORE, array( 'beta', 'rc', 'development', 'branch-development' ), true ) |
|
) { |
|
|
|
$upgrade_dev = true; |
|
$upgrade_minor = true; |
|
$upgrade_major = true; |
|
} elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) { |
|
|
|
$upgrade_dev = false; |
|
$upgrade_minor = true; |
|
$upgrade_major = false; |
|
} |
|
} |
|
|
|
|
|
if ( $offered_ver === $wp_version ) { |
|
return false; |
|
} |
|
|
|
|
|
if ( version_compare( $wp_version, $offered_ver, '>' ) ) { |
|
return false; |
|
} |
|
|
|
$failure_data = get_site_option( 'auto_core_update_failed' ); |
|
if ( $failure_data ) { |
|
|
|
if ( ! empty( $failure_data['critical'] ) ) { |
|
return false; |
|
} |
|
|
|
|
|
if ( $wp_version === $failure_data['current'] && str_contains( $offered_ver, '.1.next.minor' ) ) { |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( empty( $failure_data['retry'] ) && $wp_version === $failure_data['current'] && $offered_ver === $failure_data['attempted'] ) { |
|
return false; |
|
} |
|
} |
|
|
|
|
|
if ( $current_is_development_version ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) ) { |
|
return false; |
|
} |
|
|
|
} |
|
|
|
|
|
if ( $current_branch === $new_branch ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor ); |
|
} |
|
|
|
|
|
if ( version_compare( $new_branch, $current_branch, '>' ) ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return apply_filters( 'allow_major_auto_core_updates', $upgrade_major ); |
|
} |
|
|
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function check_files() { |
|
global $wp_version, $wp_local_package; |
|
|
|
$checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' ); |
|
|
|
if ( ! is_array( $checksums ) ) { |
|
return false; |
|
} |
|
|
|
foreach ( $checksums as $file => $checksum ) { |
|
|
|
if ( str_starts_with( $file, 'wp-content' ) ) { |
|
continue; |
|
} |
|
if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum ) { |
|
return false; |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
} |
|
|