Açıklama Yok

class-wp-automatic-updater.php 50KB

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