From 929c270cf9e5a7577da8712a2d57586c79611e5a Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Tue, 27 Jun 2023 16:00:00 +0000 Subject: [PATCH] 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 --- .../includes/class-wp-plugins-list-table.php | 9 ++ .../tests/admin/wpPluginsListTable.php | 82 ++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/class-wp-plugins-list-table.php b/src/wp-admin/includes/class-wp-plugins-list-table.php index 81e45cf258..b58ba5f352 100644 --- a/src/wp-admin/includes/class-wp-plugins-list-table.php +++ b/src/wp-admin/includes/class-wp-plugins-list-table.php @@ -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 ); diff --git a/tests/phpunit/tests/admin/wpPluginsListTable.php b/tests/phpunit/tests/admin/wpPluginsListTable.php index bf59f41385..88b6d75669 100644 --- a/tests/phpunit/tests/admin/wpPluginsListTable.php +++ b/tests/phpunit/tests/admin/wpPluginsListTable.php @@ -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; + } }