[wp-trac] [WordPress Trac] #53765: Media Library shows only the selected image

WordPress Trac noreply at wordpress.org
Thu Dec 2 09:25:45 UTC 2021


#53765: Media Library shows only the selected image
--------------------------+-----------------------------
 Reporter:  benitolopez   |       Owner:  hellofromTonya
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:  5.9
Component:  Media         |     Version:  5.8
 Severity:  normal        |  Resolution:  fixed
 Keywords:  has-patch     |     Focuses:  javascript
--------------------------+-----------------------------

Comment (by szaqal21):

 Yesterday I was testing my plugin which adds ability to add second
 featured image to post (written based on WP 5.8 code), my controller:

 {{{
 var FeaturedImage2 = wp.media.controller.FeaturedImage.extend(
         {
                 defaults: _.defaults({
                         id: "featured-image2"
                 }, wp.media.controller.Library.prototype.defaults ),

                 updateSelection : function() {
                         var selection = this.get("selection"),
                         id = wp.media.view.settings.post.featuredImage2Id,
                         attachment;

                         if ( "" !== id && -1 !== id ) {
                                 attachment =
 wp.media.model.Attachment.get( id );
                                 attachment.fetch();
                         }

                         selection.reset( attachment ? [ attachment ] : []
 );
                 }
         });
 }}}

 as you can see I've overriden updateSelection() method of FeaturedImage
 controller so it doesn't contain newset patch from WP 5.9 Beta 1

 {{{
 if ( ! infiniteScrolling && library.getTotalAttachments() === 0 &&
 library.hasMore() ) {
                         library.more();
                 }
 }}}

 to my surprise it worked fine, all attachments were loaded when trying to
 change featured image (not only selected one). It turns out that removal
 of updateSelection() from activate() method of FeaturedImage controller in
 first patch
 [https://core.trac.wordpress.org/attachment/ticket/53765/53765.diff]
 already fixed the issue in this case. There is no need to conditionally
 call library.more() in updateSelection() because AttachmentsBrowser view
 already calls more() in updateContent() method when initializing. So I've
 reverted changes in updateSelection() (removed library, infiniteScrolling
 vars and conditional):

 {{{
 /**
          * @since 3.5.0
          */
         activate: function() {
                 this.frame.on( 'open', this.updateSelection, this );

                 Library.prototype.activate.apply( this, arguments );
         },

         /**
          * @since 3.5.0
          */
         deactivate: function() {
                 this.frame.off( 'open', this.updateSelection, this );

                 Library.prototype.deactivate.apply( this, arguments );
         },

         /**
          * @since 3.5.0
          */
         updateSelection: function() {
                 var selection = this.get('selection'),
                         id = wp.media.view.settings.post.featuredImageId,
                         attachment;

                 if ( '' !== id && -1 !== id ) {
                         attachment = Attachment.get( id );
                         attachment.fetch();
                 }

                 selection.reset( attachment ? [ attachment ] : [] );
         }
 }}}

 and all tests went fine.

 I've also updated ReplaceImage controller to call updateSelection() on
 frame event (not sure if this is the best choice but I couldn't find any
 better):

 {{{
 /**
          * @since 3.9.0
          */
         activate: function() {
                 this.frame.on( 'content:render:browse',
 this.updateSelection, this );

                 Library.prototype.activate.apply( this, arguments );
         },

         /**
          * @since 5.9.0
          */
         deactivate: function() {
                 this.frame.off( 'content:render:browse',
 this.updateSelection, this );

                 Library.prototype.deactivate.apply( this, arguments );
         },

         /**
          * @since 3.9.0
          */
         updateSelection: function() {
                 var selection = this.get('selection'),
                         attachment = this.image.attachment;

                 selection.reset( attachment ? [ attachment ] : [] );
         }
 }}}

 @joedolson could you test the changes?

 @hellofromTonya @joedolson what do you think, shall we reopen it?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53765#comment:51>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list