Skip to content

Commit

Permalink
Coding Standards: Use strict comparison in `wp-admin/includes/update-…
Browse files Browse the repository at this point in the history
…core.php`.

Includes minor code layout fixes for better readability.

See #52627.

git-svn-id: https://develop.svn.wordpress.org/trunk@50785 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Apr 23, 2021
1 parent e4bda14 commit edd6ec0
Showing 1 changed file with 96 additions and 16 deletions.
112 changes: 96 additions & 16 deletions src/wp-admin/includes/update-core.php
Expand Up @@ -924,14 +924,19 @@ function update_core( $from, $to ) {
// Sanity check the unzipped distribution.
$distro = '';
$roots = array( '/wordpress/', '/wordpress-mu/' );

foreach ( $roots as $root ) {
if ( $wp_filesystem->exists( $from . $root . 'readme.html' ) && $wp_filesystem->exists( $from . $root . 'wp-includes/version.php' ) ) {
if ( $wp_filesystem->exists( $from . $root . 'readme.html' )
&& $wp_filesystem->exists( $from . $root . 'wp-includes/version.php' )
) {
$distro = $root;
break;
}
}

if ( ! $distro ) {
$wp_filesystem->delete( $from, true );

return new WP_Error( 'insane_distro', __( 'The update could not be unpacked' ) );
}

Expand All @@ -942,9 +947,15 @@ function update_core( $from, $to ) {
* BC Note: $wp_filesystem->wp_content_dir() returned unslashed pre-2.8.
*/
$versions_file = trailingslashit( $wp_filesystem->wp_content_dir() ) . 'upgrade/version-current.php';

if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $versions_file ) ) {
$wp_filesystem->delete( $from, true );
return new WP_Error( 'copy_failed_for_version_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-includes/version.php' );

return new WP_Error(
'copy_failed_for_version_file',
__( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ),
'wp-includes/version.php'
);
}

$wp_filesystem->chmod( $versions_file, FS_CHMOD_FILE );
Expand All @@ -965,6 +976,7 @@ function update_core( $from, $to ) {
$old_wp_version = $GLOBALS['wp_version']; // The version of WordPress we're updating from.
$development_build = ( false !== strpos( $old_wp_version . $wp_version, '-' ) ); // A dash in the version indicates a development release.
$php_compat = version_compare( $php_version, $required_php_version, '>=' );

if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
$mysql_compat = true;
} else {
Expand Down Expand Up @@ -1057,23 +1069,31 @@ function update_core( $from, $to ) {
$working_dir_local = WP_CONTENT_DIR . '/upgrade/' . basename( $from ) . $distro;

$checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );

if ( is_array( $checksums ) && isset( $checksums[ $wp_version ] ) ) {
$checksums = $checksums[ $wp_version ]; // Compat code for 3.7-beta2.
}

if ( is_array( $checksums ) ) {
foreach ( $checksums as $file => $checksum ) {
if ( 'wp-content' === substr( $file, 0, 10 ) ) {
continue;
}

if ( ! file_exists( ABSPATH . $file ) ) {
continue;
}

if ( ! file_exists( $working_dir_local . $file ) ) {
continue;
}
if ( '.' === dirname( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true ) ) {

if ( '.' === dirname( $file )
&& in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true )
) {
continue;
}

if ( md5_file( ABSPATH . $file ) === $checksum ) {
$skip[] = $file;
} else {
Expand All @@ -1086,11 +1106,14 @@ function update_core( $from, $to ) {
// If we're using the direct method, we can predict write failures that are due to permissions.
if ( $check_is_writable && 'direct' === $wp_filesystem->method ) {
$files_writable = array_filter( $check_is_writable, array( $wp_filesystem, 'is_writable' ) );

if ( $files_writable !== $check_is_writable ) {
$files_not_writable = array_diff_key( $check_is_writable, $files_writable );

foreach ( $files_not_writable as $relative_file_not_writable => $file_not_writable ) {
// If the writable check failed, chmod file to 0644 and try again, same as copy_dir().
$wp_filesystem->chmod( $file_not_writable, FS_CHMOD_FILE );

if ( $wp_filesystem->is_writable( $file_not_writable ) ) {
unset( $files_not_writable[ $relative_file_not_writable ] );
}
Expand All @@ -1100,13 +1123,18 @@ function update_core( $from, $to ) {
$error_data = version_compare( $old_wp_version, '3.7-beta2', '>' ) ? array_keys( $files_not_writable ) : '';

if ( $files_not_writable ) {
return new WP_Error( 'files_not_writable', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), implode( ', ', $error_data ) );
return new WP_Error(
'files_not_writable',
__( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ),
implode( ', ', $error_data )
);
}
}
}

/** This filter is documented in wp-admin/includes/update-core.php */
apply_filters( 'update_feedback', __( 'Enabling Maintenance mode…' ) );

// Create maintenance file to signal that we are upgrading.
$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
$maintenance_file = $to . '.maintenance';
Expand All @@ -1115,18 +1143,29 @@ function update_core( $from, $to ) {

/** This filter is documented in wp-admin/includes/update-core.php */
apply_filters( 'update_feedback', __( 'Copying the required files&#8230;' ) );

// Copy new versions of WP files into place.
$result = _copy_dir( $from . $distro, $to, $skip );

if ( is_wp_error( $result ) ) {
$result = new WP_Error( $result->get_error_code(), $result->get_error_message(), substr( $result->get_error_data(), strlen( $to ) ) );
$result = new WP_Error(
$result->get_error_code(),
$result->get_error_message(),
substr( $result->get_error_data(), strlen( $to ) )
);
}

// Since we know the core files have copied over, we can now copy the version file.
if ( ! is_wp_error( $result ) ) {
if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $to . 'wp-includes/version.php', true /* overwrite */ ) ) {
$wp_filesystem->delete( $from, true );
$result = new WP_Error( 'copy_failed_for_version_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-includes/version.php' );
$result = new WP_Error(
'copy_failed_for_version_file',
__( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ),
'wp-includes/version.php'
);
}

$wp_filesystem->chmod( $to . 'wp-includes/version.php', FS_CHMOD_FILE );

/*
Expand All @@ -1141,19 +1180,25 @@ function update_core( $from, $to ) {
// Check to make sure everything copied correctly, ignoring the contents of wp-content.
$skip = array( 'wp-content' );
$failed = array();

if ( isset( $checksums ) && is_array( $checksums ) ) {
foreach ( $checksums as $file => $checksum ) {
if ( 'wp-content' === substr( $file, 0, 10 ) ) {
continue;
}

if ( ! file_exists( $working_dir_local . $file ) ) {
continue;
}
if ( '.' === dirname( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true ) ) {

if ( '.' === dirname( $file )
&& in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true )
) {
$skip[] = $file;
continue;
}
if ( file_exists( ABSPATH . $file ) && md5_file( ABSPATH . $file ) == $checksum ) {

if ( file_exists( ABSPATH . $file ) && md5_file( ABSPATH . $file ) === $checksum ) {
$skip[] = $file;
} else {
$failed[] = $file;
Expand All @@ -1164,6 +1209,7 @@ function update_core( $from, $to ) {
// Some files didn't copy properly.
if ( ! empty( $failed ) ) {
$total_size = 0;

foreach ( $failed as $file ) {
if ( file_exists( $working_dir_local . $file ) ) {
$total_size += filesize( $working_dir_local . $file );
Expand All @@ -1173,20 +1219,26 @@ function update_core( $from, $to ) {
// If we don't have enough free space, it isn't worth trying again.
// Unlikely to be hit due to the check in unzip_file().
$available_space = @disk_free_space( ABSPATH );

if ( $available_space && $total_size >= $available_space ) {
$result = new WP_Error( 'disk_full', __( 'There is not enough free disk space to complete the update.' ) );
} else {
$result = _copy_dir( $from . $distro, $to, $skip );

if ( is_wp_error( $result ) ) {
$result = new WP_Error( $result->get_error_code() . '_retry', $result->get_error_message(), substr( $result->get_error_data(), strlen( $to ) ) );
$result = new WP_Error(
$result->get_error_code() . '_retry',
$result->get_error_message(),
substr( $result->get_error_data(), strlen( $to ) )
);
}
}
}

// Custom content directory needs updating now.
// Copy languages.
if ( ! is_wp_error( $result ) && $wp_filesystem->is_dir( $from . $distro . 'wp-content/languages' ) ) {
if ( WP_LANG_DIR != ABSPATH . WPINC . '/languages' || @is_dir( WP_LANG_DIR ) ) {
if ( WP_LANG_DIR !== ABSPATH . WPINC . '/languages' || @is_dir( WP_LANG_DIR ) ) {
$lang_dir = WP_LANG_DIR;
} else {
$lang_dir = WP_CONTENT_DIR . '/languages';
Expand All @@ -1201,23 +1253,33 @@ function update_core( $from, $to ) {

if ( @is_dir( $lang_dir ) ) {
$wp_lang_dir = $wp_filesystem->find_folder( $lang_dir );

if ( $wp_lang_dir ) {
$result = copy_dir( $from . $distro . 'wp-content/languages/', $wp_lang_dir );

if ( is_wp_error( $result ) ) {
$result = new WP_Error( $result->get_error_code() . '_languages', $result->get_error_message(), substr( $result->get_error_data(), strlen( $wp_lang_dir ) ) );
$result = new WP_Error(
$result->get_error_code() . '_languages',
$result->get_error_message(),
substr( $result->get_error_data(), strlen( $wp_lang_dir ) )
);
}
}
}
}

/** This filter is documented in wp-admin/includes/update-core.php */
apply_filters( 'update_feedback', __( 'Disabling Maintenance mode&#8230;' ) );

// Remove maintenance file, we're done with potential site-breaking changes.
$wp_filesystem->delete( $maintenance_file );

// 3.5 -> 3.5+ - an empty twentytwelve directory was created upon upgrade to 3.5 for some users, preventing installation of Twenty Twelve.
// 3.5 -> 3.5+ - an empty twentytwelve directory was created upon upgrade to 3.5 for some users,
// preventing installation of Twenty Twelve.
if ( '3.5' === $old_wp_version ) {
if ( is_dir( WP_CONTENT_DIR . '/themes/twentytwelve' ) && ! file_exists( WP_CONTENT_DIR . '/themes/twentytwelve/style.css' ) ) {
if ( is_dir( WP_CONTENT_DIR . '/themes/twentytwelve' )
&& ! file_exists( WP_CONTENT_DIR . '/themes/twentytwelve/style.css' )
) {
$wp_filesystem->delete( $wp_filesystem->wp_themes_dir() . 'twentytwelve/' );
}
}
Expand All @@ -1228,7 +1290,9 @@ function update_core( $from, $to ) {
* future versions of WordPress whilst avoiding the re-install upon upgrade issue.
* $development_build controls us overwriting bundled themes and plugins when a non-stable release is being updated.
*/
if ( ! is_wp_error( $result ) && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) ) {
if ( ! is_wp_error( $result )
&& ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED )
) {
foreach ( (array) $_new_bundled_files as $file => $introduced_version ) {
// If a $development_build or if $introduced version is greater than what the site was previously running.
if ( $development_build || version_compare( $introduced_version, $old_wp_version, '>' ) ) {
Expand Down Expand Up @@ -1271,7 +1335,12 @@ function update_core( $from, $to ) {
if ( ! is_wp_error( $result ) ) {
$result = new WP_Error;
}
$result->add( $_result->get_error_code() . "_$type", $_result->get_error_message(), substr( $_result->get_error_data(), strlen( $dest ) ) );

$result->add(
$_result->get_error_code() . "_$type",
$_result->get_error_message(),
substr( $_result->get_error_data(), strlen( $dest ) )
);
}
}
}
Expand All @@ -1281,12 +1350,14 @@ function update_core( $from, $to ) {
// Handle $result error from the above blocks.
if ( is_wp_error( $result ) ) {
$wp_filesystem->delete( $from, true );

return $result;
}

// Remove old files.
foreach ( $_old_files as $old_file ) {
$old_file = $to . $old_file;

if ( ! $wp_filesystem->exists( $old_file ) ) {
continue;
}
Expand All @@ -1306,6 +1377,7 @@ function update_core( $from, $to ) {
// Upgrade DB with separate request.
/** This filter is documented in wp-admin/includes/update-core.php */
apply_filters( 'update_feedback', __( 'Upgrading database&#8230;' ) );

$db_upgrade_url = admin_url( 'upgrade.php?step=upgrade_db' );
wp_remote_post( $db_upgrade_url, array( 'timeout' => 60 ) );

Expand Down Expand Up @@ -1387,6 +1459,7 @@ function _copy_dir( $from, $to, $skip_list = array() ) {
if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
// If copy failed, chmod file to 0644 and try again.
$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );

if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
return new WP_Error( 'copy_failed__copy_dir', __( 'Could not copy file.' ), $to . $filename );
}
Expand All @@ -1411,18 +1484,21 @@ function _copy_dir( $from, $to, $skip_list = array() ) {
* of the existing $skip_list.
*/
$sub_skip_list = array();

foreach ( $skip_list as $skip_item ) {
if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
}
}

$result = _copy_dir( $from . $filename, $to . $filename, $sub_skip_list );

if ( is_wp_error( $result ) ) {
return $result;
}
}
}

return true;
}

Expand Down Expand Up @@ -1516,6 +1592,7 @@ function _upgrade_422_remove_genericons() {

foreach ( $affected_files as $file ) {
$gen_dir = $wp_filesystem->find_folder( trailingslashit( dirname( $file ) ) );

if ( empty( $gen_dir ) ) {
continue;
}
Expand Down Expand Up @@ -1546,11 +1623,14 @@ function _upgrade_422_find_genericons_files_in_folder( $directory ) {
$directory = trailingslashit( $directory );
$files = array();

if ( file_exists( "{$directory}example.html" ) && false !== strpos( file_get_contents( "{$directory}example.html" ), '<title>Genericons</title>' ) ) {
if ( file_exists( "{$directory}example.html" )
&& false !== strpos( file_get_contents( "{$directory}example.html" ), '<title>Genericons</title>' )
) {
$files[] = "{$directory}example.html";
}

$dirs = glob( $directory . '*', GLOB_ONLYDIR );

if ( $dirs ) {
foreach ( $dirs as $dir ) {
$files = array_merge( $files, _upgrade_422_find_genericons_files_in_folder( $dir ) );
Expand Down

0 comments on commit edd6ec0

Please sign in to comment.