From 9bbc1acca5d349d73075f63b1afe81a7c35db61a Mon Sep 17 00:00:00 2001 From: Khokan Sardar Date: Mon, 22 Jun 2026 20:34:31 +0530 Subject: [PATCH] Media: Only count attachments from the mirrored query collection. `wp.media.model.Attachments.observe()` bound the total-attachments counters to every observed collection. Because the Featured Image state observes the selection in addition to the mirrored query, an uploaded image was counted twice: once when added to the query and again when `autoSelect` added it to the selection. This made the Media Library tab report "Showing 1 of 2 media items" after the first upload into an empty library. Restrict `_addToTotalAttachments()` and `_removeFromTotalAttachments()` to the mirrored query collection so the total only reflects items in the library itself. Fixes #65513. --- src/js/media/models/attachments.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/js/media/models/attachments.js b/src/js/media/models/attachments.js index 23510bd949f4c..68680f530ad61 100644 --- a/src/js/media/models/attachments.js +++ b/src/js/media/models/attachments.js @@ -204,7 +204,7 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen * Start observing another attachments collection change events * and replicate them on this collection. * - * @param {wp.media.model.Attachments} The attachments collection to observe. + * @param {wp.media.model.Attachments} attachments The attachments collection to observe. * @return {wp.media.model.Attachments} Returns itself to allow chaining. */ observe: function( attachments ) { @@ -212,9 +212,19 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen this.observers.push( attachments ); attachments.on( 'add change remove', this._validateHandler, this ); - attachments.on( 'add', this._addToTotalAttachments, this ); - attachments.on( 'remove', this._removeFromTotalAttachments, this ); attachments.on( 'reset', this._validateAllHandler, this ); + + /* + * Only track the total number of attachments for the mirrored query + * collection. Other observed collections, such as the selection, must + * not change the count, otherwise an uploaded attachment that is also + * added to the selection is counted more than once. See ticket #65513. + */ + if ( attachments === this.mirroring ) { + attachments.on( 'add', this._addToTotalAttachments, this ); + attachments.on( 'remove', this._removeFromTotalAttachments, this ); + } + this.validateAll( attachments ); return this; }, @@ -239,7 +249,7 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen return this; }, /** - * Update total attachment count when items are added to a collection. + * Update total attachment count when items are removed from a collection. * * @access private *