From d9f09453a2b6fc0b5f3b5abc800bd336625a05d3 Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Tue, 10 Nov 2020 12:33:00 -0700
Subject: [PATCH] Adjust the counts of plugins and themes for which
 auto-updates are enabled/disabled (in their respective list table views) when
 a plugin or theme is deleted.

---
 src/js/_enqueues/wp/updates.js                | 90 ++++++++++++++-----
 .../class-wp-ms-themes-list-table.php         |  8 +-
 2 files changed, 71 insertions(+), 27 deletions(-)

diff --git a/src/js/_enqueues/wp/updates.js b/src/js/_enqueues/wp/updates.js
index f4d3472f84..e7454c0eab 100644
--- a/src/js/_enqueues/wp/updates.js
+++ b/src/js/_enqueues/wp/updates.js
@@ -8,22 +8,26 @@
 /* global pagenow */
 
 /**
- * @param {jQuery}  $                                   jQuery object.
- * @param {object}  wp                                  WP object.
- * @param {object}  settings                            WP Updates settings.
- * @param {string}  settings.ajax_nonce                 Ajax nonce.
- * @param {object=} settings.plugins                    Base names of plugins in their different states.
- * @param {Array}   settings.plugins.all                Base names of all plugins.
- * @param {Array}   settings.plugins.active             Base names of active plugins.
- * @param {Array}   settings.plugins.inactive           Base names of inactive plugins.
- * @param {Array}   settings.plugins.upgrade            Base names of plugins with updates available.
- * @param {Array}   settings.plugins.recently_activated Base names of recently activated plugins.
- * @param {object=} settings.themes                     Plugin/theme status information or null.
- * @param {number}  settings.themes.all                 Amount of all themes.
- * @param {number}  settings.themes.upgrade             Amount of themes with updates available.
- * @param {number}  settings.themes.disabled            Amount of disabled themes.
- * @param {object=} settings.totals                     Combined information for available update counts.
- * @param {number}  settings.totals.count               Holds the amount of available updates.
+ * @param {jQuery}  $                                        jQuery object.
+ * @param {object}  wp                                       WP object.
+ * @param {object}  settings                                 WP Updates settings.
+ * @param {string}  settings.ajax_nonce                      Ajax nonce.
+ * @param {object=} settings.plugins                         Base names of plugins in their different states.
+ * @param {Array}   settings.plugins.all                     Base names of all plugins.
+ * @param {Array}   settings.plugins.active                  Base names of active plugins.
+ * @param {Array}   settings.plugins.inactive                Base names of inactive plugins.
+ * @param {Array}   settings.plugins.upgrade                 Base names of plugins with updates available.
+ * @param {Array}   settings.plugins.recently_activated      Base names of recently activated plugins.
+ * @param {Array}   settings.plugins['auto-update-enabled']  Base names of plugins set to auto-update.
+ * @param {Array}   settings.plugins['auto-update-disabled'] Base names of plugins set to not auto-update.
+ * @param {object=} settings.themes                          Slugs of themes in their different states.
+ * @param {Array}   settings.themes.all                      Slugs of all themes.
+ * @param {Array}   settings.themes.upgrade                  Slugs of themes with updates available.
+ * @param {Arrat}   settings.themes.disabled                 Slugs of disabled themes.
+ * @param {Array}   settings.themes['auto-update-enabled']   Slugs of themes set to auto-update.
+ * @param {Array}   settings.themes['auto-update-disabled']  Slugs of themes set to not auto-update.
+ * @param {object=} settings.totals                          Combined information for available update counts.
+ * @param {number}  settings.totals.count                    Holds the amount of available updates.
  */
 (function( $, wp, settings ) {
 	var $document = $( document ),
@@ -1007,6 +1011,24 @@
 				}
 			}
 
+			if ( -1 !== _.indexOf( plugins['auto-update-enabled'], response.plugin ) ) {
+				plugins['auto-update-enabled'] = _.without( plugins['auto-update-enabled'], response.plugin );
+				if ( plugins['auto-update-enabled'].length ) {
+					$views.find( '.auto-update-enabled .count' ).text( '(' + plugins['auto-update-enabled'].length + ')' );
+				} else {
+					$views.find( '.auto-update-enabled' ).remove();
+				}
+			}
+
+			if ( -1 !== _.indexOf( plugins['auto-update-disabled'], response.plugin ) ) {
+				plugins['auto-update-disabled'] = _.without( plugins['auto-update-disabled'], response.plugin );
+				if ( plugins['auto-update-disabled'].length ) {
+					$views.find( '.auto-update-disabled .count' ).text( '(' + plugins['auto-update-disabled'].length + ')' );
+				} else {
+					$views.find( '.auto-update-disabled' ).remove();
+				}
+			}
+
 			plugins.all = _.without( plugins.all, response.plugin );
 
 			if ( plugins.all.length ) {
@@ -1505,7 +1527,7 @@
 			$themeRows.css( { backgroundColor: '#faafaa' } ).fadeOut( 350, function() {
 				var $views     = $( '.subsubsub' ),
 					$themeRow  = $( this ),
-					totals     = settings.themes,
+					themes     = settings.themes,
 					deletedRow = wp.template( 'item-deleted-row' );
 
 				if ( ! $themeRow.hasClass( 'plugin-update-tr' ) ) {
@@ -1521,23 +1543,43 @@
 				$themeRow.remove();
 
 				// Remove theme from update count.
-				if ( $themeRow.hasClass( 'update' ) ) {
-					totals.upgrade--;
+				if ( -1 !== _.indexOf( themes.upgrade, response.slug ) ) {
+					themes.upgrade = _.without( themes.upgrade, response.slug );
 					wp.updates.decrementCount( 'theme' );
 				}
 
 				// Remove from views.
-				if ( $themeRow.hasClass( 'inactive' ) ) {
-					totals.disabled--;
-					if ( totals.disabled ) {
-						$views.find( '.disabled .count' ).text( '(' + totals.disabled + ')' );
+				if ( -1 !== _.indexOf( themes.disabled, response.slug ) ) {
+					themes.disabled = _.without( themes.disabled, response.slug );
+					if ( themes.disabled.length ) {
+						$views.find( '.disabled .count' ).text( '(' + themes.disabled.length + ')' );						
 					} else {
 						$views.find( '.disabled' ).remove();
 					}
 				}
 
+				if ( -1 !== _.indexOf( themes['auto-update-enabled'], response.slug ) ) {
+					themes['auto-update-enabled'] = _.without( themes['auto-update-enabled'], response.slug );
+					if ( themes['auto-update-enabled'].length ) {
+						$views.find( '.auto-update-enabled .count' ).text( '(' + themes['auto-update-enabled'].length + ')' );
+					} else {
+						$views.find( '.auto-update-enabled' ).remove();
+					}
+				}
+	
+				if ( -1 !== _.indexOf( themes['auto-update-disabled'], response.slug ) ) {
+					themes['auto-update-disabled'] = _.without( themes['auto-update-disabled'], response.slug );
+					if ( themes['auto-update-disabled'].length ) {
+						$views.find( '.auto-update-disabled .count' ).text( '(' + themes['auto-update-disabled'].length + ')' );
+					} else {
+						$views.find( '.auto-update-disabled' ).remove();
+					}
+				}
+
+				themes.all = _.without( themes.all, response.slug );
+
 				// There is always at least one theme available.
-				$views.find( '.all .count' ).text( '(' + --totals.all + ')' );
+				$views.find( '.all .count' ).text( '(' + themes.all.length + ')' );
 			} );
 		}
 
diff --git a/src/wp-admin/includes/class-wp-ms-themes-list-table.php b/src/wp-admin/includes/class-wp-ms-themes-list-table.php
index a22fcb80bb..15bdc00911 100644
--- a/src/wp-admin/includes/class-wp-ms-themes-list-table.php
+++ b/src/wp-admin/includes/class-wp-ms-themes-list-table.php
@@ -206,9 +206,11 @@ class WP_MS_Themes_List_Table extends WP_List_Table {
 			$themes['search'] = array_filter( array_merge( $themes['all'], $themes['broken'] ), array( $this, '_search_callback' ) );
 		}
 
-		$totals = array();
+		$totals    = array();
+		$js_themes = array();
 		foreach ( $themes as $type => $list ) {
-			$totals[ $type ] = count( $list );
+			$totals[ $type ]    = count( $list );
+			$js_themes[ $type ] = array_keys( (array) $list );
 		}
 
 		if ( empty( $themes[ $status ] ) && ! in_array( $status, array( 'all', 'search' ), true ) ) {
@@ -225,7 +227,7 @@ class WP_MS_Themes_List_Table extends WP_List_Table {
 			'updates',
 			'_wpUpdatesItemCounts',
 			array(
-				'themes' => $totals,
+				'themes' => $js_themes,
 				'totals' => wp_get_update_data(),
 			)
 		);
-- 
2.28.0.windows.1

