update_core

Upgrades the core of WordPress.

Description

(WP_Error|null) update_core( (string) $from, (string) $to ); 

This will create a .maintenance file at the base of the WordPress directory to ensure that people can not access the web site, when the files are being copied to their locations.

The files in the $_old_files list will be removed and the new files copied from the zip file after the database is upgraded.

The files in the $_new_bundled_files list will be added to the installation if the version is greater than or equal to the old version being upgraded.

The steps for the upgrader for after the new release is downloaded and unzipped is: 1. Test unzipped location for select files to ensure that unzipped worked. 2. Create the .maintenance file in current WordPress base. 3. Copy new WordPress directory over old WordPress files. 4. Upgrade WordPress to new version. 5. Delete new WordPress directory path. 6. Delete .maintenance file. 7. Remove old files. 8. Delete update_core option.

There are several areas of failure. For instance if PHP times out before step 6, then you will not be able to access any portion of your site. Also, since the upgrade will not continue where it left off, you will not be able to automatically remove old files and remove the update_core option. This isn't that bad.

If the copy of the new WordPress over the old fails, then the worse is that the new WordPress directory will remain.

If it is assumed that every file will be copied over, including plugins and themes, then if you edit the default theme, you should rename it, so that your changes remain.

Returns (WP_Error|null)

WP_Error on failure, null on success.

Parameters (2)

0. $from (string)
New release unzipped path.
1. $to (string)
Path to old WordPress installation.

Usage

  1. if ( !function_exists( 'update_core' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/update-core.php'; 
  3.  
  4. // New release unzipped path. 
  5. $from = ''; 
  6.  
  7. // Path to old WordPress installation. 
  8. $to = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = update_core($from, $to); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/update-core.php  
  1. function update_core($from, $to) { 
  2. global $wp_filesystem, $_old_files, $_new_bundled_files, $wpdb; 
  3.  
  4. @set_time_limit( 300 ); 
  5.  
  6. /** 
  7. * Filters feedback messages displayed during the core update process. 
  8. * The filter is first evaluated after the zip file for the latest version 
  9. * has been downloaded and unzipped. It is evaluated five more times during 
  10. * the process: 
  11. * 1. Before WordPress begins the core upgrade process. 
  12. * 2. Before Maintenance Mode is enabled. 
  13. * 3. Before WordPress begins copying over the necessary files. 
  14. * 4. Before Maintenance Mode is disabled. 
  15. * 5. Before the database is upgraded. 
  16. * @since 2.5.0 
  17. * @param string $feedback The core update feedback messages. 
  18. */ 
  19. apply_filters( 'update_feedback', __( 'Verifying the unpacked files…' ) ); 
  20.  
  21. // Sanity check the unzipped distribution. 
  22. $distro = ''; 
  23. $roots = array( '/wordpress/', '/wordpress-mu/' ); 
  24. foreach ( $roots as $root ) { 
  25. if ( $wp_filesystem->exists( $from . $root . 'readme.html' ) && $wp_filesystem->exists( $from . $root . 'wp-includes/version.php' ) ) { 
  26. $distro = $root; 
  27. break; 
  28. if ( ! $distro ) { 
  29. $wp_filesystem->delete( $from, true ); 
  30. return new WP_Error( 'insane_distro', __('The update could not be unpacked') ); 
  31.  
  32.  
  33. /** 
  34. * Import $wp_version, $required_php_version, and $required_mysql_version from the new version 
  35. * $wp_filesystem->wp_content_dir() returned unslashed pre-2.8 
  36. * @global string $wp_version 
  37. * @global string $required_php_version 
  38. * @global string $required_mysql_version 
  39. */ 
  40. global $wp_version, $required_php_version, $required_mysql_version; 
  41.  
  42. $versions_file = trailingslashit( $wp_filesystem->wp_content_dir() ) . 'upgrade/version-current.php'; 
  43. if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $versions_file ) ) { 
  44. $wp_filesystem->delete( $from, true ); 
  45. 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' ); 
  46.  
  47. $wp_filesystem->chmod( $versions_file, FS_CHMOD_FILE ); 
  48. require( WP_CONTENT_DIR . '/upgrade/version-current.php' ); 
  49. $wp_filesystem->delete( $versions_file ); 
  50.  
  51. $php_version = phpversion(); 
  52. $mysql_version = $wpdb->db_version(); 
  53. $old_wp_version = $wp_version; // The version of WordPress we're updating from 
  54. $development_build = ( false !== strpos( $old_wp_version . $wp_version, '-' ) ); // a dash in the version indicates a Development release 
  55. $php_compat = version_compare( $php_version, $required_php_version, '>=' ); 
  56. if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) 
  57. $mysql_compat = true; 
  58. else 
  59. $mysql_compat = version_compare( $mysql_version, $required_mysql_version, '>=' ); 
  60.  
  61. if ( !$mysql_compat || !$php_compat ) 
  62. $wp_filesystem->delete($from, true); 
  63.  
  64. if ( !$mysql_compat && !$php_compat ) 
  65. return new WP_Error( 'php_mysql_not_compatible', sprintf( __('The update cannot be installed because WordPress %1$s requires PHP version %2$s or higher and MySQL version %3$s or higher. You are running PHP version %4$s and MySQL version %5$s.'), $wp_version, $required_php_version, $required_mysql_version, $php_version, $mysql_version ) ); 
  66. elseif ( !$php_compat ) 
  67. return new WP_Error( 'php_not_compatible', sprintf( __('The update cannot be installed because WordPress %1$s requires PHP version %2$s or higher. You are running version %3$s.'), $wp_version, $required_php_version, $php_version ) ); 
  68. elseif ( !$mysql_compat ) 
  69. return new WP_Error( 'mysql_not_compatible', sprintf( __('The update cannot be installed because WordPress %1$s requires MySQL version %2$s or higher. You are running version %3$s.'), $wp_version, $required_mysql_version, $mysql_version ) ); 
  70.  
  71. /** This filter is documented in wp-admin/includes/update-core.php */ 
  72. apply_filters( 'update_feedback', __( 'Preparing to install the latest version…' ) ); 
  73.  
  74. // Don't copy wp-content, we'll deal with that below 
  75. // We also copy version.php last so failed updates report their old version 
  76. $skip = array( 'wp-content', 'wp-includes/version.php' ); 
  77. $check_is_writable = array(); 
  78.  
  79. // Check to see which files don't really need updating - only available for 3.7 and higher 
  80. if ( function_exists( 'get_core_checksums' ) ) { 
  81. // Find the local version of the working directory 
  82. $working_dir_local = WP_CONTENT_DIR . '/upgrade/' . basename( $from ) . $distro; 
  83.  
  84. $checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' ); 
  85. if ( is_array( $checksums ) && isset( $checksums[ $wp_version ] ) ) 
  86. $checksums = $checksums[ $wp_version ]; // Compat code for 3.7-beta2 
  87. if ( is_array( $checksums ) ) { 
  88. foreach ( $checksums as $file => $checksum ) { 
  89. if ( 'wp-content' == substr( $file, 0, 10 ) ) 
  90. continue; 
  91. if ( ! file_exists( ABSPATH . $file ) ) 
  92. continue; 
  93. if ( ! file_exists( $working_dir_local . $file ) ) 
  94. continue; 
  95. if ( '.' === dirname( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ) ) ) 
  96. continue; 
  97. if ( md5_file( ABSPATH . $file ) === $checksum ) 
  98. $skip[] = $file; 
  99. else 
  100. $check_is_writable[ $file ] = ABSPATH . $file; 
  101.  
  102. // If we're using the direct method, we can predict write failures that are due to permissions. 
  103. if ( $check_is_writable && 'direct' === $wp_filesystem->method ) { 
  104. $files_writable = array_filter( $check_is_writable, array( $wp_filesystem, 'is_writable' ) ); 
  105. if ( $files_writable !== $check_is_writable ) { 
  106. $files_not_writable = array_diff_key( $check_is_writable, $files_writable ); 
  107. foreach ( $files_not_writable as $relative_file_not_writable => $file_not_writable ) { 
  108. // If the writable check failed, chmod file to 0644 and try again, same as copy_dir(). 
  109. $wp_filesystem->chmod( $file_not_writable, FS_CHMOD_FILE ); 
  110. if ( $wp_filesystem->is_writable( $file_not_writable ) ) 
  111. unset( $files_not_writable[ $relative_file_not_writable ] ); 
  112.  
  113. // Store package-relative paths (the key) of non-writable files in the WP_Error object. 
  114. $error_data = version_compare( $old_wp_version, '3.7-beta2', '>' ) ? array_keys( $files_not_writable ) : ''; 
  115.  
  116. if ( $files_not_writable ) 
  117. 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 ) ); 
  118.  
  119. /** This filter is documented in wp-admin/includes/update-core.php */ 
  120. apply_filters( 'update_feedback', __( 'Enabling Maintenance mode…' ) ); 
  121. // Create maintenance file to signal that we are upgrading 
  122. $maintenance_string = '<?php $upgrading = ' . time() . '; ?>'; 
  123. $maintenance_file = $to . '.maintenance'; 
  124. $wp_filesystem->delete($maintenance_file); 
  125. $wp_filesystem->put_contents($maintenance_file, $maintenance_string, FS_CHMOD_FILE); 
  126.  
  127. /** This filter is documented in wp-admin/includes/update-core.php */ 
  128. apply_filters( 'update_feedback', __( 'Copying the required files…' ) ); 
  129. // Copy new versions of WP files into place. 
  130. $result = _copy_dir( $from . $distro, $to, $skip ); 
  131. if ( is_wp_error( $result ) ) 
  132. $result = new WP_Error( $result->get_error_code(), $result->get_error_message(), substr( $result->get_error_data(), strlen( $to ) ) ); 
  133.  
  134. // Since we know the core files have copied over, we can now copy the version file 
  135. if ( ! is_wp_error( $result ) ) { 
  136. if ( ! $wp_filesystem->copy( $from . $distro . 'wp-includes/version.php', $to . 'wp-includes/version.php', true /** overwrite */ ) ) { 
  137. $wp_filesystem->delete( $from, true ); 
  138. $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' ); 
  139. $wp_filesystem->chmod( $to . 'wp-includes/version.php', FS_CHMOD_FILE ); 
  140.  
  141. // Check to make sure everything copied correctly, ignoring the contents of wp-content 
  142. $skip = array( 'wp-content' ); 
  143. $failed = array(); 
  144. if ( isset( $checksums ) && is_array( $checksums ) ) { 
  145. foreach ( $checksums as $file => $checksum ) { 
  146. if ( 'wp-content' == substr( $file, 0, 10 ) ) 
  147. continue; 
  148. if ( ! file_exists( $working_dir_local . $file ) ) 
  149. continue; 
  150. if ( '.' === dirname( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), array( 'html', 'txt' ) ) ) { 
  151. $skip[] = $file; 
  152. continue; 
  153. if ( file_exists( ABSPATH . $file ) && md5_file( ABSPATH . $file ) == $checksum ) 
  154. $skip[] = $file; 
  155. else 
  156. $failed[] = $file; 
  157.  
  158. // Some files didn't copy properly 
  159. if ( ! empty( $failed ) ) { 
  160. $total_size = 0; 
  161. foreach ( $failed as $file ) { 
  162. if ( file_exists( $working_dir_local . $file ) ) 
  163. $total_size += filesize( $working_dir_local . $file ); 
  164.  
  165. // If we don't have enough free space, it isn't worth trying again. 
  166. // Unlikely to be hit due to the check in unzip_file(). 
  167. $available_space = @disk_free_space( ABSPATH ); 
  168. if ( $available_space && $total_size >= $available_space ) { 
  169. $result = new WP_Error( 'disk_full', __( 'There is not enough free disk space to complete the update.' ) ); 
  170. } else { 
  171. $result = _copy_dir( $from . $distro, $to, $skip ); 
  172. if ( is_wp_error( $result ) ) 
  173. $result = new WP_Error( $result->get_error_code() . '_retry', $result->get_error_message(), substr( $result->get_error_data(), strlen( $to ) ) ); 
  174.  
  175. // Custom Content Directory needs updating now. 
  176. // Copy Languages 
  177. if ( !is_wp_error($result) && $wp_filesystem->is_dir($from . $distro . 'wp-content/languages') ) { 
  178. if ( WP_LANG_DIR != ABSPATH . WPINC . '/languages' || @is_dir(WP_LANG_DIR) ) 
  179. $lang_dir = WP_LANG_DIR
  180. else 
  181. $lang_dir = WP_CONTENT_DIR . '/languages'; 
  182.  
  183. if ( !@is_dir($lang_dir) && 0 === strpos($lang_dir, ABSPATH) ) { // Check the language directory exists first 
  184. $wp_filesystem->mkdir($to . str_replace(ABSPATH, '', $lang_dir), FS_CHMOD_DIR); // If it's within the ABSPATH we can handle it here, otherwise they're out of luck. 
  185. clearstatcache(); // for FTP, Need to clear the stat cache 
  186.  
  187. if ( @is_dir($lang_dir) ) { 
  188. $wp_lang_dir = $wp_filesystem->find_folder($lang_dir); 
  189. if ( $wp_lang_dir ) { 
  190. $result = copy_dir($from . $distro . 'wp-content/languages/', $wp_lang_dir); 
  191. if ( is_wp_error( $result ) ) 
  192. $result = new WP_Error( $result->get_error_code() . '_languages', $result->get_error_message(), substr( $result->get_error_data(), strlen( $wp_lang_dir ) ) ); 
  193.  
  194. /** This filter is documented in wp-admin/includes/update-core.php */ 
  195. apply_filters( 'update_feedback', __( 'Disabling Maintenance mode…' ) ); 
  196. // Remove maintenance file, we're done with potential site-breaking changes 
  197. $wp_filesystem->delete( $maintenance_file ); 
  198.  
  199. // 3.5 -> 3.5+ - an empty twentytwelve directory was created upon upgrade to 3.5 for some users, preventing installation of Twenty Twelve. 
  200. if ( '3.5' == $old_wp_version ) { 
  201. if ( is_dir( WP_CONTENT_DIR . '/themes/twentytwelve' ) && ! file_exists( WP_CONTENT_DIR . '/themes/twentytwelve/style.css' ) ) { 
  202. $wp_filesystem->delete( $wp_filesystem->wp_themes_dir() . 'twentytwelve/' ); 
  203.  
  204. // Copy New bundled plugins & themes 
  205. // This gives us the ability to install new plugins & themes bundled with future versions of WordPress whilst avoiding the re-install upon upgrade issue. 
  206. // $development_build controls us overwriting bundled themes and plugins when a non-stable release is being updated 
  207. if ( !is_wp_error($result) && ( ! defined('CORE_UPGRADE_SKIP_NEW_BUNDLED') || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) ) { 
  208. foreach ( (array) $_new_bundled_files as $file => $introduced_version ) { 
  209. // If a $development_build or if $introduced version is greater than what the site was previously running 
  210. if ( $development_build || version_compare( $introduced_version, $old_wp_version, '>' ) ) { 
  211. $directory = ('/' == $file[ strlen($file)-1 ]); 
  212. list($type, $filename) = explode('/', $file, 2); 
  213.  
  214. // Check to see if the bundled items exist before attempting to copy them 
  215. if ( ! $wp_filesystem->exists( $from . $distro . 'wp-content/' . $file ) ) 
  216. continue; 
  217.  
  218. if ( 'plugins' == $type ) 
  219. $dest = $wp_filesystem->wp_plugins_dir(); 
  220. elseif ( 'themes' == $type ) 
  221. $dest = trailingslashit($wp_filesystem->wp_themes_dir()); // Back-compat, ::wp_themes_dir() did not return trailingslash'd pre-3.2 
  222. else 
  223. continue; 
  224.  
  225. if ( ! $directory ) { 
  226. if ( ! $development_build && $wp_filesystem->exists( $dest . $filename ) ) 
  227. continue; 
  228.  
  229. if ( ! $wp_filesystem->copy($from . $distro . 'wp-content/' . $file, $dest . $filename, FS_CHMOD_FILE) ) 
  230. $result = new WP_Error( "copy_failed_for_new_bundled_$type", __( 'Could not copy file.' ), $dest . $filename ); 
  231. } else { 
  232. if ( ! $development_build && $wp_filesystem->is_dir( $dest . $filename ) ) 
  233. continue; 
  234.  
  235. $wp_filesystem->mkdir($dest . $filename, FS_CHMOD_DIR); 
  236. $_result = copy_dir( $from . $distro . 'wp-content/' . $file, $dest . $filename); 
  237.  
  238. // If aerroroccurs partway through this final step, keep theerrorflowing through, but keep process going. 
  239. if ( is_wp_error( $_result ) ) { 
  240. if ( ! is_wp_error( $result ) ) 
  241. $result = new WP_Error; 
  242. $result->add( $_result->get_error_code() . "_$type", $_result->get_error_message(), substr( $_result->get_error_data(), strlen( $dest ) ) ); 
  243. } //end foreach 
  244.  
  245. // Handle $resulterrorfrom the above blocks 
  246. if ( is_wp_error($result) ) { 
  247. $wp_filesystem->delete($from, true); 
  248. return $result; 
  249.  
  250. // Remove old files 
  251. foreach ( $_old_files as $old_file ) { 
  252. $old_file = $to . $old_file; 
  253. if ( !$wp_filesystem->exists($old_file) ) 
  254. continue; 
  255. $wp_filesystem->delete($old_file, true); 
  256.  
  257. // Remove any Genericons example.html's from the filesystem 
  258.  
  259. // Remove the REST API plugin if its version is Beta 4 or lower 
  260.  
  261. // Upgrade DB with separate request 
  262. /** This filter is documented in wp-admin/includes/update-core.php */ 
  263. apply_filters( 'update_feedback', __( 'Upgrading database…' ) ); 
  264. $db_upgrade_url = admin_url('upgrade.php?step=upgrade_db'); 
  265. wp_remote_post($db_upgrade_url, array('timeout' => 60)); 
  266.  
  267. // Clear the cache to prevent an update_option() from saving a stale db_version to the cache 
  268. // (Not all cache back ends listen to 'flush') 
  269. wp_cache_delete( 'alloptions', 'options' ); 
  270.  
  271. // Remove working directory 
  272. $wp_filesystem->delete($from, true); 
  273.  
  274. // Force refresh of update information 
  275. if ( function_exists('delete_site_transient') ) 
  276. delete_site_transient('update_core'); 
  277. else 
  278. delete_option('update_core'); 
  279.  
  280. /** 
  281. * Fires after WordPress core has been successfully updated. 
  282. * @since 3.3.0 
  283. * @param string $wp_version The current WordPress version. 
  284. */ 
  285.  
  286. // Clear the option that blocks auto updates after failures, now that we've been successful. 
  287. if ( function_exists( 'delete_site_option' ) ) 
  288. delete_site_option( 'auto_core_update_failed' ); 
  289.  
  290. return $wp_version;