Нема описа

class-wp-automatic-updater.php 49KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. <?php
  2. /**
  3. * Upgrade API: WP_Automatic_Updater class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for handling automatic background updates.
  11. *
  12. * @since 3.7.0
  13. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  14. */
  15. class WP_Automatic_Updater {
  16. /**
  17. * Tracks update results during processing.
  18. *
  19. * @var array
  20. */
  21. protected $update_results = array();
  22. /**
  23. * Whether the entire automatic updater is disabled.
  24. *
  25. * @since 3.7.0
  26. */
  27. public function is_disabled() {
  28. // Background updates are disabled if you don't want file changes.
  29. if ( ! wp_is_file_mod_allowed( 'automatic_updater' ) ) {
  30. return true;
  31. }
  32. if ( wp_installing() ) {
  33. return true;
  34. }
  35. // More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
  36. $disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED;
  37. /**
  38. * Filters whether to entirely disable background updates.
  39. *
  40. * There are more fine-grained filters and controls for selective disabling.
  41. * This filter parallels the AUTOMATIC_UPDATER_DISABLED constant in name.
  42. *
  43. * This also disables update notification emails. That may change in the future.
  44. *
  45. * @since 3.7.0
  46. *
  47. * @param bool $disabled Whether the updater should be disabled.
  48. */
  49. return apply_filters( 'automatic_updater_disabled', $disabled );
  50. }
  51. /**
  52. * Check for version control checkouts.
  53. *
  54. * Checks for Subversion, Git, Mercurial, and Bazaar. It recursively looks up the
  55. * filesystem to the top of the drive, erring on the side of detecting a VCS
  56. * checkout somewhere.
  57. *
  58. * ABSPATH is always checked in addition to whatever `$context` is (which may be the
  59. * wp-content directory, for example). The underlying assumption is that if you are
  60. * using version control *anywhere*, then you should be making decisions for
  61. * how things get updated.
  62. *
  63. * @since 3.7.0
  64. *
  65. * @param string $context The filesystem path to check, in addition to ABSPATH.
  66. * @return bool True if a VCS checkout was discovered at `$context` or ABSPATH,
  67. * or anywhere higher. False otherwise.
  68. */
  69. public function is_vcs_checkout( $context ) {
  70. $context_dirs = array( untrailingslashit( $context ) );
  71. if ( ABSPATH !== $context ) {
  72. $context_dirs[] = untrailingslashit( ABSPATH );
  73. }
  74. $vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
  75. $check_dirs = array();
  76. foreach ( $context_dirs as $context_dir ) {
  77. // Walk up from $context_dir to the root.
  78. do {
  79. $check_dirs[] = $context_dir;
  80. // Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
  81. if ( dirname( $context_dir ) === $context_dir ) {
  82. break;
  83. }
  84. // Continue one level at a time.
  85. } while ( $context_dir = dirname( $context_dir ) );
  86. }
  87. $check_dirs = array_unique( $check_dirs );
  88. // Search all directories we've found for evidence of version control.
  89. foreach ( $vcs_dirs as $vcs_dir ) {
  90. foreach ( $check_dirs as $check_dir ) {
  91. $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" );
  92. if ( $checkout ) {
  93. break 2;
  94. }
  95. }
  96. }
  97. /**
  98. * Filters whether the automatic updater should consider a filesystem
  99. * location to be potentially managed by a version control system.
  100. *
  101. * @since 3.7.0
  102. *
  103. * @param bool $checkout Whether a VCS checkout was discovered at `$context`
  104. * or ABSPATH, or anywhere higher.
  105. * @param string $context The filesystem context (a path) against which
  106. * filesystem status should be checked.
  107. */
  108. return apply_filters( 'automatic_updates_is_vcs_checkout', $checkout, $context );
  109. }
  110. /**
  111. * Tests to see if we can and should update a specific item.
  112. *
  113. * @since 3.7.0
  114. *
  115. * @global wpdb $wpdb WordPress database abstraction object.
  116. *
  117. * @param string $type The type of update being checked: 'core', 'theme',
  118. * 'plugin', 'translation'.
  119. * @param object $item The update offer.
  120. * @param string $context The filesystem context (a path) against which filesystem
  121. * access and status should be checked.
  122. * @return bool True if the item should be updated, false otherwise.
  123. */
  124. public function should_update( $type, $item, $context ) {
  125. // Used to see if WP_Filesystem is set up to allow unattended updates.
  126. $skin = new Automatic_Upgrader_Skin;
  127. if ( $this->is_disabled() ) {
  128. return false;
  129. }
  130. // Only relax the filesystem checks when the update doesn't include new files.
  131. $allow_relaxed_file_ownership = false;
  132. if ( 'core' === $type && isset( $item->new_files ) && ! $item->new_files ) {
  133. $allow_relaxed_file_ownership = true;
  134. }
  135. // If we can't do an auto core update, we may still be able to email the user.
  136. if ( ! $skin->request_filesystem_credentials( false, $context, $allow_relaxed_file_ownership )
  137. || $this->is_vcs_checkout( $context )
  138. ) {
  139. if ( 'core' === $type ) {
  140. $this->send_core_update_notification_email( $item );
  141. }
  142. return false;
  143. }
  144. // Next up, is this an item we can update?
  145. if ( 'core' === $type ) {
  146. $update = Core_Upgrader::should_update_to_version( $item->current );
  147. } elseif ( 'plugin' === $type || 'theme' === $type ) {
  148. $update = ! empty( $item->autoupdate );
  149. if ( ! $update && wp_is_auto_update_enabled_for_type( $type ) ) {
  150. // Check if the site admin has enabled auto-updates by default for the specific item.
  151. $auto_updates = (array) get_site_option( "auto_update_{$type}s", array() );
  152. $update = in_array( $item->{$type}, $auto_updates, true );
  153. }
  154. } else {
  155. $update = ! empty( $item->autoupdate );
  156. }
  157. // If the `disable_autoupdate` flag is set, override any user-choice, but allow filters.
  158. if ( ! empty( $item->disable_autoupdate ) ) {
  159. $update = $item->disable_autoupdate;
  160. }
  161. /**
  162. * Filters whether to automatically update core, a plugin, a theme, or a language.
  163. *
  164. * The dynamic portion of the hook name, `$type`, refers to the type of update
  165. * being checked.
  166. *
  167. * Possible hook names include:
  168. *
  169. * - `auto_update_core`
  170. * - `auto_update_plugin`
  171. * - `auto_update_theme`
  172. * - `auto_update_translation`
  173. *
  174. * Generally speaking, plugins, themes, and major core versions are not updated
  175. * by default, while translations and minor and development versions for core
  176. * are updated by default.
  177. *
  178. * See the {@see 'allow_dev_auto_core_updates'}, {@see 'allow_minor_auto_core_updates'},
  179. * and {@see 'allow_major_auto_core_updates'} filters for a more straightforward way to
  180. * adjust core updates.
  181. *
  182. * @since 3.7.0
  183. * @since 5.5.0 The `$update` parameter accepts the value of null.
  184. *
  185. * @param bool|null $update Whether to update. The value of null is internally used
  186. * to detect whether nothing has hooked into this filter.
  187. * @param object $item The update offer.
  188. */
  189. $update = apply_filters( "auto_update_{$type}", $update, $item );
  190. if ( ! $update ) {
  191. if ( 'core' === $type ) {
  192. $this->send_core_update_notification_email( $item );
  193. }
  194. return false;
  195. }
  196. // If it's a core update, are we actually compatible with its requirements?
  197. if ( 'core' === $type ) {
  198. global $wpdb;
  199. $php_compat = version_compare( phpversion(), $item->php_version, '>=' );
  200. if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
  201. $mysql_compat = true;
  202. } else {
  203. $mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
  204. }
  205. if ( ! $php_compat || ! $mysql_compat ) {
  206. return false;
  207. }
  208. }
  209. // If updating a plugin or theme, ensure the minimum PHP version requirements are satisfied.
  210. if ( in_array( $type, array( 'plugin', 'theme' ), true ) ) {
  211. if ( ! empty( $item->requires_php ) && version_compare( phpversion(), $item->requires_php, '<' ) ) {
  212. return false;
  213. }
  214. }
  215. return true;
  216. }
  217. /**
  218. * Notifies an administrator of a core update.
  219. *
  220. * @since 3.7.0
  221. *
  222. * @param object $item The update offer.
  223. * @return bool True if the site administrator is notified of a core update,
  224. * false otherwise.
  225. */
  226. protected function send_core_update_notification_email( $item ) {
  227. $notified = get_site_option( 'auto_core_update_notified' );
  228. // Don't notify if we've already notified the same email address of the same version.
  229. if ( $notified
  230. && get_site_option( 'admin_email' ) === $notified['email']
  231. && $notified['version'] === $item->current
  232. ) {
  233. return false;
  234. }
  235. // See if we need to notify users of a core update.
  236. $notify = ! empty( $item->notify_email );
  237. /**
  238. * Filters whether to notify the site administrator of a new core update.
  239. *
  240. * By default, administrators are notified when the update offer received
  241. * from WordPress.org sets a particular flag. This allows some discretion
  242. * in if and when to notify.
  243. *
  244. * This filter is only evaluated once per release. If the same email address
  245. * was already notified of the same new version, WordPress won't repeatedly
  246. * email the administrator.
  247. *
  248. * This filter is also used on about.php to check if a plugin has disabled
  249. * these notifications.
  250. *
  251. * @since 3.7.0
  252. *
  253. * @param bool $notify Whether the site administrator is notified.
  254. * @param object $item The update offer.
  255. */
  256. if ( ! apply_filters( 'send_core_update_notification_email', $notify, $item ) ) {
  257. return false;
  258. }
  259. $this->send_email( 'manual', $item );
  260. return true;
  261. }
  262. /**
  263. * Update an item, if appropriate.
  264. *
  265. * @since 3.7.0
  266. *
  267. * @param string $type The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
  268. * @param object $item The update offer.
  269. * @return null|WP_Error
  270. */
  271. public function update( $type, $item ) {
  272. $skin = new Automatic_Upgrader_Skin;
  273. switch ( $type ) {
  274. case 'core':
  275. // The Core upgrader doesn't use the Upgrader's skin during the actual main part of the upgrade, instead, firing a filter.
  276. add_filter( 'update_feedback', array( $skin, 'feedback' ) );
  277. $upgrader = new Core_Upgrader( $skin );
  278. $context = ABSPATH;
  279. break;
  280. case 'plugin':
  281. $upgrader = new Plugin_Upgrader( $skin );
  282. $context = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR.
  283. break;
  284. case 'theme':
  285. $upgrader = new Theme_Upgrader( $skin );
  286. $context = get_theme_root( $item->theme );
  287. break;
  288. case 'translation':
  289. $upgrader = new Language_Pack_Upgrader( $skin );
  290. $context = WP_CONTENT_DIR; // WP_LANG_DIR;
  291. break;
  292. }
  293. // Determine whether we can and should perform this update.
  294. if ( ! $this->should_update( $type, $item, $context ) ) {
  295. return false;
  296. }
  297. /**
  298. * Fires immediately prior to an auto-update.
  299. *
  300. * @since 4.4.0
  301. *
  302. * @param string $type The type of update being checked: 'core', 'theme', 'plugin', or 'translation'.
  303. * @param object $item The update offer.
  304. * @param string $context The filesystem context (a path) against which filesystem access and status
  305. * should be checked.
  306. */
  307. do_action( 'pre_auto_update', $type, $item, $context );
  308. $upgrader_item = $item;
  309. switch ( $type ) {
  310. case 'core':
  311. /* translators: %s: WordPress version. */
  312. $skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
  313. /* translators: %s: WordPress version. */
  314. $item_name = sprintf( __( 'WordPress %s' ), $item->version );
  315. break;
  316. case 'theme':
  317. $upgrader_item = $item->theme;
  318. $theme = wp_get_theme( $upgrader_item );
  319. $item_name = $theme->Get( 'Name' );
  320. // Add the current version so that it can be reported in the notification email.
  321. $item->current_version = $theme->get( 'Version' );
  322. if ( empty( $item->current_version ) ) {
  323. $item->current_version = false;
  324. }
  325. /* translators: %s: Theme name. */
  326. $skin->feedback( __( 'Updating theme: %s' ), $item_name );
  327. break;
  328. case 'plugin':
  329. $upgrader_item = $item->plugin;
  330. $plugin_data = get_plugin_data( $context . '/' . $upgrader_item );
  331. $item_name = $plugin_data['Name'];
  332. // Add the current version so that it can be reported in the notification email.
  333. $item->current_version = $plugin_data['Version'];
  334. if ( empty( $item->current_version ) ) {
  335. $item->current_version = false;
  336. }
  337. /* translators: %s: Plugin name. */
  338. $skin->feedback( __( 'Updating plugin: %s' ), $item_name );
  339. break;
  340. case 'translation':
  341. $language_item_name = $upgrader->get_name_for_update( $item );
  342. /* translators: %s: Project name (plugin, theme, or WordPress). */
  343. $item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
  344. /* translators: 1: Project name (plugin, theme, or WordPress), 2: Language. */
  345. $skin->feedback( sprintf( __( 'Updating translations for %1$s (%2$s)&#8230;' ), $language_item_name, $item->language ) );
  346. break;
  347. }
  348. $allow_relaxed_file_ownership = false;
  349. if ( 'core' === $type && isset( $item->new_files ) && ! $item->new_files ) {
  350. $allow_relaxed_file_ownership = true;
  351. }
  352. // Boom, this site's about to get a whole new splash of paint!
  353. $upgrade_result = $upgrader->upgrade(
  354. $upgrader_item,
  355. array(
  356. 'clear_update_cache' => false,
  357. // Always use partial builds if possible for core updates.
  358. 'pre_check_md5' => false,
  359. // Only available for core updates.
  360. 'attempt_rollback' => true,
  361. // Allow relaxed file ownership in some scenarios.
  362. 'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership,
  363. )
  364. );
  365. // If the filesystem is unavailable, false is returned.
  366. if ( false === $upgrade_result ) {
  367. $upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
  368. }
  369. if ( 'core' === $type ) {
  370. if ( is_wp_error( $upgrade_result )
  371. && ( 'up_to_date' === $upgrade_result->get_error_code()
  372. || 'locked' === $upgrade_result->get_error_code() )
  373. ) {
  374. // These aren't actual errors, treat it as a skipped-update instead
  375. // to avoid triggering the post-core update failure routines.
  376. return false;
  377. }
  378. // Core doesn't output this, so let's append it so we don't get confused.
  379. if ( is_wp_error( $upgrade_result ) ) {
  380. $skin->error( __( 'Installation failed.' ), $upgrade_result );
  381. } else {
  382. $skin->feedback( __( 'WordPress updated successfully.' ) );
  383. }
  384. }
  385. $this->update_results[ $type ][] = (object) array(
  386. 'item' => $item,
  387. 'result' => $upgrade_result,
  388. 'name' => $item_name,
  389. 'messages' => $skin->get_upgrade_messages(),
  390. );
  391. return $upgrade_result;
  392. }
  393. /**
  394. * Kicks off the background update process, looping through all pending updates.
  395. *
  396. * @since 3.7.0
  397. */
  398. public function run() {
  399. if ( $this->is_disabled() ) {
  400. return;
  401. }
  402. if ( ! is_main_network() || ! is_main_site() ) {
  403. return;
  404. }
  405. if ( ! WP_Upgrader::create_lock( 'auto_updater' ) ) {
  406. return;
  407. }
  408. // Don't automatically run these things, as we'll handle it ourselves.
  409. remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
  410. remove_action( 'upgrader_process_complete', 'wp_version_check' );
  411. remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
  412. remove_action( 'upgrader_process_complete', 'wp_update_themes' );
  413. // Next, plugins.
  414. wp_update_plugins(); // Check for plugin updates.
  415. $plugin_updates = get_site_transient( 'update_plugins' );
  416. if ( $plugin_updates && ! empty( $plugin_updates->response ) ) {
  417. foreach ( $plugin_updates->response as $plugin ) {
  418. $this->update( 'plugin', $plugin );
  419. }
  420. // Force refresh of plugin update information.
  421. wp_clean_plugins_cache();
  422. }
  423. // Next, those themes we all love.
  424. wp_update_themes(); // Check for theme updates.
  425. $theme_updates = get_site_transient( 'update_themes' );
  426. if ( $theme_updates && ! empty( $theme_updates->response ) ) {
  427. foreach ( $theme_updates->response as $theme ) {
  428. $this->update( 'theme', (object) $theme );
  429. }
  430. // Force refresh of theme update information.
  431. wp_clean_themes_cache();
  432. }
  433. // Next, process any core update.
  434. wp_version_check(); // Check for core updates.
  435. $core_update = find_core_auto_update();
  436. if ( $core_update ) {
  437. $this->update( 'core', $core_update );
  438. }
  439. // Clean up, and check for any pending translations.
  440. // (Core_Upgrader checks for core updates.)
  441. $theme_stats = array();
  442. if ( isset( $this->update_results['theme'] ) ) {
  443. foreach ( $this->update_results['theme'] as $upgrade ) {
  444. $theme_stats[ $upgrade->item->theme ] = ( true === $upgrade->result );
  445. }
  446. }
  447. wp_update_themes( $theme_stats ); // Check for theme updates.
  448. $plugin_stats = array();
  449. if ( isset( $this->update_results['plugin'] ) ) {
  450. foreach ( $this->update_results['plugin'] as $upgrade ) {
  451. $plugin_stats[ $upgrade->item->plugin ] = ( true === $upgrade->result );
  452. }
  453. }
  454. wp_update_plugins( $plugin_stats ); // Check for plugin updates.
  455. // Finally, process any new translations.
  456. $language_updates = wp_get_translation_updates();
  457. if ( $language_updates ) {
  458. foreach ( $language_updates as $update ) {
  459. $this->update( 'translation', $update );
  460. }
  461. // Clear existing caches.
  462. wp_clean_update_cache();
  463. wp_version_check(); // Check for core updates.
  464. wp_update_themes(); // Check for theme updates.
  465. wp_update_plugins(); // Check for plugin updates.
  466. }
  467. // Send debugging email to admin for all development installations.
  468. if ( ! empty( $this->update_results ) ) {
  469. $development_version = false !== strpos( get_bloginfo( 'version' ), '-' );
  470. /**
  471. * Filters whether to send a debugging email for each automatic background update.
  472. *
  473. * @since 3.7.0
  474. *
  475. * @param bool $development_version By default, emails are sent if the
  476. * install is a development version.
  477. * Return false to avoid the email.
  478. */
  479. if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) ) {
  480. $this->send_debug_email();
  481. }
  482. if ( ! empty( $this->update_results['core'] ) ) {
  483. $this->after_core_update( $this->update_results['core'][0] );
  484. } elseif ( ! empty( $this->update_results['plugin'] ) || ! empty( $this->update_results['theme'] ) ) {
  485. $this->after_plugin_theme_update( $this->update_results );
  486. }
  487. /**
  488. * Fires after all automatic updates have run.
  489. *
  490. * @since 3.8.0
  491. *
  492. * @param array $update_results The results of all attempted updates.
  493. */
  494. do_action( 'automatic_updates_complete', $this->update_results );
  495. }
  496. WP_Upgrader::release_lock( 'auto_updater' );
  497. }
  498. /**
  499. * If we tried to perform a core update, check if we should send an email,
  500. * and if we need to avoid processing future updates.
  501. *
  502. * @since 3.7.0
  503. *
  504. * @param object $update_result The result of the core update. Includes the update offer and result.
  505. */
  506. protected function after_core_update( $update_result ) {
  507. $wp_version = get_bloginfo( 'version' );
  508. $core_update = $update_result->item;
  509. $result = $update_result->result;
  510. if ( ! is_wp_error( $result ) ) {
  511. $this->send_email( 'success', $core_update );
  512. return;
  513. }
  514. $error_code = $result->get_error_code();
  515. // Any of these WP_Error codes are critical failures, as in they occurred after we started to copy core files.
  516. // We should not try to perform a background update again until there is a successful one-click update performed by the user.
  517. $critical = false;
  518. if ( 'disk_full' === $error_code || false !== strpos( $error_code, '__copy_dir' ) ) {
  519. $critical = true;
  520. } elseif ( 'rollback_was_required' === $error_code && is_wp_error( $result->get_error_data()->rollback ) ) {
  521. // A rollback is only critical if it failed too.
  522. $critical = true;
  523. $rollback_result = $result->get_error_data()->rollback;
  524. } elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
  525. $critical = true;
  526. }
  527. if ( $critical ) {
  528. $critical_data = array(
  529. 'attempted' => $core_update->current,
  530. 'current' => $wp_version,
  531. 'error_code' => $error_code,
  532. 'error_data' => $result->get_error_data(),
  533. 'timestamp' => time(),
  534. 'critical' => true,
  535. );
  536. if ( isset( $rollback_result ) ) {
  537. $critical_data['rollback_code'] = $rollback_result->get_error_code();
  538. $critical_data['rollback_data'] = $rollback_result->get_error_data();
  539. }
  540. update_site_option( 'auto_core_update_failed', $critical_data );
  541. $this->send_email( 'critical', $core_update, $result );
  542. return;
  543. }
  544. /*
  545. * Any other WP_Error code (like download_failed or files_not_writable) occurs before
  546. * we tried to copy over core files. Thus, the failures are early and graceful.
  547. *
  548. * We should avoid trying to perform a background update again for the same version.
  549. * But we can try again if another version is released.
  550. *
  551. * For certain 'transient' failures, like download_failed, we should allow retries.
  552. * In fact, let's schedule a special update for an hour from now. (It's possible
  553. * the issue could actually be on WordPress.org's side.) If that one fails, then email.
  554. */
  555. $send = true;
  556. $transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro', 'locked' );
  557. if ( in_array( $error_code, $transient_failures, true ) && ! get_site_option( 'auto_core_update_failed' ) ) {
  558. wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
  559. $send = false;
  560. }
  561. $notified = get_site_option( 'auto_core_update_notified' );
  562. // Don't notify if we've already notified the same email address of the same version of the same notification type.
  563. if ( $notified
  564. && 'fail' === $notified['type']
  565. && get_site_option( 'admin_email' ) === $notified['email']
  566. && $notified['version'] === $core_update->current
  567. ) {
  568. $send = false;
  569. }
  570. update_site_option(
  571. 'auto_core_update_failed',
  572. array(
  573. 'attempted' => $core_update->current,
  574. 'current' => $wp_version,
  575. 'error_code' => $error_code,
  576. 'error_data' => $result->get_error_data(),
  577. 'timestamp' => time(),
  578. 'retry' => in_array( $error_code, $transient_failures, true ),
  579. )
  580. );
  581. if ( $send ) {
  582. $this->send_email( 'fail', $core_update, $result );
  583. }
  584. }
  585. /**
  586. * Sends an email upon the completion or failure of a background core update.
  587. *
  588. * @since 3.7.0
  589. *
  590. * @param string $type The type of email to send. Can be one of 'success', 'fail', 'manual', 'critical'.
  591. * @param object $core_update The update offer that was attempted.
  592. * @param mixed $result Optional. The result for the core update. Can be WP_Error.
  593. */
  594. protected function send_email( $type, $core_update, $result = null ) {
  595. update_site_option(
  596. 'auto_core_update_notified',
  597. array(
  598. 'type' => $type,
  599. 'email' => get_site_option( 'admin_email' ),
  600. 'version' => $core_update->current,
  601. 'timestamp' => time(),
  602. )
  603. );
  604. $next_user_core_update = get_preferred_from_update_core();
  605. // If the update transient is empty, use the update we just performed.
  606. if ( ! $next_user_core_update ) {
  607. $next_user_core_update = $core_update;
  608. }
  609. if ( 'upgrade' === $next_user_core_update->response
  610. && version_compare( $next_user_core_update->version, $core_update->version, '>' )
  611. ) {
  612. $newer_version_available = true;
  613. } else {
  614. $newer_version_available = false;
  615. }
  616. /**
  617. * Filters whether to send an email following an automatic background core update.
  618. *
  619. * @since 3.7.0
  620. *
  621. * @param bool $send Whether to send the email. Default true.
  622. * @param string $type The type of email to send. Can be one of
  623. * 'success', 'fail', 'critical'.
  624. * @param object $core_update The update offer that was attempted.
  625. * @param mixed $result The result for the core update. Can be WP_Error.
  626. */
  627. if ( 'manual' !== $type && ! apply_filters( 'auto_core_update_send_email', true, $type, $core_update, $result ) ) {
  628. return;
  629. }
  630. switch ( $type ) {
  631. case 'success': // We updated.
  632. /* translators: Site updated notification email subject. 1: Site title, 2: WordPress version. */
  633. $subject = __( '[%1$s] Your site has updated to WordPress %2$s' );
  634. break;
  635. case 'fail': // We tried to update but couldn't.
  636. case 'manual': // We can't update (and made no attempt).
  637. /* translators: Update available notification email subject. 1: Site title, 2: WordPress version. */
  638. $subject = __( '[%1$s] WordPress %2$s is available. Please update!' );
  639. break;
  640. case 'critical': // We tried to update, started to copy files, then things went wrong.
  641. /* translators: Site down notification email subject. 1: Site title. */
  642. $subject = __( '[%1$s] URGENT: Your site may be down due to a failed update' );
  643. break;
  644. default:
  645. return;
  646. }
  647. // If the auto-update is not to the latest version, say that the current version of WP is available instead.
  648. $version = 'success' === $type ? $core_update->current : $next_user_core_update->current;
  649. $subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $version );
  650. $body = '';
  651. switch ( $type ) {
  652. case 'success':
  653. $body .= sprintf(
  654. /* translators: 1: Home URL, 2: WordPress version. */
  655. __( 'Howdy! Your site at %1$s has been updated automatically to WordPress %2$s.' ),
  656. home_url(),
  657. $core_update->current
  658. );
  659. $body .= "\n\n";
  660. if ( ! $newer_version_available ) {
  661. $body .= __( 'No further action is needed on your part.' ) . ' ';
  662. }
  663. // Can only reference the About screen if their update was successful.
  664. list( $about_version ) = explode( '-', $core_update->current, 2 );
  665. /* translators: %s: WordPress version. */
  666. $body .= sprintf( __( 'For more on version %s, see the About WordPress screen:' ), $about_version );
  667. $body .= "\n" . admin_url( 'about.php' );
  668. if ( $newer_version_available ) {
  669. /* translators: %s: WordPress latest version. */
  670. $body .= "\n\n" . sprintf( __( 'WordPress %s is also now available.' ), $next_user_core_update->current ) . ' ';
  671. $body .= __( 'Updating is easy and only takes a few moments:' );
  672. $body .= "\n" . network_admin_url( 'update-core.php' );
  673. }
  674. break;
  675. case 'fail':
  676. case 'manual':
  677. $body .= sprintf(
  678. /* translators: 1: Home URL, 2: WordPress version. */
  679. __( 'Please update your site at %1$s to WordPress %2$s.' ),
  680. home_url(),
  681. $next_user_core_update->current
  682. );
  683. $body .= "\n\n";
  684. // Don't show this message if there is a newer version available.
  685. // Potential for confusion, and also not useful for them to know at this point.
  686. if ( 'fail' === $type && ! $newer_version_available ) {
  687. $body .= __( 'We tried but were unable to update your site automatically.' ) . ' ';
  688. }
  689. $body .= __( 'Updating is easy and only takes a few moments:' );
  690. $body .= "\n" . network_admin_url( 'update-core.php' );
  691. break;
  692. case 'critical':
  693. if ( $newer_version_available ) {
  694. $body .= sprintf(
  695. /* translators: 1: Home URL, 2: WordPress version. */
  696. __( 'Your site at %1$s experienced a critical failure while trying to update WordPress to version %2$s.' ),
  697. home_url(),
  698. $core_update->current
  699. );
  700. } else {
  701. $body .= sprintf(
  702. /* translators: 1: Home URL, 2: WordPress latest version. */
  703. __( 'Your site at %1$s experienced a critical failure while trying to update to the latest version of WordPress, %2$s.' ),
  704. home_url(),
  705. $core_update->current
  706. );
  707. }
  708. $body .= "\n\n" . __( "This means your site may be offline or broken. Don't panic; this can be fixed." );
  709. $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:" );
  710. $body .= "\n" . network_admin_url( 'update-core.php' );
  711. break;
  712. }
  713. $critical_support = 'critical' === $type && ! empty( $core_update->support_email );
  714. if ( $critical_support ) {
  715. // Support offer if available.
  716. $body .= "\n\n" . sprintf(
  717. /* translators: %s: Support email address. */
  718. __( '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.' ),
  719. $core_update->support_email
  720. );
  721. } else {
  722. // Add a note about the support forums.
  723. $body .= "\n\n" . __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
  724. $body .= "\n" . __( 'https://wordpress.org/support/forums/' );
  725. }
  726. // Updates are important!
  727. if ( 'success' !== $type || $newer_version_available ) {
  728. $body .= "\n\n" . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' );
  729. }
  730. if ( $critical_support ) {
  731. $body .= ' ' . __( "If you reach out to us, we'll also ensure you'll never have this problem again." );
  732. }
  733. // If things are successful and we're now on the latest, mention plugins and themes if any are out of date.
  734. if ( 'success' === $type && ! $newer_version_available && ( get_plugin_updates() || get_theme_updates() ) ) {
  735. $body .= "\n\n" . __( 'You also have some plugins or themes with updates available. Update them now:' );
  736. $body .= "\n" . network_admin_url();
  737. }
  738. $body .= "\n\n" . __( 'The WordPress Team' ) . "\n";
  739. if ( 'critical' === $type && is_wp_error( $result ) ) {
  740. $body .= "\n***\n\n";
  741. /* translators: %s: WordPress version. */
  742. $body .= sprintf( __( 'Your site was running version %s.' ), get_bloginfo( 'version' ) );
  743. $body .= ' ' . __( 'We have some data that describes the error your site encountered.' );
  744. $body .= ' ' . __( 'Your hosting company, support forum volunteers, or a friendly developer may be able to use this information to help you:' );
  745. // If we had a rollback and we're still critical, then the rollback failed too.
  746. // Loop through all errors (the main WP_Error, the update result, the rollback result) for code, data, etc.
  747. if ( 'rollback_was_required' === $result->get_error_code() ) {
  748. $errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
  749. } else {
  750. $errors = array( $result );
  751. }
  752. foreach ( $errors as $error ) {
  753. if ( ! is_wp_error( $error ) ) {
  754. continue;
  755. }
  756. $error_code = $error->get_error_code();
  757. /* translators: %s: Error code. */
  758. $body .= "\n\n" . sprintf( __( 'Error code: %s' ), $error_code );
  759. if ( 'rollback_was_required' === $error_code ) {
  760. continue;
  761. }
  762. if ( $error->get_error_message() ) {
  763. $body .= "\n" . $error->get_error_message();
  764. }
  765. $error_data = $error->get_error_data();
  766. if ( $error_data ) {
  767. $body .= "\n" . implode( ', ', (array) $error_data );
  768. }
  769. }
  770. $body .= "\n";
  771. }
  772. $to = get_site_option( 'admin_email' );
  773. $headers = '';
  774. $email = compact( 'to', 'subject', 'body', 'headers' );
  775. /**
  776. * Filters the email sent following an automatic background core update.
  777. *
  778. * @since 3.7.0
  779. *
  780. * @param array $email {
  781. * Array of email arguments that will be passed to wp_mail().
  782. *
  783. * @type string $to The email recipient. An array of emails
  784. * can be returned, as handled by wp_mail().
  785. * @type string $subject The email's subject.
  786. * @type string $body The email message body.
  787. * @type string $headers Any email headers, defaults to no headers.
  788. * }
  789. * @param string $type The type of email being sent. Can be one of
  790. * 'success', 'fail', 'manual', 'critical'.
  791. * @param object $core_update The update offer that was attempted.
  792. * @param mixed $result The result for the core update. Can be WP_Error.
  793. */
  794. $email = apply_filters( 'auto_core_update_email', $email, $type, $core_update, $result );
  795. wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
  796. }
  797. /**
  798. * If we tried to perform plugin or theme updates, check if we should send an email.
  799. *
  800. * @since 5.5.0
  801. *
  802. * @param array $update_results The results of update tasks.
  803. */
  804. protected function after_plugin_theme_update( $update_results ) {
  805. $successful_updates = array();
  806. $failed_updates = array();
  807. if ( ! empty( $update_results['plugin'] ) ) {
  808. /**
  809. * Filters whether to send an email following an automatic background plugin update.
  810. *
  811. * @since 5.5.0
  812. * @since 5.5.1 Added the `$update_results` parameter.
  813. *
  814. * @param bool $enabled True if plugin update notifications are enabled, false otherwise.
  815. * @param array $update_results The results of plugins update tasks.
  816. */
  817. $notifications_enabled = apply_filters( 'auto_plugin_update_send_email', true, $update_results['plugin'] );
  818. if ( $notifications_enabled ) {
  819. foreach ( $update_results['plugin'] as $update_result ) {
  820. if ( true === $update_result->result ) {
  821. $successful_updates['plugin'][] = $update_result;
  822. } else {
  823. $failed_updates['plugin'][] = $update_result;
  824. }
  825. }
  826. }
  827. }
  828. if ( ! empty( $update_results['theme'] ) ) {
  829. /**
  830. * Filters whether to send an email following an automatic background theme update.
  831. *
  832. * @since 5.5.0
  833. * @since 5.5.1 Added the `$update_results` parameter.
  834. *
  835. * @param bool $enabled True if theme update notifications are enabled, false otherwise.
  836. * @param array $update_results The results of theme update tasks.
  837. */
  838. $notifications_enabled = apply_filters( 'auto_theme_update_send_email', true, $update_results['theme'] );
  839. if ( $notifications_enabled ) {
  840. foreach ( $update_results['theme'] as $update_result ) {
  841. if ( true === $update_result->result ) {
  842. $successful_updates['theme'][] = $update_result;
  843. } else {
  844. $failed_updates['theme'][] = $update_result;
  845. }
  846. }
  847. }
  848. }
  849. if ( empty( $successful_updates ) && empty( $failed_updates ) ) {
  850. return;
  851. }
  852. if ( empty( $failed_updates ) ) {
  853. $this->send_plugin_theme_email( 'success', $successful_updates, $failed_updates );
  854. } elseif ( empty( $successful_updates ) ) {
  855. $this->send_plugin_theme_email( 'fail', $successful_updates, $failed_updates );
  856. } else {
  857. $this->send_plugin_theme_email( 'mixed', $successful_updates, $failed_updates );
  858. }
  859. }
  860. /**
  861. * Sends an email upon the completion or failure of a plugin or theme background update.
  862. *
  863. * @since 5.5.0
  864. *
  865. * @param string $type The type of email to send. Can be one of 'success', 'fail', 'mixed'.
  866. * @param array $successful_updates A list of updates that succeeded.
  867. * @param array $failed_updates A list of updates that failed.
  868. */
  869. protected function send_plugin_theme_email( $type, $successful_updates, $failed_updates ) {
  870. // No updates were attempted.
  871. if ( empty( $successful_updates ) && empty( $failed_updates ) ) {
  872. return;
  873. }
  874. $unique_failures = false;
  875. $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
  876. /*
  877. * When only failures have occurred, an email should only be sent if there are unique failures.
  878. * A failure is considered unique if an email has not been sent for an update attempt failure
  879. * to a plugin or theme with the same new_version.
  880. */
  881. if ( 'fail' === $type ) {
  882. foreach ( $failed_updates as $update_type => $failures ) {
  883. foreach ( $failures as $failed_update ) {
  884. if ( ! isset( $past_failure_emails[ $failed_update->item->{$update_type} ] ) ) {
  885. $unique_failures = true;
  886. continue;
  887. }
  888. // Check that the failure represents a new failure based on the new_version.
  889. if ( version_compare( $past_failure_emails[ $failed_update->item->{$update_type} ], $failed_update->item->new_version, '<' ) ) {
  890. $unique_failures = true;
  891. }
  892. }
  893. }
  894. if ( ! $unique_failures ) {
  895. return;
  896. }
  897. }
  898. $body = array();
  899. $successful_plugins = ( ! empty( $successful_updates['plugin'] ) );
  900. $successful_themes = ( ! empty( $successful_updates['theme'] ) );
  901. $failed_plugins = ( ! empty( $failed_updates['plugin'] ) );
  902. $failed_themes = ( ! empty( $failed_updates['theme'] ) );
  903. switch ( $type ) {
  904. case 'success':
  905. if ( $successful_plugins && $successful_themes ) {
  906. /* translators: %s: Site title. */
  907. $subject = __( '[%s] Some plugins and themes have automatically updated' );
  908. $body[] = sprintf(
  909. /* translators: %s: Home URL. */
  910. __( 'Howdy! Some plugins and themes have automatically updated to their latest versions on your site at %s. No further action is needed on your part.' ),
  911. home_url()
  912. );
  913. } elseif ( $successful_plugins ) {
  914. /* translators: %s: Site title. */
  915. $subject = __( '[%s] Some plugins were automatically updated' );
  916. $body[] = sprintf(
  917. /* translators: %s: Home URL. */
  918. __( 'Howdy! Some plugins have automatically updated to their latest versions on your site at %s. No further action is needed on your part.' ),
  919. home_url()
  920. );
  921. } else {
  922. /* translators: %s: Site title. */
  923. $subject = __( '[%s] Some themes were automatically updated' );
  924. $body[] = sprintf(
  925. /* translators: %s: Home URL. */
  926. __( 'Howdy! Some themes have automatically updated to their latest versions on your site at %s. No further action is needed on your part.' ),
  927. home_url()
  928. );
  929. }
  930. break;
  931. case 'fail':
  932. case 'mixed':
  933. if ( $failed_plugins && $failed_themes ) {
  934. /* translators: %s: Site title. */
  935. $subject = __( '[%s] Some plugins and themes have failed to update' );
  936. $body[] = sprintf(
  937. /* translators: %s: Home URL. */
  938. __( 'Howdy! Plugins and themes failed to update on your site at %s.' ),
  939. home_url()
  940. );
  941. } elseif ( $failed_plugins ) {
  942. /* translators: %s: Site title. */
  943. $subject = __( '[%s] Some plugins have failed to update' );
  944. $body[] = sprintf(
  945. /* translators: %s: Home URL. */
  946. __( 'Howdy! Plugins failed to update on your site at %s.' ),
  947. home_url()
  948. );
  949. } else {
  950. /* translators: %s: Site title. */
  951. $subject = __( '[%s] Some themes have failed to update' );
  952. $body[] = sprintf(
  953. /* translators: %s: Home URL. */
  954. __( 'Howdy! Themes failed to update on your site at %s.' ),
  955. home_url()
  956. );
  957. }
  958. break;
  959. }
  960. if ( in_array( $type, array( 'fail', 'mixed' ), true ) ) {
  961. $body[] = "\n";
  962. $body[] = __( 'Please check your site now. It’s possible that everything is working. If there are updates available, you should update.' );
  963. $body[] = "\n";
  964. // List failed plugin updates.
  965. if ( ! empty( $failed_updates['plugin'] ) ) {
  966. $body[] = __( 'These plugins failed to update:' );
  967. foreach ( $failed_updates['plugin'] as $item ) {
  968. if ( $item->item->current_version ) {
  969. $body[] = sprintf(
  970. /* translators: 1: Plugin name, 2: Current version number, 3: New version number. */
  971. __( '- %1$s (from version %2$s to %3$s)' ),
  972. $item->name,
  973. $item->item->current_version,
  974. $item->item->new_version
  975. );
  976. } else {
  977. $body[] = sprintf(
  978. /* translators: 1: Plugin name, 2: Version number. */
  979. __( '- %1$s version %2$s' ),
  980. $item->name,
  981. $item->item->new_version
  982. );
  983. }
  984. $past_failure_emails[ $item->item->plugin ] = $item->item->new_version;
  985. }
  986. $body[] = "\n";
  987. }
  988. // List failed theme updates.
  989. if ( ! empty( $failed_updates['theme'] ) ) {
  990. $body[] = __( 'These themes failed to update:' );
  991. foreach ( $failed_updates['theme'] as $item ) {
  992. if ( $item->item->current_version ) {
  993. $body[] = sprintf(
  994. /* translators: 1: Theme name, 2: Current version number, 3: New version number. */
  995. __( '- %1$s (from version %2$s to %3$s)' ),
  996. $item->name,
  997. $item->item->current_version,
  998. $item->item->new_version
  999. );
  1000. } else {
  1001. $body[] = sprintf(
  1002. /* translators: 1: Theme name, 2: Version number. */
  1003. __( '- %1$s version %2$s' ),
  1004. $item->name,
  1005. $item->item->new_version
  1006. );
  1007. }
  1008. $past_failure_emails[ $item->item->theme ] = $item->item->new_version;
  1009. }
  1010. $body[] = "\n";
  1011. }
  1012. }
  1013. // List successful updates.
  1014. if ( in_array( $type, array( 'success', 'mixed' ), true ) ) {
  1015. $body[] = "\n";
  1016. // List successful plugin updates.
  1017. if ( ! empty( $successful_updates['plugin'] ) ) {
  1018. $body[] = __( 'These plugins are now up to date:' );
  1019. foreach ( $successful_updates['plugin'] as $item ) {
  1020. if ( $item->item->current_version ) {
  1021. $body[] = sprintf(
  1022. /* translators: 1: Plugin name, 2: Current version number, 3: New version number. */
  1023. __( '- %1$s (from version %2$s to %3$s)' ),
  1024. $item->name,
  1025. $item->item->current_version,
  1026. $item->item->new_version
  1027. );
  1028. } else {
  1029. $body[] = sprintf(
  1030. /* translators: 1: Plugin name, 2: Version number. */
  1031. __( '- %1$s version %2$s' ),
  1032. $item->name,
  1033. $item->item->new_version
  1034. );
  1035. }
  1036. unset( $past_failure_emails[ $item->item->plugin ] );
  1037. }
  1038. $body[] = "\n";
  1039. }
  1040. // List successful theme updates.
  1041. if ( ! empty( $successful_updates['theme'] ) ) {
  1042. $body[] = __( 'These themes are now up to date:' );
  1043. foreach ( $successful_updates['theme'] as $item ) {
  1044. if ( $item->item->current_version ) {
  1045. $body[] = sprintf(
  1046. /* translators: 1: Theme name, 2: Current version number, 3: New version number. */
  1047. __( '- %1$s (from version %2$s to %3$s)' ),
  1048. $item->name,
  1049. $item->item->current_version,
  1050. $item->item->new_version
  1051. );
  1052. } else {
  1053. $body[] = sprintf(
  1054. /* translators: 1: Theme name, 2: Version number. */
  1055. __( '- %1$s version %2$s' ),
  1056. $item->name,
  1057. $item->item->new_version
  1058. );
  1059. }
  1060. unset( $past_failure_emails[ $item->item->theme ] );
  1061. }
  1062. $body[] = "\n";
  1063. }
  1064. }
  1065. if ( $failed_plugins ) {
  1066. $body[] = sprintf(
  1067. /* translators: %s: Plugins screen URL. */
  1068. __( 'To manage plugins on your site, visit the Plugins page: %s' ),
  1069. admin_url( 'plugins.php' )
  1070. );
  1071. $body[] = "\n";
  1072. }
  1073. if ( $failed_themes ) {
  1074. $body[] = sprintf(
  1075. /* translators: %s: Themes screen URL. */
  1076. __( 'To manage themes on your site, visit the Themes page: %s' ),
  1077. admin_url( 'themes.php' )
  1078. );
  1079. $body[] = "\n";
  1080. }
  1081. // Add a note about the support forums.
  1082. $body[] = __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
  1083. $body[] = __( 'https://wordpress.org/support/forums/' );
  1084. $body[] = "\n" . __( 'The WordPress Team' );
  1085. $body = implode( "\n", $body );
  1086. $to = get_site_option( 'admin_email' );
  1087. $subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) );
  1088. $headers = '';
  1089. $email = compact( 'to', 'subject', 'body', 'headers' );
  1090. /**
  1091. * Filters the email sent following an automatic background update for plugins and themes.
  1092. *
  1093. * @since 5.5.0
  1094. *
  1095. * @param array $email {
  1096. * Array of email arguments that will be passed to wp_mail().
  1097. *
  1098. * @type string $to The email recipient. An array of emails
  1099. * can be returned, as handled by wp_mail().
  1100. * @type string $subject The email's subject.
  1101. * @type string $body The email message body.
  1102. * @type string $headers Any email headers, defaults to no headers.
  1103. * }
  1104. * @param string $type The type of email being sent. Can be one of 'success', 'fail', 'mixed'.
  1105. * @param array $successful_updates A list of updates that succeeded.
  1106. * @param array $failed_updates A list of updates that failed.
  1107. */
  1108. $email = apply_filters( 'auto_plugin_theme_update_email', $email, $type, $successful_updates, $failed_updates );
  1109. $result = wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
  1110. if ( $result ) {
  1111. update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
  1112. }
  1113. }
  1114. /**
  1115. * Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
  1116. *
  1117. * @since 3.7.0
  1118. */
  1119. protected function send_debug_email() {
  1120. $update_count = 0;
  1121. foreach ( $this->update_results as $type => $updates ) {
  1122. $update_count += count( $updates );
  1123. }
  1124. $body = array();
  1125. $failures = 0;
  1126. /* translators: %s: Network home URL. */
  1127. $body[] = sprintf( __( 'WordPress site: %s' ), network_home_url( '/' ) );
  1128. // Core.
  1129. if ( isset( $this->update_results['core'] ) ) {
  1130. $result = $this->update_results['core'][0];
  1131. if ( $result->result && ! is_wp_error( $result->result ) ) {
  1132. /* translators: %s: WordPress version. */
  1133. $body[] = sprintf( __( 'SUCCESS: WordPress was successfully updated to %s' ), $result->name );
  1134. } else {
  1135. /* translators: %s: WordPress version. */
  1136. $body[] = sprintf( __( 'FAILED: WordPress failed to update to %s' ), $result->name );
  1137. $failures++;
  1138. }
  1139. $body[] = '';
  1140. }
  1141. // Plugins, Themes, Translations.
  1142. foreach ( array( 'plugin', 'theme', 'translation' ) as $type ) {
  1143. if ( ! isset( $this->update_results[ $type ] ) ) {
  1144. continue;
  1145. }
  1146. $success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
  1147. if ( $success_items ) {
  1148. $messages = array(
  1149. 'plugin' => __( 'The following plugins were successfully updated:' ),
  1150. 'theme' => __( 'The following themes were successfully updated:' ),
  1151. 'translation' => __( 'The following translations were successfully updated:' ),
  1152. );
  1153. $body[] = $messages[ $type ];
  1154. foreach ( wp_list_pluck( $success_items, 'name' ) as $name ) {
  1155. /* translators: %s: Name of plugin / theme / translation. */
  1156. $body[] = ' * ' . sprintf( __( 'SUCCESS: %s' ), $name );
  1157. }
  1158. }
  1159. if ( $success_items !== $this->update_results[ $type ] ) {
  1160. // Failed updates.
  1161. $messages = array(
  1162. 'plugin' => __( 'The following plugins failed to update:' ),
  1163. 'theme' => __( 'The following themes failed to update:' ),
  1164. 'translation' => __( 'The following translations failed to update:' ),
  1165. );
  1166. $body[] = $messages[ $type ];
  1167. foreach ( $this->update_results[ $type ] as $item ) {
  1168. if ( ! $item->result || is_wp_error( $item->result ) ) {
  1169. /* translators: %s: Name of plugin / theme / translation. */
  1170. $body[] = ' * ' . sprintf( __( 'FAILED: %s' ), $item->name );
  1171. $failures++;
  1172. }
  1173. }
  1174. }
  1175. $body[] = '';
  1176. }
  1177. $site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
  1178. if ( $failures ) {
  1179. $body[] = trim(
  1180. __(
  1181. "BETA TESTING?
  1182. =============
  1183. This debugging email is sent when you are using a development version of WordPress.
  1184. If you think these failures might be due to a bug in WordPress, could you report it?
  1185. * Open a thread in the support forums: https://wordpress.org/support/forum/alphabeta
  1186. * Or, if you're comfortable writing a bug report: https://core.trac.wordpress.org/
  1187. Thanks! -- The WordPress Team"
  1188. )
  1189. );
  1190. $body[] = '';
  1191. /* translators: Background update failed notification email subject. %s: Site title. */
  1192. $subject = sprintf( __( '[%s] Background Update Failed' ), $site_title );
  1193. } else {
  1194. /* translators: Background update finished notification email subject. %s: Site title. */
  1195. $subject = sprintf( __( '[%s] Background Update Finished' ), $site_title );
  1196. }
  1197. $body[] = trim(
  1198. __(
  1199. 'UPDATE LOG
  1200. =========='
  1201. )
  1202. );
  1203. $body[] = '';
  1204. foreach ( array( 'core', 'plugin', 'theme', 'translation' ) as $type ) {
  1205. if ( ! isset( $this->update_results[ $type ] ) ) {
  1206. continue;
  1207. }
  1208. foreach ( $this->update_results[ $type ] as $update ) {
  1209. $body[] = $update->name;
  1210. $body[] = str_repeat( '-', strlen( $update->name ) );
  1211. foreach ( $update->messages as $message ) {
  1212. $body[] = ' ' . html_entity_decode( str_replace( '&#8230;', '...', $message ) );
  1213. }
  1214. if ( is_wp_error( $update->result ) ) {
  1215. $results = array( 'update' => $update->result );
  1216. // If we rolled back, we want to know an error that occurred then too.
  1217. if ( 'rollback_was_required' === $update->result->get_error_code() ) {
  1218. $results = (array) $update->result->get_error_data();
  1219. }
  1220. foreach ( $results as $result_type => $result ) {
  1221. if ( ! is_wp_error( $result ) ) {
  1222. continue;
  1223. }
  1224. if ( 'rollback' === $result_type ) {
  1225. /* translators: 1: Error code, 2: Error message. */
  1226. $body[] = ' ' . sprintf( __( 'Rollback Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
  1227. } else {
  1228. /* translators: 1: Error code, 2: Error message. */
  1229. $body[] = ' ' . sprintf( __( 'Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
  1230. }
  1231. if ( $result->get_error_data() ) {
  1232. $body[] = ' ' . implode( ', ', (array) $result->get_error_data() );
  1233. }
  1234. }
  1235. }
  1236. $body[] = '';
  1237. }
  1238. }
  1239. $email = array(
  1240. 'to' => get_site_option( 'admin_email' ),
  1241. 'subject' => $subject,
  1242. 'body' => implode( "\n", $body ),
  1243. 'headers' => '',
  1244. );
  1245. /**
  1246. * Filters the debug email that can be sent following an automatic
  1247. * background core update.
  1248. *
  1249. * @since 3.8.0
  1250. *
  1251. * @param array $email {
  1252. * Array of email arguments that will be passed to wp_mail().
  1253. *
  1254. * @type string $to The email recipient. An array of emails
  1255. * can be returned, as handled by wp_mail().
  1256. * @type string $subject Email subject.
  1257. * @type string $body Email message body.
  1258. * @type string $headers Any email headers. Default empty.
  1259. * }
  1260. * @param int $failures The number of failures encountered while upgrading.
  1261. * @param mixed $results The results of all attempted updates.
  1262. */
  1263. $email = apply_filters( 'automatic_updates_debug_email', $email, $failures, $this->update_results );
  1264. wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
  1265. }
  1266. }