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


Generated: Tue Oct 19 01:00:04 2021 Cross-referenced by PHPXref 0.7.1