Make WordPress Core

Changeset 56068


Ignore:
Timestamp:
06/27/2023 04:00:00 PM (3 years ago)
Author:
audrasjb
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-plugins-list-table.php

    r55954 r56068  
    296296                        $plugins['search'] = array_filter( $plugins['all'], array( $this, '_search_callback' ) );
    297297                }
     298
     299                /**
     300                 * Filters the array of plugins for the list table.
     301                 *
     302                 * @since 6.3.0
     303                 *
     304                 * @param array[] $plugins An array of arrays of plugin data, keyed by context.
     305                 */
     306                $plugins = apply_filters( 'plugins_list', $plugins );
    298307
    299308                $totals = array();
  • trunk/tests/phpunit/tests/admin/wpPluginsListTable.php

    r55903 r56068  
    2020
    2121        /**
    22          * Creates an admin user before any tests run.
     22         * The original value of the `$s` global.
     23         *
     24         * @var string|null
     25         */
     26        private static $original_s;
     27
     28        /**
     29         * @var array
     30         */
     31        public $fake_plugin = array(
     32                'fake-plugin.php' => array(
     33                        'Name'        => 'Fake Plugin',
     34                        'PluginURI'   => 'https://wordpress.org/',
     35                        'Version'     => '1.0.0',
     36                        'Description' => 'A fake plugin for testing.',
     37                        'Author'      => 'WordPress',
     38                        'AuthorURI'   => 'https://wordpress.org/',
     39                        'TextDomain'  => 'fake-plugin',
     40                        'DomainPath'  => '/languages',
     41                        'Network'     => false,
     42                        'Title'       => 'Fake Plugin',
     43                        'AuthorName'  => 'WordPress',
     44                ),
     45        );
     46
     47        /**
     48         * Creates an admin user before any tests run and backs up the `$s` global.
    2349         */
    2450        public static function set_up_before_class() {
     51                global $s;
     52
    2553                parent::set_up_before_class();
    2654
     
    3361                        )
    3462                );
     63                self::$original_s = $s;
    3564        }
    3665
     
    3867                parent::set_up();
    3968                $this->table = _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => 'plugins' ) );
     69        }
     70
     71        /**
     72         * Restores the `$s` global after each test.
     73         */
     74        public function tear_down() {
     75                global $s;
     76
     77                $s = self::$original_s;
     78
     79                parent::tear_down();
    4080        }
    4181
     
    254294                );
    255295        }
     296
     297        /**
     298         * Tests that WP_Plugins_List_Table::prepare_items()
     299         * applies 'plugins_list' filters.
     300         *
     301         * @ticket 57278
     302         *
     303         * @covers WP_Plugins_List_Table::prepare_items
     304         */
     305        public function test_plugins_list_filter() {
     306                global $status, $s;
     307
     308                $old_status = $status;
     309                $status     = 'mustuse';
     310                $s          = '';
     311
     312                add_filter( 'plugins_list', array( $this, 'plugins_list_filter' ), 10, 1 );
     313                $this->table->prepare_items();
     314                $plugins = $this->table->items;
     315                remove_filter( 'plugins_list', array( $this, 'plugins_list_filter' ), 10 );
     316
     317                // Restore to default.
     318                $status = $old_status;
     319                $this->table->prepare_items();
     320
     321                $this->assertSame( $plugins, $this->fake_plugin );
     322        }
     323
     324        /**
     325         * Adds a fake plugin to an array of plugins.
     326         *
     327         * Used as a callback for the 'plugins_list' hook.
     328         *
     329         * @return array
     330         */
     331        public function plugins_list_filter( $plugins_list ) {
     332                $plugins_list['mustuse'] = $this->fake_plugin;
     333
     334                return $plugins_list;
     335        }
    256336}
Note: See TracChangeset for help on using the changeset viewer.