Opened 4 years ago

Closed 3 years ago

#10781 closed enhancement (fixed)

Make WP_User_Search plugin friendly

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

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

Download all attachments as: .zip

Change History (22)

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.

scribu4 years ago

The second patch fixes possible ID column ambiguity.

  • Keywords commit added

Patch still current.

scribu4 years ago

Fixed paging bug and improved consistency

comment:4   ryan3 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: ↓ 6   transom3 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=)

transom3 years ago

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

comment:6 in reply to: ↑ 5   westi3 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.

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?

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

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

scribu3 years ago

example plugin

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

Requires user.php.2.diff

  • 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).

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

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.

  • 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.

scribu3 years ago

refresh

  • Keywords commit added

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

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.

  • 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.