Make WordPress Core

Opened 6 months ago

Last modified 3 weeks ago

#57278 new enhancement

Add filter to allow filtering of the must-use plugins list in the admin

Reported by: nateallen's profile nateallen Owned by:
Milestone: 6.3 Priority: normal
Severity: normal Version: 3.0
Component: Plugins Keywords: has-patch has-unit-tests needs-testing has-testing-info
Focuses: administration Cc:

Description

Currently, the Must-Use plugin list in the WordPress admin only shows files that end in .php, and there is no way to filter this list. This is a problem because we use a single autoloader that allows us to add plugins to mu-plugins without needing to create a separate PHP file for each one.

As a result, the Must-Use plugin list appears like this:

https://cldup.com/DYT8nmF91L.png

Admins have no way of knowing which must-use plugins are actually installed.

If there was a filter, we could remove mu-loader.php from the list and add each plugin individually, like this:

<?php
function custom_mu_plugin_filter( $mu_plugins ) {
        unset( $mu_plugins['mu-loader.php'] );

        $directories = glob( WPMU_PLUGIN_DIR . '/*', GLOB_ONLYDIR );

        foreach ( $directories as $directory ) {
                $plugin_file = $directory . '/' . basename( $directory ) . '.php';

                $mu_plugins[ $plugin_file ] = get_plugin_data( $plugin_file );
        }

        return $mu_plugins;
}
add_filter( 'mu_plugins', 'custom_mu_plugin_filter' );

Which would result in:

https://cldup.com/Fvk7r2jvc1.png

There are probably other uses for this filter, like hiding a must-use plugin from the list that you don't want clients/users seeing.

Attachments (2)

Capture d’écran 2023-04-20 à 14.38.43.png (72.8 KB) - added by ecorica 7 weeks ago.
Screenshot after testing PR 3728
Testing Feature - 57278.png (129.0 KB) - added by zunaid321 3 weeks ago.
Feature Working For Ticket #57278

Download all attachments as: .zip

Change History (21)

This ticket was mentioned in PR #3728 on WordPress/wordpress-develop by nate-allen.


6 months ago
#1

  • Keywords has-patch added

Adds a new filter called mu_plugins that allows the filtering of the must-use plugins array.

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

#2 @fischfood
6 months ago

Test Report

This report confirms that the indicated patch addresses the enhancement.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/3728

Environment

OS: macOS 12.4
PHP: 8.0.0+2
WordPress: 6.1.1
Browsers: Brave v1.46.134, Safari 15.5
Theme: Twenty Twenty-Two

Steps

  • Added a single file plugin to the mu-plugins directory (visible)
  • Added Akismet Anti-Spam and Jetpack to the mu-plugins directory (not visible)
  • Added the above filter to my theme

Results

After adding the example 'custom_mu_plugin_filter' to my theme, instead of just seeing my standalone single file plugin, I am now able to see the two directory based plugins as well

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


6 months ago

#4 @SergeyBiryukov
6 months ago

  • Milestone changed from Awaiting Review to 6.2

#5 @mukesh27
6 months ago

  • Keywords needs-unit-tests changes-requested added
  • Version set to 3.0

Thanks @nateallen, Left one nit-pick document changes feedback on PR.

Can you please add unit tests for the new filter?

get_mu_plugins() introduce in version to 3.0

#6 follow-up: @peterwilsoncc
6 months ago

Instead of a filter for mu-plugins, I think a general purpose filter for items displayed in the plugin list table could be of more use here.

This would allow developers to show/hide plugins in each category on an as needs basis.

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


4 months ago

#8 in reply to: ↑ 6 @SergeyBiryukov
4 months ago

Replying to peterwilsoncc:

Instead of a filter for mu-plugins, I think a general purpose filter for items displayed in the plugin list table could be of more use here.

I tend to agree, this seems more flexible in the long run.

#9 @audrasjb
4 months ago

Also, I'm unsure about the name of the filter in the proposed PR, maybe it's too general. Adding _list` would clarify that it is filtering the list items.

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


4 months ago

#11 @nateallen
4 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

Thanks everyone for the suggestions! I implemented the change as a filter for all of the plugins instead of just must-use plugins.

The filter is added right before the totals get calculated. This ensures accuracy of the plugin count even if the filter is used for adding or removing plugins.

I also added a test for the filter.

#12 @audrasjb
4 months ago

  • Keywords needs-testing added; changes-requested removed

#13 @costdev
4 months ago

  • Milestone changed from 6.2 to Future Release

As the PR still needs to be tested and we're releasing 6.2 Beta 1 today, I'm going to move this to Future Release.

P.S. There are some failures on the PR, but they don't appear to be related to the patch, I think?

Additional props: @mukesh27

#14 @nateallen
2 months ago

  • Keywords has-testing-info added

The failures with the PR were caused by null being passed to strlen in class-wp-plugins-list-table.php, which results in a TypeError in PHP 8. I added an isset check before calling strlen which fixes that issue.

Here are testing instructions for the new plugins_list filter:

To reproduce the issue:

  1. Add a normal plugin to the mu-plugins directory (like Jetpack)
  2. Add the mu-loader.php file to the mu-plugins directory. See code at the bottom of this comment
  3. In the admin, go to Plugins > Installed Plugins and click on the "Must-Use" link
  4. Observe that the mu-loader.php is listed, but the plugin is not

To test the fix:

  1. Apply the patch attached to this ticket.
  2. Ensure a plugin and mu-loader.php are in mu-plugins as before
  3. Use the new plugins_list filter to add the plugin to the list. See example code at the bottom of this comment.
  4. In the admin, go to Plugins > Installed Plugins and click on the "Must-Use" link
  5. Observe that the plugin is now listed and mu-loader.php is removed

Code used for testing:

The must-use autoloader example
Save this code to an mu-loader.php file in your mu-plugins directory

<?php
require_once ABSPATH . 'wp-admin/includes/plugin.php';

foreach ( glob( WPMU_PLUGIN_DIR . '/*/*.php' ) as $file ) {
        $plugin_data = get_plugin_data( $file, false, false );

        if ( empty( $plugin_data['Name'] ) ) {
                continue;
        }

        include_once $file;
}

Using the new plugins_list filter
Place this code in your theme's functions.php file

<?php
function custom_mu_plugin_filter( $mu_plugins ) {
        unset( $mu_plugins['mustuse']['mu-loader.php'] );

        foreach ( glob( WPMU_PLUGIN_DIR . '/*/*.php' ) as $file ) {
                $plugin_data = get_plugin_data( $file, false, false );

                if ( empty( $plugin_data['Name'] ) ) {
                        continue;
                }

                $mu_plugins['mustuse'][ basename( $file ) ] = $plugin_data;
        }

        return $mu_plugins;
}
add_filter( 'plugins_list', 'custom_mu_plugin_filter' );

#15 @SergeyBiryukov
2 months ago

  • Milestone changed from Future Release to 6.3

#17 @ecorica
7 weeks ago

Test Report

This report validates that the indicated patch addresses the issue.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/3728

Environment

  • OS: macOS 13.3.1
  • Web Server: Nginx
  • PHP: 7.4.33
  • WordPress: 6.3-alpha-55505-src
  • Browser: Google Chrome 112.0.5615.121
  • Theme: Twenty Twenty-Three
  • Active Plugins:
    • Jetpack 12.0

Actual Results

✅ After following the steps of @nateallen I confirm that the jetpack plugin appears in the list of "Must use" plugins and that the mu-loader.php does not appear in the list.

Last edited 7 weeks ago by ecorica (previous) (diff)

@ecorica
7 weeks ago

Screenshot after testing PR 3728

#18 @zunaid321
3 weeks ago

Report

This report confirms that the suggested patch fixes the problem.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/3728

Environment

  • OS: Windows 11 (22H2)
  • Web Server: nginx/1.23.4
  • PHP: 7.4.33
  • WordPress: 6.3-alpha-55505-src
  • Browsers: Chrome Version 113.0.5672.126 (Official Build) (64-bit)
  • Theme: Twenty Twenty-Three

Steps:

  • Added the file: mu-loader.php with the mentioned code to the mu-plugins directory.
  • Added Jetpack to the mu-plugins directory
  • Added the above filter code to functions.php of my current theme.

Results

I was able to replicate the feature as mentioned in this ticket.

Last edited 3 weeks ago by zunaid321 (previous) (diff)

@zunaid321
3 weeks ago

Feature Working For Ticket #57278

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


3 weeks ago

Note: See TracTickets for help on using tickets.