From 04373e6772d55e73474503407fcdeaacea290297 Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Fri, 31 Jul 2020 13:05:39 -0600
Subject: [PATCH] Site Health: Apply `auto_update_{plugin,theme}` when
 constructing the strings for displaying whether auto-updates are
 enabled/disabled.

---
 src/wp-admin/includes/class-wp-debug-data.php | 189 +++++++++++++-----
 1 file changed, 144 insertions(+), 45 deletions(-)

diff --git a/src/wp-admin/includes/class-wp-debug-data.php b/src/wp-admin/includes/class-wp-debug-data.php
index 24ca2bdfd2..eb5977bc0f 100644
--- a/src/wp-admin/includes/class-wp-debug-data.php
+++ b/src/wp-admin/includes/class-wp-debug-data.php
@@ -947,29 +947,52 @@ class WP_Debug_Data {
 			}
 
 			if ( $auto_updates_enabled ) {
-				if ( in_array( $plugin_path, $auto_updates, true ) ) {
+				if ( isset( $plugin_updates[ $plugin_path ]->update ) ) {
+					$item = $plugin_updates[ $plugin_path ]->update;
+				} else {
+					$item = (object) array(
+						'id'            => $plugin_path,
+						'slug'          => ! empty( basename( $plugin_path ) ) ? basename( $plugin_path ) : $plugin_path,
+						'plugin'        => $plugin_path,
+						'new_version'   => $plugin['Version'],
+						'url'           => '',
+						'package'       => '',
+						'icons'         => array(),
+						'banners'       => array(),
+						'banners_rtl'   => array(),
+						'tested'        => '',
+						'requires_php'  => '',
+						'compatibility' => new stdClass(),
+					);
+				}
+
+				/** This action is documented in wp-admin/includes/class-wp-automatic-updater.php */
+				$auto_update_forced = apply_filters( 'auto_update_plugin', null, $item );
+
+				if ( ! is_null( $auto_update_forced ) ) {
+					$enabled = $auto_update_forced;
+				} else {
+					$enabled = in_array( $plugin_path, $auto_updates, true );
+				}
+
+				if ( $enabled ) {
 					$auto_updates_string = __( 'Auto-updates enabled' );
-					$enabled             = true;
-
-					/**
-					 * Filters the text string of the auto-updates setting for each plugin in the Site Health debug data.
-					 *
-					 * @since 5.5.0
-					 *
-					 * @param string $auto_updates_string The string output for the auto-updates column.
-					 * @param string $plugin_path         The path to the plugin file.
-					 * @param array  $plugin              An array of plugin data.
-					 * @param bool   $enabled             Whether auto-updates are enabled for this item.
-					 */
-					$auto_updates_string = apply_filters( 'plugin_auto_update_debug_string', $auto_updates_string, $plugin_path, $plugin, $enabled );
 				} else {
 					$auto_updates_string = __( 'Auto-updates disabled' );
-					$enabled             = false;
-
-					/** This filter is documented in wp-admin/includes/class-wp-debug-data.php */
-					$auto_updates_string = apply_filters( 'plugin_auto_update_debug_string', $auto_updates_string, $plugin_path, $plugin, $enabled );
 				}
 
+				/**
+				 * Filters the text string of the auto-updates setting for each plugin in the Site Health debug data.
+				 *
+				 * @since 5.5.0
+				 *
+				 * @param string $auto_updates_string The string output for the auto-updates column.
+				 * @param string $plugin_path         The path to the plugin file.
+				 * @param array  $plugin              An array of plugin data.
+				 * @param bool   $enabled             Whether auto-updates are enabled for this item.
+				 */
+				$auto_updates_string = apply_filters( 'plugin_auto_update_debug_string', $auto_updates_string, $plugin_path, $plugin, $enabled );
+
 				$plugin_version_string       .= ' | ' . $auto_updates_string;
 				$plugin_version_string_debug .= ', ' . $auto_updates_string;
 			}
@@ -1070,28 +1093,45 @@ class WP_Debug_Data {
 		);
 
 		if ( $auto_updates_enabled ) {
-			if ( in_array( $active_theme->stylesheet, $auto_updates, true ) ) {
-				$auto_updates_string = __( 'Enabled' );
-				$enabled             = true;
+			if ( isset( $theme_updates[ $active_theme->stylesheet ]->update ) ) {
+				$item = $theme_updates[ $active_theme->stylesheet ]->update;
+			} else {
+				$item = (object) array(
+					'theme'        => $active_theme->stylesheet,
+					'new_version'  => $active_theme->version,
+					'url'          => '',
+					'package'      => '',
+					'requires'     => '',
+					'requires_php' => '',
+				);
+			}
 
-				/**
-				 * Filters the text string of the auto-updates setting for each theme in the Site Health debug data.
-				 *
-				 * @since 5.5.0
-				 *
-				 * @param string   $auto_updates_string The string output for the auto-updates column.
-				 * @param WP_Theme $theme               An object of theme data.
-				 * @param bool     $enabled             Whether auto-updates are enabled for this item.
-				 */
-				$auto_updates_string = apply_filters( 'theme_auto_update_debug_string', $auto_updates_string, $active_theme, $enabled );
+			/** This action is documented in wp-admin/includes/class-wp-automatic-updater.php */
+			$auto_update_forced = apply_filters( 'auto_update_theme', null, $item );
+
+			if ( ! is_null( $auto_update_forced ) ) {
+				$enabled = $auto_update_forced;
 			} else {
-				$auto_updates_string = __( 'Disabled' );
-				$enabled             = false;
+				$enabled = in_array( $active_theme->stylesheet, $auto_updates, true );
+			}
 
-				/** This filter is documented in wp-admin/includes/class-wp-debug-data.php */
-				$auto_updates_string = apply_filters( 'theme_auto_update_debug_string', $auto_updates_string, $active_theme, $enabled );
+			if ( $enabled ) {
+				$auto_updates_string = __( 'Enabled' );
+			} else {
+				$auto_updates_string = __( 'Disabled' );
 			}
 
+			/**
+			 * Filters the text string of the auto-updates setting for each theme in the Site Health debug data.
+			 *
+			 * @since 5.5.0
+			 *
+			 * @param string   $auto_updates_string The string output for the auto-updates column.
+			 * @param WP_Theme $theme               An object of theme data.
+			 * @param bool     $enabled             Whether auto-updates are enabled for this item.
+			 */
+			$auto_updates_string = apply_filters( 'theme_auto_update_debug_string', $auto_updates_string, $active_theme, $enabled );
+
 			$info['wp-active-theme']['fields']['auto_update'] = array(
 				'label' => __( 'Auto-updates' ),
 				'value' => $auto_updates_string,
@@ -1144,13 +1184,47 @@ class WP_Debug_Data {
 					'value' => get_template_directory(),
 				),
 			);
+
 			if ( $auto_updates_enabled ) {
-				if ( in_array( $parent_theme->stylesheet, $auto_updates, true ) ) {
+				if ( isset( $theme_updates[ $parent_theme->stylesheet ]->update ) ) {
+					$item = $theme_updates[ $parent_theme->stylesheet ]->update;
+				} else {
+					$item = (object) array(
+						'theme'        => $parent_theme->stylesheet,
+						'new_version'  => $parent_theme->version,
+						'url'          => '',
+						'package'      => '',
+						'requires'     => '',
+						'requires_php' => '',
+					);
+				}
+
+				/** This action is documented in wp-admin/includes/class-wp-automatic-updater.php */
+				$auto_update_forced = apply_filters( 'auto_update_theme', null, $item );
+
+				if ( ! is_null( $auto_update_forced ) ) {
+					$enabled = $auto_update_forced;
+				} else {
+					$enabled = in_array( $parent_theme->stylesheet, $auto_updates, true );
+				}
+
+				if ( $enabled ) {
 					$parent_theme_auto_update_string = __( 'Enabled' );
 				} else {
 					$parent_theme_auto_update_string = __( 'Disabled' );
 				}
 
+				/**
+				 * Filters the text string of the auto-updates setting for each theme in the Site Health debug data.
+				 *
+				 * @since 5.5.0
+				 *
+				 * @param string   $auto_updates_string The string output for the auto-updates column.
+				 * @param WP_Theme $theme               An object of theme data.
+				 * @param bool     $enabled             Whether auto-updates are enabled for this item.
+				 */
+				$parent_theme_auto_update_string = apply_filters( 'theme_auto_update_debug_string', $auto_updates_string, $parent_theme, $enabled );
+
 				$info['wp-parent-theme']['fields']['auto_update'] = array(
 					'label' => __( 'Auto-update' ),
 					'value' => $parent_theme_auto_update_string,
@@ -1207,20 +1281,45 @@ class WP_Debug_Data {
 			}
 
 			if ( $auto_updates_enabled ) {
-				if ( in_array( $theme_slug, $auto_updates, true ) ) {
-					$auto_updates_string = __( 'Auto-updates enabled' );
-					$enabled             = true;
+				if ( isset( $theme_updates[ $theme_slug ]->update ) ) {
+					$item = $theme_updates[ $theme_slug ]->update;
+				} else {
+					$item = (object) array(
+						'theme'        => $theme_slug,
+						'new_version'  => $theme->version,
+						'url'          => '',
+						'package'      => '',
+						'requires'     => '',
+						'requires_php' => '',
+					);
+				}
+
+				/** This action is documented in wp-admin/includes/class-wp-automatic-updater.php */
+				$auto_update_forced = apply_filters( 'auto_update_theme', null, $item );
 
-					/** This filter is documented in wp-admin/includes/class-wp-debug-data.php */
-					$auto_updates_string = apply_filters( 'theme_auto_update_debug_string', $auto_updates_string, $theme, $enabled );
+				if ( ! is_null( $auto_update_forced ) ) {
+					$enabled = $auto_update_forced;
 				} else {
-					$auto_updates_string = __( 'Auto-updates disabled' );
-					$enabled             = false;
+					$enabled = in_array( $theme_slug, $auto_updates, true );
+				}
 
-					/** This filter is documented in wp-admin/includes/class-wp-debug-data.php */
-					$auto_updates_string = apply_filters( 'theme_auto_update_debug_string', $auto_updates_string, $theme, $enabled );
+				if ( $enabled ) {
+					$auto_updates_string = __( 'Auto-updates enabled' );
+				} else {
+					$auto_updates_string = __( 'Auto-updates disabled' );
 				}
 
+				/**
+				 * Filters the text string of the auto-updates setting for each theme in the Site Health debug data.
+				 *
+				 * @since 5.5.0
+				 *
+				 * @param string   $auto_updates_string The string output for the auto-updates column.
+				 * @param WP_Theme $theme               An object of theme data.
+				 * @param bool     $enabled             Whether auto-updates are enabled for this item.
+				 */
+				$auto_updates_string = apply_filters( 'theme_auto_update_debug_string', $auto_updates_string, $theme, $enabled );
+
 				$theme_version_string       .= ' | ' . $auto_updates_string;
 				$theme_version_string_debug .= ',' . $auto_updates_string;
 			}
-- 
2.26.2.windows.1

