Skip to:
Content

BuddyPress.org

Ticket #5170: 5170.2.patch

File 5170.2.patch, 4.3 KB (added by johnjamesjacoby, 13 years ago)

set_main_query() is more flexible when not a static method

  • bp-core/bp-core-widgets.php

     
    100100
    101101                <?php endif; ?>
    102102
    103                 <?php echo $after_widget; ?>
    104         <?php
     103                <?php echo $after_widget;
     104
     105                // Reset the members query
     106                bp_reset_members_query();
    105107        }
    106108
    107109        function update( $new_instance, $old_instance ) {
     
    189191
    190192                <?php endif; ?>
    191193
    192                 <?php echo $after_widget; ?>
    193         <?php
     194                <?php echo $after_widget;
     195
     196                // Reset the members query
     197                bp_reset_members_query();
    194198        }
    195199
    196200        function update( $new_instance, $old_instance ) {
     
    258262
    259263                <?php endif; ?>
    260264
    261                 <?php echo $after_widget; ?>
    262         <?php
     265                <?php echo $after_widget;
     266
     267                // Reset the members query
     268                bp_reset_members_query();
    263269        }
    264270
    265271        function update( $new_instance, $old_instance ) {
  • bp-loader.php

     
    9696         */
    9797        public $active_components = array();
    9898
     99        /**
     100         * @var array Main query loops (akin to $wp_the_query)
     101         */
     102        public $main_queries = array();
     103
    99104        /** Option Overload *******************************************************/
    100105
    101106        /**
  • bp-members/bp-members-template.php

     
    253253                if ( 0 == $this->current_member )
    254254                        do_action( 'member_loop_start' );
    255255        }
     256
     257        /**
     258         * Is this the main members query?
     259         *
     260         * @since BuddyPress (1.9)
     261         *
     262         * @return boolean
     263         */
     264        public function is_main_query() {
     265                return (bool) ( self::get_main_query() === $this );
     266        }
     267
     268        /**
     269         * @since BuddyPress (1.9)
     270         */
     271        public function set_main_query( $members_query = '' ) {
     272                $bp = buddypress();
     273
     274                // Bail if no members component (how did this happen)
     275                if ( empty( $bp->members->id ) ) {
     276                        return;
     277                }
     278
     279                // Use the current object if no query passed
     280                if ( empty( $members_query ) ) {
     281                        $members_query = $this;
     282                }
     283
     284                // Set the main query
     285                $bp->main_queries[ $bp->members->id ] = $members_query;
     286        }
     287
     288        /** Static Methods ********************************************************/
     289
     290        /**
     291         * @since BuddyPress (1.9)
     292         */
     293        public static function get_main_query() {
     294                $bp = buddypress();
     295
     296                // Bail if no members component (how did this happen)
     297                if ( empty( $bp->members->id ) ) {
     298                        return false;
     299                }
     300
     301                // Bail if no main query already set
     302                if ( empty( $bp->main_queries[ $bp->members->id ] ) ) {
     303                        return false;
     304                }
     305
     306                // Get and return the main members query
     307                return $bp->main_queries[ $bp->members->id ];
     308        }
     309
     310        /**
     311         *
     312         * @since BuddyPress (1.9)
     313         * @global type $members_template
     314         */
     315        public static function reset_query() {
     316                global $members_template;
     317
     318                $main_query = self::get_main_query();
     319
     320                if ( false !== $main_query ) {
     321                        $members_template = $main_query;
     322                } else {
     323                        unset( $members_template );
     324                }
     325        }
    256326}
    257327
     328/**
     329 * Destroy the previous members query and set up a new members query.
     330 *
     331 * This should be used after {@link bp_has_members()} and before another {@link
     332 * bp_has_members()}. This will remove obscure bugs that occur when the previous
     333 * BP_Core_Members_Template object is not destroyed properly before another is
     334 * set up. (This is akin to wp_reset_query() in WordPress core.)
     335 *
     336 * @since BuddyPress (1.9)
     337 *
     338 * @uses $members_template
     339 * @uses BP_Core_Members_Template::reset_query()
     340 */
     341function bp_reset_members_query() {
     342        global $members_template;
     343
     344        $members_template->reset_query();
     345}
     346
    258347function bp_rewind_members() {
    259348        global $members_template;
    260349
     
    315404        if ( !empty( $max ) && ( $per_page > $max ) )
    316405                $per_page = $max;
    317406
    318         $members_template = new BP_Core_Members_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $include, (bool)$populate_extras, $exclude, $meta_key, $meta_value, $page_arg );
     407        // Query for members
     408        $members_query = new BP_Core_Members_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $include, (bool)$populate_extras, $exclude, $meta_key, $meta_value, $page_arg );
     409
     410        // Setup the main query
     411        if ( empty( $members_template ) ) {
     412                $members_query->set_main_query();
     413        }
     414
     415        // Setup the members template with the most recent query
     416        $members_template = $members_query;
     417
     418        // Clean up
     419        unset( $members_query );
     420
    319421        return apply_filters( 'bp_has_members', $members_template->has_members(), $members_template );
    320422}
    321423