Make WordPress Core

Opened 7 weeks ago

Closed 4 days ago

#64948 closed defect (bug) (fixed)

Media Modal: Can no longer set attachment filters

Reported by: bernhard-reiter's profile Bernhard Reiter Owned by: joedolson's profile joedolson
Milestone: 7.0 Priority: normal
Severity: normal Version: trunk
Component: Media Keywords: has-patch commit dev-reviewed fixed-major
Focuses: javascript Cc:

Description

I have a plugin which modifies the media library modal by removing some of the attachment filters. It does so by getting a handle for the toolbar, and then doing

var filters = toolbar.get( 'filters' );

if ( this.get( 'type' ) == 'image' ) {
	// update all
	filters.filters.all.text = acf.__( 'All images' );

	// remove some filters
	delete filters.filters.audio;
	delete filters.filters.video;
	delete filters.filters.image;
}

etc.

This is no longer working as of [61757] -- filters.filters is now undefined.

The reason for this is that [61757] wrapped the media attachment filters <select> and its <label> in a container <div> for accessibility purposes. However, this changes the return value of toolbar.get('filters') from the AttachmentFilters view instance to the wp.media.View corresponding to the <div>, breaking plugins that rely on this to customize the media library filters.

Attachments (2)

before-64948.webp (114.3 KB) - added by ozgursar 4 weeks ago.
Browser console before applying patch PR 11415
after-64948.webp (237.8 KB) - added by ozgursar 4 weeks ago.
Browser console after applying patch PR 11415

Download all attachments as: .zip

Change History (18)

#1 @joedolson
7 weeks ago

  • Focuses javascript added
  • Owner set to joedolson
  • Status changed from new to accepted

#2 @audrasjb
6 weeks ago

As per today's bug scrub:
@joedolson is this one still on your radar?

This ticket was mentioned in PR #11415 on WordPress/wordpress-develop by @joedolson.


6 weeks ago
#3

  • Keywords has-patch added

This change reverts the modifications to the JS API in the media modal so that filters can be modified, and insteads implements the visible labels using CSS.

This results in a more complex CSS to modify, since the lack of a wrapper complicates the layout, but avoids breaking access to the filters in 3rd party code.

Trac ticket: https://core.trac.wordpress.org/ticket/64948

## Use of AI Tools

None.

This ticket was mentioned in PR #11426 on WordPress/wordpress-develop by @kapilpaul.


6 weeks ago
#4

Trac ticket: https://core.trac.wordpress.org/ticket/64948

## Use of AI Tools

@joedolson commented on PR #11426:


6 weeks ago
#5

This looks to be a duplicate of https://github.com/WordPress/wordpress-develop/pull/11415; can you indicate how this is different?

This ticket was mentioned in Slack in #core-test by gaisma22. View the logs.


4 weeks ago

@ozgursar
4 weeks ago

Browser console before applying patch PR 11415

@ozgursar
4 weeks ago

Browser console after applying patch PR 11415

#7 @ozgursar
4 weeks ago

Patch Testing Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/11415

Environment

  • WordPress: 7.1-alpha-62161-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 146.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • Code Snippets 3.9.5
    • Test Reports 1.2.1

Steps taken

  1. Add the following PHP snippet to your active theme's functions.php or via Code Snippets plugin to simulate accessing the filters.
add_action( 'admin_footer', function () {
    if ( ! did_action( 'wp_enqueue_media' ) ) {
        return;
    }
    ?>
    <script>
    jQuery( document ).ready( function ( $ ) {
        if ( ! wp || ! wp.media || ! wp.media.view || ! wp.media.view.AttachmentsBrowser ) {
            return;
        }

        var OrigBrowser = wp.media.view.AttachmentsBrowser;

        wp.media.view.AttachmentsBrowser = OrigBrowser.extend( {
            createToolbar: function () {
                OrigBrowser.prototype.createToolbar.apply( this, arguments );

                var toolbar = this.toolbar;
                var filters = toolbar.get( 'filters' );

                console.log( '[Ticket 64948] toolbar.get("filters"):', filters );
                console.log( '[Ticket 64948] filters.filters:', filters && filters.filters );

                // Simulate what affected plugins do:
                if ( filters && typeof filters.filters !== 'undefined' ) {
                    console.info( '[Ticket 64948] PASS: filters.filters is accessible.' );
                } else {
                    console.error( '[Ticket 64948] FAIL: filters.filters is undefined.' );
                }
            }
        } );
    } );
    </script>
    <?php
} );

  1. Navigate to any post/page editor or the Media Library
  2. Click Add Media to open the media modal
  3. Observe the browser console
  4. Confirm that filters.filters is undefined
  5. Apply the patch and repeat steps 2-4
  6. Observe the browser console again
  7. Confirm that toolbar.get("filters") and filters.filters are accessible
  8. ✅ Patch is solving the problem.

Expected result

  • Plugins that customize media library filters by accessing toolbar.get("filters") should continue to work correctly, and no errors related to filters.filters being undefined should appear in the browser console.

Screenshots/Screencast with results

Before
https://core.trac.wordpress.org/raw-attachment/ticket/64948/before-64948.webp

After
https://core.trac.wordpress.org/raw-attachment/ticket/64948/after-64948.webp

@audrasjb commented on PR #11426:


2 weeks ago
#8

Closing in favor of #11415.

#9 @darshitrajyaguru97
13 days ago

Patch Testing Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/11415

Environment

  • WordPress: 7.1-alpha-62161-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: MySQL
  • Browser: Chrome
  • OS: Windows
  • Theme: Twenty Twenty-Five
  • Plugins:
    1. Code Snippets 3.9.5
    2. Test Reports 1.2.1
  • MU Plugins: None

Steps to Reproduce (Before Patch)

  1. Add the following snippet to functions.php or via Code Snippets:
add_action( 'admin_footer', function () {
    if ( ! did_action( 'wp_enqueue_media' ) ) {
        return;
    }
    ?>
    <script>
    jQuery( document ).ready( function ( $ ) {
        if ( ! wp || ! wp.media || ! wp.media.view || ! wp.media.view.AttachmentsBrowser ) {
            return;
        }

        var OrigBrowser = wp.media.view.AttachmentsBrowser;

        wp.media.view.AttachmentsBrowser = OrigBrowser.extend( {
            createToolbar: function () {
                OrigBrowser.prototype.createToolbar.apply( this, arguments );

                var toolbar = this.toolbar;
                var filters = toolbar.get( 'filters' );

                console.log( '[Ticket 64948] toolbar.get("filters"):', filters );
                console.log( '[Ticket 64948] filters.filters:', filters && filters.filters );

                if ( filters && typeof filters.filters !== 'undefined' ) {
                    console.info( '[Ticket 64948] PASS: filters.filters is accessible.' );
                } else {
                    console.error( '[Ticket 64948] FAIL: filters.filters is undefined.' );
                }
            }
        } );
    } );
    </script>
    <?php
} );
  1. Open any post/page editor or Media Library
  2. Click "Add Media"
  3. Check the browser console

Actual Result (Before Patch)

  • toolbar.get('filters') returns an object
  • filters.filters is undefined
  • Console output:
    FAIL: filters.filters is undefined
    

Steps After Applying Patch

  1. Apply the patch from PR #11415
  2. Repeat the steps above

Result (After Patch)

  • toolbar.get('filters') is accessible
  • filters.filters is properly defined
  • Console output:
    PASS: filters.filters is accessible
    

Expected Result

Plugins relying on toolbar.get('filters') should be able to access filters.filters without encountering undefined, ensuring backward compatibility for media library customizations.

Conclusion

  • ✅ Patch successfully resolves the issue
  • ✅ Restores expected behavior for plugin integrations
  • ✅ No regressions observed during testing

This ticket was mentioned in Slack in #core by jorbin. View the logs.


5 days ago

#11 @jorbin
5 days ago

  • Keywords commit added

https://github.com/WordPress/wordpress-develop/pull/11415/ has an approval and test reports, so I think this is good to move forward.

@joedolson commented on PR #11415:


4 days ago
#12

I actually changed that intentionally; so that's as intended.

#13 @joedolson
4 days ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 62326:

Media: Restore usage of attachment filters in media modal.

[61757] wrapped the media attachment filters, which changed the return value of toolbar filters from AttachmentFilters to a wp.media.View instance, breaking the API to customize media library filters.

Reverts the shape change in the attachment filter return value and implements CSS based positioning for the visible labels.

Props bernhard-reiter, joedolson, ozgursar, audrasjb, darshitrajyaguru97, jorbin.
Fixes #64948.

#14 @joedolson
4 days ago

  • Keywords dev-feedback added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Re-opening for dev review to commit to the 7.0 branch.

#15 @jorbin
4 days ago

  • Keywords dev-reviewed fixed-major added; dev-feedback removed

[62326] looks good for backport to the 7.0 branch.

#16 @joedolson
4 days ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 62327:

Media: Restore usage of attachment filters in media modal.

[61757] wrapped the media attachment filters, which changed the return value of toolbar filters from AttachmentFilters to a wp.media.View instance, breaking the API to customize media library filters.

Reverts the shape change in the attachment filter return value and implements CSS based positioning for the visible labels.

Reviewed by jorbin.
Merges [62326] to the 7.0 branch.

Props bernhard-reiter, joedolson, ozgursar, audrasjb, darshitrajyaguru97, jorbin.
Fixes #64948.

Note: See TracTickets for help on using tickets.