class-core-upgrader.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * Upgrade API: Core_Upgrader class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for updating core.
  11. *
  12. * It allows for WordPress to upgrade itself in combination with
  13. * the wp-admin/includes/update-core.php file.
  14. *
  15. * @since 2.8.0
  16. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  17. *
  18. * @see WP_Upgrader
  19. */
  20. class Core_Upgrader extends WP_Upgrader {
  21. /**
  22. * Initialize the upgrade strings.
  23. *
  24. * @since 2.8.0
  25. */
  26. public function upgrade_strings() {
  27. $this->strings['up_to_date'] = __('WordPress is at the latest version.');
  28. $this->strings['locked'] = __('Another update is currently in progress.');
  29. $this->strings['no_package'] = __('Update package not available.');
  30. /* translators: %s: package URL */
  31. $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
  32. $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
  33. $this->strings['copy_failed'] = __('Could not copy files.');
  34. $this->strings['copy_failed_space'] = __('Could not copy files. You may have run out of disk space.' );
  35. $this->strings['start_rollback'] = __( 'Attempting to roll back to previous version.' );
  36. $this->strings['rollback_was_required'] = __( 'Due to an error during updating, WordPress has rolled back to your previous version.' );
  37. }
  38. /**
  39. * Upgrade WordPress core.
  40. *
  41. * @since 2.8.0
  42. *
  43. * @global WP_Filesystem_Base $wp_filesystem Subclass
  44. * @global callable $_wp_filesystem_direct_method
  45. *
  46. * @param object $current Response object for whether WordPress is current.
  47. * @param array $args {
  48. * Optional. Arguments for upgrading WordPress core. Default empty array.
  49. *
  50. * @type bool $pre_check_md5 Whether to check the file checksums before
  51. * attempting the upgrade. Default true.
  52. * @type bool $attempt_rollback Whether to attempt to rollback the chances if
  53. * there is a problem. Default false.
  54. * @type bool $do_rollback Whether to perform this "upgrade" as a rollback.
  55. * Default false.
  56. * }
  57. * @return null|false|WP_Error False or WP_Error on failure, null on success.
  58. */
  59. public function upgrade( $current, $args = array() ) {
  60. global $wp_filesystem;
  61. include( ABSPATH . WPINC . '/version.php' ); // $wp_version;
  62. $start_time = time();
  63. $defaults = array(
  64. 'pre_check_md5' => true,
  65. 'attempt_rollback' => false,
  66. 'do_rollback' => false,
  67. 'allow_relaxed_file_ownership' => false,
  68. );
  69. $parsed_args = wp_parse_args( $args, $defaults );
  70. $this->init();
  71. $this->upgrade_strings();
  72. // Is an update available?
  73. if ( !isset( $current->response ) || $current->response == 'latest' )
  74. return new WP_Error('up_to_date', $this->strings['up_to_date']);
  75. $res = $this->fs_connect( array( ABSPATH, WP_CONTENT_DIR ), $parsed_args['allow_relaxed_file_ownership'] );
  76. if ( ! $res || is_wp_error( $res ) ) {
  77. return $res;
  78. }
  79. $wp_dir = trailingslashit($wp_filesystem->abspath());
  80. $partial = true;
  81. if ( $parsed_args['do_rollback'] )
  82. $partial = false;
  83. elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() )
  84. $partial = false;
  85. /*
  86. * If partial update is returned from the API, use that, unless we're doing
  87. * a reinstallation. If we cross the new_bundled version number, then use
  88. * the new_bundled zip. Don't though if the constant is set to skip bundled items.
  89. * If the API returns a no_content zip, go with it. Finally, default to the full zip.
  90. */
  91. if ( $parsed_args['do_rollback'] && $current->packages->rollback )
  92. $to_download = 'rollback';
  93. elseif ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version && $partial )
  94. $to_download = 'partial';
  95. elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' )
  96. && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) )
  97. $to_download = 'new_bundled';
  98. elseif ( $current->packages->no_content )
  99. $to_download = 'no_content';
  100. else
  101. $to_download = 'full';
  102. // Lock to prevent multiple Core Updates occurring
  103. $lock = WP_Upgrader::create_lock( 'core_updater', 15 * MINUTE_IN_SECONDS );
  104. if ( ! $lock ) {
  105. return new WP_Error( 'locked', $this->strings['locked'] );
  106. }
  107. $download = $this->download_package( $current->packages->$to_download );
  108. if ( is_wp_error( $download ) ) {
  109. WP_Upgrader::release_lock( 'core_updater' );
  110. return $download;
  111. }
  112. $working_dir = $this->unpack_package( $download );
  113. if ( is_wp_error( $working_dir ) ) {
  114. WP_Upgrader::release_lock( 'core_updater' );
  115. return $working_dir;
  116. }
  117. // Copy update-core.php from the new version into place.
  118. if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
  119. $wp_filesystem->delete($working_dir, true);
  120. WP_Upgrader::release_lock( 'core_updater' );
  121. return new WP_Error( 'copy_failed_for_update_core_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-admin/includes/update-core.php' );
  122. }
  123. $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
  124. require_once( ABSPATH . 'wp-admin/includes/update-core.php' );
  125. if ( ! function_exists( 'update_core' ) ) {
  126. WP_Upgrader::release_lock( 'core_updater' );
  127. return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] );
  128. }
  129. $result = update_core( $working_dir, $wp_dir );
  130. // In the event of an issue, we may be able to roll back.
  131. if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) {
  132. $try_rollback = false;
  133. if ( is_wp_error( $result ) ) {
  134. $error_code = $result->get_error_code();
  135. /*
  136. * Not all errors are equal. These codes are critical: copy_failed__copy_dir,
  137. * mkdir_failed__copy_dir, copy_failed__copy_dir_retry, and disk_full.
  138. * do_rollback allows for update_core() to trigger a rollback if needed.
  139. */
  140. if ( false !== strpos( $error_code, 'do_rollback' ) )
  141. $try_rollback = true;
  142. elseif ( false !== strpos( $error_code, '__copy_dir' ) )
  143. $try_rollback = true;
  144. elseif ( 'disk_full' === $error_code )
  145. $try_rollback = true;
  146. }
  147. if ( $try_rollback ) {
  148. /** This filter is documented in wp-admin/includes/update-core.php */
  149. apply_filters( 'update_feedback', $result );
  150. /** This filter is documented in wp-admin/includes/update-core.php */
  151. apply_filters( 'update_feedback', $this->strings['start_rollback'] );
  152. $rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) );
  153. $original_result = $result;
  154. $result = new WP_Error( 'rollback_was_required', $this->strings['rollback_was_required'], (object) array( 'update' => $original_result, 'rollback' => $rollback_result ) );
  155. }
  156. }
  157. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
  158. do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'core' ) );
  159. // Clear the current updates
  160. delete_site_transient( 'update_core' );
  161. if ( ! $parsed_args['do_rollback'] ) {
  162. $stats = array(
  163. 'update_type' => $current->response,
  164. 'success' => true,
  165. 'fs_method' => $wp_filesystem->method,
  166. 'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
  167. 'fs_method_direct' => !empty( $GLOBALS['_wp_filesystem_direct_method'] ) ? $GLOBALS['_wp_filesystem_direct_method'] : '',
  168. 'time_taken' => time() - $start_time,
  169. 'reported' => $wp_version,
  170. 'attempted' => $current->version,
  171. );
  172. if ( is_wp_error( $result ) ) {
  173. $stats['success'] = false;
  174. // Did a rollback occur?
  175. if ( ! empty( $try_rollback ) ) {
  176. $stats['error_code'] = $original_result->get_error_code();
  177. $stats['error_data'] = $original_result->get_error_data();
  178. // Was the rollback successful? If not, collect its error too.
  179. $stats['rollback'] = ! is_wp_error( $rollback_result );
  180. if ( is_wp_error( $rollback_result ) ) {
  181. $stats['rollback_code'] = $rollback_result->get_error_code();
  182. $stats['rollback_data'] = $rollback_result->get_error_data();
  183. }
  184. } else {
  185. $stats['error_code'] = $result->get_error_code();
  186. $stats['error_data'] = $result->get_error_data();
  187. }
  188. }
  189. wp_version_check( $stats );
  190. }
  191. WP_Upgrader::release_lock( 'core_updater' );
  192. return $result;
  193. }
  194. /**
  195. * Determines if this WordPress Core version should update to an offered version or not.
  196. *
  197. * @since 3.7.0
  198. *
  199. * @static
  200. *
  201. * @param string $offered_ver The offered version, of the format x.y.z.
  202. * @return bool True if we should update to the offered version, otherwise false.
  203. */
  204. public static function should_update_to_version( $offered_ver ) {
  205. include( ABSPATH . WPINC . '/version.php' ); // $wp_version; // x.y.z
  206. $current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y
  207. $new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
  208. $current_is_development_version = (bool) strpos( $wp_version, '-' );
  209. // Defaults:
  210. $upgrade_dev = true;
  211. $upgrade_minor = true;
  212. $upgrade_major = false;
  213. // WP_AUTO_UPDATE_CORE = true (all), 'minor', false.
  214. if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
  215. if ( false === WP_AUTO_UPDATE_CORE ) {
  216. // Defaults to turned off, unless a filter allows it
  217. $upgrade_dev = $upgrade_minor = $upgrade_major = false;
  218. } elseif ( true === WP_AUTO_UPDATE_CORE ) {
  219. // ALL updates for core
  220. $upgrade_dev = $upgrade_minor = $upgrade_major = true;
  221. } elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
  222. // Only minor updates for core
  223. $upgrade_dev = $upgrade_major = false;
  224. $upgrade_minor = true;
  225. }
  226. }
  227. // 1: If we're already on that version, not much point in updating?
  228. if ( $offered_ver == $wp_version )
  229. return false;
  230. // 2: If we're running a newer version, that's a nope
  231. if ( version_compare( $wp_version, $offered_ver, '>' ) )
  232. return false;
  233. $failure_data = get_site_option( 'auto_core_update_failed' );
  234. if ( $failure_data ) {
  235. // If this was a critical update failure, cannot update.
  236. if ( ! empty( $failure_data['critical'] ) )
  237. return false;
  238. // Don't claim we can update on update-core.php if we have a non-critical failure logged.
  239. if ( $wp_version == $failure_data['current'] && false !== strpos( $offered_ver, '.1.next.minor' ) )
  240. return false;
  241. // Cannot update if we're retrying the same A to B update that caused a non-critical failure.
  242. // Some non-critical failures do allow retries, like download_failed.
  243. // 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.
  244. if ( empty( $failure_data['retry'] ) && $wp_version == $failure_data['current'] && $offered_ver == $failure_data['attempted'] )
  245. return false;
  246. }
  247. // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2
  248. if ( $current_is_development_version ) {
  249. /**
  250. * Filters whether to enable automatic core updates for development versions.
  251. *
  252. * @since 3.7.0
  253. *
  254. * @param bool $upgrade_dev Whether to enable automatic updates for
  255. * development versions.
  256. */
  257. if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) )
  258. return false;
  259. // Else fall through to minor + major branches below.
  260. }
  261. // 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4)
  262. if ( $current_branch == $new_branch ) {
  263. /**
  264. * Filters whether to enable minor automatic core updates.
  265. *
  266. * @since 3.7.0
  267. *
  268. * @param bool $upgrade_minor Whether to enable minor automatic core updates.
  269. */
  270. return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
  271. }
  272. // 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1)
  273. if ( version_compare( $new_branch, $current_branch, '>' ) ) {
  274. /**
  275. * Filters whether to enable major automatic core updates.
  276. *
  277. * @since 3.7.0
  278. *
  279. * @param bool $upgrade_major Whether to enable major automatic core updates.
  280. */
  281. return apply_filters( 'allow_major_auto_core_updates', $upgrade_major );
  282. }
  283. // If we're not sure, we don't want it
  284. return false;
  285. }
  286. /**
  287. * Compare the disk file checksums against the expected checksums.
  288. *
  289. * @since 3.7.0
  290. *
  291. * @global string $wp_version
  292. * @global string $wp_local_package
  293. *
  294. * @return bool True if the checksums match, otherwise false.
  295. */
  296. public function check_files() {
  297. global $wp_version, $wp_local_package;
  298. $checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );
  299. if ( ! is_array( $checksums ) )
  300. return false;
  301. foreach ( $checksums as $file => $checksum ) {
  302. // Skip files which get updated
  303. if ( 'wp-content' == substr( $file, 0, 10 ) )
  304. continue;
  305. if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum )
  306. return false;
  307. }
  308. return true;
  309. }
  310. }