[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/includes/ -> class-wp-automatic-updater.php (source)

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


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1