| 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 |
/** |
| 72 |
* Gets all friends. |
| 73 |
* |
| 74 |
* @return User_Query The requested users. |
| 75 |
*/ |
| 76 |
public static function all_associated_users() { |
| 77 |
static $all = array(); |
| 78 |
if ( ! self::$cache || ! isset( $all[ get_current_blog_id() ] ) ) { |
| 79 |
$query = array( |
| 80 |
'capability__in' => array_keys( Admin::get_associated_roles() ), |
| 81 |
); |
| 82 |
$sort = array( |
| 83 |
'order' => 'ASC', |
| 84 |
'orderby' => 'display_name', |
| 85 |
); |
| 86 |
$all[ get_current_blog_id() ] = new self( array_merge( $query, $sort ) ); |
| 87 |
$all[ get_current_blog_id() ]->add_virtual_subscriptions(); |
| 88 |
$all[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] ); |
| 89 |
} |
| 90 |
return $all[ get_current_blog_id() ]; |
| 91 |
} |
| 92 |
|
| 93 |
public function add_virtual_subscriptions( $args = array() ) { |
| 94 |
if ( isset( $args['meta_key'] ) && substr( $args['meta_key'], -9 ) === '_starred' ) { |
| 95 |
$args['meta_key'] = 'starred'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 96 |
} |
| 97 |
$searches = array(); |
| 98 |
|
| 99 |
if ( isset( $args['search'] ) ) { |
| 100 |
// WP_Term_Query will add wildcarts before and after. |
| 101 |
$args['search'] = trim( $args['search'], '*' ); |
| 102 |
$searches[] = $args; |
| 103 |
$searches[] = array( |
| 104 |
'meta_key' => 'display_name', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 105 |
'meta_value' => $args['search'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 106 |
'meta_compare' => 'LIKE', |
| 107 |
); |
| 108 |
} else { |
| 109 |
$searches[] = $args; |
| 110 |
} |
| 111 |
|
| 112 |
foreach ( $searches as $args ) { |
| 113 |
$term_query = new \WP_Term_Query( |
| 114 |
array_merge( |
| 115 |
array( |
| 116 |
'taxonomy' => Subscription::TAXONOMY, |
| 117 |
'hide_empty' => false, |
| 118 |
), |
| 119 |
$args |
| 120 |
) |
| 121 |
); |
| 122 |
|
| 123 |
foreach ( $term_query->get_terms() as $term ) { |
| 124 |
if ( ! isset( $this->results[ $term->term_id ] ) && ! Subscription::is_folder( $term->term_id ) ) { |
| 125 |
$this->results[ $term->term_id ] = new Subscription( $term ); |
| 126 |
$this->total_users += 1; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
public function sort( $field, $direction ) { |
| 132 |
usort( |
| 133 |
$this->results, |
| 134 |
function ( $a, $b ) use ( $field, $direction ) { |
| 135 |
$a = $a->$field; |
| 136 |
$b = $b->$field; |
| 137 |
if ( $a === $b ) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
if ( 'ASC' === $direction ) { |
| 141 |
return strcasecmp( $a, $b ); |
| 142 |
} |
| 143 |
return strcasecmp( $b, $a ); |
| 144 |
} |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
public function limit( $limit ) { |
| 149 |
$this->results = array_slice( $this->results, 0, $limit ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Gets Starred friends. |
| 154 |
* |
| 155 |
* @return User_Query The requested users. |
| 156 |
*/ |
| 157 |
public static function starred_friends_subscriptions() { |
| 158 |
static $starred_friends_subscriptions = array(); |
| 159 |
$cache_key = get_current_blog_id(); |
| 160 |
|
| 161 |
if ( ! self::$cache || ! isset( $starred_friends_subscriptions[ $cache_key ] ) ) { |
| 162 |
$meta = array( |
| 163 |
'meta_key' => 'starred', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 164 |
'meta_compare' => 'EXISTS', |
| 165 |
); |
| 166 |
$sort = array( |
| 167 |
'order' => 'ASC', |
| 168 |
'orderby' => 'display_name', |
| 169 |
); |
| 170 |
$starred_friends_subscriptions[ $cache_key ] = new self( array( 'include' => array( 0 ) ) ); |
| 171 |
$starred_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions( $meta ); |
| 172 |
$starred_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] ); |
| 173 |
} |
| 174 |
return $starred_friends_subscriptions[ $cache_key ]; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Gets subscriptions in a specific folder. |
| 179 |
* |
| 180 |
* Filters the cached all_subscriptions list by parent term ID. |
| 181 |
* |
| 182 |
* @param int $folder_term_id The folder term ID. |
| 183 |
* @return User_Query The matching subscriptions. |
| 184 |
*/ |
| 185 |
public static function subscriptions_in_folder( $folder_term_id ) { |
| 186 |
$all = self::all_subscriptions(); |
| 187 |
|
| 188 |
$results = array(); |
| 189 |
$total_users = 0; |
| 190 |
foreach ( $all->get_results() as $subscription ) { |
| 191 |
if ( $subscription instanceof Subscription && $subscription->get_folder() ) { |
| 192 |
if ( $subscription->get_folder()->term_id === $folder_term_id ) { |
| 193 |
$results[ $subscription->get_term_id() ] = $subscription; |
| 194 |
++$total_users; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return new User_Query_Result( $results, $total_users ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Gets subscriptions not in any folder (at root level). |
| 204 |
* |
| 205 |
* @return User_Query_Result The matching subscriptions. |
| 206 |
*/ |
| 207 |
public static function unfoldered_subscriptions() { |
| 208 |
$all = self::all_subscriptions(); |
| 209 |
|
| 210 |
$results = array(); |
| 211 |
$total_users = 0; |
| 212 |
foreach ( $all->get_results() as $subscription ) { |
| 213 |
if ( $subscription instanceof Subscription && ! $subscription->get_folder() ) { |
| 214 |
$results[ $subscription->get_term_id() ] = $subscription; |
| 215 |
++$total_users; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return new User_Query_Result( $results, $total_users ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Gets the recent friends. |
| 224 |
* |
| 225 |
* @param int $limit The limit. |
| 226 |
* |
| 227 |
* @return User_Query The requested users. |
| 228 |
*/ |
| 229 |
public static function recent_friends_subscriptions( $limit = 5 ) { |
| 230 |
static $recent_friends_subscriptions = array(); |
| 231 |
$cache_key = get_current_blog_id() . '_' . $limit; |
| 232 |
if ( ! self::$cache || ! isset( $recent_friends_subscriptions[ $cache_key ] ) ) { |
| 233 |
$query = array( |
| 234 |
'capability__in' => array( 'subscription' ), |
| 235 |
'number' => $limit, |
| 236 |
); |
| 237 |
$sort = array( |
| 238 |
'orderby' => 'user_registered', |
| 239 |
'order' => 'DESC', |
| 240 |
); |
| 241 |
$recent_friends_subscriptions[ $cache_key ] = new self( |
| 242 |
array_merge( |
| 243 |
$query, |
| 244 |
$sort |
| 245 |
) |
| 246 |
); |
| 247 |
$recent_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions(); |
| 248 |
$recent_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] ); |
| 249 |
$recent_friends_subscriptions[ $cache_key ]->limit( $limit ); |
| 250 |
} |
| 251 |
return $recent_friends_subscriptions[ $cache_key ]; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Search friends. |
| 256 |
* |
| 257 |
* @param string $query The query. |
| 258 |
* |
| 259 |
* @return self The search result. |
| 260 |
*/ |
| 261 |
public static function search( $query ) { |
| 262 |
$query = array( |
| 263 |
'search' => $query, |
| 264 |
); |
| 265 |
$sort = array( |
| 266 |
'order' => 'ASC', |
| 267 |
'orderby' => 'display_name', |
| 268 |
); |
| 269 |
$search = new self( array( 'include' => array( 0 ) ) ); |
| 270 |
$search->add_virtual_subscriptions( $query ); |
| 271 |
$search->sort( $sort['orderby'], $sort['order'] ); |
| 272 |
return $search; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Gets all subscriptions. |
| 277 |
*/ |
| 278 |
public static function all_subscriptions() { |
| 279 |
static $all_subscriptions = array(); |
| 280 |
if ( ! self::$cache || ! isset( $all_subscriptions[ get_current_blog_id() ] ) ) { |
| 281 |
$sort = array( |
| 282 |
'order' => 'ASC', |
| 283 |
'orderby' => 'display_name', |
| 284 |
); |
| 285 |
$all_subscriptions[ get_current_blog_id() ] = new self( array( 'include' => array( 0 ) ) ); |
| 286 |
$all_subscriptions[ get_current_blog_id() ]->add_virtual_subscriptions(); |
| 287 |
$all_subscriptions[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] ); |
| 288 |
} |
| 289 |
return $all_subscriptions[ get_current_blog_id() ]; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Gets all admin users. |
| 294 |
*/ |
| 295 |
public static function all_admin_users() { |
| 296 |
static $all_admin_users; |
| 297 |
if ( ! self::$cache || ! isset( $all_admin_users ) ) { |
| 298 |
$all_admin_users = new self( |
| 299 |
array( |
| 300 |
'capability' => 'edit_private_posts', |
| 301 |
'order' => 'ASC', |
| 302 |
'orderby' => 'display_name', |
| 303 |
) |
| 304 |
); |
| 305 |
} |
| 306 |
return $all_admin_users; |
| 307 |
} |
| 308 |
} |
| 309 |
|