class-wp-automatic-updater.php 50 KB

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