PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

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