| | 262 | |
| | 263 | /** |
| | 264 | * Is this the main members query? |
| | 265 | * |
| | 266 | * @since BuddyPress (1.9) |
| | 267 | * |
| | 268 | * @return boolean |
| | 269 | */ |
| | 270 | public function is_main_query() { |
| | 271 | return (bool) ( self::get_main_query() === $this ); |
| | 272 | } |
| | 273 | |
| | 274 | /** Static Methods ********************************************************/ |
| | 275 | |
| | 276 | /** |
| | 277 | * @since BuddyPress (1.9) |
| | 278 | */ |
| | 279 | public static function get_main_query() { |
| | 280 | $bp = buddypress(); |
| | 281 | |
| | 282 | // Bail if no members component (how did this happen) |
| | 283 | if ( empty( $bp->members->id ) ) { |
| | 284 | return false; |
| | 285 | } |
| | 286 | |
| | 287 | // Bail if no main query already set |
| | 288 | if ( empty( $bp->main_queries[ $bp->members->id ] ) ) { |
| | 289 | return false; |
| | 290 | } |
| | 291 | |
| | 292 | // Get and return the main members query |
| | 293 | return $bp->main_queries[ $bp->members->id ]; |
| | 294 | } |
| | 295 | |
| | 296 | /** |
| | 297 | * @since BuddyPress (1.9) |
| | 298 | */ |
| | 299 | public static function set_main_query( $members_query ) { |
| | 300 | $bp = buddypress(); |
| | 301 | |
| | 302 | // Bail if no members component (how did this happen) |
| | 303 | if ( empty( $bp->members->id ) ) { |
| | 304 | return; |
| | 305 | } |
| | 306 | |
| | 307 | // Set the main query |
| | 308 | $bp->main_queries[ $bp->members->id ] = $members_query; |
| | 309 | } |
| | 310 | |
| | 311 | /** |
| | 312 | * |
| | 313 | * @since BuddyPress (1.9) |
| | 314 | * @global type $members_template |
| | 315 | */ |
| | 316 | public static function reset_query() { |
| | 317 | global $members_template; |
| | 318 | |
| | 319 | $main_query = self::get_main_query(); |
| | 320 | |
| | 321 | if ( false !== $main_query ) { |
| | 322 | $members_template = $main_query; |
| | 323 | } else { |
| | 324 | unset( $members_template ); |
| | 325 | } |
| | 326 | } |
| | 329 | /** |
| | 330 | * Destroy the previous members query and set up a new members query. |
| | 331 | * |
| | 332 | * This should be used after {@link bp_has_members()} and before another {@link |
| | 333 | * bp_has_members()}. This will remove obscure bugs that occur when the previous |
| | 334 | * BP_Core_Members_Template object is not destroyed properly before another is |
| | 335 | * set up. (This is akin to wp_reset_query() in WordPress core.) |
| | 336 | * |
| | 337 | * @since BuddyPress (1.9) |
| | 338 | * |
| | 339 | * @uses $members_template |
| | 340 | * @uses BP_Core_Members_Template::reset_query() |
| | 341 | */ |
| | 342 | function bp_reset_members_query() { |
| | 343 | global $members_template; |
| | 344 | |
| | 345 | $members_template->reset_query(); |
| | 346 | } |
| | 347 | |