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.




git-svn-id: https://develop.svn.wordpress.org/trunk@56068 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras
2023-06-27 16:00:00 +00:00
parent b66fcb55e5
commit 929c270cf9
2 changed files with 90 additions and 1 deletions
@@ -296,6 +296,15 @@ class WP_Plugins_List_Table extends WP_List_Table {
$plugins['search'] = array_filter( $plugins['all'], array( $this, '_search_callback' ) );
}
/**
* Filters the array of plugins for the list table.
*
* @since 6.3.0
*
* @param array[] $plugins An array of arrays of plugin data, keyed by context.
*/
$plugins = apply_filters( 'plugins_list', $plugins );
$totals = array();
foreach ( $plugins as $type => $list ) {
$totals[ $type ] = count( $list );
@@ -19,9 +19,37 @@ class Tests_Admin_wpPluginsListTable extends WP_UnitTestCase {
private static $admin_id;
/**
* Creates an admin user before any tests run.
* The original value of the `$s` global.
*
* @var string|null
*/
private static $original_s;
/**
* @var array
*/
public $fake_plugin = array(
'fake-plugin.php' => array(
'Name' => 'Fake Plugin',
'PluginURI' => 'https://wordpress.org/',
'Version' => '1.0.0',
'Description' => 'A fake plugin for testing.',
'Author' => 'WordPress',
'AuthorURI' => 'https://wordpress.org/',
'TextDomain' => 'fake-plugin',
'DomainPath' => '/languages',
'Network' => false,
'Title' => 'Fake Plugin',
'AuthorName' => 'WordPress',
),
);
/**
* Creates an admin user before any tests run and backs up the `$s` global.
*/
public static function set_up_before_class() {
global $s;
parent::set_up_before_class();
self::$admin_id = self::factory()->user->create(
@@ -32,6 +60,7 @@ class Tests_Admin_wpPluginsListTable extends WP_UnitTestCase {
'user_email' => 'testadmin@test.com',
)
);
self::$original_s = $s;
}
public function set_up() {
@@ -39,6 +68,17 @@ class Tests_Admin_wpPluginsListTable extends WP_UnitTestCase {
$this->table = _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => 'plugins' ) );
}
/**
* Restores the `$s` global after each test.
*/
public function tear_down() {
global $s;
$s = self::$original_s;
parent::tear_down();
}
/**
* @ticket 42066
*
@@ -253,4 +293,44 @@ class Tests_Admin_wpPluginsListTable extends WP_UnitTestCase {
'Drop-ins' => array( 'dropins' ),
);
}
/**
* Tests that WP_Plugins_List_Table::prepare_items()
* applies 'plugins_list' filters.
*
* @ticket 57278
*
* @covers WP_Plugins_List_Table::prepare_items
*/
public function test_plugins_list_filter() {
global $status, $s;
$old_status = $status;
$status = 'mustuse';
$s = '';
add_filter( 'plugins_list', array( $this, 'plugins_list_filter' ), 10, 1 );
$this->table->prepare_items();
$plugins = $this->table->items;
remove_filter( 'plugins_list', array( $this, 'plugins_list_filter' ), 10 );
// Restore to default.
$status = $old_status;
$this->table->prepare_items();
$this->assertSame( $plugins, $this->fake_plugin );
}
/**
* Adds a fake plugin to an array of plugins.
*
* Used as a callback for the 'plugins_list' hook.
*
* @return array
*/
public function plugins_list_filter( $plugins_list ) {
$plugins_list['mustuse'] = $this->fake_plugin;
return $plugins_list;
}
}