PluginProbe
Friends / 2.8.3
Friends v2.8.3
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.8.3, at includes/class-user-query.php

302 lines 7.5 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 if ( isset( $args['search'] ) ) {
116 $args['search'] = str_replace( '*', '', $args['search'] );
117 }
118
119 $term_query = new \WP_Term_Query(
120 array_merge(
121 array(
122 'taxonomy' => Subscription::TAXONOMY,
123 'hide_empty' => false,
124 ),
125 $args
126 )
127 );
128
129 foreach ( $term_query->get_terms() as $term ) {
130 $this->results[] = new Subscription( $term );
131 $this->total_users += 1;
132 }
133 }
134 public function sort( $field, $direction ) {
135 usort(
136 $this->results,
137 function ( $a, $b ) use ( $field, $direction ) {
138 $a = $a->$field;
139 $b = $b->$field;
140 if ( $a === $b ) {
141 return 0;
142 }
143 if ( 'ASC' === $direction ) {
144 return strcasecmp( $a, $b );
145 }
146 return strcasecmp( $b, $a );
147 }
148 );
149 }
150
151 public function limit( $limit ) {
152 $this->results = array_slice( $this->results, 0, $limit );
153 }
154
155 /**
156 * Gets Starred friends.
157 *
158 * @return User_Query The requested users.
159 */
160 public static function starred_friends_subscriptions() {
161 static $starred_friends_subscriptions = array();
162 $cache_key = get_current_blog_id();
163
164 if ( ! self::$cache || ! isset( $starred_friends_subscriptions[ $cache_key ] ) ) {
165 global $wpdb;
166 $query = array(
167 'capability__in' => Friends::get_friends_plugin_roles(),
168 );
169 $meta = array(
170 'meta_key' => $wpdb->get_blog_prefix() . 'friends_starred',
171 // Using a meta_key EXISTS query is not slow, see https://github.com/WordPress/WordPress-Coding-Standards/issues/1871.
172 'meta_compare' => 'EXISTS',
173 );
174 $sort = array(
175 'order' => 'ASC',
176 'orderby' => 'display_name',
177 );
178 $starred_friends_subscriptions[ $cache_key ] = new self( array_merge( $query, $meta, $sort ) );
179 $starred_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions( $meta );
180 $starred_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] );
181 }
182 return $starred_friends_subscriptions[ $cache_key ];
183 }
184
185 /**
186 * Gets the recent friends.
187 *
188 * @param int $limit The limit.
189 *
190 * @return User_Query The requested users.
191 */
192 public static function recent_friends_subscriptions( $limit = 5 ) {
193 static $recent_friends_subscriptions = array();
194 $cache_key = get_current_blog_id() . '_' . $limit;
195 if ( ! self::$cache || ! isset( $recent_friends_subscriptions[ $cache_key ] ) ) {
196 $query = array(
197 'capability__in' => array( 'friend', 'acquaintance', 'pending_friend_request', 'subscription' ),
198 'number' => $limit,
199 );
200 $sort = array(
201 'orderby' => 'user_registered',
202 'order' => 'DESC',
203 );
204 $recent_friends_subscriptions[ $cache_key ] = new self(
205 array_merge(
206 $query,
207 $sort
208 )
209 );
210 $recent_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions();
211 $recent_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] );
212 $recent_friends_subscriptions[ $cache_key ]->limit( $limit );
213 }
214 return $recent_friends_subscriptions[ $cache_key ];
215 }
216
217 /**
218 * Search friends.
219 *
220 * @param string $query The query.
221 *
222 * @return self The search result.
223 */
224 public static function search( $query ) {
225 $query = array(
226 'search' => $query,
227 );
228 $sort = array(
229 'order' => 'ASC',
230 'orderby' => 'display_name',
231 );
232 $search = new self(
233 array_merge(
234 array(
235 'capability__in' => Friends::get_friends_plugin_roles(),
236 'search_columns' => array( 'display_name' ),
237 ),
238 $query,
239 $sort
240 )
241 );
242
243 $search->add_virtual_subscriptions( $query );
244 $search->sort( $sort['orderby'], $sort['order'] );
245 return $search;
246 }
247
248 /**
249 * Gets all friend requests.
250 */
251 public static function all_friend_requests() {
252 static $all_friend_requests = array();
253 if ( ! self::$cache || ! isset( $all_friend_requests[ get_current_blog_id() ] ) ) {
254 $all_friend_requests[ get_current_blog_id() ] = new self(
255 array(
256 'role' => 'friend_request',
257 'order' => 'ASC',
258 'orderby' => 'display_name',
259 )
260 );
261 }
262 return $all_friend_requests[ get_current_blog_id() ];
263 }
264
265 /**
266 * Gets all subscriptions.
267 */
268 public static function all_subscriptions() {
269 static $all_subscriptions = array();
270 if ( ! self::$cache || ! isset( $all_subscriptions[ get_current_blog_id() ] ) ) {
271 $query = array(
272 'capability__in' => array( 'pending_friend_request', 'subscription' ),
273 );
274 $sort = array(
275 'order' => 'ASC',
276 'orderby' => 'display_name',
277 );
278 $all_subscriptions[ get_current_blog_id() ] = new self( array_merge( $query, $sort ) );
279 $all_subscriptions[ get_current_blog_id() ]->add_virtual_subscriptions();
280 $all_subscriptions[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] );
281 }
282 return $all_subscriptions[ get_current_blog_id() ];
283 }
284
285 /**
286 * Gets all admin users.
287 */
288 public static function all_admin_users() {
289 static $all_admin_users;
290 if ( ! self::$cache || ! isset( $all_admin_users ) ) {
291 $all_admin_users = new self(
292 array(
293 'capability' => 'edit_private_posts',
294 'order' => 'ASC',
295 'orderby' => 'display_name',
296 )
297 );
298 }
299 return $all_admin_users;
300 }
301 }
302