| @@ -66,26 +66,8 @@ | ||
| 66 | 66 | */ |
| 67 | 67 | public function get_total() { |
| 68 | 68 | return $this->total_users; |
| 69 | 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 | 70 | |
| 89 | 71 | /** |
| 90 | 72 | * Gets all friends. |
| 91 | 73 | * |
| @@ -94,9 +76,9 @@ | ||
| 94 | 76 | public static function all_associated_users() { |
| 95 | 77 | static $all = array(); |
| 96 | 78 | if ( ! self::$cache || ! isset( $all[ get_current_blog_id() ] ) ) { |
| 97 | 79 | $query = array( |
| 98 | - 'role__in' => array_keys( Admin::get_associated_roles() ), | |
| 80 | + 'capability__in' => array_keys( Admin::get_associated_roles() ), | |
| 99 | 81 | ); |
| 100 | 82 | $sort = array( |
| 101 | 83 | 'order' => 'ASC', |
| 102 | 84 | 'orderby' => 'display_name', |
| @@ -109,27 +91,42 @@ | ||
| 109 | 91 | } |
| 110 | 92 | |
| 111 | 93 | public function add_virtual_subscriptions( $args = array() ) { |
| 112 | 94 | if ( isset( $args['meta_key'] ) && substr( $args['meta_key'], -9 ) === '_starred' ) { |
| 113 | - $args['meta_key'] = 'starred'; | |
| 95 | + $args['meta_key'] = 'starred'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key | |
| 114 | 96 | } |
| 97 | + $searches = array(); | |
| 98 | + | |
| 115 | 99 | if ( isset( $args['search'] ) ) { |
| 116 | - $args['search'] = str_replace( '*', '', $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; | |
| 117 | 110 | } |
| 118 | 111 | |
| 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 | - ); | |
| 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 | + ); | |
| 128 | 122 | |
| 129 | - foreach ( $term_query->get_terms() as $term ) { | |
| 130 | - $this->results[] = new Subscription( $term ); | |
| 131 | - $this->total_users += 1; | |
| 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 | + } | |
| 132 | 129 | } |
| 133 | 130 | } |
| 134 | 131 | public function sort( $field, $direction ) { |
| 135 | 132 | usort( |
| @@ -161,15 +158,10 @@ | ||
| 161 | 158 | static $starred_friends_subscriptions = array(); |
| 162 | 159 | $cache_key = get_current_blog_id(); |
| 163 | 160 | |
| 164 | 161 | if ( ! self::$cache || ! isset( $starred_friends_subscriptions[ $cache_key ] ) ) { |
| 165 | - global $wpdb; | |
| 166 | - $query = array( | |
| 167 | - 'role__in' => Friends::get_friends_plugin_roles(), | |
| 168 | - ); | |
| 169 | 162 | $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. | |
| 163 | + 'meta_key' => 'starred', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key | |
| 172 | 164 | 'meta_compare' => 'EXISTS', |
| 173 | 165 | ); |
| 174 | 166 | $sort = array( |
| 175 | 167 | 'order' => 'ASC', |
| @@ -174,9 +166,9 @@ | ||
| 174 | 166 | $sort = array( |
| 175 | 167 | 'order' => 'ASC', |
| 176 | 168 | 'orderby' => 'display_name', |
| 177 | 169 | ); |
| 178 | - $starred_friends_subscriptions[ $cache_key ] = new self( array_merge( $query, $meta, $sort ) ); | |
| 170 | + $starred_friends_subscriptions[ $cache_key ] = new self( array( 'include' => array( 0 ) ) ); | |
| 179 | 171 | $starred_friends_subscriptions[ $cache_key ]->add_virtual_subscriptions( $meta ); |
| 180 | 172 | $starred_friends_subscriptions[ $cache_key ]->sort( $sort['orderby'], $sort['order'] ); |
| 181 | 173 | } |
| 182 | 174 | return $starred_friends_subscriptions[ $cache_key ]; |
| @@ -182,8 +174,53 @@ | ||
| 182 | 174 | return $starred_friends_subscriptions[ $cache_key ]; |
| 183 | 175 | } |
| 184 | 176 | |
| 185 | 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 | + /** | |
| 186 | 223 | * Gets the recent friends. |
| 187 | 224 | * |
| 188 | 225 | * @param int $limit The limit. |
| 189 | 226 | * |
| @@ -193,10 +230,10 @@ | ||
| 193 | 230 | static $recent_friends_subscriptions = array(); |
| 194 | 231 | $cache_key = get_current_blog_id() . '_' . $limit; |
| 195 | 232 | if ( ! self::$cache || ! isset( $recent_friends_subscriptions[ $cache_key ] ) ) { |
| 196 | 233 | $query = array( |
| 197 | - 'role__in' => array( 'friend', 'acquaintance', 'pending_friend_request', 'subscription' ), | |
| 198 | - 'number' => $limit, | |
| 234 | + 'capability__in' => array( 'subscription' ), | |
| 235 | + 'number' => $limit, | |
| 199 | 236 | ); |
| 200 | 237 | $sort = array( |
| 201 | 238 | 'orderby' => 'user_registered', |
| 202 | 239 | 'order' => 'DESC', |
| @@ -228,19 +265,9 @@ | ||
| 228 | 265 | $sort = array( |
| 229 | 266 | 'order' => 'ASC', |
| 230 | 267 | 'orderby' => 'display_name', |
| 231 | 268 | ); |
| 232 | - $search = new self( | |
| 233 | - array_merge( | |
| 234 | - array( | |
| 235 | - 'role__in' => Friends::get_friends_plugin_roles(), | |
| 236 | - 'search_columns' => array( 'display_name' ), | |
| 237 | - ), | |
| 238 | - $query, | |
| 239 | - $sort | |
| 240 | - ) | |
| 241 | - ); | |
| 242 | - | |
| 269 | + $search = new self( array( 'include' => array( 0 ) ) ); | |
| 243 | 270 | $search->add_virtual_subscriptions( $query ); |
| 244 | 271 | $search->sort( $sort['orderby'], $sort['order'] ); |
| 245 | 272 | return $search; |
| 246 | 273 | } |
| @@ -245,38 +272,18 @@ | ||
| 245 | 272 | return $search; |
| 246 | 273 | } |
| 247 | 274 | |
| 248 | 275 | /** |
| 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 | 276 | * Gets all subscriptions. |
| 267 | 277 | */ |
| 268 | 278 | public static function all_subscriptions() { |
| 269 | 279 | static $all_subscriptions = array(); |
| 270 | 280 | if ( ! self::$cache || ! isset( $all_subscriptions[ get_current_blog_id() ] ) ) { |
| 271 | - $query = array( | |
| 272 | - 'role__in' => array( 'pending_friend_request', 'subscription' ), | |
| 273 | - ); | |
| 274 | 281 | $sort = array( |
| 275 | 282 | 'order' => 'ASC', |
| 276 | 283 | 'orderby' => 'display_name', |
| 277 | 284 | ); |
| 278 | - $all_subscriptions[ get_current_blog_id() ] = new self( array_merge( $query, $sort ) ); | |
| 285 | + $all_subscriptions[ get_current_blog_id() ] = new self( array( 'include' => array( 0 ) ) ); | |
| 279 | 286 | $all_subscriptions[ get_current_blog_id() ]->add_virtual_subscriptions(); |
| 280 | 287 | $all_subscriptions[ get_current_blog_id() ]->sort( $sort['orderby'], $sort['order'] ); |
| 281 | 288 | } |
| 282 | 289 | return $all_subscriptions[ get_current_blog_id() ]; |