[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref