$user ) { $this->results[ $k ] = new User( $user ); $this->total_users += 1; } } /** * Return the count users. * * @return array Array of results. */ public function get_results() { return $this->results; } /** * Returns the total number of users for the current query. * * @return int Number of total users. */ public function get_total() { return $this->total_users; } /** * Gets all friends. * * @return User_Query The requested users. */ public static function all_associated_users() { static $all = array(); if ( ! self::$cache || ! isset( $all[ get_current_blog_id() ] ) ) { $query = array( 'capability__in' => array_keys( Admin::get_associated_roles() ), ); $sort = array( 'order' => 'ASC', 'orderby' => 'display_name', ); $all[ get_current_blog_id() ] = new self( array_merge( $query, $sort ) ); $all[ get_current_blog_id() ]->add_virtual_subscriptions(); $all[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] ); } return $all[ get_current_blog_id() ]; } public function add_virtual_subscriptions( $args = array() ) { if ( isset( $args['meta_key'] ) && substr( $args['meta_key'], -9 ) === '_starred' ) { $args['meta_key'] = 'starred'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key } $searches = array(); if ( isset( $args['search'] ) ) { // WP_Term_Query will add wildcarts before and after. $args['search'] = trim( $args['search'], '*' ); $searches[] = $args; $searches[] = array( 'meta_key' => 'display_name', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => $args['search'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'meta_compare' => 'LIKE', ); } else { $searches[] = $args; } foreach ( $searches as $args ) { $term_query = new \WP_Term_Query( array_merge( array( 'taxonomy' => Subscription::TAXONOMY, 'hide_empty' => false, ), $args ) ); foreach ( $term_query->get_terms() as $term ) { if ( ! isset( $this->results[ $term->term_id ] ) && ! Subscription::is_folder( $term->term_id ) ) { $this->results[ $term->term_id ] = new Subscription( $term ); $this->total_users += 1; } } } } public function sort( $field, $direction ) { usort( $this->results, function ( $a, $b ) use ( $field, $direction ) { $a = $a->$field; $b = $b->$field; if ( $a === $b ) { return 0; } if ( 'ASC' === $direction ) { return strcasecmp( $a, $b ); } return strcasecmp( $b, $a ); } ); } public function limit( $limit ) { $this->results = array_slice( $this->results, 0, $limit ); } /** * Gets Starred friends. * * @return User_Query The requested users. */ public static function starred_friends_subscriptions() { static $starred_friends_subscriptions = array(); $cache_key = get_current_blog_id(); if ( ! self::$cache || ! isset( $starred_friends_subscriptions[ $cache_key ] ) ) { $meta = array( 'meta_key' => 'starred', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_compare' => 'EXISTS', ); $sort = array( 'order' => 'ASC', 'orderby' => 'display_name', ); $starred_friends_subscriptions[ $cache_key ] = new self( array( 'include' => array( 0 ) ) ); $starred_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions( $meta ); $starred_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] ); } return $starred_friends_subscriptions[ $cache_key ]; } /** * Gets subscriptions in a specific folder. * * Filters the cached all_subscriptions list by parent term ID. * * @param int $folder_term_id The folder term ID. * @return User_Query The matching subscriptions. */ public static function subscriptions_in_folder( $folder_term_id ) { $all = self::all_subscriptions(); $results = array(); $total_users = 0; foreach ( $all->get_results() as $subscription ) { if ( $subscription instanceof Subscription && $subscription->get_folder() ) { if ( $subscription->get_folder()->term_id === $folder_term_id ) { $results[ $subscription->get_term_id() ] = $subscription; ++$total_users; } } } return new User_Query_Result( $results, $total_users ); } /** * Gets subscriptions not in any folder (at root level). * * @return User_Query_Result The matching subscriptions. */ public static function unfoldered_subscriptions() { $all = self::all_subscriptions(); $results = array(); $total_users = 0; foreach ( $all->get_results() as $subscription ) { if ( $subscription instanceof Subscription && ! $subscription->get_folder() ) { $results[ $subscription->get_term_id() ] = $subscription; ++$total_users; } } return new User_Query_Result( $results, $total_users ); } /** * Gets the recent friends. * * @param int $limit The limit. * * @return User_Query The requested users. */ public static function recent_friends_subscriptions( $limit = 5 ) { static $recent_friends_subscriptions = array(); $cache_key = get_current_blog_id() . '_' . $limit; if ( ! self::$cache || ! isset( $recent_friends_subscriptions[ $cache_key ] ) ) { $query = array( 'capability__in' => array( 'subscription' ), 'number' => $limit, ); $sort = array( 'orderby' => 'user_registered', 'order' => 'DESC', ); $recent_friends_subscriptions[ $cache_key ] = new self( array_merge( $query, $sort ) ); $recent_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions(); $recent_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] ); $recent_friends_subscriptions[ $cache_key ]->limit( $limit ); } return $recent_friends_subscriptions[ $cache_key ]; } /** * Search friends. * * @param string $query The query. * * @return self The search result. */ public static function search( $query ) { $query = array( 'search' => $query, ); $sort = array( 'order' => 'ASC', 'orderby' => 'display_name', ); $search = new self( array( 'include' => array( 0 ) ) ); $search->add_virtual_subscriptions( $query ); $search->sort( $sort['orderby'], $sort['order'] ); return $search; } /** * Gets all subscriptions. */ public static function all_subscriptions() { static $all_subscriptions = array(); if ( ! self::$cache || ! isset( $all_subscriptions[ get_current_blog_id() ] ) ) { $sort = array( 'order' => 'ASC', 'orderby' => 'display_name', ); $all_subscriptions[ get_current_blog_id() ] = new self( array( 'include' => array( 0 ) ) ); $all_subscriptions[ get_current_blog_id() ]->add_virtual_subscriptions(); $all_subscriptions[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] ); } return $all_subscriptions[ get_current_blog_id() ]; } /** * Gets all admin users. */ public static function all_admin_users() { static $all_admin_users; if ( ! self::$cache || ! isset( $all_admin_users ) ) { $all_admin_users = new self( array( 'capability' => 'edit_private_posts', 'order' => 'ASC', 'orderby' => 'display_name', ) ); } return $all_admin_users; } }