[wp-trac] [WordPress Trac] #44169: New filter to short circuit WP_User_Query results
WordPress Trac
noreply at wordpress.org
Mon Sep 24 19:30:19 UTC 2018
#44169: New filter to short circuit WP_User_Query results
--------------------------------------+------------------------------
Reporter: tlovett1 | Owner: adamsilverstein
Type: enhancement | Status: assigned
Priority: normal | Milestone: Awaiting Review
Component: Users | Version:
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests | Focuses:
--------------------------------------+------------------------------
Comment (by boonebgorges):
Thanks for the ping, @adamsilverstein. The approach in
[attachment:"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
<?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`.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/44169#comment:10>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list