[wp-trac] [WordPress Trac] #60495: Add a filter for $allowed_statuses and get_views() in class-wp-plugins-list-table
WordPress Trac
noreply at wordpress.org
Sun Feb 11 23:21:34 UTC 2024
#60495: Add a filter for $allowed_statuses and get_views() in class-wp-plugins-
list-table
-------------------------+---------------------------------------
Reporter: juliobox | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Plugins | Version: 6.3
Severity: normal | Keywords: needs-patch needs-testing
Focuses: |
-------------------------+---------------------------------------
Since WP 6.3 there is a new filter named "**plugins_list**" added in
#57278
Using this filter, we can now add a new array key like "''my_plugins''"
and set some plugins in here.
"''my_plugins''" is now considered as a "''status''" by the WP behavior,
like "''all''", "''recently_activated''", etc, the whole list is here :
https://github.com/WordPress/WordPress/blob/master/wp-admin/includes
/class-wp-plugins-list-table.php#L49
We can also delete (unset()) one of them if we want too, (like hide the
must-use plugins or the "''upgraded''" tab)
So now WordPress will go through each iteration of the array keys (aka
statuses) and create a tab link to get a new view, here :
https://github.com/WordPress/WordPress/blob/master/wp-admin/includes
/class-wp-plugins-list-table.php#L495
**What's the issue here.**
WP will try to display a "text" for each iteration, if there is no case in
the switch, it will still display the $text var, and indeed the last used
value, aka incorrect value.
But remember line 49, WP sets a list of allowed statuses, this shouldn't
be there anymore since the new filter '''plugins_list''' allow us to add
ANY status using an array key. We have to remove line 49 and modify line
52 to remove the in_array() stuff.
Still need a check to keep the same behavior when a wrong status is
loaded? it's already done line 315:
https://github.com/WordPress/WordPress/blob/master/wp-admin/includes
/class-wp-plugins-list-table.php#L315
Now to get the correct translated label in get_views() we need a hook line
586 like:
{{{#!php
<?php
// ...
default:
$text = apply_filters( 'plugins_list_status_text', '', $count, $status
);
if ( empty( $text ) ) {
$text = $status;
}
$text .= ' <span class="count">(%s)</span>';
break;
}
}}}
Now please test in WP 6.3.x, using this:
{{{#!php
<?php
add_filter( 'plugins_list', 'my_plugins_tab' );
function my_plugins_tab( $plugins ) {
$plugins['my_plugins'] = $plugins['all']['hello-dolly/hello.php']
return $plugins;
}
}}}
And go to the /plugins.php page. You'll see the last tab with "(1)" and
its label is a duplicated one, the same as the last one.
Then click on it: your view is the 'All' one, not the desired one.
Now apply my modifications/patch using this:
{{{#!php
<?php
add_filter( 'plugins_list', 'my_plugins_tab' );
function my_plugins_tab( $plugins ) {
$plugins['my_plugins'] = $plugins['all']['hello-dolly/hello.php']
return $plugins;
}
add_filter( 'plugins_list_status_text', 'my_plugins_tab_label', 10, 3 );
function my_plugins_tab_label( $text, $count, $type ) {
if ( 'my_plugins' === $type ) {
$text = _nx( 'My Plugin', 'My Plugins', $count, 'domain' );
}
return $text;
}
}}}
Tadaaa, the label is fine and since we remove the allowed status line
code, the view is correct.
This ticket now fully finish the job started in #57278!
Thanks for your time and tests and opinions.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/60495>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list