PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

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

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