#44169 closed enhancement (fixed)
New filter to short circuit WP_User_Query results
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Users | Keywords: | has-patch has-unit-tests has-dev-note |
Focuses: | Cc: | ||
PR Number: |
Description ¶
#41700 added the filter posts_pre_query
to WP_Query. This filter lets you short circuit the WP_Query MySQL query to return your own results. We use this filter in ElasticPress to return results from Elasticsearch instead of MySQL.
This ticket (patch incoming) is to add a similar filter to WP_User_Query.
Commits (2)
- [44373] Users: Add a
users_pre_query
filter to short circuit WP_User_Query results.… by @adamsilverstein 14 months ago - [44410] Docs: Correct
@since
tag forusers_pre_query
filter added in [44373].… by @SergeyBiryukov 13 months ago
Attachments (7)
Pull requests
- Loading...
Change History (44)
#5
@ Lead Developer
18 months ago
@tlovett1 Would like some reassurance that this doesn't impact login and to clarify that you aren't returning early because you still want to continue on to the caching part of the query()
function.
#8
@ Core Committer
17 months ago
- Owner set to adamsilverstein
- Status changed from new to assigned
@tlovett1 - Thanks for the ticket, patch and tests! This looks good and I can see how this would be useful!
I posted a new combined diff with your patch and tests in 44169.1.diff.
The diff is unfortunately complicated and hard to look at because of the existing lines that are intended in the patch (because they sit inside a conditional); the diff has them oddly interspersed and that makes it hard to see what you are actually changing.
I created 44169-indent-fixed.diff as a workaround - in this diff i left the existing lines unindented so you can much more clearly see what is added by your patch. in this diff it is clearer that your code runs the filter passing null, then if the result is still null it runs the existing code that queries the database. if the result is not null it continues as normal with the results just after the existing database code executes.
I'm seeing some test failures I'll look into: https://travis-ci.org/adamsilverstein/wordpress-develop-fork/jobs/431694171
I'd love to see what @boonebgorges thinks since he did so much of the original work on #36687 and has a deep understanding of the implications of any changes here.
#10
@ Core Committer
17 months ago
Thanks for the ping, @adamsilverstein. The approach in 44169.2.diff looks good to me. To be clear, callbacks will have to following a similar pattern to what I outlined in https://core.trac.wordpress.org/ticket/36687#comment:8:
<?php function wp44169_do_external_users_query( $users, WP_User_Query $query ) { $response = wp_remote_get( 'https://my-remote-data-store/foo/bar' ); if ( 200 === wp_remote_response_code( $response ) ) { $response_body = wp_remote_retrieve_body( $response ); // Assuming the API response is a JSON object containing `user_ids` and an int `found_users` $response_body = json_decode( $response_body ); $users = array_map( 'intval', $response_body->user_ids ); $query->found_users = (int) $response_body->found_users; } return $posts; } add_filter( 'users_pre_query', 'wp44169_do_external_users_query', 10, 2 );
As in the case of posts_pre_query
, callbacks will have to be careful about fields
- the external API may have to return user IDs or quasi-WP_User objects, depending on the value of fields
.
#11
@ Core Committer
17 months ago
- Milestone changed from Awaiting Review to 4.9.9
@boonebgorges - Thanks for the review and documentation details about how the filter would be used. I'll get this committed - is this the type of thing that warrants dev notes?
#12
@ Core Committer
17 months ago
@adamsilverstein I don't recall a dev note about the corresponding posts_pre_query, but I think it couldn't hurt to have one here. Thanks!
#16
@ Core Committer
16 months ago
@pento can we consider this for 5.0/.01? This change is a dependency for user support in elasticpress' upcoming version.
#19
@
14 months ago
I updated @adamsilverstein 's patch, so it now applies to trunk.
#20
@ Core Committer
14 months ago
Thanks @spacedmonkey - I'll get this committed as soon as trunk is open for business.
#22
@
14 months ago
@adamsilverstein can we get this committed?
#25
@ Core Committer
14 months ago
In 44169.diff - refresh against trunk & set @since versions to '5.0.3'
#26
@
14 months ago
Hello,
5.0.3 is going to be released soon. We are currently sorting the remaining tickets in the milestone. Do you think this ticket can be committed in the next few days? If not, let's address this in 5.1 which is coming in February.
Thanks,
Jb
#28
@ Core Committer
14 months ago
- Resolution set to fixed
- Status changed from assigned to closed
In 44373:
This ticket was mentioned in Slack in #core-committers by adamsilverstein. View the logs.
14 months ago
#30
@ Core Committer
14 months ago
- Resolution fixed deleted
- Status changed from closed to reopened
re-opening to consider 5.0.3 backporting
#31
@ Core Committer
14 months ago
@pento shall I go ahead and backport this to the 5.0 branch/5.0.3 or is this more appropriate for the 5.1 release? @boonebgorges suggested in slack that 5.1 is more appropriate since this adds a new filter.
#32
@ Core Committer
14 months ago
- Milestone changed from 5.0.3 to 5.1
tagging as 5.1 based on feedback in slack #committers channel.
#33
@ Core Committer
14 months ago
- Resolution set to fixed
- Status changed from reopened to closed
closing as this is already merged to trunk/5.1
#35
@ Core Committer
13 months ago
Thanks for fixing that @SergeyBiryukov!
#36
@ Core Committer
13 months ago
- Keywords has-dev-note added; needs-dev-note removed
Dev note has been posted: https://make.wordpress.org/core/2019/01/18/new-user-related-short-circuit-filters/.
@tlovett1 Thanks for the patch.
It passes the current unit tests for the user group.
I will mark that this will need unit tests, if this goes forward.
Regarding the use cases, I can imagine this would be mostly used to override the search query of
WP_User_Query
, both front and backend?I wonder if this could affect other processes? I don't think
WP_User_Query
is used during the login progress, but one could e.g. test what happens ifWP_User_Query
returns no users at all, during login.ps: It seems that
posts_pre_query
was introduced toWP_Query
ticket #36687.