wp/wp-admin/includes/class-wp-automatic-updater.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    27 	 *
    27 	 *
    28 	 * @since 3.7.0
    28 	 * @since 3.7.0
    29 	 */
    29 	 */
    30 	public function is_disabled() {
    30 	public function is_disabled() {
    31 		// Background updates are disabled if you don't want file changes.
    31 		// Background updates are disabled if you don't want file changes.
    32 		if ( ! wp_is_file_mod_allowed( 'automatic_updater' ) )
    32 		if ( ! wp_is_file_mod_allowed( 'automatic_updater' ) ) {
    33 			return true;
    33 			return true;
    34 
    34 		}
    35 		if ( wp_installing() )
    35 
       
    36 		if ( wp_installing() ) {
    36 			return true;
    37 			return true;
       
    38 		}
    37 
    39 
    38 		// More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
    40 		// More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
    39 		$disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED;
    41 		$disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED;
    40 
    42 
    41 		/**
    43 		/**
    69 	 *
    71 	 *
    70 	 * @param string $context The filesystem path to check, in addition to ABSPATH.
    72 	 * @param string $context The filesystem path to check, in addition to ABSPATH.
    71 	 */
    73 	 */
    72 	public function is_vcs_checkout( $context ) {
    74 	public function is_vcs_checkout( $context ) {
    73 		$context_dirs = array( untrailingslashit( $context ) );
    75 		$context_dirs = array( untrailingslashit( $context ) );
    74 		if ( $context !== ABSPATH )
    76 		if ( $context !== ABSPATH ) {
    75 			$context_dirs[] = untrailingslashit( ABSPATH );
    77 			$context_dirs[] = untrailingslashit( ABSPATH );
    76 
    78 		}
    77 		$vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
    79 
       
    80 		$vcs_dirs   = array( '.svn', '.git', '.hg', '.bzr' );
    78 		$check_dirs = array();
    81 		$check_dirs = array();
    79 
    82 
    80 		foreach ( $context_dirs as $context_dir ) {
    83 		foreach ( $context_dirs as $context_dir ) {
    81 			// Walk up from $context_dir to the root.
    84 			// Walk up from $context_dir to the root.
    82 			do {
    85 			do {
    83 				$check_dirs[] = $context_dir;
    86 				$check_dirs[] = $context_dir;
    84 
    87 
    85 				// Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
    88 				// Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
    86 				if ( $context_dir == dirname( $context_dir ) )
    89 				if ( $context_dir == dirname( $context_dir ) ) {
    87 					break;
    90 					break;
    88 
    91 				}
    89 			// Continue one level at a time.
    92 
       
    93 				// Continue one level at a time.
    90 			} while ( $context_dir = dirname( $context_dir ) );
    94 			} while ( $context_dir = dirname( $context_dir ) );
    91 		}
    95 		}
    92 
    96 
    93 		$check_dirs = array_unique( $check_dirs );
    97 		$check_dirs = array_unique( $check_dirs );
    94 
    98 
    95 		// Search all directories we've found for evidence of version control.
    99 		// Search all directories we've found for evidence of version control.
    96 		foreach ( $vcs_dirs as $vcs_dir ) {
   100 		foreach ( $vcs_dirs as $vcs_dir ) {
    97 			foreach ( $check_dirs as $check_dir ) {
   101 			foreach ( $check_dirs as $check_dir ) {
    98 				if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) )
   102 				if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) ) {
    99 					break 2;
   103 					break 2;
       
   104 				}
   100 			}
   105 			}
   101 		}
   106 		}
   102 
   107 
   103 		/**
   108 		/**
   104 		 * Filters whether the automatic updater should consider a filesystem
   109 		 * Filters whether the automatic updater should consider a filesystem
   129 	 */
   134 	 */
   130 	public function should_update( $type, $item, $context ) {
   135 	public function should_update( $type, $item, $context ) {
   131 		// Used to see if WP_Filesystem is set up to allow unattended updates.
   136 		// Used to see if WP_Filesystem is set up to allow unattended updates.
   132 		$skin = new Automatic_Upgrader_Skin;
   137 		$skin = new Automatic_Upgrader_Skin;
   133 
   138 
   134 		if ( $this->is_disabled() )
   139 		if ( $this->is_disabled() ) {
   135 			return false;
   140 			return false;
       
   141 		}
   136 
   142 
   137 		// Only relax the filesystem checks when the update doesn't include new files
   143 		// Only relax the filesystem checks when the update doesn't include new files
   138 		$allow_relaxed_file_ownership = false;
   144 		$allow_relaxed_file_ownership = false;
   139 		if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
   145 		if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
   140 			$allow_relaxed_file_ownership = true;
   146 			$allow_relaxed_file_ownership = true;
   141 		}
   147 		}
   142 
   148 
   143 		// If we can't do an auto core update, we may still be able to email the user.
   149 		// If we can't do an auto core update, we may still be able to email the user.
   144 		if ( ! $skin->request_filesystem_credentials( false, $context, $allow_relaxed_file_ownership ) || $this->is_vcs_checkout( $context ) ) {
   150 		if ( ! $skin->request_filesystem_credentials( false, $context, $allow_relaxed_file_ownership ) || $this->is_vcs_checkout( $context ) ) {
   145 			if ( 'core' == $type )
   151 			if ( 'core' == $type ) {
   146 				$this->send_core_update_notification_email( $item );
   152 				$this->send_core_update_notification_email( $item );
       
   153 			}
   147 			return false;
   154 			return false;
   148 		}
   155 		}
   149 
   156 
   150 		// Next up, is this an item we can update?
   157 		// Next up, is this an item we can update?
   151 		if ( 'core' == $type )
   158 		if ( 'core' == $type ) {
   152 			$update = Core_Upgrader::should_update_to_version( $item->current );
   159 			$update = Core_Upgrader::should_update_to_version( $item->current );
   153 		else
   160 		} else {
   154 			$update = ! empty( $item->autoupdate );
   161 			$update = ! empty( $item->autoupdate );
       
   162 		}
   155 
   163 
   156 		/**
   164 		/**
   157 		 * Filters whether to automatically update core, a plugin, a theme, or a language.
   165 		 * Filters whether to automatically update core, a plugin, a theme, or a language.
   158 		 *
   166 		 *
   159 		 * The dynamic portion of the hook name, `$type`, refers to the type of update
   167 		 * The dynamic portion of the hook name, `$type`, refers to the type of update
   161 		 *
   169 		 *
   162 		 * Generally speaking, plugins, themes, and major core versions are not updated
   170 		 * Generally speaking, plugins, themes, and major core versions are not updated
   163 		 * by default, while translations and minor and development versions for core
   171 		 * by default, while translations and minor and development versions for core
   164 		 * are updated by default.
   172 		 * are updated by default.
   165 		 *
   173 		 *
   166 		 * See the {@see 'allow_dev_auto_core_updates', {@see 'allow_minor_auto_core_updates'},
   174 		 * See the {@see 'allow_dev_auto_core_updates'}, {@see 'allow_minor_auto_core_updates'},
   167 		 * and {@see 'allow_major_auto_core_updates'} filters for a more straightforward way to
   175 		 * and {@see 'allow_major_auto_core_updates'} filters for a more straightforward way to
   168 		 * adjust core updates.
   176 		 * adjust core updates.
   169 		 *
   177 		 *
   170 		 * @since 3.7.0
   178 		 * @since 3.7.0
   171 		 *
   179 		 *
   173 		 * @param object $item   The update offer.
   181 		 * @param object $item   The update offer.
   174 		 */
   182 		 */
   175 		$update = apply_filters( "auto_update_{$type}", $update, $item );
   183 		$update = apply_filters( "auto_update_{$type}", $update, $item );
   176 
   184 
   177 		if ( ! $update ) {
   185 		if ( ! $update ) {
   178 			if ( 'core' == $type )
   186 			if ( 'core' == $type ) {
   179 				$this->send_core_update_notification_email( $item );
   187 				$this->send_core_update_notification_email( $item );
       
   188 			}
   180 			return false;
   189 			return false;
   181 		}
   190 		}
   182 
   191 
   183 		// If it's a core update, are we actually compatible with its requirements?
   192 		// If it's a core update, are we actually compatible with its requirements?
   184 		if ( 'core' == $type ) {
   193 		if ( 'core' == $type ) {
   185 			global $wpdb;
   194 			global $wpdb;
   186 
   195 
   187 			$php_compat = version_compare( phpversion(), $item->php_version, '>=' );
   196 			$php_compat = version_compare( phpversion(), $item->php_version, '>=' );
   188 			if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) )
   197 			if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
   189 				$mysql_compat = true;
   198 				$mysql_compat = true;
   190 			else
   199 			} else {
   191 				$mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
   200 				$mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
   192 
   201 			}
   193 			if ( ! $php_compat || ! $mysql_compat )
   202 
       
   203 			if ( ! $php_compat || ! $mysql_compat ) {
   194 				return false;
   204 				return false;
       
   205 			}
       
   206 		}
       
   207 
       
   208 		// If updating a plugin, ensure the minimum PHP version requirements are satisfied.
       
   209 		if ( 'plugin' === $type ) {
       
   210 			if ( ! empty( $item->requires_php ) && version_compare( phpversion(), $item->requires_php, '<' ) ) {
       
   211 				return false;
       
   212 			}
   195 		}
   213 		}
   196 
   214 
   197 		return true;
   215 		return true;
   198 	}
   216 	}
   199 
   217 
   206 	 */
   224 	 */
   207 	protected function send_core_update_notification_email( $item ) {
   225 	protected function send_core_update_notification_email( $item ) {
   208 		$notified = get_site_option( 'auto_core_update_notified' );
   226 		$notified = get_site_option( 'auto_core_update_notified' );
   209 
   227 
   210 		// Don't notify if we've already notified the same email address of the same version.
   228 		// Don't notify if we've already notified the same email address of the same version.
   211 		if ( $notified && $notified['email'] == get_site_option( 'admin_email' ) && $notified['version'] == $item->current )
   229 		if ( $notified && $notified['email'] == get_site_option( 'admin_email' ) && $notified['version'] == $item->current ) {
   212 			return false;
   230 			return false;
       
   231 		}
   213 
   232 
   214 		// See if we need to notify users of a core update.
   233 		// See if we need to notify users of a core update.
   215 		$notify = ! empty( $item->notify_email );
   234 		$notify = ! empty( $item->notify_email );
   216 
   235 
   217 		/**
   236 		/**
   231 		 * @since 3.7.0
   250 		 * @since 3.7.0
   232 		 *
   251 		 *
   233 		 * @param bool   $notify Whether the site administrator is notified.
   252 		 * @param bool   $notify Whether the site administrator is notified.
   234 		 * @param object $item   The update offer.
   253 		 * @param object $item   The update offer.
   235 		 */
   254 		 */
   236 		if ( ! apply_filters( 'send_core_update_notification_email', $notify, $item ) )
   255 		if ( ! apply_filters( 'send_core_update_notification_email', $notify, $item ) ) {
   237 			return false;
   256 			return false;
       
   257 		}
   238 
   258 
   239 		$this->send_email( 'manual', $item );
   259 		$this->send_email( 'manual', $item );
   240 		return true;
   260 		return true;
   241 	}
   261 	}
   242 
   262 
   273 				$context  = WP_CONTENT_DIR; // WP_LANG_DIR;
   293 				$context  = WP_CONTENT_DIR; // WP_LANG_DIR;
   274 				break;
   294 				break;
   275 		}
   295 		}
   276 
   296 
   277 		// Determine whether we can and should perform this update.
   297 		// Determine whether we can and should perform this update.
   278 		if ( ! $this->should_update( $type, $item, $context ) )
   298 		if ( ! $this->should_update( $type, $item, $context ) ) {
   279 			return false;
   299 			return false;
       
   300 		}
   280 
   301 
   281 		/**
   302 		/**
   282 		 * Fires immediately prior to an auto-update.
   303 		 * Fires immediately prior to an auto-update.
   283 		 *
   304 		 *
   284 		 * @since 4.4.0
   305 		 * @since 4.4.0
   291 		do_action( 'pre_auto_update', $type, $item, $context );
   312 		do_action( 'pre_auto_update', $type, $item, $context );
   292 
   313 
   293 		$upgrader_item = $item;
   314 		$upgrader_item = $item;
   294 		switch ( $type ) {
   315 		switch ( $type ) {
   295 			case 'core':
   316 			case 'core':
       
   317 				/* translators: %s: WordPress version */
   296 				$skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
   318 				$skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
       
   319 				/* translators: %s: WordPress version */
   297 				$item_name = sprintf( __( 'WordPress %s' ), $item->version );
   320 				$item_name = sprintf( __( 'WordPress %s' ), $item->version );
   298 				break;
   321 				break;
   299 			case 'theme':
   322 			case 'theme':
   300 				$upgrader_item = $item->theme;
   323 				$upgrader_item = $item->theme;
   301 				$theme = wp_get_theme( $upgrader_item );
   324 				$theme         = wp_get_theme( $upgrader_item );
   302 				$item_name = $theme->Get( 'Name' );
   325 				$item_name     = $theme->Get( 'Name' );
       
   326 				/* translators: %s: Theme name */
   303 				$skin->feedback( __( 'Updating theme: %s' ), $item_name );
   327 				$skin->feedback( __( 'Updating theme: %s' ), $item_name );
   304 				break;
   328 				break;
   305 			case 'plugin':
   329 			case 'plugin':
   306 				$upgrader_item = $item->plugin;
   330 				$upgrader_item = $item->plugin;
   307 				$plugin_data = get_plugin_data( $context . '/' . $upgrader_item );
   331 				$plugin_data   = get_plugin_data( $context . '/' . $upgrader_item );
   308 				$item_name = $plugin_data['Name'];
   332 				$item_name     = $plugin_data['Name'];
       
   333 				/* translators: %s: Plugin name */
   309 				$skin->feedback( __( 'Updating plugin: %s' ), $item_name );
   334 				$skin->feedback( __( 'Updating plugin: %s' ), $item_name );
   310 				break;
   335 				break;
   311 			case 'translation':
   336 			case 'translation':
   312 				$language_item_name = $upgrader->get_name_for_update( $item );
   337 				$language_item_name = $upgrader->get_name_for_update( $item );
       
   338 				/* translators: %s: Name of language item */
   313 				$item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
   339 				$item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
       
   340 				/* translators: 1: Name of language item, 2: Language */
   314 				$skin->feedback( sprintf( __( 'Updating translations for %1$s (%2$s)&#8230;' ), $language_item_name, $item->language ) );
   341 				$skin->feedback( sprintf( __( 'Updating translations for %1$s (%2$s)&#8230;' ), $language_item_name, $item->language ) );
   315 				break;
   342 				break;
   316 		}
   343 		}
   317 
   344 
   318 		$allow_relaxed_file_ownership = false;
   345 		$allow_relaxed_file_ownership = false;
   319 		if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
   346 		if ( 'core' == $type && isset( $item->new_files ) && ! $item->new_files ) {
   320 			$allow_relaxed_file_ownership = true;
   347 			$allow_relaxed_file_ownership = true;
   321 		}
   348 		}
   322 
   349 
   323 		// Boom, This sites about to get a whole new splash of paint!
   350 		// Boom, This sites about to get a whole new splash of paint!
   324 		$upgrade_result = $upgrader->upgrade( $upgrader_item, array(
   351 		$upgrade_result = $upgrader->upgrade(
   325 			'clear_update_cache' => false,
   352 			$upgrader_item,
   326 			// Always use partial builds if possible for core updates.
   353 			array(
   327 			'pre_check_md5'      => false,
   354 				'clear_update_cache'           => false,
   328 			// Only available for core updates.
   355 				// Always use partial builds if possible for core updates.
   329 			'attempt_rollback'   => true,
   356 				'pre_check_md5'                => false,
   330 			// Allow relaxed file ownership in some scenarios
   357 				// Only available for core updates.
   331 			'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership,
   358 				'attempt_rollback'             => true,
   332 		) );
   359 				// Allow relaxed file ownership in some scenarios
       
   360 				'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership,
       
   361 			)
       
   362 		);
   333 
   363 
   334 		// If the filesystem is unavailable, false is returned.
   364 		// If the filesystem is unavailable, false is returned.
   335 		if ( false === $upgrade_result ) {
   365 		if ( false === $upgrade_result ) {
   336 			$upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
   366 			$upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
   337 		}
   367 		}
   352 
   382 
   353 		$this->update_results[ $type ][] = (object) array(
   383 		$this->update_results[ $type ][] = (object) array(
   354 			'item'     => $item,
   384 			'item'     => $item,
   355 			'result'   => $upgrade_result,
   385 			'result'   => $upgrade_result,
   356 			'name'     => $item_name,
   386 			'name'     => $item_name,
   357 			'messages' => $skin->get_upgrade_messages()
   387 			'messages' => $skin->get_upgrade_messages(),
   358 		);
   388 		);
   359 
   389 
   360 		return $upgrade_result;
   390 		return $upgrade_result;
   361 	}
   391 	}
   362 
   392 
   364 	 * Kicks off the background update process, looping through all pending updates.
   394 	 * Kicks off the background update process, looping through all pending updates.
   365 	 *
   395 	 *
   366 	 * @since 3.7.0
   396 	 * @since 3.7.0
   367 	 */
   397 	 */
   368 	public function run() {
   398 	public function run() {
   369 		if ( $this->is_disabled() )
   399 		if ( $this->is_disabled() ) {
   370 			return;
   400 			return;
   371 
   401 		}
   372 		if ( ! is_main_network() || ! is_main_site() )
   402 
       
   403 		if ( ! is_main_network() || ! is_main_site() ) {
   373 			return;
   404 			return;
   374 
   405 		}
   375 		if ( ! WP_Upgrader::create_lock( 'auto_updater' ) )
   406 
       
   407 		if ( ! WP_Upgrader::create_lock( 'auto_updater' ) ) {
   376 			return;
   408 			return;
       
   409 		}
   377 
   410 
   378 		// Don't automatically run these thins, as we'll handle it ourselves
   411 		// Don't automatically run these thins, as we'll handle it ourselves
   379 		remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
   412 		remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
   380 		remove_action( 'upgrader_process_complete', 'wp_version_check' );
   413 		remove_action( 'upgrader_process_complete', 'wp_version_check' );
   381 		remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
   414 		remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
   382 		remove_action( 'upgrader_process_complete', 'wp_update_themes' );
   415 		remove_action( 'upgrader_process_complete', 'wp_update_themes' );
   383 
   416 
   384 		// Next, Plugins
   417 		// Next, Plugins
   385 		wp_update_plugins(); // Check for Plugin updates
   418 		wp_update_plugins(); // Check for Plugin updates
   386 		$plugin_updates = get_site_transient( 'update_plugins' );
   419 		$plugin_updates = get_site_transient( 'update_plugins' );
   387 		if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
   420 		if ( $plugin_updates && ! empty( $plugin_updates->response ) ) {
   388 			foreach ( $plugin_updates->response as $plugin ) {
   421 			foreach ( $plugin_updates->response as $plugin ) {
   389 				$this->update( 'plugin', $plugin );
   422 				$this->update( 'plugin', $plugin );
   390 			}
   423 			}
   391 			// Force refresh of plugin update information
   424 			// Force refresh of plugin update information
   392 			wp_clean_plugins_cache();
   425 			wp_clean_plugins_cache();
   393 		}
   426 		}
   394 
   427 
   395 		// Next, those themes we all love
   428 		// Next, those themes we all love
   396 		wp_update_themes();  // Check for Theme updates
   429 		wp_update_themes();  // Check for Theme updates
   397 		$theme_updates = get_site_transient( 'update_themes' );
   430 		$theme_updates = get_site_transient( 'update_themes' );
   398 		if ( $theme_updates && !empty( $theme_updates->response ) ) {
   431 		if ( $theme_updates && ! empty( $theme_updates->response ) ) {
   399 			foreach ( $theme_updates->response as $theme ) {
   432 			foreach ( $theme_updates->response as $theme ) {
   400 				$this->update( 'theme', (object) $theme );
   433 				$this->update( 'theme', (object) $theme );
   401 			}
   434 			}
   402 			// Force refresh of theme update information
   435 			// Force refresh of theme update information
   403 			wp_clean_themes_cache();
   436 			wp_clean_themes_cache();
   405 
   438 
   406 		// Next, Process any core update
   439 		// Next, Process any core update
   407 		wp_version_check(); // Check for Core updates
   440 		wp_version_check(); // Check for Core updates
   408 		$core_update = find_core_auto_update();
   441 		$core_update = find_core_auto_update();
   409 
   442 
   410 		if ( $core_update )
   443 		if ( $core_update ) {
   411 			$this->update( 'core', $core_update );
   444 			$this->update( 'core', $core_update );
       
   445 		}
   412 
   446 
   413 		// Clean up, and check for any pending translations
   447 		// Clean up, and check for any pending translations
   414 		// (Core_Upgrader checks for core updates)
   448 		// (Core_Upgrader checks for core updates)
   415 		$theme_stats = array();
   449 		$theme_stats = array();
   416 		if ( isset( $this->update_results['theme'] ) ) {
   450 		if ( isset( $this->update_results['theme'] ) ) {
   454 			 *
   488 			 *
   455 			 * @param bool $development_version By default, emails are sent if the
   489 			 * @param bool $development_version By default, emails are sent if the
   456 			 *                                  install is a development version.
   490 			 *                                  install is a development version.
   457 			 *                                  Return false to avoid the email.
   491 			 *                                  Return false to avoid the email.
   458 			 */
   492 			 */
   459 			if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) )
   493 			if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) ) {
   460 				$this->send_debug_email();
   494 				$this->send_debug_email();
   461 
   495 			}
   462 			if ( ! empty( $this->update_results['core'] ) )
   496 
       
   497 			if ( ! empty( $this->update_results['core'] ) ) {
   463 				$this->after_core_update( $this->update_results['core'][0] );
   498 				$this->after_core_update( $this->update_results['core'][0] );
       
   499 			}
   464 
   500 
   465 			/**
   501 			/**
   466 			 * Fires after all automatic updates have run.
   502 			 * Fires after all automatic updates have run.
   467 			 *
   503 			 *
   468 			 * @since 3.8.0
   504 			 * @since 3.8.0
   501 		$critical = false;
   537 		$critical = false;
   502 		if ( $error_code === 'disk_full' || false !== strpos( $error_code, '__copy_dir' ) ) {
   538 		if ( $error_code === 'disk_full' || false !== strpos( $error_code, '__copy_dir' ) ) {
   503 			$critical = true;
   539 			$critical = true;
   504 		} elseif ( $error_code === 'rollback_was_required' && is_wp_error( $result->get_error_data()->rollback ) ) {
   540 		} elseif ( $error_code === 'rollback_was_required' && is_wp_error( $result->get_error_data()->rollback ) ) {
   505 			// A rollback is only critical if it failed too.
   541 			// A rollback is only critical if it failed too.
   506 			$critical = true;
   542 			$critical        = true;
   507 			$rollback_result = $result->get_error_data()->rollback;
   543 			$rollback_result = $result->get_error_data()->rollback;
   508 		} elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
   544 		} elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
   509 			$critical = true;
   545 			$critical = true;
   510 		}
   546 		}
   511 
   547 
   536 		 *
   572 		 *
   537 		 * For certain 'transient' failures, like download_failed, we should allow retries.
   573 		 * For certain 'transient' failures, like download_failed, we should allow retries.
   538 		 * In fact, let's schedule a special update for an hour from now. (It's possible
   574 		 * In fact, let's schedule a special update for an hour from now. (It's possible
   539 		 * the issue could actually be on WordPress.org's side.) If that one fails, then email.
   575 		 * the issue could actually be on WordPress.org's side.) If that one fails, then email.
   540 		 */
   576 		 */
   541 		$send = true;
   577 		$send               = true;
   542   		$transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro', 'locked' );
   578 		$transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro', 'locked' );
   543   		if ( in_array( $error_code, $transient_failures ) && ! get_site_option( 'auto_core_update_failed' ) ) {
   579 		if ( in_array( $error_code, $transient_failures ) && ! get_site_option( 'auto_core_update_failed' ) ) {
   544   			wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
   580 			wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
   545   			$send = false;
   581 			$send = false;
   546   		}
   582 		}
   547 
   583 
   548   		$n = get_site_option( 'auto_core_update_notified' );
   584 		$n = get_site_option( 'auto_core_update_notified' );
   549 		// Don't notify if we've already notified the same email address of the same version of the same notification type.
   585 		// Don't notify if we've already notified the same email address of the same version of the same notification type.
   550 		if ( $n && 'fail' == $n['type'] && $n['email'] == get_site_option( 'admin_email' ) && $n['version'] == $core_update->current )
   586 		if ( $n && 'fail' == $n['type'] && $n['email'] == get_site_option( 'admin_email' ) && $n['version'] == $core_update->current ) {
   551 			$send = false;
   587 			$send = false;
   552 
   588 		}
   553 		update_site_option( 'auto_core_update_failed', array(
   589 
   554 			'attempted'  => $core_update->current,
   590 		update_site_option(
   555 			'current'    => $wp_version,
   591 			'auto_core_update_failed',
   556 			'error_code' => $error_code,
   592 			array(
   557 			'error_data' => $result->get_error_data(),
   593 				'attempted'  => $core_update->current,
   558 			'timestamp'  => time(),
   594 				'current'    => $wp_version,
   559 			'retry'      => in_array( $error_code, $transient_failures ),
   595 				'error_code' => $error_code,
   560 		) );
   596 				'error_data' => $result->get_error_data(),
   561 
   597 				'timestamp'  => time(),
   562 		if ( $send )
   598 				'retry'      => in_array( $error_code, $transient_failures ),
       
   599 			)
       
   600 		);
       
   601 
       
   602 		if ( $send ) {
   563 			$this->send_email( 'fail', $core_update, $result );
   603 			$this->send_email( 'fail', $core_update, $result );
       
   604 		}
   564 	}
   605 	}
   565 
   606 
   566 	/**
   607 	/**
   567 	 * Sends an email upon the completion or failure of a background core update.
   608 	 * Sends an email upon the completion or failure of a background core update.
   568 	 *
   609 	 *
   571 	 * @param string $type        The type of email to send. Can be one of 'success', 'fail', 'manual', 'critical'.
   612 	 * @param string $type        The type of email to send. Can be one of 'success', 'fail', 'manual', 'critical'.
   572 	 * @param object $core_update The update offer that was attempted.
   613 	 * @param object $core_update The update offer that was attempted.
   573 	 * @param mixed  $result      Optional. The result for the core update. Can be WP_Error.
   614 	 * @param mixed  $result      Optional. The result for the core update. Can be WP_Error.
   574 	 */
   615 	 */
   575 	protected function send_email( $type, $core_update, $result = null ) {
   616 	protected function send_email( $type, $core_update, $result = null ) {
   576 		update_site_option( 'auto_core_update_notified', array(
   617 		update_site_option(
   577 			'type'      => $type,
   618 			'auto_core_update_notified',
   578 			'email'     => get_site_option( 'admin_email' ),
   619 			array(
   579 			'version'   => $core_update->current,
   620 				'type'      => $type,
   580 			'timestamp' => time(),
   621 				'email'     => get_site_option( 'admin_email' ),
   581 		) );
   622 				'version'   => $core_update->current,
       
   623 				'timestamp' => time(),
       
   624 			)
       
   625 		);
   582 
   626 
   583 		$next_user_core_update = get_preferred_from_update_core();
   627 		$next_user_core_update = get_preferred_from_update_core();
   584 		// If the update transient is empty, use the update we just performed
   628 		// If the update transient is empty, use the update we just performed
   585 		if ( ! $next_user_core_update )
   629 		if ( ! $next_user_core_update ) {
   586 			$next_user_core_update = $core_update;
   630 			$next_user_core_update = $core_update;
       
   631 		}
   587 		$newer_version_available = ( 'upgrade' == $next_user_core_update->response && version_compare( $next_user_core_update->version, $core_update->version, '>' ) );
   632 		$newer_version_available = ( 'upgrade' == $next_user_core_update->response && version_compare( $next_user_core_update->version, $core_update->version, '>' ) );
   588 
   633 
   589 		/**
   634 		/**
   590 		 * Filters whether to send an email following an automatic background core update.
   635 		 * Filters whether to send an email following an automatic background core update.
   591 		 *
   636 		 *
   595 		 * @param string $type        The type of email to send. Can be one of
   640 		 * @param string $type        The type of email to send. Can be one of
   596 		 *                            'success', 'fail', 'critical'.
   641 		 *                            'success', 'fail', 'critical'.
   597 		 * @param object $core_update The update offer that was attempted.
   642 		 * @param object $core_update The update offer that was attempted.
   598 		 * @param mixed  $result      The result for the core update. Can be WP_Error.
   643 		 * @param mixed  $result      The result for the core update. Can be WP_Error.
   599 		 */
   644 		 */
   600 		if ( 'manual' !== $type && ! apply_filters( 'auto_core_update_send_email', true, $type, $core_update, $result ) )
   645 		if ( 'manual' !== $type && ! apply_filters( 'auto_core_update_send_email', true, $type, $core_update, $result ) ) {
   601 			return;
   646 			return;
       
   647 		}
   602 
   648 
   603 		switch ( $type ) {
   649 		switch ( $type ) {
   604 			case 'success' : // We updated.
   650 			case 'success': // We updated.
   605 				/* translators: 1: Site name, 2: WordPress version number. */
   651 				/* translators: Site updated notification email subject. 1: Site title, 2: WordPress version number. */
   606 				$subject = __( '[%1$s] Your site has updated to WordPress %2$s' );
   652 				$subject = __( '[%1$s] Your site has updated to WordPress %2$s' );
   607 				break;
   653 				break;
   608 
   654 
   609 			case 'fail' :   // We tried to update but couldn't.
   655 			case 'fail':   // We tried to update but couldn't.
   610 			case 'manual' : // We can't update (and made no attempt).
   656 			case 'manual': // We can't update (and made no attempt).
   611 				/* translators: 1: Site name, 2: WordPress version number. */
   657 				/* translators: Update available notification email subject. 1: Site title, 2: WordPress version number. */
   612 				$subject = __( '[%1$s] WordPress %2$s is available. Please update!' );
   658 				$subject = __( '[%1$s] WordPress %2$s is available. Please update!' );
   613 				break;
   659 				break;
   614 
   660 
   615 			case 'critical' : // We tried to update, started to copy files, then things went wrong.
   661 			case 'critical': // We tried to update, started to copy files, then things went wrong.
   616 				/* translators: 1: Site name. */
   662 				/* translators: Site down notification email subject. 1: Site title. */
   617 				$subject = __( '[%1$s] URGENT: Your site may be down due to a failed update' );
   663 				$subject = __( '[%1$s] URGENT: Your site may be down due to a failed update' );
   618 				break;
   664 				break;
   619 
   665 
   620 			default :
   666 			default:
   621 				return;
   667 				return;
   622 		}
   668 		}
   623 
   669 
   624 		// If the auto update is not to the latest version, say that the current version of WP is available instead.
   670 		// If the auto update is not to the latest version, say that the current version of WP is available instead.
   625 		$version = 'success' === $type ? $core_update->current : $next_user_core_update->current;
   671 		$version = 'success' === $type ? $core_update->current : $next_user_core_update->current;
   626 		$subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $version );
   672 		$subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $version );
   627 
   673 
   628 		$body = '';
   674 		$body = '';
   629 
   675 
   630 		switch ( $type ) {
   676 		switch ( $type ) {
   631 			case 'success' :
   677 			case 'success':
       
   678 				/* translators: 1: Home URL, 2: WordPress version */
   632 				$body .= sprintf( __( 'Howdy! Your site at %1$s has been updated automatically to WordPress %2$s.' ), home_url(), $core_update->current );
   679 				$body .= sprintf( __( 'Howdy! Your site at %1$s has been updated automatically to WordPress %2$s.' ), home_url(), $core_update->current );
   633 				$body .= "\n\n";
   680 				$body .= "\n\n";
   634 				if ( ! $newer_version_available )
   681 				if ( ! $newer_version_available ) {
   635 					$body .= __( 'No further action is needed on your part.' ) . ' ';
   682 					$body .= __( 'No further action is needed on your part.' ) . ' ';
       
   683 				}
   636 
   684 
   637 				// Can only reference the About screen if their update was successful.
   685 				// Can only reference the About screen if their update was successful.
   638 				list( $about_version ) = explode( '-', $core_update->current, 2 );
   686 				list( $about_version ) = explode( '-', $core_update->current, 2 );
   639 				$body .= sprintf( __( "For more on version %s, see the About WordPress screen:" ), $about_version );
   687 				/* translators: %s: WordPress core version */
       
   688 				$body .= sprintf( __( 'For more on version %s, see the About WordPress screen:' ), $about_version );
   640 				$body .= "\n" . admin_url( 'about.php' );
   689 				$body .= "\n" . admin_url( 'about.php' );
   641 
   690 
   642 				if ( $newer_version_available ) {
   691 				if ( $newer_version_available ) {
       
   692 					/* translators: %s: WordPress core latest version */
   643 					$body .= "\n\n" . sprintf( __( 'WordPress %s is also now available.' ), $next_user_core_update->current ) . ' ';
   693 					$body .= "\n\n" . sprintf( __( 'WordPress %s is also now available.' ), $next_user_core_update->current ) . ' ';
   644 					$body .= __( 'Updating is easy and only takes a few moments:' );
   694 					$body .= __( 'Updating is easy and only takes a few moments:' );
   645 					$body .= "\n" . network_admin_url( 'update-core.php' );
   695 					$body .= "\n" . network_admin_url( 'update-core.php' );
   646 				}
   696 				}
   647 
   697 
   648 				break;
   698 				break;
   649 
   699 
   650 			case 'fail' :
   700 			case 'fail':
   651 			case 'manual' :
   701 			case 'manual':
       
   702 				/* translators: 1: Home URL, 2: WordPress core latest version */
   652 				$body .= sprintf( __( 'Please update your site at %1$s to WordPress %2$s.' ), home_url(), $next_user_core_update->current );
   703 				$body .= sprintf( __( 'Please update your site at %1$s to WordPress %2$s.' ), home_url(), $next_user_core_update->current );
   653 
   704 
   654 				$body .= "\n\n";
   705 				$body .= "\n\n";
   655 
   706 
   656 				// Don't show this message if there is a newer version available.
   707 				// Don't show this message if there is a newer version available.
   657 				// Potential for confusion, and also not useful for them to know at this point.
   708 				// Potential for confusion, and also not useful for them to know at this point.
   658 				if ( 'fail' == $type && ! $newer_version_available )
   709 				if ( 'fail' == $type && ! $newer_version_available ) {
   659 					$body .= __( 'We tried but were unable to update your site automatically.' ) . ' ';
   710 					$body .= __( 'We tried but were unable to update your site automatically.' ) . ' ';
       
   711 				}
   660 
   712 
   661 				$body .= __( 'Updating is easy and only takes a few moments:' );
   713 				$body .= __( 'Updating is easy and only takes a few moments:' );
   662 				$body .= "\n" . network_admin_url( 'update-core.php' );
   714 				$body .= "\n" . network_admin_url( 'update-core.php' );
   663 				break;
   715 				break;
   664 
   716 
   665 			case 'critical' :
   717 			case 'critical':
   666 				if ( $newer_version_available )
   718 				if ( $newer_version_available ) {
       
   719 					/* translators: 1: Home URL, 2: WordPress core latest version */
   667 					$body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update WordPress to version %2$s.' ), home_url(), $core_update->current );
   720 					$body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update WordPress to version %2$s.' ), home_url(), $core_update->current );
   668 				else
   721 				} else {
       
   722 					/* translators: 1: Home URL, 2: Core update version */
   669 					$body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update to the latest version of WordPress, %2$s.' ), home_url(), $core_update->current );
   723 					$body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update to the latest version of WordPress, %2$s.' ), home_url(), $core_update->current );
       
   724 				}
   670 
   725 
   671 				$body .= "\n\n" . __( "This means your site may be offline or broken. Don't panic; this can be fixed." );
   726 				$body .= "\n\n" . __( "This means your site may be offline or broken. Don't panic; this can be fixed." );
   672 
   727 
   673 				$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:" );
   728 				$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:" );
   674 				$body .= "\n" . network_admin_url( 'update-core.php' );
   729 				$body .= "\n" . network_admin_url( 'update-core.php' );
   676 		}
   731 		}
   677 
   732 
   678 		$critical_support = 'critical' === $type && ! empty( $core_update->support_email );
   733 		$critical_support = 'critical' === $type && ! empty( $core_update->support_email );
   679 		if ( $critical_support ) {
   734 		if ( $critical_support ) {
   680 			// Support offer if available.
   735 			// Support offer if available.
   681 			$body .= "\n\n" . sprintf( __( "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." ), $core_update->support_email );
   736 			/* translators: %s: Support e-mail */
       
   737 			$body .= "\n\n" . sprintf( __( '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.' ), $core_update->support_email );
   682 		} else {
   738 		} else {
   683 			// Add a note about the support forums.
   739 			// Add a note about the support forums.
   684 			$body .= "\n\n" . __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
   740 			$body .= "\n\n" . __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
   685 			$body .= "\n" . __( 'https://wordpress.org/support/' );
   741 			$body .= "\n" . __( 'https://wordpress.org/support/' );
   686 		}
   742 		}
   689 		if ( $type != 'success' || $newer_version_available ) {
   745 		if ( $type != 'success' || $newer_version_available ) {
   690 			$body .= "\n\n" . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' );
   746 			$body .= "\n\n" . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' );
   691 		}
   747 		}
   692 
   748 
   693 		if ( $critical_support ) {
   749 		if ( $critical_support ) {
   694 			$body .= " " . __( "If you reach out to us, we'll also ensure you'll never have this problem again." );
   750 			$body .= ' ' . __( "If you reach out to us, we'll also ensure you'll never have this problem again." );
   695 		}
   751 		}
   696 
   752 
   697 		// If things are successful and we're now on the latest, mention plugins and themes if any are out of date.
   753 		// If things are successful and we're now on the latest, mention plugins and themes if any are out of date.
   698 		if ( $type == 'success' && ! $newer_version_available && ( get_plugin_updates() || get_theme_updates() ) ) {
   754 		if ( $type == 'success' && ! $newer_version_available && ( get_plugin_updates() || get_theme_updates() ) ) {
   699 			$body .= "\n\n" . __( 'You also have some plugins or themes with updates available. Update them now:' );
   755 			$body .= "\n\n" . __( 'You also have some plugins or themes with updates available. Update them now:' );
   702 
   758 
   703 		$body .= "\n\n" . __( 'The WordPress Team' ) . "\n";
   759 		$body .= "\n\n" . __( 'The WordPress Team' ) . "\n";
   704 
   760 
   705 		if ( 'critical' == $type && is_wp_error( $result ) ) {
   761 		if ( 'critical' == $type && is_wp_error( $result ) ) {
   706 			$body .= "\n***\n\n";
   762 			$body .= "\n***\n\n";
       
   763 			/* translators: %s: WordPress version */
   707 			$body .= sprintf( __( 'Your site was running version %s.' ), get_bloginfo( 'version' ) );
   764 			$body .= sprintf( __( 'Your site was running version %s.' ), get_bloginfo( 'version' ) );
   708 			$body .= ' ' . __( 'We have some data that describes the error your site encountered.' );
   765 			$body .= ' ' . __( 'We have some data that describes the error your site encountered.' );
   709 			$body .= ' ' . __( 'Your hosting company, support forum volunteers, or a friendly developer may be able to use this information to help you:' );
   766 			$body .= ' ' . __( 'Your hosting company, support forum volunteers, or a friendly developer may be able to use this information to help you:' );
   710 
   767 
   711 			// If we had a rollback and we're still critical, then the rollback failed too.
   768 			// If we had a rollback and we're still critical, then the rollback failed too.
   712 			// Loop through all errors (the main WP_Error, the update result, the rollback result) for code, data, etc.
   769 			// Loop through all errors (the main WP_Error, the update result, the rollback result) for code, data, etc.
   713 			if ( 'rollback_was_required' == $result->get_error_code() )
   770 			if ( 'rollback_was_required' == $result->get_error_code() ) {
   714 				$errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
   771 				$errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
   715 			else
   772 			} else {
   716 				$errors = array( $result );
   773 				$errors = array( $result );
       
   774 			}
   717 
   775 
   718 			foreach ( $errors as $error ) {
   776 			foreach ( $errors as $error ) {
   719 				if ( ! is_wp_error( $error ) )
   777 				if ( ! is_wp_error( $error ) ) {
   720 					continue;
   778 					continue;
       
   779 				}
   721 				$error_code = $error->get_error_code();
   780 				$error_code = $error->get_error_code();
   722 				$body .= "\n\n" . sprintf( __( "Error code: %s" ), $error_code );
   781 				/* translators: %s: Error code */
   723 				if ( 'rollback_was_required' == $error_code )
   782 				$body .= "\n\n" . sprintf( __( 'Error code: %s' ), $error_code );
       
   783 				if ( 'rollback_was_required' == $error_code ) {
   724 					continue;
   784 					continue;
   725 				if ( $error->get_error_message() )
   785 				}
       
   786 				if ( $error->get_error_message() ) {
   726 					$body .= "\n" . $error->get_error_message();
   787 					$body .= "\n" . $error->get_error_message();
       
   788 				}
   727 				$error_data = $error->get_error_data();
   789 				$error_data = $error->get_error_data();
   728 				if ( $error_data )
   790 				if ( $error_data ) {
   729 					$body .= "\n" . implode( ', ', (array) $error_data );
   791 					$body .= "\n" . implode( ', ', (array) $error_data );
       
   792 				}
   730 			}
   793 			}
   731 			$body .= "\n";
   794 			$body .= "\n";
   732 		}
   795 		}
   733 
   796 
   734 		$to  = get_site_option( 'admin_email' );
   797 		$to      = get_site_option( 'admin_email' );
   735 		$headers = '';
   798 		$headers = '';
   736 
   799 
   737 		$email = compact( 'to', 'subject', 'body', 'headers' );
   800 		$email = compact( 'to', 'subject', 'body', 'headers' );
   738 
   801 
   739 		/**
   802 		/**
   765 	 *
   828 	 *
   766 	 * @since 3.7.0
   829 	 * @since 3.7.0
   767 	 */
   830 	 */
   768 	protected function send_debug_email() {
   831 	protected function send_debug_email() {
   769 		$update_count = 0;
   832 		$update_count = 0;
   770 		foreach ( $this->update_results as $type => $updates )
   833 		foreach ( $this->update_results as $type => $updates ) {
   771 			$update_count += count( $updates );
   834 			$update_count += count( $updates );
   772 
   835 		}
   773 		$body = array();
   836 
       
   837 		$body     = array();
   774 		$failures = 0;
   838 		$failures = 0;
   775 
   839 
       
   840 		/* translators: %s: Network home URL */
   776 		$body[] = sprintf( __( 'WordPress site: %s' ), network_home_url( '/' ) );
   841 		$body[] = sprintf( __( 'WordPress site: %s' ), network_home_url( '/' ) );
   777 
   842 
   778 		// Core
   843 		// Core
   779 		if ( isset( $this->update_results['core'] ) ) {
   844 		if ( isset( $this->update_results['core'] ) ) {
   780 			$result = $this->update_results['core'][0];
   845 			$result = $this->update_results['core'][0];
   781 			if ( $result->result && ! is_wp_error( $result->result ) ) {
   846 			if ( $result->result && ! is_wp_error( $result->result ) ) {
       
   847 				/* translators: %s: WordPress core version */
   782 				$body[] = sprintf( __( 'SUCCESS: WordPress was successfully updated to %s' ), $result->name );
   848 				$body[] = sprintf( __( 'SUCCESS: WordPress was successfully updated to %s' ), $result->name );
   783 			} else {
   849 			} else {
       
   850 				/* translators: %s: WordPress core version */
   784 				$body[] = sprintf( __( 'FAILED: WordPress failed to update to %s' ), $result->name );
   851 				$body[] = sprintf( __( 'FAILED: WordPress failed to update to %s' ), $result->name );
   785 				$failures++;
   852 				$failures++;
   786 			}
   853 			}
   787 			$body[] = '';
   854 			$body[] = '';
   788 		}
   855 		}
   789 
   856 
   790 		// Plugins, Themes, Translations
   857 		// Plugins, Themes, Translations
   791 		foreach ( array( 'plugin', 'theme', 'translation' ) as $type ) {
   858 		foreach ( array( 'plugin', 'theme', 'translation' ) as $type ) {
   792 			if ( ! isset( $this->update_results[ $type ] ) )
   859 			if ( ! isset( $this->update_results[ $type ] ) ) {
   793 				continue;
   860 				continue;
       
   861 			}
   794 			$success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
   862 			$success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
   795 			if ( $success_items ) {
   863 			if ( $success_items ) {
   796 				$messages = array(
   864 				$messages = array(
   797 					'plugin'      => __( 'The following plugins were successfully updated:' ),
   865 					'plugin'      => __( 'The following plugins were successfully updated:' ),
   798 					'theme'       => __( 'The following themes were successfully updated:' ),
   866 					'theme'       => __( 'The following themes were successfully updated:' ),
   799 					'translation' => __( 'The following translations were successfully updated:' ),
   867 					'translation' => __( 'The following translations were successfully updated:' ),
   800 				);
   868 				);
   801 
   869 
   802 				$body[] = $messages[ $type ];
   870 				$body[] = $messages[ $type ];
   803 				foreach ( wp_list_pluck( $success_items, 'name' ) as $name ) {
   871 				foreach ( wp_list_pluck( $success_items, 'name' ) as $name ) {
       
   872 					/* translators: %s: name of plugin / theme / translations */
   804 					$body[] = ' * ' . sprintf( __( 'SUCCESS: %s' ), $name );
   873 					$body[] = ' * ' . sprintf( __( 'SUCCESS: %s' ), $name );
   805 				}
   874 				}
   806 			}
   875 			}
   807 			if ( $success_items != $this->update_results[ $type ] ) {
   876 			if ( $success_items != $this->update_results[ $type ] ) {
   808 				// Failed updates
   877 				// Failed updates
   813 				);
   882 				);
   814 
   883 
   815 				$body[] = $messages[ $type ];
   884 				$body[] = $messages[ $type ];
   816 				foreach ( $this->update_results[ $type ] as $item ) {
   885 				foreach ( $this->update_results[ $type ] as $item ) {
   817 					if ( ! $item->result || is_wp_error( $item->result ) ) {
   886 					if ( ! $item->result || is_wp_error( $item->result ) ) {
       
   887 						/* translators: %s: name of plugin / theme / translations */
   818 						$body[] = ' * ' . sprintf( __( 'FAILED: %s' ), $item->name );
   888 						$body[] = ' * ' . sprintf( __( 'FAILED: %s' ), $item->name );
   819 						$failures++;
   889 						$failures++;
   820 					}
   890 					}
   821 				}
   891 				}
   822 			}
   892 			}
   823 			$body[] = '';
   893 			$body[] = '';
   824 		}
   894 		}
   825 
   895 
   826 		$site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
   896 		$site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
   827 		if ( $failures ) {
   897 		if ( $failures ) {
   828 			$body[] = trim( __(
   898 			$body[] = trim(
   829 "BETA TESTING?
   899 				__(
       
   900 					"BETA TESTING?
   830 =============
   901 =============
   831 
   902 
   832 This debugging email is sent when you are using a development version of WordPress.
   903 This debugging email is sent when you are using a development version of WordPress.
   833 
   904 
   834 If you think these failures might be due to a bug in WordPress, could you report it?
   905 If you think these failures might be due to a bug in WordPress, could you report it?
   835  * Open a thread in the support forums: https://wordpress.org/support/forum/alphabeta
   906  * Open a thread in the support forums: https://wordpress.org/support/forum/alphabeta
   836  * Or, if you're comfortable writing a bug report: https://core.trac.wordpress.org/
   907  * Or, if you're comfortable writing a bug report: https://core.trac.wordpress.org/
   837 
   908 
   838 Thanks! -- The WordPress Team" ) );
   909 Thanks! -- The WordPress Team"
       
   910 				)
       
   911 			);
   839 			$body[] = '';
   912 			$body[] = '';
   840 
   913 
   841 			$subject = sprintf( __( '[%s] There were failures during background updates' ), $site_title );
   914 			/* translators: Background update failed notification email subject. %s: Site title */
       
   915 			$subject = sprintf( __( '[%s] Background Update Failed' ), $site_title );
   842 		} else {
   916 		} else {
   843 			$subject = sprintf( __( '[%s] Background updates have finished' ), $site_title );
   917 			/* translators: Background update finished notification email subject. %s: Site title */
   844 		}
   918 			$subject = sprintf( __( '[%s] Background Update Finished' ), $site_title );
   845 
   919 		}
   846 		$body[] = trim( __(
   920 
   847 'UPDATE LOG
   921 		$body[] = trim(
   848 ==========' ) );
   922 			__(
       
   923 				'UPDATE LOG
       
   924 =========='
       
   925 			)
       
   926 		);
   849 		$body[] = '';
   927 		$body[] = '';
   850 
   928 
   851 		foreach ( array( 'core', 'plugin', 'theme', 'translation' ) as $type ) {
   929 		foreach ( array( 'core', 'plugin', 'theme', 'translation' ) as $type ) {
   852 			if ( ! isset( $this->update_results[ $type ] ) )
   930 			if ( ! isset( $this->update_results[ $type ] ) ) {
   853 				continue;
   931 				continue;
       
   932 			}
   854 			foreach ( $this->update_results[ $type ] as $update ) {
   933 			foreach ( $this->update_results[ $type ] as $update ) {
   855 				$body[] = $update->name;
   934 				$body[] = $update->name;
   856 				$body[] = str_repeat( '-', strlen( $update->name ) );
   935 				$body[] = str_repeat( '-', strlen( $update->name ) );
   857 				foreach ( $update->messages as $message )
   936 				foreach ( $update->messages as $message ) {
   858 					$body[] = "  " . html_entity_decode( str_replace( '&#8230;', '...', $message ) );
   937 					$body[] = '  ' . html_entity_decode( str_replace( '&#8230;', '...', $message ) );
       
   938 				}
   859 				if ( is_wp_error( $update->result ) ) {
   939 				if ( is_wp_error( $update->result ) ) {
   860 					$results = array( 'update' => $update->result );
   940 					$results = array( 'update' => $update->result );
   861 					// If we rolled back, we want to know an error that occurred then too.
   941 					// If we rolled back, we want to know an error that occurred then too.
   862 					if ( 'rollback_was_required' === $update->result->get_error_code() )
   942 					if ( 'rollback_was_required' === $update->result->get_error_code() ) {
   863 						$results = (array) $update->result->get_error_data();
   943 						$results = (array) $update->result->get_error_data();
       
   944 					}
   864 					foreach ( $results as $result_type => $result ) {
   945 					foreach ( $results as $result_type => $result ) {
   865 						if ( ! is_wp_error( $result ) )
   946 						if ( ! is_wp_error( $result ) ) {
   866 							continue;
   947 							continue;
       
   948 						}
   867 
   949 
   868 						if ( 'rollback' === $result_type ) {
   950 						if ( 'rollback' === $result_type ) {
   869 							/* translators: 1: Error code, 2: Error message. */
   951 							/* translators: 1: Error code, 2: Error message. */
   870 							$body[] = '  ' . sprintf( __( 'Rollback Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
   952 							$body[] = '  ' . sprintf( __( 'Rollback Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
   871 						} else {
   953 						} else {
   872 							/* translators: 1: Error code, 2: Error message. */
   954 							/* translators: 1: Error code, 2: Error message. */
   873 							$body[] = '  ' . sprintf( __( 'Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
   955 							$body[] = '  ' . sprintf( __( 'Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
   874 						}
   956 						}
   875 
   957 
   876 						if ( $result->get_error_data() )
   958 						if ( $result->get_error_data() ) {
   877 							$body[] = '         ' . implode( ', ', (array) $result->get_error_data() );
   959 							$body[] = '         ' . implode( ', ', (array) $result->get_error_data() );
       
   960 						}
   878 					}
   961 					}
   879 				}
   962 				}
   880 				$body[] = '';
   963 				$body[] = '';
   881 			}
   964 			}
   882 		}
   965 		}
   883 
   966 
   884 		$email = array(
   967 		$email = array(
   885 			'to'      => get_site_option( 'admin_email' ),
   968 			'to'      => get_site_option( 'admin_email' ),
   886 			'subject' => $subject,
   969 			'subject' => $subject,
   887 			'body'    => implode( "\n", $body ),
   970 			'body'    => implode( "\n", $body ),
   888 			'headers' => ''
   971 			'headers' => '',
   889 		);
   972 		);
   890 
   973 
   891 		/**
   974 		/**
   892 		 * Filters the debug email that can be sent following an automatic
   975 		 * Filters the debug email that can be sent following an automatic
   893 		 * background core update.
   976 		 * background core update.