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

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