Make WordPress Core

Opened 20 months ago

Closed 14 months ago

Last modified 13 months ago

#57278 closed enhancement (fixed)

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

Reported by: nateallen's profile nateallen Owned by: audrasjb's profile audrasjb
Milestone: 6.3 Priority: normal
Severity: normal Version: 3.0
Component: Plugins Keywords: has-patch has-unit-tests has-testing-info commit add-to-field-guide has-dev-note
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 16 months ago.
Screenshot after testing PR 3728
Testing Feature - 57278.png (129.0 KB) - added by zunaid321 15 months ago.
Feature Working For Ticket #57278

Download all attachments as: .zip

Change History (33)

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


20 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
20 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.


20 months ago

#4 @SergeyBiryukov
20 months ago

  • Milestone changed from Awaiting Review to 6.2

#5 @mukesh27
20 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
20 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.


19 months ago

#8 in reply to: ↑ 6 @SergeyBiryukov
19 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
19 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.


18 months ago

#11 @nateallen
18 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
18 months ago

  • Keywords needs-testing added; changes-requested removed

#13 @costdev
18 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
17 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
17 months ago

  • Milestone changed from Future Release to 6.3

#17 @ecorica
16 months 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 16 months ago by ecorica (previous) (diff)

@ecorica
16 months ago

Screenshot after testing PR 3728

#18 @zunaid321
15 months 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 15 months ago by zunaid321 (previous) (diff)

@zunaid321
15 months ago

Feature Working For Ticket #57278

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


15 months ago

#20 @oglekler
14 months ago

@audrasjb and @costdev can you please look at this PR, if it is ready, it can go into the trunk for not to wait another release. Thanks 🙏

#21 @costdev
14 months ago

  • Keywords changes-requested needs-refresh added; needs-testing removed

Thanks for the ping @oglekler!

  • I've left a review on the PR with minor changes requested. Adding changes-requested.
  • There are merge conflicts with trunk. Adding needs-refresh.

Test results for the PR look good, so once the feedback and merge conflicts are addressed, this seems ready for commit consideration, and should be able to land in 6.3.

During the next scrub, if you see that Nate hasn't had a chance to update the PR and you're about to punt this ticket, ping me and I'll submit an updated PR so this can be committed ahead of 6.3 Beta 1.

@audrasjb commented on PR #3728:


14 months ago
#22

I committed fb4a416 to solve a conflict against trunk.

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


14 months ago

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


14 months ago
#24

  • Keywords needs-refresh removed

#25 @audrasjb
14 months ago

  • Keywords needs-dev-note commit added; changes-requested removed
  • Owner set to audrasjb
  • Status changed from new to accepted

I tested this filter using the following code:

function wporg_filter_plugins_list( $plugins ) {
	unset( $plugins['all']['hello-dolly/hello.php'] );
	return $plugins;
}
add_filter( 'plugins_list', 'wporg_filter_plugins_list' );

The plugin is removed from the list. Works fine.

Self assigning for commit.

#26 @audrasjb
14 months ago

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

In 56068:

Plugins: Introduce the plugins_list filter.

This changeset adds the plugins_list hook, which can be use to filter the list of plugin displayed on WP Admin Plugins screen.

Props nateallen, fischfood, mukesh27, peterwilsoncc, SergeyBiryukov, audrasjb, costdev, ecorica, zunaid321.
Fixes #57278.

#28 @stevenlinx
13 months ago

  • Keywords add-to-field-guide added

#30 @stevenlinx
13 months ago

  • Keywords has-dev-note added; needs-dev-note removed

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


13 months ago

Note: See TracTickets for help on using tickets.