-
Notifications
You must be signed in to change notification settings - Fork 0
/
meom-user-query.php
234 lines (195 loc) · 6.77 KB
/
meom-user-query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Plugin Name: MEOM user query
* Description: Adds new route /wp-json/user_query/args/ to REST API.
* Author: MEOM
* Author URI: https://meom.fi/
* Version: 0.1.0
* License: GPL2+
**/
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
class WP_User_Query_To_REST_API extends WP_REST_Posts_Controller {
/**
* Constructor
*/
public function __construct() {
// Plugin compatibility.
add_filter( 'wp_user_query_to_rest_api_allowed_args', [ $this, 'plugin_compatibility_args' ] );
add_action( 'wp_user_query_to_rest_api_after_query', [ $this, 'plugin_compatibility_after_query' ] );
// Register REST route.
$this->register_routes();
}
/**
* Register read-only /user_query/args/ route
*/
public function register_routes() {
register_rest_route(
'user_query',
'args',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => [ $this, 'get_items_permissions_check' ],
]
);
}
/**
* Check if a given request has access to get items
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|bool
*/
public function get_items_permissions_check( $request ) {
return apply_filters( 'wp_user_query_to_rest_api_permissions_check', true, $request );
}
/**
* Get a collection of items
*
* @param WP_REST_Request $request Full data about the request.
*/
public function get_items( $request ) {
$parameters = $request->get_query_params();
$default_args = [
'role' => 'Author',
'number' => 10,
];
$default_args = apply_filters( 'wp_user_query_to_rest_api_default_args', $default_args );
// Allow these args => what isn't explicitly allowed, is forbidden
$allowed_args = [
'blog_id',
'role',
'role__in',
'role__not_in',
'capability',
'capability__in',
'capability__not_in',
'include',
'exclude',
'orderby',
'order',
'offset',
'number',
'paged',
'count_total',
'fields',
'who',
'has_published_posts',
'nicename',
'nicename__in',
'nicename__not_in',
'login',
'login__in',
'login__not_in',
'lang', // Polylang
];
// Allow filtering by meta: default true.
if ( apply_filters( 'wp_user_query_to_rest_api_allow_meta', true ) ) {
$allowed_args[] = 'meta_key';
$allowed_args[] = 'meta_value';
$allowed_args[] = 'meta_compare';
$allowed_args[] = 'meta_compare_key';
$allowed_args[] = 'meta_type';
$allowed_args[] = 'meta_type_key';
$allowed_args[] = 'meta_query';
}
// Allow search: default true.
if ( apply_filters( 'wp_user_query_to_rest_api_allow_search', true ) ) {
$allowed_args[] = 'search';
$allowed_args[] = 'search_columns';
}
// Let themes and plugins ultimately decide what to allow.
$allowed_args = apply_filters( 'wp_user_query_to_rest_api_allowed_args', $allowed_args );
// Args from url.
$query_args = [];
foreach ( $parameters as $key => $value ) {
// Skip keys that are not explicitly allowed.
if ( in_array( $key, $allowed_args, true ) ) {
switch ( $key ) {
// Set given value.
default:
$query_args[ $key ] = $value;
break;
}
}
}
// Combine defaults and query_args.
$args = wp_parse_args( $query_args, $default_args );
// Make all the values filterable
foreach ( $args as $key => $value ) {
$args[ $key ] = apply_filters( 'wp_user_query_to_rest_api_arg_value', $value, $key, $args );
}
// Before query: hook your plugins here.
do_action( 'wp_user_query_to_rest_api_before_query', $args );
// Run query.
$user_query = new WP_User_Query( $args );
// After query: hook your plugins here.
do_action( 'wp_user_query_to_rest_api_after_query', $user_query );
$data = [];
$data = apply_filters( 'wp_user_query_to_rest_api_default_data', $data );
if ( ! empty( $user_query->get_results() ) ) {
// Allow query: default true.
if ( apply_filters( 'wp_user_query_to_rest_api_allow_query', true ) ) {
foreach ( $user_query->get_results() as $user ) {
$data[] = $user;
}
}
// After loop hook.
$data = apply_filters( 'wp_user_query_to_rest_api_after_loop_data', $data, $user_query, $args );
}
return $this->get_response( $request, $args, $user_query, $data );
}
/**
* Get response
*
* @access protected
*
* @param WP_REST_Request $request Full details about the request
* @param array $args WP_User_Query args
* @param WP_Query $user_query
* @param array $data response data
*
* @return WP_REST_Response
*/
protected function get_response( $request, $args, $user_query, $data ) {
// Prepare data.
$response = new WP_REST_Response( $data, 200 );
// Total amount of users.
$total_count = intval( $user_query->total_users );
$response->header( 'X-WP-Total', $total_count );
// Total number of pages.
$max_pages = ( absint( $args['number'] ) === 0 ) ? 1 : ceil( $total_count / $args['number'] );
$response->header( 'X-WP-TotalPages', intval( $max_pages ) );
return $response;
}
/**
* Plugin compatibility args
*
* @param array $args
*
* @return array $args
*/
public function plugin_compatibility_args( $args ) {
// Polylang compatibility
$args[] = 'lang';
return $args;
}
/**
* Plugin compatibility after query
*
* @param WP_Query $user_query
*/
public function plugin_compatibility_after_query( $user_query ) {
// Relevanssi compatibility.
if ( function_exists( 'relevanssi_do_query' ) && ! empty( $user_query->query_vars['s'] ) ) {
relevanssi_do_query( $user_query );
}
}
}
/**
* Init only when needed
*/
function wp_user_query_to_rest_api_init() {
new WP_User_Query_To_REST_API();
}
add_action( 'rest_api_init', 'wp_user_query_to_rest_api_init' );