WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#10781 closed enhancement (fixed)

Make WP_User_Search plugin friendly

Reported by: scribu Owned by: westi
Milestone: 3.0 Priority: normal
Severity: normal Version: 2.9
Component: Administration Keywords: has-patch commit early
Focuses: Cc:

Description

Currently, there is no way to modify the query done when displaying wp-admin/users.php

This can be easily changed by allowing plugins to modify query components in the WP_User_Search class.

Attachments (5)

user.php.diff (2.2 KB) - added by scribu 6 years ago.
user.php.2.diff (2.9 KB) - added by scribu 6 years ago.
Fixed paging bug and improved consistency
user.php.3.diff (4.2 KB) - added by transom 6 years ago.
Generalizes WP_User_Search for any meta key/value pair while defaulting to capabilities
sort-users.php (512 bytes) - added by scribu 6 years ago.
example plugin
user.php.4.diff (3.4 KB) - added by scribu 6 years ago.
refresh

Download all attachments as: .zip

Change History (22)

comment:1 @scribu6 years ago

Proposed patch separates query_from_where into 2, so that plugins can more easily add or remove specific bits.

Also, it adds a do_action_ref_array() on $this.

@scribu6 years ago

comment:2 @scribu6 years ago

The second patch fixes possible ID column ambiguity.

comment:3 @scribu6 years ago

  • Keywords commit added

Patch still current.

@scribu6 years ago

Fixed paging bug and improved consistency

comment:4 @ryan6 years ago

  • Keywords early added
  • Milestone changed from 2.9 to 3.0

Let's do this early 3.0. Locking 2.9 down.

comment:5 follow-up: @transom6 years ago

I'll supply a diff shortly but if this is going into 3.0 - let's stretch it just a little bit to allow search on any meta key - assuming capabilities by default by adding a optional fourth parameter.

e.g. function WP_User_Search ($search_term = , $page = , $meta_value = , $meta_key=)

@transom6 years ago

Generalizes WP_User_Search for any meta key/value pair while defaulting to capabilities

comment:6 in reply to: ↑ 5 @westi6 years ago

  • Cc westi added
  • Keywords commit removed

Replying to transom:

I'll supply a diff shortly but if this is going into 3.0 - let's stretch it just a little bit to allow search on any meta key - assuming capabilities by default by adding a optional fourth parameter.

e.g. function WP_User_Search ($search_term = , $page = , $meta_value = , $meta_key=)

Patch looks good.

Could you put together a simple example plugin which shows how this is used and attach it to the ticket.

This will make the changes easy to test and mean that we can get them committed.

comment:7 @hakre6 years ago

To have WP_User_Search become more plugin friendly would be if we can make it extendable and provide a pointer (via filter?) to the actual implementation. Then plugin authors only need to little extend the class to their needs. Or is this already possible?

comment:8 @scribu6 years ago

I think that's a little too much. Not even WP_Query has that option, yet it's still pretty extensible.

comment:9 @scribu6 years ago

user.php.3.diff causes two parser errors.

@scribu6 years ago

example plugin

comment:10 @scribu6 years ago

Added an example plugin that sorts the users by number of posts (only when viewing a single role).

Requires user.php.2.diff

comment:11 @batmoo6 years ago

  • Cc batmoo@… added

I was going to create a new ticket for this, but figured that since the class is being worked on, it makes sense just to bring this up here. One issue to watch for with the role search (which is tricky because we're doing a LIKE search through a serialized array). If I run the following code:

add_role('edit', 'edit');
$u = new WP_User_Search('', '', 'edit');
print_r($u->get_results());

...it will return all users with the "Editor" role (and any user with an edit_* cap). It's sort of an edge case, but likely should still be patched (until the role system is revamped, at least). Best option I see (for now) is to wrap the role term in double quotes (since that's how strings vars in arrays seem to be serialized).

comment:12 @batmoo6 years ago

I actually ended up creating a User Query function (in the same vein as WP_Query) for one of my plugins, that let's you query for users based a number of criteria. Basically you can look up users based on a search_term (as with WP_User_Search), specific user fields, role, usermeta, and so on.

It's completely barebones right now (i.e. not really finished/polished), but if there's interest, I can look into fleshing it out and submitting a patch.

See it here: http://plugins.trac.wordpress.org/browser/edit-flow/trunk/php/util.php

comment:13 @scribu6 years ago

Thanks for the sample, batmoo. It wouldn't hurt to have a more powerful user search class.

But the main point of this ticket is to allow plugins a way in. After that, it can be extended indefinetly.

comment:14 @westi6 years ago

  • Owner set to westi
  • Status changed from new to reviewing

Ok this looks good.

Could you update the patch so it applies to current trunk please and then I will finish the review and commit - I think some of the merge changes have made it no longer apply.

@scribu6 years ago

refresh

comment:15 @scribu6 years ago

  • Keywords commit added

Refreshed patch: user.php.4.diff, compatible with MS.

comment:16 @westi6 years ago

Works well for me.

The role check is the example code causes it not to run when no role is specified which confused my testing for a little while.

comment:17 @westi6 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

(In [12989]) Allow plugins to modify the query run by WP_User_Search. Fixes #10781 props scribu.

Note: See TracTickets for help on using tickets.