WP_Automatic_Updater

Core class used for handling automatic background updates.

Defined (1)

The class is defined in the following location(s).

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