PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-automatic-updater.php

http://github.com/wordpress/wordpress
PHP | 1016 lines | 579 code | 117 blank | 320 comment | 136 complexity | 428616b03be80f13d5626b5d3435ea29 MD5 | raw file
Possible License(s): 0BSD
  1. <?php
  2. /**
  3. * Upgrade API: WP_Automatic_Updater class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for handling automatic background updates.
  11. *
  12. * @since 3.7.0
  13. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  14. */
  15. class WP_Automatic_Updater {
  16. /**
  17. * Tracks update results during processing.
  18. *
  19. * @var array
  20. */
  21. protected $update_results = array();
  22. /**
  23. * Whether the entire automatic updater is disabled.
  24. *
  25. * @since 3.7.0
  26. */
  27. public function is_disabled() {
  28. // Background updates are disabled if you don't want file changes.
  29. if ( ! wp_is_file_mod_allowed( 'automatic_updater' ) ) {
  30. return true;
  31. }
  32. if ( wp_installing() ) {
  33. return true;
  34. }
  35. // More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
  36. $disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED;
  37. /**
  38. * Filters whether to entirely disable background updates.
  39. *
  40. * There are more fine-grained filters and controls for selective disabling.
  41. * This filter parallels the AUTOMATIC_UPDATER_DISABLED constant in name.
  42. *
  43. * This also disables update notification emails. That may change in the future.
  44. *
  45. * @since 3.7.0
  46. *
  47. * @param bool $disabled Whether the updater should be disabled.
  48. */
  49. return apply_filters( 'automatic_updater_disabled', $disabled );
  50. }
  51. /**
  52. * Check for version control checkouts.
  53. *
  54. * Checks for Subversion, Git, Mercurial, and Bazaar. It recursively looks up the
  55. * filesystem to the top of the drive, erring on the side of detecting a VCS
  56. * checkout somewhere.
  57. *
  58. * ABSPATH is always checked in addition to whatever $context is (which may be the
  59. * wp-content directory, for example). The underlying assumption is that if you are
  60. * using version control *anywhere*, then you should be making decisions for
  61. * how things get updated.
  62. *
  63. * @since 3.7.0
  64. *
  65. * @param string $context The filesystem path to check, in addition to ABSPATH.
  66. */
  67. public function is_vcs_checkout( $context ) {
  68. $context_dirs = array( untrailingslashit( $context ) );
  69. if ( ABSPATH !== $context ) {
  70. $context_dirs[] = untrailingslashit( ABSPATH );
  71. }
  72. $vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
  73. $check_dirs = array();
  74. foreach ( $context_dirs as $context_dir ) {
  75. // Walk up from $context_dir to the root.
  76. do {
  77. $check_dirs[] = $context_dir;
  78. // Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
  79. if ( dirname( $context_dir ) === $context_dir ) {
  80. break;
  81. }
  82. // Continue one level at a time.
  83. } while ( $context_dir = dirname( $context_dir ) );
  84. }
  85. $check_dirs = array_unique( $check_dirs );
  86. // Search all directories we've found for evidence of version control.
  87. foreach ( $vcs_dirs as $vcs_dir ) {
  88. foreach ( $check_dirs as $check_dir ) {
  89. $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" );
  90. if ( $checkout ) {
  91. break 2;
  92. }
  93. }
  94. }
  95. /**
  96. * Filters whether the automatic updater should consider a filesystem
  97. * location to be potentially managed by a version control system.
  98. *
  99. * @since 3.7.0
  100. *
  101. * @param bool $checkout Whether a VCS checkout was discovered at $context
  102. * or ABSPATH, or anywhere higher.
  103. * @param string $context The filesystem context (a path) against which
  104. * filesystem status should be checked.
  105. */
  106. return apply_filters( 'automatic_updates_is_vcs_checkout', $checkout, $context );
  107. }
  108. /**
  109. * Tests to see if we can and should update a specific item.
  110. *
  111. * @since 3.7.0
  112. *
  113. * @global wpdb $wpdb WordPress database abstraction object.
  114. *
  115. * @param string $type The type of update being checked: 'core', 'theme',
  116. * 'plugin', 'translation'.
  117. * @param object $item The update offer.
  118. * @param string $context The filesystem context (a path) against which filesystem
  119. * access and status should be checked.
  120. */
  121. public function should_update( $type, $item, $context ) {
  122. // Used to see if WP_Filesystem is set up to allow unattended updates.
  123. $skin = new Automatic_Upgrader_Skin;
  124. if ( $this->is_disabled() ) {
  125. return false;
  126. }
  127. // Only relax the filesystem checks when the update doesn't include new files.
  128. $allow_relaxed_file_ownership = false;
  129. if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
  130. $allow_relaxed_file_ownership = true;
  131. }
  132. // If we can't do an auto core update, we may still be able to email the user.
  133. if ( ! $skin->request_filesystem_credentials( false, $context, $allow_relaxed_file_ownership ) || $this->is_vcs_checkout( $context ) ) {
  134. if ( 'core' == $type ) {
  135. $this->send_core_update_notification_email( $item );
  136. }
  137. return false;
  138. }
  139. // Next up, is this an item we can update?
  140. if ( 'core' == $type ) {
  141. $update = Core_Upgrader::should_update_to_version( $item->current );
  142. } else {
  143. $update = ! empty( $item->autoupdate );
  144. }
  145. /**
  146. * Filters whether to automatically update core, a plugin, a theme, or a language.
  147. *
  148. * The dynamic portion of the hook name, `$type`, refers to the type of update
  149. * being checked. Can be 'core', 'theme', 'plugin', or 'translation'.
  150. *
  151. * Generally speaking, plugins, themes, and major core versions are not updated
  152. * by default, while translations and minor and development versions for core
  153. * are updated by default.
  154. *
  155. * See the {@see 'allow_dev_auto_core_updates'}, {@see 'allow_minor_auto_core_updates'},
  156. * and {@see 'allow_major_auto_core_updates'} filters for a more straightforward way to
  157. * adjust core updates.
  158. *
  159. * @since 3.7.0
  160. *
  161. * @param bool $update Whether to update.
  162. * @param object $item The update offer.
  163. */
  164. $update = apply_filters( "auto_update_{$type}", $update, $item );
  165. if ( ! $update ) {
  166. if ( 'core' == $type ) {
  167. $this->send_core_update_notification_email( $item );
  168. }
  169. return false;
  170. }
  171. // If it's a core update, are we actually compatible with its requirements?
  172. if ( 'core' == $type ) {
  173. global $wpdb;
  174. $php_compat = version_compare( phpversion(), $item->php_version, '>=' );
  175. if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
  176. $mysql_compat = true;
  177. } else {
  178. $mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
  179. }
  180. if ( ! $php_compat || ! $mysql_compat ) {
  181. return false;
  182. }
  183. }
  184. // If updating a plugin, ensure the minimum PHP version requirements are satisfied.
  185. if ( 'plugin' === $type ) {
  186. if ( ! empty( $item->requires_php ) && version_compare( phpversion(), $item->requires_php, '<' ) ) {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. /**
  193. * Notifies an administrator of a core update.
  194. *
  195. * @since 3.7.0
  196. *
  197. * @param object $item The update offer.
  198. */
  199. protected function send_core_update_notification_email( $item ) {
  200. $notified = get_site_option( 'auto_core_update_notified' );
  201. // Don't notify if we've already notified the same email address of the same version.
  202. if ( $notified && get_site_option( 'admin_email' ) === $notified['email'] && $notified['version'] == $item->current ) {
  203. return false;
  204. }
  205. // See if we need to notify users of a core update.
  206. $notify = ! empty( $item->notify_email );
  207. /**
  208. * Filters whether to notify the site administrator of a new core update.
  209. *
  210. * By default, administrators are notified when the update offer received
  211. * from WordPress.org sets a particular flag. This allows some discretion
  212. * in if and when to notify.
  213. *
  214. * This filter is only evaluated once per release. If the same email address
  215. * was already notified of the same new version, WordPress won't repeatedly
  216. * email the administrator.
  217. *
  218. * This filter is also used on about.php to check if a plugin has disabled
  219. * these notifications.
  220. *
  221. * @since 3.7.0
  222. *
  223. * @param bool $notify Whether the site administrator is notified.
  224. * @param object $item The update offer.
  225. */
  226. if ( ! apply_filters( 'send_core_update_notification_email', $notify, $item ) ) {
  227. return false;
  228. }
  229. $this->send_email( 'manual', $item );
  230. return true;
  231. }
  232. /**
  233. * Update an item, if appropriate.
  234. *
  235. * @since 3.7.0
  236. *
  237. * @param string $type The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
  238. * @param object $item The update offer.
  239. *
  240. * @return null|WP_Error
  241. */
  242. public function update( $type, $item ) {
  243. $skin = new Automatic_Upgrader_Skin;
  244. switch ( $type ) {
  245. case 'core':
  246. // The Core upgrader doesn't use the Upgrader's skin during the actual main part of the upgrade, instead, firing a filter.
  247. add_filter( 'update_feedback', array( $skin, 'feedback' ) );
  248. $upgrader = new Core_Upgrader( $skin );
  249. $context = ABSPATH;
  250. break;
  251. case 'plugin':
  252. $upgrader = new Plugin_Upgrader( $skin );
  253. $context = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR.
  254. break;
  255. case 'theme':
  256. $upgrader = new Theme_Upgrader( $skin );
  257. $context = get_theme_root( $item->theme );
  258. break;
  259. case 'translation':
  260. $upgrader = new Language_Pack_Upgrader( $skin );
  261. $context = WP_CONTENT_DIR; // WP_LANG_DIR;
  262. break;
  263. }
  264. // Determine whether we can and should perform this update.
  265. if ( ! $this->should_update( $type, $item, $context ) ) {
  266. return false;
  267. }
  268. /**
  269. * Fires immediately prior to an auto-update.
  270. *
  271. * @since 4.4.0
  272. *
  273. * @param string $type The type of update being checked: 'core', 'theme', 'plugin', or 'translation'.
  274. * @param object $item The update offer.
  275. * @param string $context The filesystem context (a path) against which filesystem access and status
  276. * should be checked.
  277. */
  278. do_action( 'pre_auto_update', $type, $item, $context );
  279. $upgrader_item = $item;
  280. switch ( $type ) {
  281. case 'core':
  282. /* translators: %s: WordPress version. */
  283. $skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
  284. /* translators: %s: WordPress version. */
  285. $item_name = sprintf( __( 'WordPress %s' ), $item->version );
  286. break;
  287. case 'theme':
  288. $upgrader_item = $item->theme;
  289. $theme = wp_get_theme( $upgrader_item );
  290. $item_name = $theme->Get( 'Name' );
  291. /* translators: %s: Theme name. */
  292. $skin->feedback( __( 'Updating theme: %s' ), $item_name );
  293. break;
  294. case 'plugin':
  295. $upgrader_item = $item->plugin;
  296. $plugin_data = get_plugin_data( $context . '/' . $upgrader_item );
  297. $item_name = $plugin_data['Name'];
  298. /* translators: %s: Plugin name. */
  299. $skin->feedback( __( 'Updating plugin: %s' ), $item_name );
  300. break;
  301. case 'translation':
  302. $language_item_name = $upgrader->get_name_for_update( $item );
  303. /* translators: %s: Project name (plugin, theme, or WordPress). */
  304. $item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
  305. /* translators: 1: Project name (plugin, theme, or WordPress), 2: Language. */
  306. $skin->feedback( sprintf( __( 'Updating translations for %1$s (%2$s)&#8230;' ), $language_item_name, $item->language ) );
  307. break;
  308. }
  309. $allow_relaxed_file_ownership = false;
  310. if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
  311. $allow_relaxed_file_ownership = true;
  312. }
  313. // Boom, this site's about to get a whole new splash of paint!
  314. $upgrade_result = $upgrader->upgrade(
  315. $upgrader_item,
  316. array(
  317. 'clear_update_cache' => false,
  318. // Always use partial builds if possible for core updates.
  319. 'pre_check_md5' => false,
  320. // Only available for core updates.
  321. 'attempt_rollback' => true,
  322. // Allow relaxed file ownership in some scenarios.
  323. 'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership,
  324. )
  325. );
  326. // If the filesystem is unavailable, false is returned.
  327. if ( false === $upgrade_result ) {
  328. $upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
  329. }
  330. if ( 'core' == $type ) {
  331. if ( is_wp_error( $upgrade_result ) && ( 'up_to_date' == $upgrade_result->get_error_code() || 'locked' == $upgrade_result->get_error_code() ) ) {
  332. // These aren't actual errors, treat it as a skipped-update instead to avoid triggering the post-core update failure routines.
  333. return false;
  334. }
  335. // Core doesn't output this, so let's append it so we don't get confused.
  336. if ( is_wp_error( $upgrade_result ) ) {
  337. $skin->error( __( 'Installation Failed' ), $upgrade_result );
  338. } else {
  339. $skin->feedback( __( 'WordPress updated successfully' ) );
  340. }
  341. }
  342. $this->update_results[ $type ][] = (object) array(
  343. 'item' => $item,
  344. 'result' => $upgrade_result,
  345. 'name' => $item_name,
  346. 'messages' => $skin->get_upgrade_messages(),
  347. );
  348. return $upgrade_result;
  349. }
  350. /**
  351. * Kicks off the background update process, looping through all pending updates.
  352. *
  353. * @since 3.7.0
  354. */
  355. public function run() {
  356. if ( $this->is_disabled() ) {
  357. return;
  358. }
  359. if ( ! is_main_network() || ! is_main_site() ) {
  360. return;
  361. }
  362. if ( ! WP_Upgrader::create_lock( 'auto_updater' ) ) {
  363. return;
  364. }
  365. // Don't automatically run these things, as we'll handle it ourselves.
  366. remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
  367. remove_action( 'upgrader_process_complete', 'wp_version_check' );
  368. remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
  369. remove_action( 'upgrader_process_complete', 'wp_update_themes' );
  370. // Next, plugins.
  371. wp_update_plugins(); // Check for plugin updates.
  372. $plugin_updates = get_site_transient( 'update_plugins' );
  373. if ( $plugin_updates && ! empty( $plugin_updates->response ) ) {
  374. foreach ( $plugin_updates->response as $plugin ) {
  375. $this->update( 'plugin', $plugin );
  376. }
  377. // Force refresh of plugin update information.
  378. wp_clean_plugins_cache();
  379. }
  380. // Next, those themes we all love.
  381. wp_update_themes(); // Check for theme updates.
  382. $theme_updates = get_site_transient( 'update_themes' );
  383. if ( $theme_updates && ! empty( $theme_updates->response ) ) {
  384. foreach ( $theme_updates->response as $theme ) {
  385. $this->update( 'theme', (object) $theme );
  386. }
  387. // Force refresh of theme update information.
  388. wp_clean_themes_cache();
  389. }
  390. // Next, process any core update.
  391. wp_version_check(); // Check for core updates.
  392. $core_update = find_core_auto_update();
  393. if ( $core_update ) {
  394. $this->update( 'core', $core_update );
  395. }
  396. // Clean up, and check for any pending translations.
  397. // (Core_Upgrader checks for core updates.)
  398. $theme_stats = array();
  399. if ( isset( $this->update_results['theme'] ) ) {
  400. foreach ( $this->update_results['theme'] as $upgrade ) {
  401. $theme_stats[ $upgrade->item->theme ] = ( true === $upgrade->result );
  402. }
  403. }
  404. wp_update_themes( $theme_stats ); // Check for theme updates.
  405. $plugin_stats = array();
  406. if ( isset( $this->update_results['plugin'] ) ) {
  407. foreach ( $this->update_results['plugin'] as $upgrade ) {
  408. $plugin_stats[ $upgrade->item->plugin ] = ( true === $upgrade->result );
  409. }
  410. }
  411. wp_update_plugins( $plugin_stats ); // Check for plugin updates.
  412. // Finally, process any new translations.
  413. $language_updates = wp_get_translation_updates();
  414. if ( $language_updates ) {
  415. foreach ( $language_updates as $update ) {
  416. $this->update( 'translation', $update );
  417. }
  418. // Clear existing caches.
  419. wp_clean_update_cache();
  420. wp_version_check(); // Check for core updates.
  421. wp_update_themes(); // Check for theme updates.
  422. wp_update_plugins(); // Check for plugin updates.
  423. }
  424. // Send debugging email to admin for all development installations.
  425. if ( ! empty( $this->update_results ) ) {
  426. $development_version = false !== strpos( get_bloginfo( 'version' ), '-' );
  427. /**
  428. * Filters whether to send a debugging email for each automatic background update.
  429. *
  430. * @since 3.7.0
  431. *
  432. * @param bool $development_version By default, emails are sent if the
  433. * install is a development version.
  434. * Return false to avoid the email.
  435. */
  436. if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) ) {
  437. $this->send_debug_email();
  438. }
  439. if ( ! empty( $this->update_results['core'] ) ) {
  440. $this->after_core_update( $this->update_results['core'][0] );
  441. }
  442. /**
  443. * Fires after all automatic updates have run.
  444. *
  445. * @since 3.8.0
  446. *
  447. * @param array $update_results The results of all attempted updates.
  448. */
  449. do_action( 'automatic_updates_complete', $this->update_results );
  450. }
  451. WP_Upgrader::release_lock( 'auto_updater' );
  452. }
  453. /**
  454. * If we tried to perform a core update, check if we should send an email,
  455. * and if we need to avoid processing future updates.
  456. *
  457. * @since 3.7.0
  458. *
  459. * @param object $update_result The result of the core update. Includes the update offer and result.
  460. */
  461. protected function after_core_update( $update_result ) {
  462. $wp_version = get_bloginfo( 'version' );
  463. $core_update = $update_result->item;
  464. $result = $update_result->result;
  465. if ( ! is_wp_error( $result ) ) {
  466. $this->send_email( 'success', $core_update );
  467. return;
  468. }
  469. $error_code = $result->get_error_code();
  470. // Any of these WP_Error codes are critical failures, as in they occurred after we started to copy core files.
  471. // We should not try to perform a background update again until there is a successful one-click update performed by the user.
  472. $critical = false;
  473. if ( 'disk_full' === $error_code || false !== strpos( $error_code, '__copy_dir' ) ) {
  474. $critical = true;
  475. } elseif ( 'rollback_was_required' === $error_code && is_wp_error( $result->get_error_data()->rollback ) ) {
  476. // A rollback is only critical if it failed too.
  477. $critical = true;
  478. $rollback_result = $result->get_error_data()->rollback;
  479. } elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
  480. $critical = true;
  481. }
  482. if ( $critical ) {
  483. $critical_data = array(
  484. 'attempted' => $core_update->current,
  485. 'current' => $wp_version,
  486. 'error_code' => $error_code,
  487. 'error_data' => $result->get_error_data(),
  488. 'timestamp' => time(),
  489. 'critical' => true,
  490. );
  491. if ( isset( $rollback_result ) ) {
  492. $critical_data['rollback_code'] = $rollback_result->get_error_code();
  493. $critical_data['rollback_data'] = $rollback_result->get_error_data();
  494. }
  495. update_site_option( 'auto_core_update_failed', $critical_data );
  496. $this->send_email( 'critical', $core_update, $result );
  497. return;
  498. }
  499. /*
  500. * Any other WP_Error code (like download_failed or files_not_writable) occurs before
  501. * we tried to copy over core files. Thus, the failures are early and graceful.
  502. *
  503. * We should avoid trying to perform a background update again for the same version.
  504. * But we can try again if another version is released.
  505. *
  506. * For certain 'transient' failures, like download_failed, we should allow retries.
  507. * In fact, let's schedule a special update for an hour from now. (It's possible
  508. * the issue could actually be on WordPress.org's side.) If that one fails, then email.
  509. */
  510. $send = true;
  511. $transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro', 'locked' );
  512. if ( in_array( $error_code, $transient_failures, true ) && ! get_site_option( 'auto_core_update_failed' ) ) {
  513. wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
  514. $send = false;
  515. }
  516. $n = get_site_option( 'auto_core_update_notified' );
  517. // Don't notify if we've already notified the same email address of the same version of the same notification type.
  518. if ( $n && 'fail' === $n['type'] && get_site_option( 'admin_email' ) === $n['email'] && $n['version'] == $core_update->current ) {
  519. $send = false;
  520. }
  521. update_site_option(
  522. 'auto_core_update_failed',
  523. array(
  524. 'attempted' => $core_update->current,
  525. 'current' => $wp_version,
  526. 'error_code' => $error_code,
  527. 'error_data' => $result->get_error_data(),
  528. 'timestamp' => time(),
  529. 'retry' => in_array( $error_code, $transient_failures, true ),
  530. )
  531. );
  532. if ( $send ) {
  533. $this->send_email( 'fail', $core_update, $result );
  534. }
  535. }
  536. /**
  537. * Sends an email upon the completion or failure of a background core update.
  538. *
  539. * @since 3.7.0
  540. *
  541. * @param string $type The type of email to send. Can be one of 'success', 'fail', 'manual', 'critical'.
  542. * @param object $core_update The update offer that was attempted.
  543. * @param mixed $result Optional. The result for the core update. Can be WP_Error.
  544. */
  545. protected function send_email( $type, $core_update, $result = null ) {
  546. update_site_option(
  547. 'auto_core_update_notified',
  548. array(
  549. 'type' => $type,
  550. 'email' => get_site_option( 'admin_email' ),
  551. 'version' => $core_update->current,
  552. 'timestamp' => time(),
  553. )
  554. );
  555. $next_user_core_update = get_preferred_from_update_core();
  556. // If the update transient is empty, use the update we just performed.
  557. if ( ! $next_user_core_update ) {
  558. $next_user_core_update = $core_update;
  559. }
  560. $newer_version_available = ( 'upgrade' == $next_user_core_update->response && version_compare( $next_user_core_update->version, $core_update->version, '>' ) );
  561. /**
  562. * Filters whether to send an email following an automatic background core update.
  563. *
  564. * @since 3.7.0
  565. *
  566. * @param bool $send Whether to send the email. Default true.
  567. * @param string $type The type of email to send. Can be one of
  568. * 'success', 'fail', 'critical'.
  569. * @param object $core_update The update offer that was attempted.
  570. * @param mixed $result The result for the core update. Can be WP_Error.
  571. */
  572. if ( 'manual' !== $type && ! apply_filters( 'auto_core_update_send_email', true, $type, $core_update, $result ) ) {
  573. return;
  574. }
  575. switch ( $type ) {
  576. case 'success': // We updated.
  577. /* translators: Site updated notification email subject. 1: Site title, 2: WordPress version. */
  578. $subject = __( '[%1$s] Your site has updated to WordPress %2$s' );
  579. break;
  580. case 'fail': // We tried to update but couldn't.
  581. case 'manual': // We can't update (and made no attempt).
  582. /* translators: Update available notification email subject. 1: Site title, 2: WordPress version. */
  583. $subject = __( '[%1$s] WordPress %2$s is available. Please update!' );
  584. break;
  585. case 'critical': // We tried to update, started to copy files, then things went wrong.
  586. /* translators: Site down notification email subject. 1: Site title. */
  587. $subject = __( '[%1$s] URGENT: Your site may be down due to a failed update' );
  588. break;
  589. default:
  590. return;
  591. }
  592. // If the auto update is not to the latest version, say that the current version of WP is available instead.
  593. $version = 'success' === $type ? $core_update->current : $next_user_core_update->current;
  594. $subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $version );
  595. $body = '';
  596. switch ( $type ) {
  597. case 'success':
  598. $body .= sprintf(
  599. /* translators: 1: Home URL, 2: WordPress version. */
  600. __( 'Howdy! Your site at %1$s has been updated automatically to WordPress %2$s.' ),
  601. home_url(),
  602. $core_update->current
  603. );
  604. $body .= "\n\n";
  605. if ( ! $newer_version_available ) {
  606. $body .= __( 'No further action is needed on your part.' ) . ' ';
  607. }
  608. // Can only reference the About screen if their update was successful.
  609. list( $about_version ) = explode( '-', $core_update->current, 2 );
  610. /* translators: %s: WordPress version. */
  611. $body .= sprintf( __( 'For more on version %s, see the About WordPress screen:' ), $about_version );
  612. $body .= "\n" . admin_url( 'about.php' );
  613. if ( $newer_version_available ) {
  614. /* translators: %s: WordPress latest version. */
  615. $body .= "\n\n" . sprintf( __( 'WordPress %s is also now available.' ), $next_user_core_update->current ) . ' ';
  616. $body .= __( 'Updating is easy and only takes a few moments:' );
  617. $body .= "\n" . network_admin_url( 'update-core.php' );
  618. }
  619. break;
  620. case 'fail':
  621. case 'manual':
  622. $body .= sprintf(
  623. /* translators: 1: Home URL, 2: WordPress version. */
  624. __( 'Please update your site at %1$s to WordPress %2$s.' ),
  625. home_url(),
  626. $next_user_core_update->current
  627. );
  628. $body .= "\n\n";
  629. // Don't show this message if there is a newer version available.
  630. // Potential for confusion, and also not useful for them to know at this point.
  631. if ( 'fail' == $type && ! $newer_version_available ) {
  632. $body .= __( 'We tried but were unable to update your site automatically.' ) . ' ';
  633. }
  634. $body .= __( 'Updating is easy and only takes a few moments:' );
  635. $body .= "\n" . network_admin_url( 'update-core.php' );
  636. break;
  637. case 'critical':
  638. if ( $newer_version_available ) {
  639. $body .= sprintf(
  640. /* translators: 1: Home URL, 2: WordPress version. */
  641. __( 'Your site at %1$s experienced a critical failure while trying to update WordPress to version %2$s.' ),
  642. home_url(),
  643. $core_update->current
  644. );
  645. } else {
  646. $body .= sprintf(
  647. /* translators: 1: Home URL, 2: WordPress latest version. */
  648. __( 'Your site at %1$s experienced a critical failure while trying to update to the latest version of WordPress, %2$s.' ),
  649. home_url(),
  650. $core_update->current
  651. );
  652. }
  653. $body .= "\n\n" . __( "This means your site may be offline or broken. Don't panic; this can be fixed." );
  654. $body .= "\n\n" . __( "Please check out your site now. It's possible that everything is working. If it says you need to update, you should do so:" );
  655. $body .= "\n" . network_admin_url( 'update-core.php' );
  656. break;
  657. }
  658. $critical_support = 'critical' === $type && ! empty( $core_update->support_email );
  659. if ( $critical_support ) {
  660. // Support offer if available.
  661. $body .= "\n\n" . sprintf(
  662. /* translators: %s: Support email address. */
  663. __( 'The WordPress team is willing to help you. Forward this email to %s and the team will work with you to make sure your site is working.' ),
  664. $core_update->support_email
  665. );
  666. } else {
  667. // Add a note about the support forums.
  668. $body .= "\n\n" . __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
  669. $body .= "\n" . __( 'https://wordpress.org/support/forums/' );
  670. }
  671. // Updates are important!
  672. if ( 'success' !== $type || $newer_version_available ) {
  673. $body .= "\n\n" . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' );
  674. }
  675. if ( $critical_support ) {
  676. $body .= ' ' . __( "If you reach out to us, we'll also ensure you'll never have this problem again." );
  677. }
  678. // If things are successful and we're now on the latest, mention plugins and themes if any are out of date.
  679. if ( 'success' === $type && ! $newer_version_available && ( get_plugin_updates() || get_theme_updates() ) ) {
  680. $body .= "\n\n" . __( 'You also have some plugins or themes with updates available. Update them now:' );
  681. $body .= "\n" . network_admin_url();
  682. }
  683. $body .= "\n\n" . __( 'The WordPress Team' ) . "\n";
  684. if ( 'critical' == $type && is_wp_error( $result ) ) {
  685. $body .= "\n***\n\n";
  686. /* translators: %s: WordPress version. */
  687. $body .= sprintf( __( 'Your site was running version %s.' ), get_bloginfo( 'version' ) );
  688. $body .= ' ' . __( 'We have some data that describes the error your site encountered.' );
  689. $body .= ' ' . __( 'Your hosting company, support forum volunteers, or a friendly developer may be able to use this information to help you:' );
  690. // If we had a rollback and we're still critical, then the rollback failed too.
  691. // Loop through all errors (the main WP_Error, the update result, the rollback result) for code, data, etc.
  692. if ( 'rollback_was_required' == $result->get_error_code() ) {
  693. $errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
  694. } else {
  695. $errors = array( $result );
  696. }
  697. foreach ( $errors as $error ) {
  698. if ( ! is_wp_error( $error ) ) {
  699. continue;
  700. }
  701. $error_code = $error->get_error_code();
  702. /* translators: %s: Error code. */
  703. $body .= "\n\n" . sprintf( __( 'Error code: %s' ), $error_code );
  704. if ( 'rollback_was_required' == $error_code ) {
  705. continue;
  706. }
  707. if ( $error->get_error_message() ) {
  708. $body .= "\n" . $error->get_error_message();
  709. }
  710. $error_data = $error->get_error_data();
  711. if ( $error_data ) {
  712. $body .= "\n" . implode( ', ', (array) $error_data );
  713. }
  714. }
  715. $body .= "\n";
  716. }
  717. $to = get_site_option( 'admin_email' );
  718. $headers = '';
  719. $email = compact( 'to', 'subject', 'body', 'headers' );
  720. /**
  721. * Filters the email sent following an automatic background core update.
  722. *
  723. * @since 3.7.0
  724. *
  725. * @param array $email {
  726. * Array of email arguments that will be passed to wp_mail().
  727. *
  728. * @type string $to The email recipient. An array of emails
  729. * can be returned, as handled by wp_mail().
  730. * @type string $subject The email's subject.
  731. * @type string $body The email message body.
  732. * @type string $headers Any email headers, defaults to no headers.
  733. * }
  734. * @param string $type The type of email being sent. Can be one of
  735. * 'success', 'fail', 'manual', 'critical'.
  736. * @param object $core_update The update offer that was attempted.
  737. * @param mixed $result The result for the core update. Can be WP_Error.
  738. */
  739. $email = apply_filters( 'auto_core_update_email', $email, $type, $core_update, $result );
  740. wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
  741. }
  742. /**
  743. * Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
  744. *
  745. * @since 3.7.0
  746. */
  747. protected function send_debug_email() {
  748. $update_count = 0;
  749. foreach ( $this->update_results as $type => $updates ) {
  750. $update_count += count( $updates );
  751. }
  752. $body = array();
  753. $failures = 0;
  754. /* translators: %s: Network home URL. */
  755. $body[] = sprintf( __( 'WordPress site: %s' ), network_home_url( '/' ) );
  756. // Core.
  757. if ( isset( $this->update_results['core'] ) ) {
  758. $result = $this->update_results['core'][0];
  759. if ( $result->result && ! is_wp_error( $result->result ) ) {
  760. /* translators: %s: WordPress version. */
  761. $body[] = sprintf( __( 'SUCCESS: WordPress was successfully updated to %s' ), $result->name );
  762. } else {
  763. /* translators: %s: WordPress version. */
  764. $body[] = sprintf( __( 'FAILED: WordPress failed to update to %s' ), $result->name );
  765. $failures++;
  766. }
  767. $body[] = '';
  768. }
  769. // Plugins, Themes, Translations.
  770. foreach ( array( 'plugin', 'theme', 'translation' ) as $type ) {
  771. if ( ! isset( $this->update_results[ $type ] ) ) {
  772. continue;
  773. }
  774. $success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
  775. if ( $success_items ) {
  776. $messages = array(
  777. 'plugin' => __( 'The following plugins were successfully updated:' ),
  778. 'theme' => __( 'The following themes were successfully updated:' ),
  779. 'translation' => __( 'The following translations were successfully updated:' ),
  780. );
  781. $body[] = $messages[ $type ];
  782. foreach ( wp_list_pluck( $success_items, 'name' ) as $name ) {
  783. /* translators: %s: Name of plugin / theme / translation. */
  784. $body[] = ' * ' . sprintf( __( 'SUCCESS: %s' ), $name );
  785. }
  786. }
  787. if ( $success_items != $this->update_results[ $type ] ) {
  788. // Failed updates.
  789. $messages = array(
  790. 'plugin' => __( 'The following plugins failed to update:' ),
  791. 'theme' => __( 'The following themes failed to update:' ),
  792. 'translation' => __( 'The following translations failed to update:' ),
  793. );
  794. $body[] = $messages[ $type ];
  795. foreach ( $this->update_results[ $type ] as $item ) {
  796. if ( ! $item->result || is_wp_error( $item->result ) ) {
  797. /* translators: %s: Name of plugin / theme / translation. */
  798. $body[] = ' * ' . sprintf( __( 'FAILED: %s' ), $item->name );
  799. $failures++;
  800. }
  801. }
  802. }
  803. $body[] = '';
  804. }
  805. $site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
  806. if ( $failures ) {
  807. $body[] = trim(
  808. __(
  809. "BETA TESTING?
  810. =============
  811. This debugging email is sent when you are using a development version of WordPress.
  812. If you think these failures might be due to a bug in WordPress, could you report it?
  813. * Open a thread in the support forums: https://wordpress.org/support/forum/alphabeta
  814. * Or, if you're comfortable writing a bug report: https://core.trac.wordpress.org/
  815. Thanks! -- The WordPress Team"
  816. )
  817. );
  818. $body[] = '';
  819. /* translators: Background update failed notification email subject. %s: Site title. */
  820. $subject = sprintf( __( '[%s] Background Update Failed' ), $site_title );
  821. } else {
  822. /* translators: Background update finished notification email subject. %s: Site title. */
  823. $subject = sprintf( __( '[%s] Background Update Finished' ), $site_title );
  824. }
  825. $body[] = trim(
  826. __(
  827. 'UPDATE LOG
  828. =========='
  829. )
  830. );
  831. $body[] = '';
  832. foreach ( array( 'core', 'plugin', 'theme', 'translation' ) as $type ) {
  833. if ( ! isset( $this->update_results[ $type ] ) ) {
  834. continue;
  835. }
  836. foreach ( $this->update_results[ $type ] as $update ) {
  837. $body[] = $update->name;
  838. $body[] = str_repeat( '-', strlen( $update->name ) );
  839. foreach ( $update->messages as $message ) {
  840. $body[] = ' ' . html_entity_decode( str_replace( '&#8230;', '...', $message ) );
  841. }
  842. if ( is_wp_error( $update->result ) ) {
  843. $results = array( 'update' => $update->result );
  844. // If we rolled back, we want to know an error that occurred then too.
  845. if ( 'rollback_was_required' === $update->result->get_error_code() ) {
  846. $results = (array) $update->result->get_error_data();
  847. }
  848. foreach ( $results as $result_type => $result ) {
  849. if ( ! is_wp_error( $result ) ) {
  850. continue;
  851. }
  852. if ( 'rollback' === $result_type ) {
  853. /* translators: 1: Error code, 2: Error message. */
  854. $body[] = ' ' . sprintf( __( 'Rollback Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
  855. } else {
  856. /* translators: 1: Error code, 2: Error message. */
  857. $body[] = ' ' . sprintf( __( 'Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
  858. }
  859. if ( $result->get_error_data() ) {
  860. $body[] = ' ' . implode( ', ', (array) $result->get_error_data() );
  861. }
  862. }
  863. }
  864. $body[] = '';
  865. }
  866. }
  867. $email = array(
  868. 'to' => get_site_option( 'admin_email' ),
  869. 'subject' => $subject,
  870. 'body' => implode( "\n", $body ),
  871. 'headers' => '',
  872. );
  873. /**
  874. * Filters the debug email that can be sent following an automatic
  875. * background core update.
  876. *
  877. * @since 3.8.0
  878. *
  879. * @param array $email {
  880. * Array of email arguments that will be passed to wp_mail().
  881. *
  882. * @type string $to The email recipient. An array of emails
  883. * can be returned, as handled by wp_mail().
  884. * @type string $subject Email subject.
  885. * @type string $body Email message body.
  886. * @type string $headers Any email headers. Default empty.
  887. * }
  888. * @param int $failures The number of failures encountered while upgrading.
  889. * @param mixed $results The results of all attempted updates.
  890. */
  891. $email = apply_filters( 'automatic_updates_debug_email', $email, $failures, $this->update_results );
  892. wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
  893. }
  894. }