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