PluginProbe
Friends / 2.9.0
Friends v2.9.0
4.3.2 4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 All 88 releases
friends / includes / class-user-query.php

class-user-query.php in Friends 2.9.0, at includes/class-user-query.php

317 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Friend User Query
4 *
5 * This wraps \WP_User_Query and so that it generates instances of User.
6 *
7 * @package Friends
8 */
9
10 namespace Friends;
11
12 /**
13 * This is the class for the User part of the Friends Plugin.
14 *
15 * @since 0.21
16 *
17 * @package Friends
18 * @author Alex Kirk
19 */
20 class User_Query extends \WP_User_Query {
21 /**
22 * Whether to cache the retrieved users
23 *
24 * @var boolean
25 */
26 public static $cache = true;
27
28 /**
29 * List of found User objects.
30 *
31 * @var array
32 */
33 private $results = array();
34
35 /**
36 * Total number of found users for the current query
37 *
38 * @var int
39 */
40 private $total_users = 0;
41
42 /**
43 * Execute the query and ensure that we populate User objects
44 */
45 public function query() {
46 parent::query();
47 foreach ( parent::get_results() as $k => $user ) {
48 $this->results[ $k ] = new User( $user );
49 $this->total_users += 1;
50 }
51 }
52
53 /**
54 * Return the count users.
55 *
56 * @return array Array of results.
57 */
58 public function get_results() {
59 return $this->results;
60 }
61
62 /**
63 * Returns the total number of users for the current query.
64 *
65 * @return int Number of total users.
66 */
67 public function get_total() {
68 return $this->total_users;
69 }
70 /**
71 * Gets all friends.
72 *
73 * @return User_Query The requested users.
74 */
75 public static function all_friends() {
76 static $all_friends = array();
77 if ( ! self::$cache || ! isset( $all_friends[ get_current_blog_id() ] ) ) {
78 $all_friends[ get_current_blog_id() ] = new self(
79 array(
80 'capability' => 'friend',
81 'order' => 'ASC',
82 'orderby' => 'display_name',
83 )
84 );
85 }
86 return $all_friends[ get_current_blog_id() ];
87 }
88
89 /**
90 * Gets all friends.
91 *
92 * @return User_Query The requested users.
93 */
94 public static function all_associated_users() {
95 static $all = array();
96 if ( ! self::$cache || ! isset( $all[ get_current_blog_id() ] ) ) {
97 $query = array(
98 'capability__in' => array_keys( Admin::get_associated_roles() ),
99 );
100 $sort = array(
101 'order' => 'ASC',
102 'orderby' => 'display_name',
103 );
104 $all[ get_current_blog_id() ] = new self( array_merge( $query, $sort ) );
105 $all[ get_current_blog_id() ]->add_virtual_subscriptions();
106 $all[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] );
107 }
108 return $all[ get_current_blog_id() ];
109 }
110
111 public function add_virtual_subscriptions( $args = array() ) {
112 if ( isset( $args['meta_key'] ) && substr( $args['meta_key'], -9 ) === '_starred' ) {
113 $args['meta_key'] = 'starred';
114 }
115 $searches = array();
116
117 if ( isset( $args['search'] ) ) {
118 // WP_Term_Query will add wildcarts before and after.
119 $args['search'] = trim( $args['search'], '*' );
120 $searches[] = $args;
121 $searches[] = array(
122 'meta_key' => 'display_name',
123 'meta_value' => $args['search'],
124 'meta_compare' => 'LIKE',
125 );
126 } else {
127 $searches[] = $args;
128 }
129
130 foreach ( $searches as $args ) {
131 $term_query = new \WP_Term_Query(
132 array_merge(
133 array(
134 'taxonomy' => Subscription::TAXONOMY,
135 'hide_empty' => false,
136 ),
137 $args
138 )
139 );
140
141 foreach ( $term_query->get_terms() as $term ) {
142 if ( ! isset( $this->results[ $term->term_id ] ) ) {
143 $this->results[ $term->term_id ] = new Subscription( $term );
144 $this->total_users += 1;
145 }
146 }
147 }
148 }
149 public function sort( $field, $direction ) {
150 usort(
151 $this->results,
152 function ( $a, $b ) use ( $field, $direction ) {
153 $a = $a->$field;
154 $b = $b->$field;
155 if ( $a === $b ) {
156 return 0;
157 }
158 if ( 'ASC' === $direction ) {
159 return strcasecmp( $a, $b );
160 }
161 return strcasecmp( $b, $a );
162 }
163 );
164 }
165
166 public function limit( $limit ) {
167 $this->results = array_slice( $this->results, 0, $limit );
168 }
169
170 /**
171 * Gets Starred friends.
172 *
173 * @return User_Query The requested users.
174 */
175 public static function starred_friends_subscriptions() {
176 static $starred_friends_subscriptions = array();
177 $cache_key = get_current_blog_id();
178
179 if ( ! self::$cache || ! isset( $starred_friends_subscriptions[ $cache_key ] ) ) {
180 global $wpdb;
181 $query = array(
182 'capability__in' => Friends::get_friends_plugin_roles(),
183 );
184 $meta = array(
185 'meta_key' => $wpdb->get_blog_prefix() . 'friends_starred',
186 // Using a meta_key EXISTS query is not slow, see https://github.com/WordPress/WordPress-Coding-Standards/issues/1871.
187 'meta_compare' => 'EXISTS',
188 );
189 $sort = array(
190 'order' => 'ASC',
191 'orderby' => 'display_name',
192 );
193 $starred_friends_subscriptions[ $cache_key ] = new self( array_merge( $query, $meta, $sort ) );
194 $starred_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions( $meta );
195 $starred_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] );
196 }
197 return $starred_friends_subscriptions[ $cache_key ];
198 }
199
200 /**
201 * Gets the recent friends.
202 *
203 * @param int $limit The limit.
204 *
205 * @return User_Query The requested users.
206 */
207 public static function recent_friends_subscriptions( $limit = 5 ) {
208 static $recent_friends_subscriptions = array();
209 $cache_key = get_current_blog_id() . '_' . $limit;
210 if ( ! self::$cache || ! isset( $recent_friends_subscriptions[ $cache_key ] ) ) {
211 $query = array(
212 'capability__in' => array( 'friend', 'acquaintance', 'pending_friend_request', 'subscription' ),
213 'number' => $limit,
214 );
215 $sort = array(
216 'orderby' => 'user_registered',
217 'order' => 'DESC',
218 );
219 $recent_friends_subscriptions[ $cache_key ] = new self(
220 array_merge(
221 $query,
222 $sort
223 )
224 );
225 $recent_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions();
226 $recent_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] );
227 $recent_friends_subscriptions[ $cache_key ]->limit( $limit );
228 }
229 return $recent_friends_subscriptions[ $cache_key ];
230 }
231
232 /**
233 * Search friends.
234 *
235 * @param string $query The query.
236 *
237 * @return self The search result.
238 */
239 public static function search( $query ) {
240 $query = array(
241 'search' => $query,
242 );
243 $sort = array(
244 'order' => 'ASC',
245 'orderby' => 'display_name',
246 );
247 $search = new self(
248 array_merge(
249 array(
250 'capability__in' => Friends::get_friends_plugin_roles(),
251 'search_columns' => array( 'display_name' ),
252 ),
253 $query,
254 $sort
255 )
256 );
257
258 $search->add_virtual_subscriptions( $query );
259 $search->sort( $sort['orderby'], $sort['order'] );
260 return $search;
261 }
262
263 /**
264 * Gets all friend requests.
265 */
266 public static function all_friend_requests() {
267 static $all_friend_requests = array();
268 if ( ! self::$cache || ! isset( $all_friend_requests[ get_current_blog_id() ] ) ) {
269 $all_friend_requests[ get_current_blog_id() ] = new self(
270 array(
271 'role' => 'friend_request',
272 'order' => 'ASC',
273 'orderby' => 'display_name',
274 )
275 );
276 }
277 return $all_friend_requests[ get_current_blog_id() ];
278 }
279
280 /**
281 * Gets all subscriptions.
282 */
283 public static function all_subscriptions() {
284 static $all_subscriptions = array();
285 if ( ! self::$cache || ! isset( $all_subscriptions[ get_current_blog_id() ] ) ) {
286 $query = array(
287 'capability__in' => array( 'pending_friend_request', 'subscription' ),
288 );
289 $sort = array(
290 'order' => 'ASC',
291 'orderby' => 'display_name',
292 );
293 $all_subscriptions[ get_current_blog_id() ] = new self( array_merge( $query, $sort ) );
294 $all_subscriptions[ get_current_blog_id() ]->add_virtual_subscriptions();
295 $all_subscriptions[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] );
296 }
297 return $all_subscriptions[ get_current_blog_id() ];
298 }
299
300 /**
301 * Gets all admin users.
302 */
303 public static function all_admin_users() {
304 static $all_admin_users;
305 if ( ! self::$cache || ! isset( $all_admin_users ) ) {
306 $all_admin_users = new self(
307 array(
308 'capability' => 'edit_private_posts',
309 'order' => 'ASC',
310 'orderby' => 'display_name',
311 )
312 );
313 }
314 return $all_admin_users;
315 }
316 }
317