wp/wp-admin/includes/update-core.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
   796 	// 5.5
   796 	// 5.5
   797 	'wp-admin/css/ie.css',
   797 	'wp-admin/css/ie.css',
   798 	'wp-admin/css/ie.min.css',
   798 	'wp-admin/css/ie.min.css',
   799 	'wp-admin/css/ie-rtl.css',
   799 	'wp-admin/css/ie-rtl.css',
   800 	'wp-admin/css/ie-rtl.min.css',
   800 	'wp-admin/css/ie-rtl.min.css',
       
   801 	// 5.6
       
   802 	'wp-includes/js/jquery/ui/position.min.js',
       
   803 	'wp-includes/js/jquery/ui/widget.min.js',
       
   804 	// 5.7
       
   805 	'wp-includes/blocks/classic/block.json',
       
   806 	// 5.8
       
   807 	'wp-admin/images/freedoms.png',
       
   808 	'wp-admin/images/privacy.png',
       
   809 	'wp-admin/images/about-badge.svg',
       
   810 	'wp-admin/images/about-color-palette.svg',
       
   811 	'wp-admin/images/about-color-palette-vert.svg',
       
   812 	'wp-admin/images/about-header-brushes.svg',
       
   813 	'wp-includes/block-patterns/large-header.php',
       
   814 	'wp-includes/block-patterns/heading-paragraph.php',
       
   815 	'wp-includes/block-patterns/quote.php',
       
   816 	'wp-includes/block-patterns/text-three-columns-buttons.php',
       
   817 	'wp-includes/block-patterns/two-buttons.php',
       
   818 	'wp-includes/block-patterns/two-images.php',
       
   819 	'wp-includes/block-patterns/three-buttons.php',
       
   820 	'wp-includes/block-patterns/text-two-columns-with-images.php',
       
   821 	'wp-includes/block-patterns/text-two-columns.php',
       
   822 	'wp-includes/block-patterns/large-header-button.php',
       
   823 	'wp-includes/blocks/subhead/block.json',
       
   824 	'wp-includes/blocks/subhead',
       
   825 	'wp-includes/css/dist/editor/editor-styles.css',
       
   826 	'wp-includes/css/dist/editor/editor-styles.min.css',
       
   827 	'wp-includes/css/dist/editor/editor-styles-rtl.css',
       
   828 	'wp-includes/css/dist/editor/editor-styles-rtl.min.css',
   801 );
   829 );
   802 
   830 
   803 /**
   831 /**
   804  * Stores new files in wp-content to copy
   832  * Stores new files in wp-content to copy
   805  *
   833  *
   833 	'themes/twentyfifteen/'   => '4.1',
   861 	'themes/twentyfifteen/'   => '4.1',
   834 	'themes/twentysixteen/'   => '4.4',
   862 	'themes/twentysixteen/'   => '4.4',
   835 	'themes/twentyseventeen/' => '4.7',
   863 	'themes/twentyseventeen/' => '4.7',
   836 	'themes/twentynineteen/'  => '5.0',
   864 	'themes/twentynineteen/'  => '5.0',
   837 	'themes/twentytwenty/'    => '5.3',
   865 	'themes/twentytwenty/'    => '5.3',
       
   866 	'themes/twentytwentyone/' => '5.6',
   838 );
   867 );
   839 
   868 
   840 /**
   869 /**
   841  * Upgrades the core of WordPress.
   870  * Upgrades the core of WordPress.
   842  *
   871  *
   916 	apply_filters( 'update_feedback', __( 'Verifying the unpacked files…' ) );
   945 	apply_filters( 'update_feedback', __( 'Verifying the unpacked files…' ) );
   917 
   946 
   918 	// Sanity check the unzipped distribution.
   947 	// Sanity check the unzipped distribution.
   919 	$distro = '';
   948 	$distro = '';
   920 	$roots  = array( '/wordpress/', '/wordpress-mu/' );
   949 	$roots  = array( '/wordpress/', '/wordpress-mu/' );
       
   950 
   921 	foreach ( $roots as $root ) {
   951 	foreach ( $roots as $root ) {
   922 		if ( $wp_filesystem->exists( $from . $root . 'readme.html' ) && $wp_filesystem->exists( $from . $root . 'wp-includes/version.php' ) ) {
   952 		if ( $wp_filesystem->exists( $from . $root . 'readme.html' )
       
   953 			&& $wp_filesystem->exists( $from . $root . 'wp-includes/version.php' )
       
   954 		) {
   923 			$distro = $root;
   955 			$distro = $root;
   924 			break;
   956 			break;
   925 		}
   957 		}
   926 	}
   958 	}
       
   959 
   927 	if ( ! $distro ) {
   960 	if ( ! $distro ) {
   928 		$wp_filesystem->delete( $from, true );
   961 		$wp_filesystem->delete( $from, true );
       
   962 
   929 		return new WP_Error( 'insane_distro', __( 'The update could not be unpacked' ) );
   963 		return new WP_Error( 'insane_distro', __( 'The update could not be unpacked' ) );
   930 	}
   964 	}
   931 
   965 
   932 	/*
   966 	/*
   933 	 * Import $wp_version, $required_php_version, and $required_mysql_version from the new version.
   967 	 * Import $wp_version, $required_php_version, and $required_mysql_version from the new version.
   934 	 * DO NOT globalise any variables imported from `version-current.php` in this function.
   968 	 * DO NOT globalise any variables imported from `version-current.php` in this function.
   935 	 *
   969 	 *
   936 	 * BC Note: $wp_filesystem->wp_content_dir() returned unslashed pre-2.8
   970 	 * BC Note: $wp_filesystem->wp_content_dir() returned unslashed pre-2.8.
   937 	 */
   971 	 */
   938 	$versions_file = trailingslashit( $wp_filesystem->wp_content_dir() ) . 'upgrade/version-current.php';
   972 	$versions_file = trailingslashit( $wp_filesystem->wp_content_dir() ) . 'upgrade/version-current.php';
       
   973 
   939 	if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $versions_file ) ) {
   974 	if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $versions_file ) ) {
   940 		$wp_filesystem->delete( $from, true );
   975 		$wp_filesystem->delete( $from, true );
   941 		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' );
   976 
       
   977 		return new WP_Error(
       
   978 			'copy_failed_for_version_file',
       
   979 			__( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ),
       
   980 			'wp-includes/version.php'
       
   981 		);
   942 	}
   982 	}
   943 
   983 
   944 	$wp_filesystem->chmod( $versions_file, FS_CHMOD_FILE );
   984 	$wp_filesystem->chmod( $versions_file, FS_CHMOD_FILE );
       
   985 
       
   986 	/*
       
   987 	 * `wp_opcache_invalidate()` only exists in WordPress 5.5 or later,
       
   988 	 * so don't run it when upgrading from older versions.
       
   989 	 */
       
   990 	if ( function_exists( 'wp_opcache_invalidate' ) ) {
       
   991 		wp_opcache_invalidate( $versions_file );
       
   992 	}
       
   993 
   945 	require WP_CONTENT_DIR . '/upgrade/version-current.php';
   994 	require WP_CONTENT_DIR . '/upgrade/version-current.php';
   946 	$wp_filesystem->delete( $versions_file );
   995 	$wp_filesystem->delete( $versions_file );
   947 
   996 
   948 	$php_version       = phpversion();
   997 	$php_version       = phpversion();
   949 	$mysql_version     = $wpdb->db_version();
   998 	$mysql_version     = $wpdb->db_version();
   950 	$old_wp_version    = $GLOBALS['wp_version']; // The version of WordPress we're updating from.
   999 	$old_wp_version    = $GLOBALS['wp_version']; // The version of WordPress we're updating from.
   951 	$development_build = ( false !== strpos( $old_wp_version . $wp_version, '-' ) ); // A dash in the version indicates a development release.
  1000 	$development_build = ( false !== strpos( $old_wp_version . $wp_version, '-' ) ); // A dash in the version indicates a development release.
   952 	$php_compat        = version_compare( $php_version, $required_php_version, '>=' );
  1001 	$php_compat        = version_compare( $php_version, $required_php_version, '>=' );
       
  1002 
   953 	if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
  1003 	if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
   954 		$mysql_compat = true;
  1004 		$mysql_compat = true;
   955 	} else {
  1005 	} else {
   956 		$mysql_compat = version_compare( $mysql_version, $required_mysql_version, '>=' );
  1006 		$mysql_compat = version_compare( $mysql_version, $required_mysql_version, '>=' );
   957 	}
  1007 	}
   961 	}
  1011 	}
   962 
  1012 
   963 	$php_update_message = '';
  1013 	$php_update_message = '';
   964 
  1014 
   965 	if ( function_exists( 'wp_get_update_php_url' ) ) {
  1015 	if ( function_exists( 'wp_get_update_php_url' ) ) {
   966 		/* translators: %s: URL to Update PHP page. */
       
   967 		$php_update_message = '</p><p>' . sprintf(
  1016 		$php_update_message = '</p><p>' . sprintf(
       
  1017 			/* translators: %s: URL to Update PHP page. */
   968 			__( '<a href="%s">Learn more about updating PHP</a>.' ),
  1018 			__( '<a href="%s">Learn more about updating PHP</a>.' ),
   969 			esc_url( wp_get_update_php_url() )
  1019 			esc_url( wp_get_update_php_url() )
   970 		);
  1020 		);
   971 
  1021 
   972 		if ( function_exists( 'wp_get_update_php_annotation' ) ) {
  1022 		if ( function_exists( 'wp_get_update_php_annotation' ) ) {
  1040 	if ( function_exists( 'get_core_checksums' ) ) {
  1090 	if ( function_exists( 'get_core_checksums' ) ) {
  1041 		// Find the local version of the working directory.
  1091 		// Find the local version of the working directory.
  1042 		$working_dir_local = WP_CONTENT_DIR . '/upgrade/' . basename( $from ) . $distro;
  1092 		$working_dir_local = WP_CONTENT_DIR . '/upgrade/' . basename( $from ) . $distro;
  1043 
  1093 
  1044 		$checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );
  1094 		$checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );
       
  1095 
  1045 		if ( is_array( $checksums ) && isset( $checksums[ $wp_version ] ) ) {
  1096 		if ( is_array( $checksums ) && isset( $checksums[ $wp_version ] ) ) {
  1046 			$checksums = $checksums[ $wp_version ]; // Compat code for 3.7-beta2.
  1097 			$checksums = $checksums[ $wp_version ]; // Compat code for 3.7-beta2.
  1047 		}
  1098 		}
       
  1099 
  1048 		if ( is_array( $checksums ) ) {
  1100 		if ( is_array( $checksums ) ) {
  1049 			foreach ( $checksums as $file => $checksum ) {
  1101 			foreach ( $checksums as $file => $checksum ) {
  1050 				if ( 'wp-content' === substr( $file, 0, 10 ) ) {
  1102 				if ( 'wp-content' === substr( $file, 0, 10 ) ) {
  1051 					continue;
  1103 					continue;
  1052 				}
  1104 				}
       
  1105 
  1053 				if ( ! file_exists( ABSPATH . $file ) ) {
  1106 				if ( ! file_exists( ABSPATH . $file ) ) {
  1054 					continue;
  1107 					continue;
  1055 				}
  1108 				}
       
  1109 
  1056 				if ( ! file_exists( $working_dir_local . $file ) ) {
  1110 				if ( ! file_exists( $working_dir_local . $file ) ) {
  1057 					continue;
  1111 					continue;
  1058 				}
  1112 				}
  1059 				if ( '.' === dirname( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true ) ) {
  1113 
       
  1114 				if ( '.' === dirname( $file )
       
  1115 					&& in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true )
       
  1116 				) {
  1060 					continue;
  1117 					continue;
  1061 				}
  1118 				}
       
  1119 
  1062 				if ( md5_file( ABSPATH . $file ) === $checksum ) {
  1120 				if ( md5_file( ABSPATH . $file ) === $checksum ) {
  1063 					$skip[] = $file;
  1121 					$skip[] = $file;
  1064 				} else {
  1122 				} else {
  1065 					$check_is_writable[ $file ] = ABSPATH . $file;
  1123 					$check_is_writable[ $file ] = ABSPATH . $file;
  1066 				}
  1124 				}
  1069 	}
  1127 	}
  1070 
  1128 
  1071 	// If we're using the direct method, we can predict write failures that are due to permissions.
  1129 	// If we're using the direct method, we can predict write failures that are due to permissions.
  1072 	if ( $check_is_writable && 'direct' === $wp_filesystem->method ) {
  1130 	if ( $check_is_writable && 'direct' === $wp_filesystem->method ) {
  1073 		$files_writable = array_filter( $check_is_writable, array( $wp_filesystem, 'is_writable' ) );
  1131 		$files_writable = array_filter( $check_is_writable, array( $wp_filesystem, 'is_writable' ) );
       
  1132 
  1074 		if ( $files_writable !== $check_is_writable ) {
  1133 		if ( $files_writable !== $check_is_writable ) {
  1075 			$files_not_writable = array_diff_key( $check_is_writable, $files_writable );
  1134 			$files_not_writable = array_diff_key( $check_is_writable, $files_writable );
       
  1135 
  1076 			foreach ( $files_not_writable as $relative_file_not_writable => $file_not_writable ) {
  1136 			foreach ( $files_not_writable as $relative_file_not_writable => $file_not_writable ) {
  1077 				// If the writable check failed, chmod file to 0644 and try again, same as copy_dir().
  1137 				// If the writable check failed, chmod file to 0644 and try again, same as copy_dir().
  1078 				$wp_filesystem->chmod( $file_not_writable, FS_CHMOD_FILE );
  1138 				$wp_filesystem->chmod( $file_not_writable, FS_CHMOD_FILE );
       
  1139 
  1079 				if ( $wp_filesystem->is_writable( $file_not_writable ) ) {
  1140 				if ( $wp_filesystem->is_writable( $file_not_writable ) ) {
  1080 					unset( $files_not_writable[ $relative_file_not_writable ] );
  1141 					unset( $files_not_writable[ $relative_file_not_writable ] );
  1081 				}
  1142 				}
  1082 			}
  1143 			}
  1083 
  1144 
  1084 			// Store package-relative paths (the key) of non-writable files in the WP_Error object.
  1145 			// Store package-relative paths (the key) of non-writable files in the WP_Error object.
  1085 			$error_data = version_compare( $old_wp_version, '3.7-beta2', '>' ) ? array_keys( $files_not_writable ) : '';
  1146 			$error_data = version_compare( $old_wp_version, '3.7-beta2', '>' ) ? array_keys( $files_not_writable ) : '';
  1086 
  1147 
  1087 			if ( $files_not_writable ) {
  1148 			if ( $files_not_writable ) {
  1088 				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 ) );
  1149 				return new WP_Error(
       
  1150 					'files_not_writable',
       
  1151 					__( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ),
       
  1152 					implode( ', ', $error_data )
       
  1153 				);
  1089 			}
  1154 			}
  1090 		}
  1155 		}
  1091 	}
  1156 	}
  1092 
  1157 
  1093 	/** This filter is documented in wp-admin/includes/update-core.php */
  1158 	/** This filter is documented in wp-admin/includes/update-core.php */
  1094 	apply_filters( 'update_feedback', __( 'Enabling Maintenance mode&#8230;' ) );
  1159 	apply_filters( 'update_feedback', __( 'Enabling Maintenance mode&#8230;' ) );
       
  1160 
  1095 	// Create maintenance file to signal that we are upgrading.
  1161 	// Create maintenance file to signal that we are upgrading.
  1096 	$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
  1162 	$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
  1097 	$maintenance_file   = $to . '.maintenance';
  1163 	$maintenance_file   = $to . '.maintenance';
  1098 	$wp_filesystem->delete( $maintenance_file );
  1164 	$wp_filesystem->delete( $maintenance_file );
  1099 	$wp_filesystem->put_contents( $maintenance_file, $maintenance_string, FS_CHMOD_FILE );
  1165 	$wp_filesystem->put_contents( $maintenance_file, $maintenance_string, FS_CHMOD_FILE );
  1100 
  1166 
  1101 	/** This filter is documented in wp-admin/includes/update-core.php */
  1167 	/** This filter is documented in wp-admin/includes/update-core.php */
  1102 	apply_filters( 'update_feedback', __( 'Copying the required files&#8230;' ) );
  1168 	apply_filters( 'update_feedback', __( 'Copying the required files&#8230;' ) );
       
  1169 
  1103 	// Copy new versions of WP files into place.
  1170 	// Copy new versions of WP files into place.
  1104 	$result = _copy_dir( $from . $distro, $to, $skip );
  1171 	$result = _copy_dir( $from . $distro, $to, $skip );
       
  1172 
  1105 	if ( is_wp_error( $result ) ) {
  1173 	if ( is_wp_error( $result ) ) {
  1106 		$result = new WP_Error( $result->get_error_code(), $result->get_error_message(), substr( $result->get_error_data(), strlen( $to ) ) );
  1174 		$result = new WP_Error(
       
  1175 			$result->get_error_code(),
       
  1176 			$result->get_error_message(),
       
  1177 			substr( $result->get_error_data(), strlen( $to ) )
       
  1178 		);
  1107 	}
  1179 	}
  1108 
  1180 
  1109 	// Since we know the core files have copied over, we can now copy the version file.
  1181 	// Since we know the core files have copied over, we can now copy the version file.
  1110 	if ( ! is_wp_error( $result ) ) {
  1182 	if ( ! is_wp_error( $result ) ) {
  1111 		if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $to . 'wp-includes/version.php', true /* overwrite */ ) ) {
  1183 		if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $to . 'wp-includes/version.php', true /* overwrite */ ) ) {
  1112 			$wp_filesystem->delete( $from, true );
  1184 			$wp_filesystem->delete( $from, true );
  1113 			$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' );
  1185 			$result = new WP_Error(
  1114 		}
  1186 				'copy_failed_for_version_file',
       
  1187 				__( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ),
       
  1188 				'wp-includes/version.php'
       
  1189 			);
       
  1190 		}
       
  1191 
  1115 		$wp_filesystem->chmod( $to . 'wp-includes/version.php', FS_CHMOD_FILE );
  1192 		$wp_filesystem->chmod( $to . 'wp-includes/version.php', FS_CHMOD_FILE );
       
  1193 
       
  1194 		/*
       
  1195 		 * `wp_opcache_invalidate()` only exists in WordPress 5.5 or later,
       
  1196 		 * so don't run it when upgrading from older versions.
       
  1197 		 */
       
  1198 		if ( function_exists( 'wp_opcache_invalidate' ) ) {
       
  1199 			wp_opcache_invalidate( $to . 'wp-includes/version.php' );
       
  1200 		}
  1116 	}
  1201 	}
  1117 
  1202 
  1118 	// Check to make sure everything copied correctly, ignoring the contents of wp-content.
  1203 	// Check to make sure everything copied correctly, ignoring the contents of wp-content.
  1119 	$skip   = array( 'wp-content' );
  1204 	$skip   = array( 'wp-content' );
  1120 	$failed = array();
  1205 	$failed = array();
       
  1206 
  1121 	if ( isset( $checksums ) && is_array( $checksums ) ) {
  1207 	if ( isset( $checksums ) && is_array( $checksums ) ) {
  1122 		foreach ( $checksums as $file => $checksum ) {
  1208 		foreach ( $checksums as $file => $checksum ) {
  1123 			if ( 'wp-content' === substr( $file, 0, 10 ) ) {
  1209 			if ( 'wp-content' === substr( $file, 0, 10 ) ) {
  1124 				continue;
  1210 				continue;
  1125 			}
  1211 			}
       
  1212 
  1126 			if ( ! file_exists( $working_dir_local . $file ) ) {
  1213 			if ( ! file_exists( $working_dir_local . $file ) ) {
  1127 				continue;
  1214 				continue;
  1128 			}
  1215 			}
  1129 			if ( '.' === dirname( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true ) ) {
  1216 
       
  1217 			if ( '.' === dirname( $file )
       
  1218 				&& in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ), true )
       
  1219 			) {
  1130 				$skip[] = $file;
  1220 				$skip[] = $file;
  1131 				continue;
  1221 				continue;
  1132 			}
  1222 			}
  1133 			if ( file_exists( ABSPATH . $file ) && md5_file( ABSPATH . $file ) == $checksum ) {
  1223 
       
  1224 			if ( file_exists( ABSPATH . $file ) && md5_file( ABSPATH . $file ) === $checksum ) {
  1134 				$skip[] = $file;
  1225 				$skip[] = $file;
  1135 			} else {
  1226 			} else {
  1136 				$failed[] = $file;
  1227 				$failed[] = $file;
  1137 			}
  1228 			}
  1138 		}
  1229 		}
  1139 	}
  1230 	}
  1140 
  1231 
  1141 	// Some files didn't copy properly.
  1232 	// Some files didn't copy properly.
  1142 	if ( ! empty( $failed ) ) {
  1233 	if ( ! empty( $failed ) ) {
  1143 		$total_size = 0;
  1234 		$total_size = 0;
       
  1235 
  1144 		foreach ( $failed as $file ) {
  1236 		foreach ( $failed as $file ) {
  1145 			if ( file_exists( $working_dir_local . $file ) ) {
  1237 			if ( file_exists( $working_dir_local . $file ) ) {
  1146 				$total_size += filesize( $working_dir_local . $file );
  1238 				$total_size += filesize( $working_dir_local . $file );
  1147 			}
  1239 			}
  1148 		}
  1240 		}
  1149 
  1241 
  1150 		// If we don't have enough free space, it isn't worth trying again.
  1242 		// If we don't have enough free space, it isn't worth trying again.
  1151 		// Unlikely to be hit due to the check in unzip_file().
  1243 		// Unlikely to be hit due to the check in unzip_file().
  1152 		$available_space = @disk_free_space( ABSPATH );
  1244 		$available_space = @disk_free_space( ABSPATH );
       
  1245 
  1153 		if ( $available_space && $total_size >= $available_space ) {
  1246 		if ( $available_space && $total_size >= $available_space ) {
  1154 			$result = new WP_Error( 'disk_full', __( 'There is not enough free disk space to complete the update.' ) );
  1247 			$result = new WP_Error( 'disk_full', __( 'There is not enough free disk space to complete the update.' ) );
  1155 		} else {
  1248 		} else {
  1156 			$result = _copy_dir( $from . $distro, $to, $skip );
  1249 			$result = _copy_dir( $from . $distro, $to, $skip );
       
  1250 
  1157 			if ( is_wp_error( $result ) ) {
  1251 			if ( is_wp_error( $result ) ) {
  1158 				$result = new WP_Error( $result->get_error_code() . '_retry', $result->get_error_message(), substr( $result->get_error_data(), strlen( $to ) ) );
  1252 				$result = new WP_Error(
       
  1253 					$result->get_error_code() . '_retry',
       
  1254 					$result->get_error_message(),
       
  1255 					substr( $result->get_error_data(), strlen( $to ) )
       
  1256 				);
  1159 			}
  1257 			}
  1160 		}
  1258 		}
  1161 	}
  1259 	}
  1162 
  1260 
  1163 	// Custom content directory needs updating now.
  1261 	// Custom content directory needs updating now.
  1164 	// Copy languages.
  1262 	// Copy languages.
  1165 	if ( ! is_wp_error( $result ) && $wp_filesystem->is_dir( $from . $distro . 'wp-content/languages' ) ) {
  1263 	if ( ! is_wp_error( $result ) && $wp_filesystem->is_dir( $from . $distro . 'wp-content/languages' ) ) {
  1166 		if ( WP_LANG_DIR != ABSPATH . WPINC . '/languages' || @is_dir( WP_LANG_DIR ) ) {
  1264 		if ( WP_LANG_DIR !== ABSPATH . WPINC . '/languages' || @is_dir( WP_LANG_DIR ) ) {
  1167 			$lang_dir = WP_LANG_DIR;
  1265 			$lang_dir = WP_LANG_DIR;
  1168 		} else {
  1266 		} else {
  1169 			$lang_dir = WP_CONTENT_DIR . '/languages';
  1267 			$lang_dir = WP_CONTENT_DIR . '/languages';
  1170 		}
  1268 		}
  1171 
  1269 
  1176 			clearstatcache(); // For FTP, need to clear the stat cache.
  1274 			clearstatcache(); // For FTP, need to clear the stat cache.
  1177 		}
  1275 		}
  1178 
  1276 
  1179 		if ( @is_dir( $lang_dir ) ) {
  1277 		if ( @is_dir( $lang_dir ) ) {
  1180 			$wp_lang_dir = $wp_filesystem->find_folder( $lang_dir );
  1278 			$wp_lang_dir = $wp_filesystem->find_folder( $lang_dir );
       
  1279 
  1181 			if ( $wp_lang_dir ) {
  1280 			if ( $wp_lang_dir ) {
  1182 				$result = copy_dir( $from . $distro . 'wp-content/languages/', $wp_lang_dir );
  1281 				$result = copy_dir( $from . $distro . 'wp-content/languages/', $wp_lang_dir );
       
  1282 
  1183 				if ( is_wp_error( $result ) ) {
  1283 				if ( is_wp_error( $result ) ) {
  1184 					$result = new WP_Error( $result->get_error_code() . '_languages', $result->get_error_message(), substr( $result->get_error_data(), strlen( $wp_lang_dir ) ) );
  1284 					$result = new WP_Error(
       
  1285 						$result->get_error_code() . '_languages',
       
  1286 						$result->get_error_message(),
       
  1287 						substr( $result->get_error_data(), strlen( $wp_lang_dir ) )
       
  1288 					);
  1185 				}
  1289 				}
  1186 			}
  1290 			}
  1187 		}
  1291 		}
  1188 	}
  1292 	}
  1189 
  1293 
  1190 	/** This filter is documented in wp-admin/includes/update-core.php */
  1294 	/** This filter is documented in wp-admin/includes/update-core.php */
  1191 	apply_filters( 'update_feedback', __( 'Disabling Maintenance mode&#8230;' ) );
  1295 	apply_filters( 'update_feedback', __( 'Disabling Maintenance mode&#8230;' ) );
       
  1296 
  1192 	// Remove maintenance file, we're done with potential site-breaking changes.
  1297 	// Remove maintenance file, we're done with potential site-breaking changes.
  1193 	$wp_filesystem->delete( $maintenance_file );
  1298 	$wp_filesystem->delete( $maintenance_file );
  1194 
  1299 
  1195 	// 3.5 -> 3.5+ - an empty twentytwelve directory was created upon upgrade to 3.5 for some users, preventing installation of Twenty Twelve.
  1300 	// 3.5 -> 3.5+ - an empty twentytwelve directory was created upon upgrade to 3.5 for some users,
       
  1301 	// preventing installation of Twenty Twelve.
  1196 	if ( '3.5' === $old_wp_version ) {
  1302 	if ( '3.5' === $old_wp_version ) {
  1197 		if ( is_dir( WP_CONTENT_DIR . '/themes/twentytwelve' ) && ! file_exists( WP_CONTENT_DIR . '/themes/twentytwelve/style.css' ) ) {
  1303 		if ( is_dir( WP_CONTENT_DIR . '/themes/twentytwelve' )
       
  1304 			&& ! file_exists( WP_CONTENT_DIR . '/themes/twentytwelve/style.css' )
       
  1305 		) {
  1198 			$wp_filesystem->delete( $wp_filesystem->wp_themes_dir() . 'twentytwelve/' );
  1306 			$wp_filesystem->delete( $wp_filesystem->wp_themes_dir() . 'twentytwelve/' );
  1199 		}
  1307 		}
  1200 	}
  1308 	}
  1201 
  1309 
  1202 	/*
  1310 	/*
  1203 	 * Copy new bundled plugins & themes.
  1311 	 * Copy new bundled plugins & themes.
  1204 	 * This gives us the ability to install new plugins & themes bundled with
  1312 	 * This gives us the ability to install new plugins & themes bundled with
  1205 	 * future versions of WordPress whilst avoiding the re-install upon upgrade issue.
  1313 	 * future versions of WordPress whilst avoiding the re-install upon upgrade issue.
  1206 	 * $development_build controls us overwriting bundled themes and plugins when a non-stable release is being updated.
  1314 	 * $development_build controls us overwriting bundled themes and plugins when a non-stable release is being updated.
  1207 	 */
  1315 	 */
  1208 	if ( ! is_wp_error( $result ) && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) ) {
  1316 	if ( ! is_wp_error( $result )
       
  1317 		&& ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED )
       
  1318 	) {
  1209 		foreach ( (array) $_new_bundled_files as $file => $introduced_version ) {
  1319 		foreach ( (array) $_new_bundled_files as $file => $introduced_version ) {
  1210 			// If a $development_build or if $introduced version is greater than what the site was previously running.
  1320 			// If a $development_build or if $introduced version is greater than what the site was previously running.
  1211 			if ( $development_build || version_compare( $introduced_version, $old_wp_version, '>' ) ) {
  1321 			if ( $development_build || version_compare( $introduced_version, $old_wp_version, '>' ) ) {
  1212 				$directory = ( '/' === $file[ strlen( $file ) - 1 ] );
  1322 				$directory = ( '/' === $file[ strlen( $file ) - 1 ] );
  1213 
  1323 
  1246 					// If a error occurs partway through this final step, keep the error flowing through, but keep process going.
  1356 					// If a error occurs partway through this final step, keep the error flowing through, but keep process going.
  1247 					if ( is_wp_error( $_result ) ) {
  1357 					if ( is_wp_error( $_result ) ) {
  1248 						if ( ! is_wp_error( $result ) ) {
  1358 						if ( ! is_wp_error( $result ) ) {
  1249 							$result = new WP_Error;
  1359 							$result = new WP_Error;
  1250 						}
  1360 						}
  1251 						$result->add( $_result->get_error_code() . "_$type", $_result->get_error_message(), substr( $_result->get_error_data(), strlen( $dest ) ) );
  1361 
       
  1362 						$result->add(
       
  1363 							$_result->get_error_code() . "_$type",
       
  1364 							$_result->get_error_message(),
       
  1365 							substr( $_result->get_error_data(), strlen( $dest ) )
       
  1366 						);
  1252 					}
  1367 					}
  1253 				}
  1368 				}
  1254 			}
  1369 			}
  1255 		} // End foreach.
  1370 		} // End foreach.
  1256 	}
  1371 	}
  1257 
  1372 
  1258 	// Handle $result error from the above blocks.
  1373 	// Handle $result error from the above blocks.
  1259 	if ( is_wp_error( $result ) ) {
  1374 	if ( is_wp_error( $result ) ) {
  1260 		$wp_filesystem->delete( $from, true );
  1375 		$wp_filesystem->delete( $from, true );
       
  1376 
  1261 		return $result;
  1377 		return $result;
  1262 	}
  1378 	}
  1263 
  1379 
  1264 	// Remove old files.
  1380 	// Remove old files.
  1265 	foreach ( $_old_files as $old_file ) {
  1381 	foreach ( $_old_files as $old_file ) {
  1266 		$old_file = $to . $old_file;
  1382 		$old_file = $to . $old_file;
       
  1383 
  1267 		if ( ! $wp_filesystem->exists( $old_file ) ) {
  1384 		if ( ! $wp_filesystem->exists( $old_file ) ) {
  1268 			continue;
  1385 			continue;
  1269 		}
  1386 		}
  1270 
  1387 
  1271 		// If the file isn't deleted, try writing an empty string to the file instead.
  1388 		// If the file isn't deleted, try writing an empty string to the file instead.
  1275 	}
  1392 	}
  1276 
  1393 
  1277 	// Remove any Genericons example.html's from the filesystem.
  1394 	// Remove any Genericons example.html's from the filesystem.
  1278 	_upgrade_422_remove_genericons();
  1395 	_upgrade_422_remove_genericons();
  1279 
  1396 
  1280 	// Remove the REST API plugin if its version is Beta 4 or lower.
  1397 	// Deactivate the REST API plugin if its version is 2.0 Beta 4 or lower.
  1281 	_upgrade_440_force_deactivate_incompatible_plugins();
  1398 	_upgrade_440_force_deactivate_incompatible_plugins();
       
  1399 
       
  1400 	// Deactivate the Gutenberg plugin if its version is 10.7 or lower.
       
  1401 	_upgrade_580_force_deactivate_incompatible_plugins();
  1282 
  1402 
  1283 	// Upgrade DB with separate request.
  1403 	// Upgrade DB with separate request.
  1284 	/** This filter is documented in wp-admin/includes/update-core.php */
  1404 	/** This filter is documented in wp-admin/includes/update-core.php */
  1285 	apply_filters( 'update_feedback', __( 'Upgrading database&#8230;' ) );
  1405 	apply_filters( 'update_feedback', __( 'Upgrading database&#8230;' ) );
       
  1406 
  1286 	$db_upgrade_url = admin_url( 'upgrade.php?step=upgrade_db' );
  1407 	$db_upgrade_url = admin_url( 'upgrade.php?step=upgrade_db' );
  1287 	wp_remote_post( $db_upgrade_url, array( 'timeout' => 60 ) );
  1408 	wp_remote_post( $db_upgrade_url, array( 'timeout' => 60 ) );
  1288 
  1409 
  1289 	// Clear the cache to prevent an update_option() from saving a stale db_version to the cache.
  1410 	// Clear the cache to prevent an update_option() from saving a stale db_version to the cache.
  1290 	wp_cache_flush();
  1411 	wp_cache_flush();
  1346 function _copy_dir( $from, $to, $skip_list = array() ) {
  1467 function _copy_dir( $from, $to, $skip_list = array() ) {
  1347 	global $wp_filesystem;
  1468 	global $wp_filesystem;
  1348 
  1469 
  1349 	$dirlist = $wp_filesystem->dirlist( $from );
  1470 	$dirlist = $wp_filesystem->dirlist( $from );
  1350 
  1471 
       
  1472 	if ( false === $dirlist ) {
       
  1473 		return new WP_Error( 'dirlist_failed__copy_dir', __( 'Directory listing failed.' ), basename( $to ) );
       
  1474 	}
       
  1475 
  1351 	$from = trailingslashit( $from );
  1476 	$from = trailingslashit( $from );
  1352 	$to   = trailingslashit( $to );
  1477 	$to   = trailingslashit( $to );
  1353 
  1478 
  1354 	foreach ( (array) $dirlist as $filename => $fileinfo ) {
  1479 	foreach ( (array) $dirlist as $filename => $fileinfo ) {
  1355 		if ( in_array( $filename, $skip_list, true ) ) {
  1480 		if ( in_array( $filename, $skip_list, true ) ) {
  1358 
  1483 
  1359 		if ( 'f' === $fileinfo['type'] ) {
  1484 		if ( 'f' === $fileinfo['type'] ) {
  1360 			if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
  1485 			if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
  1361 				// If copy failed, chmod file to 0644 and try again.
  1486 				// If copy failed, chmod file to 0644 and try again.
  1362 				$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
  1487 				$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
       
  1488 
  1363 				if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
  1489 				if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
  1364 					return new WP_Error( 'copy_failed__copy_dir', __( 'Could not copy file.' ), $to . $filename );
  1490 					return new WP_Error( 'copy_failed__copy_dir', __( 'Could not copy file.' ), $to . $filename );
  1365 				}
  1491 				}
  1366 			}
  1492 			}
  1367 
  1493 
  1368 			// `wp_opcache_invalidate()` only exists in WordPress 5.5, so don't run it when upgrading to 5.5.
  1494 			/*
       
  1495 			 * `wp_opcache_invalidate()` only exists in WordPress 5.5 or later,
       
  1496 			 * so don't run it when upgrading from older versions.
       
  1497 			 */
  1369 			if ( function_exists( 'wp_opcache_invalidate' ) ) {
  1498 			if ( function_exists( 'wp_opcache_invalidate' ) ) {
  1370 				wp_opcache_invalidate( $to . $filename );
  1499 				wp_opcache_invalidate( $to . $filename );
  1371 			}
  1500 			}
  1372 		} elseif ( 'd' === $fileinfo['type'] ) {
  1501 		} elseif ( 'd' === $fileinfo['type'] ) {
  1373 			if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
  1502 			if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
  1379 			/*
  1508 			/*
  1380 			 * Generate the $sub_skip_list for the subdirectory as a sub-set
  1509 			 * Generate the $sub_skip_list for the subdirectory as a sub-set
  1381 			 * of the existing $skip_list.
  1510 			 * of the existing $skip_list.
  1382 			 */
  1511 			 */
  1383 			$sub_skip_list = array();
  1512 			$sub_skip_list = array();
       
  1513 
  1384 			foreach ( $skip_list as $skip_item ) {
  1514 			foreach ( $skip_list as $skip_item ) {
  1385 				if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
  1515 				if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
  1386 					$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
  1516 					$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
  1387 				}
  1517 				}
  1388 			}
  1518 			}
  1389 
  1519 
  1390 			$result = _copy_dir( $from . $filename, $to . $filename, $sub_skip_list );
  1520 			$result = _copy_dir( $from . $filename, $to . $filename, $sub_skip_list );
       
  1521 
  1391 			if ( is_wp_error( $result ) ) {
  1522 			if ( is_wp_error( $result ) ) {
  1392 				return $result;
  1523 				return $result;
  1393 			}
  1524 			}
  1394 		}
  1525 		}
  1395 	}
  1526 	}
       
  1527 
  1396 	return true;
  1528 	return true;
  1397 }
  1529 }
  1398 
  1530 
  1399 /**
  1531 /**
  1400  * Redirect to the About WordPress page after a successful upgrade.
  1532  * Redirect to the About WordPress page after a successful upgrade.
  1484 	$affected_plugin_files = _upgrade_422_find_genericons_files_in_folder( WP_PLUGIN_DIR );
  1616 	$affected_plugin_files = _upgrade_422_find_genericons_files_in_folder( WP_PLUGIN_DIR );
  1485 	$affected_files        = array_merge( $affected_files, $affected_plugin_files );
  1617 	$affected_files        = array_merge( $affected_files, $affected_plugin_files );
  1486 
  1618 
  1487 	foreach ( $affected_files as $file ) {
  1619 	foreach ( $affected_files as $file ) {
  1488 		$gen_dir = $wp_filesystem->find_folder( trailingslashit( dirname( $file ) ) );
  1620 		$gen_dir = $wp_filesystem->find_folder( trailingslashit( dirname( $file ) ) );
       
  1621 
  1489 		if ( empty( $gen_dir ) ) {
  1622 		if ( empty( $gen_dir ) ) {
  1490 			continue;
  1623 			continue;
  1491 		}
  1624 		}
  1492 
  1625 
  1493 		// The path when the file is accessed via WP_Filesystem may differ in the case of FTP.
  1626 		// The path when the file is accessed via WP_Filesystem may differ in the case of FTP.
  1514  */
  1647  */
  1515 function _upgrade_422_find_genericons_files_in_folder( $directory ) {
  1648 function _upgrade_422_find_genericons_files_in_folder( $directory ) {
  1516 	$directory = trailingslashit( $directory );
  1649 	$directory = trailingslashit( $directory );
  1517 	$files     = array();
  1650 	$files     = array();
  1518 
  1651 
  1519 	if ( file_exists( "{$directory}example.html" ) && false !== strpos( file_get_contents( "{$directory}example.html" ), '<title>Genericons</title>' ) ) {
  1652 	if ( file_exists( "{$directory}example.html" )
       
  1653 		&& false !== strpos( file_get_contents( "{$directory}example.html" ), '<title>Genericons</title>' )
       
  1654 	) {
  1520 		$files[] = "{$directory}example.html";
  1655 		$files[] = "{$directory}example.html";
  1521 	}
  1656 	}
  1522 
  1657 
  1523 	$dirs = glob( $directory . '*', GLOB_ONLYDIR );
  1658 	$dirs = glob( $directory . '*', GLOB_ONLYDIR );
       
  1659 
  1524 	if ( $dirs ) {
  1660 	if ( $dirs ) {
  1525 		foreach ( $dirs as $dir ) {
  1661 		foreach ( $dirs as $dir ) {
  1526 			$files = array_merge( $files, _upgrade_422_find_genericons_files_in_folder( $dir ) );
  1662 			$files = array_merge( $files, _upgrade_422_find_genericons_files_in_folder( $dir ) );
  1527 		}
  1663 		}
  1528 	}
  1664 	}
  1537 function _upgrade_440_force_deactivate_incompatible_plugins() {
  1673 function _upgrade_440_force_deactivate_incompatible_plugins() {
  1538 	if ( defined( 'REST_API_VERSION' ) && version_compare( REST_API_VERSION, '2.0-beta4', '<=' ) ) {
  1674 	if ( defined( 'REST_API_VERSION' ) && version_compare( REST_API_VERSION, '2.0-beta4', '<=' ) ) {
  1539 		deactivate_plugins( array( 'rest-api/plugin.php' ), true );
  1675 		deactivate_plugins( array( 'rest-api/plugin.php' ), true );
  1540 	}
  1676 	}
  1541 }
  1677 }
       
  1678 
       
  1679 /**
       
  1680  * @ignore
       
  1681  * @since 5.8.0
       
  1682  */
       
  1683 function _upgrade_580_force_deactivate_incompatible_plugins() {
       
  1684 	if ( defined( 'GUTENBERG_VERSION' ) && version_compare( GUTENBERG_VERSION, '10.7', '<=' ) ) {
       
  1685 		$deactivated_gutenberg['gutenberg'] = array(
       
  1686 			'plugin_name'         => 'Gutenberg',
       
  1687 			'version_deactivated' => GUTENBERG_VERSION,
       
  1688 			'version_compatible'  => '10.8',
       
  1689 		);
       
  1690 		if ( is_plugin_active_for_network( 'gutenberg/gutenberg.php' ) ) {
       
  1691 			$deactivated_plugins = get_site_option( 'wp_force_deactivated_plugins', array() );
       
  1692 			$deactivated_plugins = array_merge( $deactivated_plugins, $deactivated_gutenberg );
       
  1693 			update_site_option( 'wp_force_deactivated_plugins', $deactivated_plugins );
       
  1694 		} else {
       
  1695 			$deactivated_plugins = get_option( 'wp_force_deactivated_plugins', array() );
       
  1696 			$deactivated_plugins = array_merge( $deactivated_plugins, $deactivated_gutenberg );
       
  1697 			update_option( 'wp_force_deactivated_plugins', $deactivated_plugins );
       
  1698 		}
       
  1699 		deactivate_plugins( array( 'gutenberg/gutenberg.php' ), true );
       
  1700 	}
       
  1701 }