[wp-trac] [WordPress Trac] #46451: wp_dropdown_users() does not pass all applicable arguments on to get_users()
WordPress Trac
noreply at wordpress.org
Fri Mar 8 23:27:18 UTC 2019
#46451: wp_dropdown_users() does not pass all applicable arguments on to
get_users()
--------------------------+------------------------------
Reporter: pbiron | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Users | Version:
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by pbiron):
There are a couple of ways this could be achieved, I list a few
below...and I'm sure there are others as well.
== Generate a whitelist from a blacklist
{{{#!php
<?php
$blacklist = array(
'show_option_all',
'show_option_none',
'hide_if_only_one_author',
'multi',
'show',
'echo',
'selected',
'name',
'class',
'id',
'include_selected',
'option_none_value',
);
$whitelist = array_diff( array_keys( $r ), $blacklist );
$query_args = wp_array_slice_assoc( $r, $whitelist );
...
$users = get_users( $query_args );
}}}
== Using array_filter() with a named callback that strips the blacklist
{{{#!php
<?php
$query_args = array_filter( $r,
'_wp_dropdown_users_filter_get_users_args', ARRAY_FILTER_USE_KEY );
...
$users = get_users( $query_args );
}}}
where `_wp_dropdown_users_filter_get_users_args()` is defined as:
{{{#!php
<?php
function _wp_dropdown_users_filter_get_users_args( $key ) {
$blacklist = array(
'show_option_all',
'show_option_none',
'hide_if_only_one_author',
'multi',
'show',
'echo',
'selected',
'name',
'class',
'id',
'include_selected',
'option_none_value',
);
return ! in_array( $key, $blacklist );
}
}}}
== Using array_filter() with an anonymous callback that strips the
blacklist
{{{#!php
<?php
$blacklist = array(
'show_option_all',
'show_option_none',
'hide_if_only_one_author',
'multi',
'show',
'echo',
'selected',
'name',
'class',
'id',
'include_selected',
'option_none_value',
);
// I think the "use( $blacklist )" syntax was only available as of PHP 5.3
// but with WP 5.2 slated to bump the minimum PHP version to 5.6 that
won't be a problem
$query_args = array_filter( $r, function( $key ) use ( $blacklist ) {
return ! in_array( $key, $blacklist );
}, ARRAY_FILTER_USE_KEY );
...
$users = get_users( $query_args );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/46451#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list