Skip to:
Content

BuddyPress.org

Ticket #1269: 1269.03.patch

File 1269.03.patch, 133.5 KB (added by imath, 9 years ago)

The patch :)

  • src/bp-activity/bp-activity-template.php

    diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php
    index 1ea141a..890b8b4 100644
    function bp_activity_directory_permalink() { 
    113113 *
    114114 * @since BuddyPress (1.0.0)
    115115 */
    116 class BP_Activity_Template {
    117         var $current_activity = -1;
    118         var $activity_count;
    119         var $total_activity_count;
    120         var $activities;
    121         var $activity;
    122 
    123         var $in_the_loop;
     116class BP_Activity_Template extends BP_Template_Loop {
    124117
    125118        /**
    126          * URL parameter key for activity pagination. Default: 'acpage'.
     119         * Default arguments for Activity loop.
    127120         *
    128          * @since BuddyPress (2.1.0)
    129          * @var string
     121         * @since BuddyPress (2.3.0)
     122         *
     123         * @access protected
     124         * @var array
    130125         */
    131         var $pag_arg;
    132 
    133         var $pag_page;
    134         var $pag_num;
    135         var $pag_links;
    136 
    137         var $full_name;
     126        protected $default_args = array(
     127                'labels' => array(
     128                        'item'              => 'activity',
     129                        'item_plural'       => 'activities',
     130                        'item_total_count'  => 'total_activity_count',
     131                        'item_count'        => 'activity_count',
     132                        'current_item'      => 'current_activity',
     133                        'hook_prefix'       => 'activity',
     134                ),
     135                'params' => array(
     136                        'page_arg'          => 'acpage',
     137                        'page'              => 1,
     138                        'per_page'          => 20,
     139                        'max'               => 0,
     140                )
     141        );
    138142
    139143        /**
    140144         * Constructor method.
    class BP_Activity_Template { 
    164168         * }
    165169         */
    166170        function __construct( $args ) {
    167                 global $bp;
    168171
    169172                // Backward compatibility with old method of passing arguments
    170173                if ( !is_array( $args ) || func_num_args() > 1 ) {
    class BP_Activity_Template { 
    211214                        'spam'              => 'ham_only',
    212215                        'update_meta_cache' => true,
    213216                );
     217
    214218                $r = wp_parse_args( $args, $defaults );
    215                 extract( $r );
    216219
    217                 $this->pag_arg  = $r['page_arg'];
    218                 $this->pag_page = isset( $_REQUEST[ $this->pag_arg ] ) ? intval( $_REQUEST[ $this->pag_arg ] ) : $page;
    219                 $this->pag_num  = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
     220                $this->set_loop( array( 'params' => $r ) );
     221        }
     222
     223        /**
     224         * Get Activities.
     225         *
     226         * @since BuddyPress (2.3.0)
     227         *
     228         * @access public
     229         */
     230        public function get_items() {
     231                $bp = buddypress();
    220232
    221233                // Check if blog/forum replies are disabled
    222234                $this->disable_blogforum_replies = isset( $bp->site_options['bp-disable-blogforum-comments'] ) ? $bp->site_options['bp-disable-blogforum-comments'] : false;
    class BP_Activity_Template { 
    224236                // Get an array of the logged in user's favorite activities
    225237                $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true ) );
    226238
     239                // Set Displayed user fullname
     240                $this->full_name = bp_get_displayed_user_fullname();
     241
    227242                // Fetch specific activity items based on ID's
    228                 if ( !empty( $include ) ) {
    229                         $this->activities = bp_activity_get_specific( array(
    230                                 'activity_ids'      => explode( ',', $include ),
    231                                 'max'               => $max,
    232                                 'count_total'       => $count_total,
    233                                 'page'              => $this->pag_page,
    234                                 'per_page'          => $this->pag_num,
    235                                 'sort'              => $sort,
    236                                 'display_comments'  => $display_comments,
    237                                 'show_hidden'       => $show_hidden,
    238                                 'spam'              => $spam,
    239                                 'update_meta_cache' => $update_meta_cache,
     243                if ( ! empty( $this->args['params']['include'] ) ) {
     244                        $activities = bp_activity_get_specific( array(
     245                                'activity_ids'      => explode( ',', $this->args['params']['include'] ),
     246                                'max'               => $this->args['params']['max'],
     247                                'count_total'       => $this->args['params']['count_total'],
     248                                'page'              => $this->page,
     249                                'per_page'          => $this->per_page,
     250                                'sort'              => $this->args['params']['sort'],
     251                                'display_comments'  => $this->args['params']['display_comments'],
     252                                'show_hidden'       => $this->args['params']['show_hidden'],
     253                                'spam'              => $this->args['params']['spam'],
     254                                'update_meta_cache' => $this->args['params']['update_meta_cache'],
    240255                        ) );
    241256
    242257                // Fetch all activity items
    243258                } else {
    244                         $this->activities = bp_activity_get( array(
    245                                 'display_comments'  => $display_comments,
    246                                 'max'               => $max,
    247                                 'count_total'       => $count_total,
    248                                 'per_page'          => $this->pag_num,
    249                                 'page'              => $this->pag_page,
    250                                 'sort'              => $sort,
    251                                 'search_terms'      => $search_terms,
    252                                 'meta_query'        => $meta_query,
    253                                 'date_query'        => $date_query,
    254                                 'filter_query'      => $filter_query,
    255                                 'filter'            => $filter,
    256                                 'scope'             => $scope,
    257                                 'show_hidden'       => $show_hidden,
    258                                 'exclude'           => $exclude,
    259                                 'in'                => $in,
    260                                 'spam'              => $spam,
    261                                 'update_meta_cache' => $update_meta_cache,
    262                         ) );
     259                        $activities = bp_activity_get( $this->args['params'] );
    263260                }
    264261
    265                 // The total_activity_count property will be set only if a
    266                 // 'count_total' query has taken place
    267                 if ( ! is_null( $this->activities['total'] ) ) {
    268                         if ( ! $max || $max >= (int) $this->activities['total'] ) {
    269                                 $this->total_activity_count = (int) $this->activities['total'];
    270                         } else {
    271                                 $this->total_activity_count = (int) $max;
    272                         }
    273                 }
    274 
    275                 $this->has_more_items = $this->activities['has_more_items'];
    276 
    277                 $this->activities = $this->activities['activities'];
    278 
    279                 if ( $max ) {
    280                         if ( $max >= count($this->activities) ) {
    281                                 $this->activity_count = count( $this->activities );
    282                         } else {
    283                                 $this->activity_count = (int) $max;
    284                         }
    285                 } else {
    286                         $this->activity_count = count( $this->activities );
    287                 }
    288 
    289                 $this->full_name = bp_get_displayed_user_fullname();
     262                $this->has_more_items = $activities['has_more_items'];
    290263
    291264                // Fetch parent content for activity comments so we do not have to query in the loop
    292                 foreach ( (array) $this->activities as $activity ) {
     265                foreach ( (array) $activities['activities'] as $activity ) {
    293266                        if ( 'activity_comment' != $activity->type )
    294267                                continue;
    295268
    296269                        $parent_ids[] = $activity->item_id;
    297270                }
    298271
    299                 if ( !empty( $parent_ids ) )
     272                if ( ! empty( $parent_ids ) ) {
    300273                        $activity_parents = bp_activity_get_specific( array( 'activity_ids' => $parent_ids ) );
    301 
    302                 if ( !empty( $activity_parents['activities'] ) ) {
    303                         foreach( $activity_parents['activities'] as $parent )
    304                                 $this->activity_parents[$parent->id] = $parent;
    305 
    306                         unset( $activity_parents );
    307                 }
    308 
    309                 if ( (int) $this->total_activity_count && (int) $this->pag_num ) {
    310                         $this->pag_links = paginate_links( array(
    311                                 'base'      => add_query_arg( $page_arg, '%#%' ),
    312                                 'format'    => '',
    313                                 'total'     => ceil( (int) $this->total_activity_count / (int) $this->pag_num ),
    314                                 'current'   => (int) $this->pag_page,
    315                                 'prev_text' => _x( '←', 'Activity pagination previous text', 'buddypress' ),
    316                                 'next_text' => _x( '→', 'Activity pagination next text', 'buddypress' ),
    317                                 'mid_size'  => 1,
    318                                 'add_args'  => array(),
    319                         ) );
    320                 }
    321         }
    322 
    323         /**
    324          * Whether there are activity items available in the loop.
    325          *
    326          * @see bp_has_activities()
    327          *
    328          * @return bool True if there are items in the loop, otherwise false.
    329          */
    330         function has_activities() {
    331                 if ( $this->activity_count )
    332                         return true;
    333 
    334                 return false;
    335         }
    336 
    337         /**
    338          * Set up the next activity item and iterate index.
    339          *
    340          * @return object The next activity item to iterate over.
    341          */
    342         function next_activity() {
    343                 $this->current_activity++;
    344                 $this->activity = $this->activities[$this->current_activity];
    345 
    346                 return $this->activity;
    347         }
    348 
    349         /**
    350          * Rewind the posts and reset post index.
    351          */
    352         function rewind_activities() {
    353                 $this->current_activity = -1;
    354                 if ( $this->activity_count > 0 ) {
    355                         $this->activity = $this->activities[0];
    356274                }
    357         }
    358275
    359         /**
    360          * Whether there are activity items left in the loop to iterate over.
    361          *
    362          * This method is used by {@link bp_activities()} as part of the while loop
    363          * that controls iteration inside the activities loop, eg:
    364          *     while ( bp_activities() ) { ...
    365          *
    366          * @see bp_activities()
    367          *
    368          * @return bool True if there are more activity items to show,
    369          *              otherwise false.
    370          */
    371         function user_activities() {
    372                 if ( $this->current_activity + 1 < $this->activity_count ) {
    373                         return true;
    374                 } elseif ( $this->current_activity + 1 == $this->activity_count ) {
     276                if ( ! empty( $activity_parents['activities'] ) ) {
     277                        foreach ( $activity_parents['activities'] as $parent ) {
     278                                $this->activity_parents[ $parent->id ] = $parent;
     279                        }
    375280
    376                         /**
    377                          * Fires right before the rewinding of activity posts.
    378                          *
    379                          * @since BuddyPress (1.1.0)
    380                          */
    381                         do_action( 'activity_loop_end' );
    382                         // Do some cleaning up after the loop
    383                         $this->rewind_activities();
     281                        unset( $activity_parents );
    384282                }
    385283
    386                 $this->in_the_loop = false;
    387                 return false;
    388         }
    389 
    390         /**
    391          * Set up the current activity item inside the loop.
    392          *
    393          * Used by {@link bp_the_activity()} to set up the current activity item
    394          * data while looping, so that template tags used during that iteration
    395          * make reference to the current activity item.
    396          *
    397          * @see bp_the_activity()
    398          */
    399         function the_activity() {
    400 
    401                 $this->in_the_loop = true;
    402                 $this->activity    = $this->next_activity();
    403 
    404                 if ( is_array( $this->activity ) )
    405                         $this->activity = (object) $this->activity;
    406 
    407                 if ( $this->current_activity == 0 ) { // loop has just started
    408 
    409                         /**
    410                          * Fires if the current activity item is the first in the activity loop.
    411                          *
    412                          * @since BuddyPress (1.1.0)
    413                          */
    414                         do_action('activity_loop_start');
    415                 }
     284                return $activities;
    416285        }
    417286}
    418287
    function bp_has_activities( $args = '' ) { 
    694563         * @param string $activities_template Current activities template being used.
    695564         * @param array  $template_args Array of arguments passed into the BP_Activity_Template class.
    696565         */
    697         return apply_filters( 'bp_has_activities', $activities_template->has_activities(), $activities_template, $template_args );
     566        return apply_filters( 'bp_has_activities', $activities_template->has_items(), $activities_template, $template_args );
    698567}
    699568
    700569/**
    function bp_has_activities( $args = '' ) { 
    703572 * @since BuddyPress (1.0.0)
    704573 *
    705574 * @global object $activities_template {@link BP_Activity_Template}
    706  * @uses BP_Activity_Template::user_activities() {@link BP_Activity_Template::user_activities()}
     575 * @uses BP_Activity_Template::items() {@link BP_Activity_Template::items()}
    707576 *
    708577 * @return bool Returns true when activities are found.
    709578 */
    710579function bp_activities() {
    711580        global $activities_template;
    712         return $activities_template->user_activities();
     581        return $activities_template->items();
    713582}
    714583
    715584/**
    function bp_activities() { 
    724593 */
    725594function bp_the_activity() {
    726595        global $activities_template;
    727         return $activities_template->the_activity();
     596        return $activities_template->the_item();
    728597}
    729598
    730599/**
    function bp_activity_load_more_link() { 
    739608                global $activities_template;
    740609
    741610                $link = bp_get_requested_url();
    742                 $link = add_query_arg( $activities_template->pag_arg, $activities_template->pag_page + 1, $link );
     611                $link = add_query_arg( $activities_template->page_arg, $activities_template->page + 1, $link );
    743612
    744613                /**
    745614                 * Filters the Load More link URL.
    function bp_activity_load_more_link() { 
    757626 * @since BuddyPress (1.0.0)
    758627 *
    759628 * @global object $activities_template {@link BP_Activity_Template}
    760  * @uses BP_Activity_Template::the_activity() {@link BP_Activity_Template::the_activity()}
     629 * @uses BP_Activity_Template::the_item() {@link BP_Activity_Template::the_item()}
    761630 */
    762631function bp_activity_pagination_count() {
    763632        echo bp_get_activity_pagination_count();
    function bp_activity_pagination_count() { 
    776645        function bp_get_activity_pagination_count() {
    777646                global $activities_template;
    778647
    779                 $start_num = intval( ( $activities_template->pag_page - 1 ) * $activities_template->pag_num ) + 1;
     648                $start_num = intval( ( $activities_template->page - 1 ) * $activities_template->per_page ) + 1;
    780649                $from_num  = bp_core_number_format( $start_num );
    781                 $to_num    = bp_core_number_format( ( $start_num + ( $activities_template->pag_num - 1 ) > $activities_template->total_activity_count ) ? $activities_template->total_activity_count : $start_num + ( $activities_template->pag_num - 1 ) );
     650                $to_num    = bp_core_number_format( ( $start_num + ( $activities_template->per_page - 1 ) > $activities_template->total_activity_count ) ? $activities_template->total_activity_count : $start_num + ( $activities_template->per_page - 1 ) );
    782651                $total     = bp_core_number_format( $activities_template->total_activity_count );
    783652
    784653                return sprintf( _n( 'Viewing 1 item', 'Viewing %1$s - %2$s of %3$s items', $total, 'buddypress' ), $from_num, $to_num, $total );
    function bp_activity_has_more_items() { 
    837706                $remaining_pages = 0;
    838707
    839708                if ( ! empty( $activities_template->pag_page ) ) {
    840                         $remaining_pages = floor( ( $activities_template->total_activity_count - 1 ) / ( $activities_template->pag_num * $activities_template->pag_page ) );
     709                        $remaining_pages = floor( ( $activities_template->total_activity_count - 1 ) / ( $activities_template->per_page * $activities_template->page ) );
    841710                }
    842711
    843712                $has_more_items = (int) $remaining_pages > 0;
    function bp_activity_per_page() { 
    916785                 *
    917786                 * @since BuddyPress (1.2.0)
    918787                 *
    919                  * @param int $pag_num How many post should be displayed for pagination.
     788                 * @param int $per_page How many post should be displayed for pagination.
    920789                 */
    921                 return apply_filters( 'bp_get_activity_per_page', (int) $activities_template->pag_num );
     790                return apply_filters( 'bp_get_activity_per_page', (int) $activities_template->per_page );
    922791        }
    923792
    924793/**
  • src/bp-blogs/bp-blogs-template.php

    diff --git src/bp-blogs/bp-blogs-template.php src/bp-blogs/bp-blogs-template.php
    index 21fdb7c..50f9f46 100644
    function bp_blogs_directory_permalink() { 
    106106 *
    107107 * Responsible for loading a group of blogs into a loop for display.
    108108 */
    109 class BP_Blogs_Template {
     109class BP_Blogs_Template extends BP_Template_Loop {
    110110
    111111        /**
    112          * The loop iterator.
     112         * Default arguments for Blogs loop.
    113113         *
    114          * @access public
    115          * @var int
    116          */
    117         public $current_blog = -1;
    118 
    119         /**
    120          * The number of blogs returned by the paged query.
    121          *
    122          * @access public
    123          * @var int
    124          */
    125         public $blog_count = 0;
    126 
    127         /**
    128          * Array of blogs located by the query..
     114         * @since BuddyPress (2.3.0)
    129115         *
    130          * @access public
     116         * @access protected
    131117         * @var array
    132118         */
    133         public $blogs = array();
    134 
    135         /**
    136          * The blog object currently being iterated on.
    137          *
    138          * @access public
    139          * @var object
    140          */
    141         public $blog;
    142 
    143         /**
    144          * A flag for whether the loop is currently being iterated.
    145          *
    146          * @access public
    147          * @var bool
    148          */
    149         public $in_the_loop = false;
    150 
    151         /**
    152          * The page number being requested.
    153          *
    154          * @access public
    155          * @var public
    156          */
    157         public $pag_page = 1;
    158 
    159         /**
    160          * The number of items being requested per page.
    161          *
    162          * @access public
    163          * @var public
    164          */
    165         public $pag_num = 20;
    166 
    167         /**
    168          * An HTML string containing pagination links.
    169          *
    170          * @access public
    171          * @var string
    172          */
    173         public $pag_links = '';
    174 
    175         /**
    176          * The total number of blogs matching the query parameters.
    177          *
    178          * @access public
    179          * @var int
    180          */
    181         public $total_blog_count = 0;
     119        protected $default_args = array(
     120                'labels' => array(
     121                        'item'              => 'blog',
     122                        'item_plural'       => 'blogs',
     123                        'item_total_count'  => 'total_blog_count',
     124                        'item_count'        => 'blog_count',
     125                        'current_item'      => 'current_blog',
     126                        'hook_prefix'       => 'blog',
     127                ),
     128                'params' => array(
     129                        'page_arg'          => 'bpage',
     130                        'page'              => 1,
     131                        'per_page'          => 20,
     132                        'max'               => 0,
     133                )
     134        );
    182135
    183136        /**
    184137         * Constructor method.
    class BP_Blogs_Template { 
    198151         * @param array $include_blog_ids Array of blog IDs to include.
    199152         */
    200153        public function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true, $include_blog_ids = false ) {
     154                $params = array( 'params' => array(
     155                        'type'              => $type,
     156                        'page'              => $page,
     157                        'per_page'          => $per_page,
     158                        'max'               => $max,
     159                        'user_id'           => $user_id,
     160                        'search_terms'      => $search_terms,
     161                        'page_arg'          => $page_arg,
     162                        'update_meta_cache' => $update_meta_cache,
     163                        'include_blog_ids'  => $include_blog_ids,
     164                ) );
    201165
    202                 $this->pag_page = isset( $_REQUEST[ $page_arg ] ) ? intval( $_REQUEST[ $page_arg ] ) : $page;
    203                 $this->pag_num  = isset( $_REQUEST['num']       ) ? intval( $_REQUEST['num']       ) : $per_page;
    204 
    205                 // Backwards compatibility support for blogs by first letter
    206                 if ( ! empty( $_REQUEST['letter'] ) ) {
    207                         $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
    208 
    209                 // Typical blogs query
    210                 } else {
    211                         $this->blogs = bp_blogs_get_blogs( array(
    212                                 'type'              => $type,
    213                                 'per_page'          => $this->pag_num,
    214                                 'page'              => $this->pag_page,
    215                                 'user_id'           => $user_id,
    216                                 'search_terms'      => $search_terms,
    217                                 'update_meta_cache' => $update_meta_cache,
    218                                 'include_blog_ids'  => $include_blog_ids,
    219                         ) );
    220                 }
    221 
    222                 // Set the total blog count
    223                 if ( empty( $max ) || ( $max >= (int) $this->blogs['total'] ) ) {
    224                         $this->total_blog_count = (int) $this->blogs['total'];
    225                 } else {
    226                         $this->total_blog_count = (int) $max;
    227                 }
    228 
    229                 // Set the blogs array (to loop through later
    230                 $this->blogs = $this->blogs['blogs'];
    231 
    232                 // Get the current blog count to compare maximum against
    233                 $blog_count = count( $this->blogs );
    234 
    235                 // Set the current blog count
    236                 if ( empty( $max ) || ( $max >= (int) $blog_count ) ) {
    237                         $this->blog_count = (int) $blog_count;
    238                 } else {
    239                         $this->blog_count = (int) $max;
    240                 }
    241 
    242                 // Build pagination links based on total blogs and current page number
    243                 if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) {
    244                         $this->pag_links = paginate_links( array(
    245                                 'base'      => add_query_arg( $page_arg, '%#%' ),
    246                                 'format'    => '',
    247                                 'total'     => ceil( (int) $this->total_blog_count / (int) $this->pag_num ),
    248                                 'current'   => (int) $this->pag_page,
    249                                 'prev_text' => _x( '&larr;', 'Blog pagination previous text', 'buddypress' ),
    250                                 'next_text' => _x( '&rarr;', 'Blog pagination next text',     'buddypress' ),
    251                                 'mid_size'  => 1,
    252                                 'add_args'  => array(),
    253                         ) );
    254                 }
    255         }
    256 
    257         /**
    258          * Whether there are blogs available in the loop.
    259          *
    260          * @see bp_has_blogs()
    261          *
    262          * @return bool True if there are items in the loop, otherwise false.
    263          */
    264         public function has_blogs() {
    265                 return (bool) ! empty( $this->blog_count );
    266         }
    267 
    268         /**
    269          * Set up the next blog and iterate index.
    270          *
    271          * @return object The next blog to iterate over.
    272          */
    273         public function next_blog() {
    274                 $this->current_blog++;
    275                 $this->blog = $this->blogs[ $this->current_blog ];
    276 
    277                 return $this->blog;
    278         }
    279 
    280         /**
    281          * Rewind the blogs and reset blog index.
    282          */
    283         public function rewind_blogs() {
    284                 $this->current_blog = -1;
    285                 if ( $this->blog_count > 0 ) {
    286                         $this->blog = $this->blogs[0];
    287                 }
     166                $this->set_loop( $params );
    288167        }
    289168
    290169        /**
    291          * Whether there are blogs left in the loop to iterate over.
    292          *
    293          * This method is used by {@link bp_blogs()} as part of the while loop
    294          * that controls iteration inside the blogs loop, eg:
    295          *     while ( bp_blogs() ) { ...
     170         * Get Blogs.
    296171         *
    297          * @see bp_blogs()
     172         * @since BuddyPress (2.3.0)
    298173         *
    299          * @return bool True if there are more blogs to show, otherwise false.
     174         * @access public
    300175         */
    301         public function blogs() {
    302                 if ( ( $this->current_blog + 1 ) < $this->blog_count ) {
    303                         return true;
    304                 } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) {
     176        public function get_items() {
     177                // Backwards compatibility support for blogs by first letter
     178                if ( ! empty( $_REQUEST['letter'] ) ) {
     179                        $blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->per_page, $this->page );
    305180
    306                         /**
    307                          * Fires right before the rewinding of blogs listing after all are shown.
    308                          *
    309                          * @since BuddyPress (1.5.0)
    310                          */
    311                         do_action( 'blog_loop_end' );
    312                         // Do some cleaning up after the loop
    313                         $this->rewind_blogs();
     181                // Typical blogs query
     182                } else {
     183                        $blogs = bp_blogs_get_blogs( $this->args['params'] );
    314184                }
    315185
    316                 $this->in_the_loop = false;
    317                 return false;
     186                return $blogs;
    318187        }
    319188
    320189        /**
    321          * Set up the current blog inside the loop.
    322          *
    323          * Used by {@link bp_the_blog()} to set up the current blog data while
    324          * looping, so that template tags used during that iteration make
    325          * reference to the current blog.
     190         * Rewind Blogs is an alias of rewind_items()
    326191         *
    327          * @see bp_the_blog()
     192         * @access public
    328193         */
    329         public function the_blog() {
    330 
    331                 $this->in_the_loop = true;
    332                 $this->blog        = $this->next_blog();
    333 
    334                 // loop has just started
    335                 if ( 0 === $this->current_blog ) {
    336 
    337                         /**
    338                          * Fires if on the first blog in the loop.
    339                          *
    340                          * @since BuddyPress (1.5.0)
    341                          */
    342                         do_action( 'blog_loop_start' );
    343                 }
     194        public function rewind_blogs() {
     195                _doing_it_wrong( __FUNCTION__, __( 'BP_Blogs_Template->rewind_blogs() should not be used directly, please use bp_rewind_blogs()', 'buddypress' ), '2.3.0' );
     196                $this->rewind_items();
    344197        }
    345198}
    346199
    class BP_Blogs_Template { 
    350203function bp_rewind_blogs() {
    351204        global $blogs_template;
    352205
    353         $blogs_template->rewind_blogs();
     206        $blogs_template->rewind_items();
    354207}
    355208
    356209/**
    function bp_has_blogs( $args = '' ) { 
    429282         * @param BP_Blogs_Template $blogs_template Current blogs template object.
    430283         * @param array             $r              Parsed arguments used in blogs template query.
    431284         */
    432         return apply_filters( 'bp_has_blogs', $blogs_template->has_blogs(), $blogs_template, $r );
     285        return apply_filters( 'bp_has_blogs', $blogs_template->has_items(), $blogs_template, $r );
    433286}
    434287
    435288/**
    function bp_has_blogs( $args = '' ) { 
    442295function bp_blogs() {
    443296        global $blogs_template;
    444297
    445         return $blogs_template->blogs();
     298        return $blogs_template->items();
    446299}
    447300
    448301/**
    function bp_blogs() { 
    455308function bp_the_blog() {
    456309        global $blogs_template;
    457310
    458         return $blogs_template->the_blog();
     311        return $blogs_template->the_item();
    459312}
    460313
    461314/**
    function bp_the_blog() { 
    466319function bp_blogs_pagination_count() {
    467320        global $blogs_template;
    468321
    469         $start_num = intval( ( $blogs_template->pag_page - 1 ) * $blogs_template->pag_num ) + 1;
     322        $start_num = intval( ( $blogs_template->page - 1 ) * $blogs_template->per_page ) + 1;
    470323        $from_num  = bp_core_number_format( $start_num );
    471         $to_num    = bp_core_number_format( ( $start_num + ( $blogs_template->pag_num - 1 ) > $blogs_template->total_blog_count ) ? $blogs_template->total_blog_count : $start_num + ( $blogs_template->pag_num - 1 ) );
     324        $to_num    = bp_core_number_format( ( $start_num + ( $blogs_template->per_page - 1 ) > $blogs_template->total_blog_count ) ? $blogs_template->total_blog_count : $start_num + ( $blogs_template->per_page - 1 ) );
    472325        $total     = bp_core_number_format( $blogs_template->total_blog_count );
    473326
    474327        echo sprintf( _n( 'Viewing 1 site', 'Viewing %1$s - %2$s of %3$s sites', $total, 'buddypress' ), $from_num, $to_num, $total );
  • src/bp-core/bp-core-classes.php

    diff --git src/bp-core/bp-core-classes.php src/bp-core/bp-core-classes.php
    index f47f0b8..0babf2b 100644
    abstract class BP_Recursive_Query { 
    29782978         * @return bool
    29792979         */
    29802980        abstract protected function is_first_order_clause( $query );
    2981 }
    2982  No newline at end of file
     2981}
     2982
     2983/**
     2984 * Base class for components template loops.
     2985 *
     2986 * Each public method can be overriden from a custom template class
     2987 * For instance, you can customize the default paginate links within
     2988 * your class.
     2989 * @see  BP_Groups_Template->set_pag_links() for an example of use
     2990 *
     2991 * the abstract method get_items() is required. This is where you define
     2992 * an associative array having your custom 'item_plural' and 'total' keys
     2993 *
     2994 * @since 2.3.0
     2995 */
     2996abstract class BP_Template_Loop {
     2997
     2998        /**
     2999         * Default arguments common to all components loop.
     3000         *
     3001         * You can add the custom item default args within the custom template loop class
     3002         * @see  BP_Groups_Template->default_args for an example of use.
     3003         *
     3004         * @since BuddyPress (2.3.0)
     3005         *
     3006         * @access protected
     3007         * @var array $default_args {
     3008         *     Array describing the default argument for the item loop.
     3009         *     @type array $labels list of custom var names for the loop
     3010         *           - @type string $item the singular item var name
     3011         *           for instance 'group' for the groups loop
     3012         *           - @type string $item_plural the plural item var name
     3013         *           for instance 'groups' for the groups loop
     3014         *           - @type string $item_total_count the total count var name
     3015         *           for instance 'total_group_count' for the groups loop
     3016         *           - @type string $item_count the current items count var name
     3017         *           for instance 'group_count' for the groups loop
     3018         *           - @type string $current_item the current item var name
     3019         *           for instance 'current_group' for the groups loop
     3020         *           - @type string $hook_prefix the hook prefix to use in loop start
     3021         *           and loop end actions for instance 'activity' for the activity loop
     3022         *     @type array $params list of default arguments for the loop
     3023         *           - @type string $page_arg the var name to get the page value.
     3024         *           for instance 'grpage' for the groups loop
     3025         *           - @type int    $page default value for the page argument
     3026         *           - @type int    $per_page default value for the per page argument
     3027         *           - @type int    $max default value for the max argument
     3028         * }
     3029         */
     3030        protected $default_args = array(
     3031                'labels' => array(
     3032                        'item'              => 'item',
     3033                        'item_plural'       => 'items',
     3034                        'item_total_count'  => 'total_item_count',
     3035                        'item_count'        => 'item_count',
     3036                        'current_item'      => 'current_item',
     3037                        'hook_prefix'       => 'item',
     3038                ),
     3039                'params' => array(
     3040                        'page_arg'          => 'page',
     3041                        'page'              => 1,
     3042                        'per_page'          => 20,
     3043                        'max'               => 0,
     3044                )
     3045        );
     3046
     3047        /**
     3048         * Holds the arguments to customize the loop labels and the component's items query.
     3049         *
     3050         * This includes `$default_args`, as well as the user-supplied values.
     3051         *
     3052         * @since BuddyPress (2.3.0)
     3053         *
     3054         * @access protected
     3055         * @var array
     3056         */
     3057        protected $args = array();
     3058
     3059        /**
     3060         * A flag for whether the loop is currently being iterated.
     3061         *
     3062         * @since BuddyPress (2.3.0)
     3063         *
     3064         * @access public
     3065         * @var bool
     3066         */
     3067        public $in_the_loop;
     3068
     3069        /**
     3070         * An HTML string containing pagination links.
     3071         *
     3072         * @since BuddyPress (2.3.0)
     3073         *
     3074         * @access public
     3075         * @var string
     3076         */
     3077        public $pag_links;
     3078
     3079        /**
     3080         * Set the loop
     3081         *
     3082         * @since BuddyPress (2.3.0)
     3083         *
     3084         * @access public
     3085         * @param array $args component's loop arguments
     3086         */
     3087        public function set_loop( $args = array() ) {
     3088                $this->args = wp_parse_args( $args, $this->default_args );
     3089
     3090                // If not set the hook prefix defaults to the singular item name
     3091                if ( empty( $this->args['labels']['hook_prefix'] ) ) {
     3092                        $this->args['labels']['hook_prefix'] = $this->args['labels']['item'];
     3093                }
     3094
     3095                $this->{$this->args['labels']['current_item']} = -1;
     3096
     3097                // Setting loop parameters
     3098                if ( ! empty( $this->args['params'] ) ) {
     3099                        foreach ( (array) $this->args['params'] as $key => $value ) {
     3100                                $this->{$key} = $value;
     3101                        }
     3102
     3103                        if ( isset( $this->page_arg ) && ! empty( $_REQUEST[ $this->page_arg ] ) ) {
     3104                                $this->page = intval( $_REQUEST[ $this->page_arg ] );
     3105                        }
     3106
     3107                        if ( ! empty( $_REQUEST['num'] ) ) {
     3108                                $this->per_page = intval( $_REQUEST['num'] );
     3109                        }
     3110                }
     3111
     3112                // Setup the Items to loop through
     3113                $items = $this->get_items();
     3114
     3115                if ( ! isset( $items[ $this->args['labels']['item_plural'] ] ) ) {
     3116                        return false;
     3117                }
     3118
     3119                // Set items
     3120                $this->{$this->args['labels']['item_plural']} = $items[ $this->args['labels']['item_plural'] ];
     3121
     3122                if ( empty( $this->{$this->args['labels']['item_plural']} ) ) {
     3123                        $this->{$this->args['labels']['item_count']}       = 0;
     3124                        $this->{$this->args['labels']['item_total_count']} = 0;
     3125
     3126                } else {
     3127                        // Set the total items count
     3128                        $this->{$this->args['labels']['item_total_count']} = (int) $items['total'];
     3129
     3130                        if ( ! empty( $this->max ) && ( $this->max < (int) $items['total'] ) ) {
     3131                                $this->{$this->args['labels']['item_total_count']} = (int) $this->max;
     3132                        }
     3133
     3134                        // Set current items count
     3135                        $this->{$this->args['labels']['item_count']} = count( $this->{$this->args['labels']['item_plural']} );
     3136
     3137                        if ( ! empty( $this->max ) && ( $this->max < (int) $this->{$this->args['labels']['item_count']} ) ) {
     3138                                $this->{$this->args['labels']['item_count']} = (int) $this->max;
     3139                        }
     3140                }
     3141
     3142                if ( ! empty( $this->{$this->args['labels']['item_total_count']} ) && ! empty( $this->per_page ) && ! empty( $this->page ) ) {
     3143                        $this->set_pag_links();
     3144                }
     3145        }
     3146
     3147        /**
     3148         * Get the component's items
     3149         *
     3150         * This method needs to be set within the custom Template class
     3151         * @see  BP_Blogs_Template->get_items() for an example of use
     3152         *
     3153         * @since BuddyPress (2.3.0)
     3154         *
     3155         * @access public
     3156         */
     3157        abstract public function get_items();
     3158
     3159        /**
     3160         * Set the pagination links
     3161         *
     3162         * Each item loop can override this and sets its own pagination links
     3163         * within its custom template class.
     3164         * @see  BP_Groups_Template->set_pag_links() for an example of use
     3165         *
     3166         * @since BuddyPress (2.3.0)
     3167         *
     3168         * @access public
     3169         */
     3170        public function set_pag_links() {
     3171                $this->pag_links = paginate_links( array(
     3172                        'base'      => add_query_arg( $this->page_arg, '%#%' ),
     3173                        'format'    => '',
     3174                        'total'     => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ),
     3175                        'current'   => (int) $this->page,
     3176                        'prev_text' => _x( '&larr;', 'pagination previous text', 'buddypress' ),
     3177                        'next_text' => _x( '&rarr;', 'pagination next text',     'buddypress' ),
     3178                        'mid_size'  => 1,
     3179                        'add_args'  => array(),
     3180                ) );
     3181        }
     3182
     3183        /**
     3184         * Whether there are Items available in the loop.
     3185         *
     3186         * @since BuddyPress (2.3.0)
     3187         *
     3188         * @return bool True if there are items in the loop, otherwise false.
     3189         */
     3190        public function has_items() {
     3191                if ( ! empty( $this->{$this->args['labels']['item_count']} ) ) {
     3192                        return true;
     3193                }
     3194
     3195                return false;
     3196        }
     3197
     3198        /**
     3199         * Set up the next item and iterate index.
     3200         *
     3201         * @since BuddyPress (2.3.0)
     3202         *
     3203         * @return object The next item to iterate over.
     3204         */
     3205        public function next_item() {
     3206
     3207                $this->{$this->args['labels']['current_item']}++;
     3208
     3209                $this->{$this->args['labels']['item']} = $this->{$this->args['labels']['item_plural']}[ $this->{$this->args['labels']['current_item']} ];
     3210
     3211                return $this->{$this->args['labels']['item']};
     3212        }
     3213
     3214        /**
     3215         * Rewind the items and reset items index.
     3216         *
     3217         * @since BuddyPress (2.3.0)
     3218         */
     3219        public function rewind_items() {
     3220
     3221                $this->{$this->args['labels']['current_item']} = -1;
     3222
     3223                if ( $this->{$this->args['labels']['item_count']} > 0 ) {
     3224                        $this->{$this->args['labels']['item']} = $this->{$this->args['labels']['item_plural']}[0];
     3225                }
     3226        }
     3227
     3228        /**
     3229         * Whether there are items left in the loop to iterate over.
     3230         *
     3231         * @since BuddyPress (2.3.0)
     3232         *
     3233         * @return bool True if there are more items to show,
     3234         *         otherwise false.
     3235         */
     3236        public function items() {
     3237
     3238                if ( $this->{$this->args['labels']['current_item']} + 1 < $this->{$this->args['labels']['item_count']} ) {
     3239                        return true;
     3240
     3241                } elseif ( $this->{$this->args['labels']['current_item']} + 1 == $this->{$this->args['labels']['item_count']} ) {
     3242                        /**
     3243                         * Inform when the item loop ends. Dynamically built using the hook_prefix label
     3244                         * For instance the messages box loop is using 'messages_box' as its item hook_prefix,
     3245                         * as a result the action will be 'messages_box_loop_end'
     3246                         */
     3247                        do_action( $this->args['labels']['hook_prefix'] . '_loop_end' );
     3248
     3249                        $this->rewind_items();
     3250                }
     3251
     3252                $this->in_the_loop = false;
     3253                return false;
     3254        }
     3255
     3256        /**
     3257         * Set up the current item inside the loop.
     3258         *
     3259         * Each item loop can override this to append custom vars to the item
     3260         * within its custom template class.
     3261         * @see  BP_Groups_Invite_Template->the_item() for an example of use
     3262         *
     3263         * @since BuddyPress (2.3.0)
     3264         */
     3265        public function the_item() {
     3266                $this->in_the_loop  = true;
     3267                $this->{$this->args['labels']['item']} = $this->next_item();
     3268
     3269                // loop has just started
     3270                if ( 0 === $this->{$this->args['labels']['current_item']} ) {
     3271                        /**
     3272                         * Inform when the item loop starts. Dynamically built using the hook_prefix label
     3273                         * For instance the groups loop is using 'group' as its hook_prefix, as a result
     3274                         * the action will be 'group_loop_start'
     3275                         */
     3276                        do_action( $this->args['labels']['hook_prefix'] . '_loop_start' );
     3277                }
     3278        }
     3279}
  • src/bp-groups/bp-groups-template.php

    diff --git src/bp-groups/bp-groups-template.php src/bp-groups/bp-groups-template.php
    index 4684804..88a6561 100644
    function bp_groups_directory_permalink() { 
    6363        function bp_get_groups_directory_permalink() {
    6464                return apply_filters( 'bp_get_groups_directory_permalink', trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() ) );
    6565        }
    66 
    6766/**
    6867 * The main Groups template loop class.
    6968 *
    7069 * Responsible for loading a group of groups into a loop for display.
    7170 */
    72 class BP_Groups_Template {
     71class BP_Groups_Template extends BP_Template_Loop {
    7372
    7473        /**
    75          * The loop iterator.
     74         * Default arguments for Groups loop.
    7675         *
    77          * @access public
    78          * @var int
    79          */
    80         var $current_group = -1;
    81 
    82         /**
    83          * The number of groups returned by the paged query.
    84          *
    85          * @access public
    86          * @var int
    87          */
    88         var $group_count;
    89 
    90         /**
    91          * Array of groups located by the query.
     76         * @since BuddyPress (2.3.0)
    9277         *
    93          * @access public
     78         * @access protected
    9479         * @var array
    9580         */
    96         var $groups;
    97 
    98         /**
    99          * The group object currently being iterated on.
    100          *
    101          * @access public
    102          * @var object
    103          */
    104         var $group;
    105 
    106         /**
    107          * A flag for whether the loop is currently being iterated.
    108          *
    109          * @access public
    110          * @var bool
    111          */
    112         var $in_the_loop;
    113 
    114         /**
    115          * The page number being requested.
    116          *
    117          * @access public
    118          * @var public
    119          */
    120         var $pag_page;
    121 
    122         /**
    123          * The number of items being requested per page.
    124          *
    125          * @access public
    126          * @var public
    127          */
    128         var $pag_num;
    129 
    130         /**
    131          * An HTML string containing pagination links.
    132          *
    133          * @access public
    134          * @var string
    135          */
    136         var $pag_links;
    137 
    138         /**
    139          * The total number of groups matching the query parameters.
    140          *
    141          * @access public
    142          * @var int
    143          */
    144         var $total_group_count;
    145 
    146         /**
    147          * Whether the template loop is for a single group page.
    148          *
    149          * @access public
    150          * @var bool
    151          */
    152         var $single_group = false;
    153 
    154         /**
    155          * Field to sort by.
    156          *
    157          * @access public
    158          * @var string
    159          */
    160         var $sort_by;
    161 
    162         /**
    163          * Sort order.
    164          *
    165          * @access public
    166          * @var string
    167          */
    168         var $order;
     81        protected $default_args = array(
     82                'labels' => array(
     83                        'item'              => 'group',
     84                        'item_plural'       => 'groups',
     85                        'item_total_count'  => 'total_group_count',
     86                        'item_count'        => 'group_count',
     87                        'current_item'      => 'current_group',
     88                        'hook_prefix'       => 'group',
     89                ),
     90                'params' => array(
     91                        'page_arg'          => 'grpage',
     92                        'page'              => 1,
     93                        'per_page'          => 20,
     94                        'max'               => 0,
     95                )
     96        );
    16997
    17098        /**
    17199         * Constructor method.
    class BP_Groups_Template { 
    180108         *     @type int $page Default: 1.
    181109         * }
    182110         */
    183         function __construct( $args = array() ){
    184 
     111        public function __construct( $args = array() ) {
    185112                // Backward compatibility with old method of passing arguments
    186113                if ( ! is_array( $args ) || func_num_args() > 1 ) {
    187114                        _deprecated_argument( __METHOD__, '1.7', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
    class BP_Groups_Template { 
    206133                }
    207134
    208135                $defaults = array(
    209                         'type'            => 'active',
    210                         'page'            => 1,
    211                         'per_page'        => 20,
    212                         'max'             => false,
    213                         'show_hidden'     => false,
    214                         'page_arg'        => 'grpage',
    215                         'user_id'         => 0,
    216                         'slug'            => false,
    217                         'include'         => false,
    218                         'exclude'         => false,
    219                         'search_terms'    => '',
    220                         'meta_query'      => false,
    221                         'populate_extras' => true,
     136                        'type'              => 'active',
     137                        'page'              => 1,
     138                        'per_page'          => 20,
     139                        'max'               => false,
     140                        'show_hidden'       => false,
     141                        'page_arg'          => 'grpage',
     142                        'user_id'           => 0,
     143                        'slug'              => false,
     144                        'include'           => false,
     145                        'exclude'           => false,
     146                        'search_terms'      => '',
     147                        'meta_query'        => false,
     148                        'populate_extras'   => true,
    222149                        'update_meta_cache' => true,
    223150                );
    224151
    225152                $r = wp_parse_args( $args, $defaults );
    226                 extract( $r );
    227153
    228                 $this->pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;
    229                 $this->pag_num  = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
     154                $this->set_loop( array( 'params' => $r ) );
     155        }
    230156
    231                 if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) )
    232                         $show_hidden = true;
     157        /**
     158         * Get Groups.
     159         *
     160         * @since BuddyPress (2.3.0)
     161         *
     162         * @access public
     163         */
     164        public function get_items() {
     165                if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $this->user_id == bp_loggedin_user_id() ) ) {
     166                        $this->args['params']['show_hidden'] = true;
     167                }
     168
     169                if ( 'invites' == $this->type ) {
     170                        return groups_get_invites_for_user( $this->user_id, $this->per_page, $this->page, $this->exclude );
    233171
    234                 if ( 'invites' == $type ) {
    235                         $this->groups = groups_get_invites_for_user( $user_id, $this->pag_num, $this->pag_page, $exclude );
    236                 } else if ( 'single-group' == $type ) {
     172                } else if ( 'single-group' == $this->type ) {
    237173                        $this->single_group = true;
    238174
    239175                        if ( groups_get_current_group() ) {
    class BP_Groups_Template { 
    241177
    242178                        } else {
    243179                                $group = groups_get_group( array(
    244                                         'group_id'        => BP_Groups_Group::get_id_from_slug( $r['slug'] ),
    245                                         'populate_extras' => $r['populate_extras'],
     180                                        'group_id'        => BP_Groups_Group::get_id_from_slug( $this->slug ),
     181                                        'populate_extras' => $this->populate_extras,
    246182                                ) );
    247183                        }
    248184
    249                         // backwards compatibility - the 'group_id' variable is not part of the
    250                         // BP_Groups_Group object, but we add it here for devs doing checks against it
    251                         //
    252                         // @see https://buddypress.trac.wordpress.org/changeset/3540
    253                         //
    254                         // this is subject to removal in a future release; devs should check against
    255                         // $group->id instead
    256                         $group->group_id = $group->id;
    257 
    258                         $this->groups = array( $group );
    259 
    260                 } else {
    261                         $this->groups = groups_get_groups( array(
    262                                 'type'              => $type,
    263                                 'order'             => $order,
    264                                 'orderby'           => $orderby,
    265                                 'per_page'          => $this->pag_num,
    266                                 'page'              => $this->pag_page,
    267                                 'user_id'           => $user_id,
    268                                 'search_terms'      => $search_terms,
    269                                 'meta_query'        => $meta_query,
    270                                 'include'           => $include,
    271                                 'exclude'           => $exclude,
    272                                 'populate_extras'   => $populate_extras,
    273                                 'update_meta_cache' => $update_meta_cache,
    274                                 'show_hidden'       => $show_hidden
    275                         ) );
    276                 }
    277 
    278                 if ( 'invites' == $type ) {
    279                         $this->total_group_count = (int) $this->groups['total'];
    280                         $this->group_count       = (int) $this->groups['total'];
    281                         $this->groups            = $this->groups['groups'];
    282                 } else if ( 'single-group' == $type ) {
    283185                        if ( empty( $group->id ) ) {
    284                                 $this->total_group_count = 0;
    285                                 $this->group_count       = 0;
    286                         } else {
    287                                 $this->total_group_count = 1;
    288                                 $this->group_count       = 1;
    289                         }
    290                 } else {
    291                         if ( empty( $max ) || $max >= (int) $this->groups['total'] ) {
    292                                 $this->total_group_count = (int) $this->groups['total'];
    293                         } else {
    294                                 $this->total_group_count = (int) $max;
    295                         }
    296 
    297                         $this->groups = $this->groups['groups'];
    298 
    299                         if ( !empty( $max ) ) {
    300                                 if ( $max >= count( $this->groups ) ) {
    301                                         $this->group_count = count( $this->groups );
    302                                 } else {
    303                                         $this->group_count = (int) $max;
    304                                 }
     186                                return array( 'groups' => array(), 'total' => 0 );
    305187                        } else {
    306                                 $this->group_count = count( $this->groups );
     188                                return array( 'groups' => array( $group ), 'total' => 1 );
    307189                        }
    308                 }
    309 
    310                 // Build pagination links
    311                 if ( (int) $this->total_group_count && (int) $this->pag_num ) {
    312                         $pag_args = array(
    313                                 $page_arg => '%#%'
    314                         );
    315190
    316                         if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
    317                                 $base = remove_query_arg( 's', wp_get_referer() );
    318                         } else {
    319                                 $base = '';
    320                         }
    321 
    322                         $add_args = array(
    323                                 'num'     => $this->pag_num,
    324                                 'sortby'  => $this->sort_by,
    325                                 'order'   => $this->order,
    326                         );
    327 
    328                         if ( ! empty( $search_terms ) ) {
    329                                 $add_args['s'] = urlencode( $search_terms );
    330                         }
    331 
    332                         $this->pag_links = paginate_links( array(
    333                                 'base'      => add_query_arg( $pag_args, $base ),
    334                                 'format'    => '',
    335                                 'total'     => ceil( (int) $this->total_group_count / (int) $this->pag_num ),
    336                                 'current'   => $this->pag_page,
    337                                 'prev_text' => _x( '&larr;', 'Group pagination previous text', 'buddypress' ),
    338                                 'next_text' => _x( '&rarr;', 'Group pagination next text', 'buddypress' ),
    339                                 'mid_size'  => 1,
    340                                 'add_args'  => $add_args,
    341                         ) );
     191                } else {
     192                        return groups_get_groups( $this->args['params'] );
    342193                }
    343194        }
    344195
    345196        /**
    346          * Whether there are groups available in the loop.
     197         * Set Groups pagination.
    347198         *
    348          * @see bp_has_groups()
     199         * @since BuddyPress (2.3.0)
    349200         *
    350          * @return bool True if there are items in the loop, otherwise false.
    351          */
    352         function has_groups() {
    353                 if ( $this->group_count )
    354                         return true;
    355 
    356                 return false;
    357         }
    358 
    359         /**
    360          * Set up the next group and iterate index.
    361          *
    362          * @return object The next group to iterate over.
    363          */
    364         function next_group() {
    365                 $this->current_group++;
    366                 $this->group = $this->groups[$this->current_group];
    367 
    368                 return $this->group;
    369         }
    370 
    371         /**
    372          * Rewind the groups and reset member index.
     201         * @access public
    373202         */
    374         function rewind_groups() {
    375                 $this->current_group = -1;
    376                 if ( $this->group_count > 0 ) {
    377                         $this->group = $this->groups[0];
    378                 }
    379         }
     203        public function set_pag_links() {
     204                $pag_args = array(
     205                        $this->page_arg => '%#%'
     206                );
    380207
    381         /**
    382          * Whether there are groups left in the loop to iterate over.
    383          *
    384          * This method is used by {@link bp_groups()} as part of the while loop
    385          * that controls iteration inside the groups loop, eg:
    386          *     while ( bp_groups() ) { ...
    387          *
    388          * @see bp_groups()
    389          *
    390          * @return bool True if there are more groups to show, otherwise false.
    391          */
    392         function groups() {
    393                 if ( $this->current_group + 1 < $this->group_count ) {
    394                         return true;
    395                 } elseif ( $this->current_group + 1 == $this->group_count ) {
    396                         do_action('group_loop_end');
    397                         // Do some cleaning up after the loop
    398                         $this->rewind_groups();
     208                if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
     209                        $base = remove_query_arg( 's', wp_get_referer() );
     210                } else {
     211                        $base = '';
    399212                }
    400213
    401                 $this->in_the_loop = false;
    402                 return false;
    403         }
     214                $add_args = array(
     215                        'num'     => $this->per_page,
     216                        'sortby'  => $this->orderby,
     217                        'order'   => $this->order,
     218                );
    404219
    405         /**
    406          * Set up the current group inside the loop.
    407          *
    408          * Used by {@link bp_the_group()} to set up the current group data
    409          * while looping, so that template tags used during that iteration make
    410          * reference to the current member.
    411          *
    412          * @see bp_the_group()
    413          */
    414         function the_group() {
    415                 $this->in_the_loop = true;
    416                 $this->group       = $this->next_group();
     220                if ( ! empty( $this->search_terms ) ) {
     221                        $add_args['s'] = urlencode( $this->search_terms );
     222                }
    417223
    418                 if ( 0 == $this->current_group ) // loop has just started
    419                         do_action('group_loop_start');
     224                $this->pag_links = paginate_links( array(
     225                        'base'      => add_query_arg( $pag_args, $base ),
     226                        'format'    => '',
     227                        'total'     => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ),
     228                        'current'   => (int) $this->page,
     229                        'prev_text' => _x( '&larr;', 'Group pagination previous text', 'buddypress' ),
     230                        'next_text' => _x( '&rarr;', 'Group pagination next text', 'buddypress' ),
     231                        'mid_size'  => 1,
     232                        'add_args'  => $add_args,
     233                ) );
    420234        }
    421235}
    422236
    function bp_has_groups( $args = '' ) { 
    539353        ) );
    540354
    541355        // Filter and return whether or not the groups loop has groups in it
    542         return apply_filters( 'bp_has_groups', $groups_template->has_groups(), $groups_template, $r );
     356        return apply_filters( 'bp_has_groups', $groups_template->has_items(), $groups_template, $r );
    543357}
    544358
    545359/**
    function bp_has_groups( $args = '' ) { 
    549363 */
    550364function bp_groups() {
    551365        global $groups_template;
    552         return $groups_template->groups();
     366        return $groups_template->items();
    553367}
    554368
    555369/**
    function bp_groups() { 
    559373 */
    560374function bp_the_group() {
    561375        global $groups_template;
    562         return $groups_template->the_group();
     376        return $groups_template->the_item();
    563377}
    564378
    565379/**
    function bp_groups_pagination_count() { 
    15751389        function bp_get_groups_pagination_count() {
    15761390                global $groups_template;
    15771391
    1578                 $start_num = intval( ( $groups_template->pag_page - 1 ) * $groups_template->pag_num ) + 1;
     1392                $start_num = intval( ( $groups_template->page - 1 ) * $groups_template->per_page ) + 1;
    15791393                $from_num  = bp_core_number_format( $start_num );
    1580                 $to_num    = bp_core_number_format( ( $start_num + ( $groups_template->pag_num - 1 ) > $groups_template->total_group_count ) ? $groups_template->total_group_count : $start_num + ( $groups_template->pag_num - 1 ) );
     1394                $to_num    = bp_core_number_format( ( $start_num + ( $groups_template->per_page - 1 ) > $groups_template->total_group_count ) ? $groups_template->total_group_count : $start_num + ( $groups_template->per_page - 1 ) );
    15811395                $total     = bp_core_number_format( $groups_template->total_group_count );
    15821396
    15831397                return apply_filters( 'bp_get_groups_pagination_count', sprintf( _n( 'Viewing 1 group', 'Viewing %1$s - %2$s of %3$s groups', $total, 'buddypress' ), $from_num, $to_num, $total ), $from_num, $to_num, $total );
    function bp_total_group_count_for_user( $user_id = 0 ) { 
    29312745 * Group Members Template Tags
    29322746 **/
    29332747
    2934 class BP_Groups_Group_Members_Template {
    2935         var $current_member = -1;
    2936         var $member_count;
    2937         var $members;
    2938         var $member;
    2939 
    2940         var $in_the_loop;
     2748class BP_Groups_Group_Members_Template extends BP_Template_Loop {
    29412749
    2942         var $pag_page;
    2943         var $pag_num;
    2944         var $pag_links;
    2945         var $total_group_count;
     2750        /**
     2751         * Default arguments for Group Members loop.
     2752         *
     2753         * @since BuddyPress (2.3.0)
     2754         *
     2755         * @access protected
     2756         * @var array
     2757         */
     2758        protected $default_args = array(
     2759                'labels' => array(
     2760                        'item'              => 'member',
     2761                        'item_plural'       => 'members',
     2762                        'item_total_count'  => 'total_member_count',
     2763                        'item_count'        => 'member_count',
     2764                        'current_item'      => 'current_member',
     2765                        'hook_prefix'       => 'group_member',
     2766                ),
     2767                'params' => array(
     2768                        'page_arg'          => 'mlpage',
     2769                        'page'              => 1,
     2770                        'per_page'          => 20,
     2771                        'max'               => 0,
     2772                )
     2773        );
    29462774
    29472775        /**
    29482776         * Constructor.
    class BP_Groups_Group_Members_Template { 
    29882816                        'group_id'            => bp_get_current_group_id(),
    29892817                        'page'                => 1,
    29902818                        'per_page'            => 20,
     2819                        'page_arg'            => 'mlpage',
    29912820                        'max'                 => false,
    29922821                        'exclude'             => false,
    29932822                        'exclude_admins_mods' => 1,
    class BP_Groups_Group_Members_Template { 
    29972826                        'type'                => 'last_joined',
    29982827                ) );
    29992828
    3000                 // @todo No
    3001                 extract( $r );
    3002 
    3003                 $this->pag_page = isset( $_REQUEST['mlpage'] ) ? intval( $_REQUEST['mlpage'] ) : $r['page'];
    3004                 $this->pag_num  = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
     2829                $this->set_loop( array( 'params' => $r ) );
     2830        }
    30052831
     2832        /**
     2833         * Get Group members.
     2834         *
     2835         * @since BuddyPress (2.3.0)
     2836         *
     2837         * @access public
     2838         */
     2839        public function get_items() {
    30062840                /**
    30072841                 * Check the current group is the same as the supplied group ID.
    30082842                 * It can differ when using {@link bp_group_has_members()} outside the Groups screens.
    30092843                 */
    30102844                $current_group = groups_get_current_group();
    30112845                if ( ! $current_group || $current_group && $current_group->id !== bp_get_current_group_id() ) {
    3012                         $current_group = groups_get_group( array( 'group_id' => $r['group_id'] ) );
     2846                        $current_group = groups_get_group( array( 'group_id' => $this->args['params']['group_id'] ) );
    30132847                }
    30142848
    3015                 // Assemble the base URL for pagination
    3016                 $base_url = trailingslashit( bp_get_group_permalink( $current_group ) . bp_current_action() );
    3017                 if ( bp_action_variable() ) {
    3018                         $base_url = trailingslashit( $base_url . bp_action_variable() );
    3019                 }
    3020 
    3021                 $members_args = $r;
    3022 
    3023                 $members_args['page']     = $this->pag_page;
    3024                 $members_args['per_page'] = $this->pag_num;
     2849                $this->current_group = $current_group;
    30252850
    3026                 $this->members = groups_get_group_members( $members_args );
     2851                $group_members = groups_get_group_members( $this->args['params'] );
    30272852
    3028                 if ( !$max || $max >= (int) $this->members['count'] )
    3029                         $this->total_member_count = (int) $this->members['count'];
    3030                 else
    3031                         $this->total_member_count = (int) $max;
    3032 
    3033                 $this->members = $this->members['members'];
     2853                // Make sure to return an array using the keys the abastract class is waiting for
     2854                return array( 'members' => $group_members['members'], 'total' => $group_members['count'] );
     2855        }
    30342856
    3035                 if ( $max ) {
    3036                         if ( $max >= count($this->members) )
    3037                                 $this->member_count = count($this->members);
    3038                         else
    3039                                 $this->member_count = (int) $max;
    3040                 } else {
    3041                         $this->member_count = count($this->members);
     2857        /**
     2858         * Set Group members pagination.
     2859         *
     2860         * @since BuddyPress (2.3.0)
     2861         *
     2862         * @access public
     2863         */
     2864        public function set_pag_links() {
     2865                // Assemble the base URL for pagination
     2866                $base_url = trailingslashit( bp_get_group_permalink( $this->current_group ) . bp_current_action() );
     2867                if ( bp_action_variable() ) {
     2868                        $base_url = trailingslashit( $base_url . bp_action_variable() );
    30422869                }
    30432870
    30442871                $this->pag_links = paginate_links( array(
    30452872                        'base'      => add_query_arg( array( 'mlpage' => '%#%' ), $base_url ),
    30462873                        'format'    => '',
    3047                         'total'     => ! empty( $this->pag_num ) ? ceil( $this->total_member_count / $this->pag_num ) : $this->total_member_count,
    3048                         'current'   => $this->pag_page,
     2874                        'total'     => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ),
     2875                        'current'   => (int) $this->page,
    30492876                        'prev_text' => '&larr;',
    30502877                        'next_text' => '&rarr;',
    30512878                        'mid_size'  => 1,
    30522879                        'add_args'  => array(),
    3053                 ));
    3054         }
    3055 
    3056         function has_members() {
    3057                 if ( $this->member_count )
    3058                         return true;
    3059 
    3060                 return false;
    3061         }
    3062 
    3063         function next_member() {
    3064                 $this->current_member++;
    3065                 $this->member = $this->members[$this->current_member];
    3066 
    3067                 return $this->member;
    3068         }
    3069 
    3070         function rewind_members() {
    3071                 $this->current_member = -1;
    3072                 if ( $this->member_count > 0 ) {
    3073                         $this->member = $this->members[0];
    3074                 }
    3075         }
    3076 
    3077         function members() {
    3078                 if ( $this->current_member + 1 < $this->member_count ) {
    3079                         return true;
    3080                 } elseif ( $this->current_member + 1 == $this->member_count ) {
    3081                         do_action('loop_end');
    3082                         // Do some cleaning up after the loop
    3083                         $this->rewind_members();
    3084                 }
    3085 
    3086                 $this->in_the_loop = false;
    3087                 return false;
    3088         }
    3089 
    3090         function the_member() {
    3091 
    3092                 $this->in_the_loop = true;
    3093                 $this->member      = $this->next_member();
    3094 
    3095                 if ( 0 == $this->current_member ) // loop has just started
    3096                         do_action('loop_start');
     2880                ) );
    30972881        }
    30982882}
    30992883
    function bp_group_has_members( $args = '' ) { 
    31492933        }
    31502934
    31512935        $members_template = new BP_Groups_Group_Members_Template( $r );
    3152         return apply_filters( 'bp_group_has_members', $members_template->has_members(), $members_template );
     2936        return apply_filters( 'bp_group_has_members', $members_template->has_items(), $members_template );
    31532937}
    31542938
    31552939function bp_group_members() {
    31562940        global $members_template;
    31572941
    3158         return $members_template->members();
     2942        return $members_template->items();
    31592943}
    31602944
    31612945function bp_group_the_member() {
    31622946        global $members_template;
    31632947
    3164         return $members_template->the_member();
     2948        return $members_template->the_item();
    31652949}
    31662950
    31672951/**
    function bp_group_member_id() { 
    33443128function bp_group_member_needs_pagination() {
    33453129        global $members_template;
    33463130
    3347         if ( $members_template->total_member_count > $members_template->pag_num )
     3131        if ( $members_template->total_member_count > $members_template->per_page )
    33483132                return true;
    33493133
    33503134        return false;
    function bp_group_member_pagination_count() { 
    33723156        function bp_get_group_member_pagination_count() {
    33733157                global $members_template;
    33743158
    3375                 $start_num = intval( ( $members_template->pag_page - 1 ) * $members_template->pag_num ) + 1;
     3159                $start_num = intval( ( $members_template->page - 1 ) * $members_template->per_page ) + 1;
    33763160                $from_num = bp_core_number_format( $start_num );
    3377                 $to_num = bp_core_number_format( ( $start_num + ( $members_template->pag_num - 1 ) > $members_template->total_member_count ) ? $members_template->total_member_count : $start_num + ( $members_template->pag_num - 1 ) );
     3161                $to_num = bp_core_number_format( ( $start_num + ( $members_template->per_page - 1 ) > $members_template->total_member_count ) ? $members_template->total_member_count : $start_num + ( $members_template->per_page - 1 ) );
    33783162                $total = bp_core_number_format( $members_template->total_member_count );
    33793163
    33803164                return apply_filters( 'bp_get_group_member_pagination_count', sprintf( _n( 'Viewing 1 member', 'Viewing %1$s - %2$s of %3$s members', $total, 'buddypress' ), $from_num, $to_num, $total ), $from_num, $to_num, $total );
    function bp_custom_group_fields() { 
    39813765 * Membership Requests Template Tags
    39823766 **/
    39833767
    3984 class BP_Groups_Membership_Requests_Template {
    3985         var $current_request = -1;
    3986         var $request_count;
    3987         var $requests;
    3988         var $request;
     3768class BP_Groups_Membership_Requests_Template extends BP_Template_Loop {
    39893769
    3990         var $in_the_loop;
    3991 
    3992         var $pag_page;
    3993         var $pag_num;
    3994         var $pag_links;
    3995         var $total_request_count;
     3770        /**
     3771         * Default arguments for Group Membership requests loop.
     3772         *
     3773         * @since BuddyPress (2.3.0)
     3774         *
     3775         * @access protected
     3776         * @var array
     3777         */
     3778        protected $default_args = array(
     3779                'labels' => array(
     3780                        'item'              => 'request',
     3781                        'item_plural'       => 'requests',
     3782                        'item_total_count'  => 'total_request_count',
     3783                        'item_count'        => 'request_count',
     3784                        'current_item'      => 'current_request',
     3785                        'hook_prefix'       => 'group_request',
     3786                ),
     3787                'params' => array(
     3788                        'page_arg'          => 'mrpage',
     3789                        'page'              => 1,
     3790                        'per_page'          => 20,
     3791                        'max'               => 0,
     3792                )
     3793        );
    39963794
    39973795        /**
    39983796         * Constructor method.
    class BP_Groups_Membership_Requests_Template { 
    40283826                        'page'     => 1,
    40293827                        'max'      => false,
    40303828                        'type'     => 'first_joined',
     3829                        'page_arg' => 'mrpage',
    40313830                ) );
    40323831
    4033                 $this->pag_page = isset( $_REQUEST['mrpage'] ) ? intval( $_REQUEST['mrpage'] ) : $r['page'];
    4034                 $this->pag_num  = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $r['per_page'];
     3832                $this->set_loop( array( 'params' => $r ) );
     3833        }
    40353834
     3835        /**
     3836         * Get Group Membership requests.
     3837         *
     3838         * @since BuddyPress (2.3.0)
     3839         *
     3840         * @access public
     3841         */
     3842        public function get_items() {
    40363843                $mquery = new BP_Group_Member_Query( array(
    4037                         'group_id' => $r['group_id'],
    4038                         'type'     => $r['type'],
    4039                         'per_page' => $this->pag_num,
    4040                         'page'     => $this->pag_page,
     3844                        'group_id' => $this->args['params']['group_id'],
     3845                        'type'     => $this->args['params']['type'],
     3846                        'per_page' => $this->per_page,
     3847                        'page'     => $this->page,
    40413848
    40423849                        // These filters ensure we only get pending requests
    40433850                        'is_confirmed' => false,
    40443851                        'inviter_id'   => 0,
    40453852                ) );
    40463853
    4047                 $this->requests      = array_values( $mquery->results );
    4048                 $this->request_count = count( $this->requests );
     3854                // Make sure to return an array using the keys the abastract class is waiting for
     3855                $requests = array( 'requests' => array_values( $mquery->results ), 'total' => $mquery->total_users );
    40493856
    40503857                // Compatibility with legacy format of request data objects
    4051                 foreach ( $this->requests as $rk => $rv ) {
     3858                foreach ( $requests['requests'] as $rk => $rv ) {
    40523859                        // For legacy reasons, the 'id' property of each
    40533860                        // request must match the membership id, not the ID of
    40543861                        // the user (as it's returned by BP_Group_Member_Query)
    4055                         $this->requests[ $rk ]->user_id = $rv->ID;
    4056                         $this->requests[ $rk ]->id      = $rv->membership_id;
     3862                        $requests['requests'][ $rk ]->user_id = $rv->ID;
     3863                        $requests['requests'][ $rk ]->id      = $rv->membership_id;
    40573864
    40583865                        // Miscellaneous values
    4059                         $this->requests[ $rk ]->group_id   = $r['group_id'];
    4060                 }
    4061 
    4062                 if ( !$r['max'] || $r['max'] >= (int) $mquery->total_users )
    4063                         $this->total_request_count = (int) $mquery->total_users;
    4064                 else
    4065                         $this->total_request_count = (int) $r['max'];
    4066 
    4067                 if ( $r['max'] ) {
    4068                         if ( $r['max'] >= count($this->requests) )
    4069                                 $this->request_count = count($this->requests);
    4070                         else
    4071                                 $this->request_count = (int) $r['max'];
    4072                 } else {
    4073                         $this->request_count = count($this->requests);
    4074                 }
    4075 
    4076                 $this->pag_links = paginate_links( array(
    4077                         'base'      => add_query_arg( 'mrpage', '%#%' ),
    4078                         'format'    => '',
    4079                         'total'     => ceil( $this->total_request_count / $this->pag_num ),
    4080                         'current'   => $this->pag_page,
    4081                         'prev_text' => '&larr;',
    4082                         'next_text' => '&rarr;',
    4083                         'mid_size'  => 1,
    4084                         'add_args'  => array(),
    4085                 ) );
    4086         }
    4087 
    4088         function has_requests() {
    4089                 if ( $this->request_count )
    4090                         return true;
    4091 
    4092                 return false;
    4093         }
    4094 
    4095         function next_request() {
    4096                 $this->current_request++;
    4097                 $this->request = $this->requests[$this->current_request];
    4098 
    4099                 return $this->request;
    4100         }
    4101 
    4102         function rewind_requests() {
    4103                 $this->current_request = -1;
    4104 
    4105                 if ( $this->request_count > 0 )
    4106                         $this->request = $this->requests[0];
    4107         }
    4108 
    4109         function requests() {
    4110                 if ( $this->current_request + 1 < $this->request_count ) {
    4111                         return true;
    4112                 } elseif ( $this->current_request + 1 == $this->request_count ) {
    4113                         do_action('group_request_loop_end');
    4114                         // Do some cleaning up after the loop
    4115                         $this->rewind_requests();
     3866                        $requests['requests'][ $rk ]->group_id   = $this->args['params']['group_id'];
    41163867                }
    41173868
    4118                 $this->in_the_loop = false;
    4119                 return false;
    4120         }
    4121 
    4122         function the_request() {
    4123                 $this->in_the_loop = true;
    4124                 $this->request     = $this->next_request();
    4125 
    4126                 if ( 0 == $this->current_request ) // loop has just started
    4127                         do_action('group_request_loop_start');
     3869                return $requests;
    41283870        }
    41293871}
    41303872
    function bp_group_has_membership_requests( $args = '' ) { 
    41523894        $r = wp_parse_args( $args, $defaults );
    41533895
    41543896        $requests_template = new BP_Groups_Membership_Requests_Template( $r );
    4155         return apply_filters( 'bp_group_has_membership_requests', $requests_template->has_requests(), $requests_template );
     3897        return apply_filters( 'bp_group_has_membership_requests', $requests_template->has_items(), $requests_template );
    41563898}
    41573899
    41583900function bp_group_membership_requests() {
    41593901        global $requests_template;
    41603902
    4161         return $requests_template->requests();
     3903        return $requests_template->items();
    41623904}
    41633905
    41643906function bp_group_the_membership_request() {
    41653907        global $requests_template;
    41663908
    4167         return $requests_template->the_request();
     3909        return $requests_template->the_item();
    41683910}
    41693911
    41703912function bp_group_request_user_avatar_thumb() {
    function bp_group_requests_pagination_count() { 
    42503992        function bp_get_group_requests_pagination_count() {
    42513993                global $requests_template;
    42523994
    4253                 $start_num = intval( ( $requests_template->pag_page - 1 ) * $requests_template->pag_num ) + 1;
     3995                $start_num = intval( ( $requests_template->page - 1 ) * $requests_template->per_page ) + 1;
    42543996                $from_num  = bp_core_number_format( $start_num );
    4255                 $to_num    = bp_core_number_format( ( $start_num + ( $requests_template->pag_num - 1 ) > $requests_template->total_request_count ) ? $requests_template->total_request_count : $start_num + ( $requests_template->pag_num - 1 ) );
     3997                $to_num    = bp_core_number_format( ( $start_num + ( $requests_template->per_page - 1 ) > $requests_template->total_request_count ) ? $requests_template->total_request_count : $start_num + ( $requests_template->per_page - 1 ) );
    42563998                $total     = bp_core_number_format( $requests_template->total_request_count );
    42573999
    42584000                return apply_filters( 'bp_get_group_requests_pagination_count', sprintf( _n( 'Viewing 1 request', 'Viewing %1$s - %2$s of %3$s requests', $total, 'buddypress' ), $from_num, $to_num, $total ), $from_num, $to_num, $total );
    function bp_group_requests_pagination_count() { 
    42624004 * Invite Friends Template Tags
    42634005 **/
    42644006
    4265 class BP_Groups_Invite_Template {
    4266         var $current_invite = -1;
    4267         var $invite_count;
    4268         var $invites;
    4269         var $invite;
     4007class BP_Groups_Invite_Template extends BP_Template_Loop {
    42704008
    4271         var $in_the_loop;
    4272 
    4273         var $pag_page;
    4274         var $pag_num;
    4275         var $pag_links;
    4276         var $total_invite_count;
     4009        /**
     4010         * Default arguments for Groups Invite loop.
     4011         *
     4012         * @since BuddyPress (2.3.0)
     4013         *
     4014         * @access protected
     4015         * @var array
     4016         */
     4017        protected $default_args = array(
     4018                'labels' => array(
     4019                        'item'              => 'invite',
     4020                        'item_plural'       => 'invites',
     4021                        'item_total_count'  => 'total_invite_count',
     4022                        'item_count'        => 'invite_count',
     4023                        'current_item'      => 'current_invite',
     4024                        'hook_prefix'       => 'group_invite',
     4025                ),
     4026                'params' => array(
     4027                        'page_arg'          => 'invitepage',
     4028                        'page'              => 1,
     4029                        'per_page'          => 10,
     4030                        'max'               => 0,
     4031                )
     4032        );
    42774033
     4034        /**
     4035         * Constructor.
     4036         *
     4037         *  @param array $args {
     4038         *     An array of optional arguments.
     4039         *     @type int $user_id ID of the user
     4040         *     @type int $group_id ID of the group
     4041         *     @type int $page Page of results to be queried. Default: 1.
     4042         *     @type int $per_page Number of items to return per page of
     4043         *           results. Default: 10.
     4044         *     @type string page_arg Optional.
     4045         * }
     4046         */
    42784047        public function __construct( $args = array() ) {
    42794048
    42804049                // Backward compatibility with old method of passing arguments
    class BP_Groups_Invite_Template { 
    42954064                        'group_id' => bp_get_current_group_id(),
    42964065                        'page'     => 1,
    42974066                        'per_page' => 10,
     4067                        'page_arg' => 'invitepage',
    42984068                ) );
    42994069
    4300                 $this->pag_num  = intval( $r['per_page'] );
    4301                 $this->pag_page = isset( $_REQUEST['invitepage'] ) ? intval( $_REQUEST['invitepage'] ) : $r['page'];
     4070                $this->set_loop( array( 'params' => $r ) );
     4071        }
    43024072
     4073        /**
     4074         * Get Groups Invite.
     4075         *
     4076         * @since BuddyPress (2.3.0)
     4077         *
     4078         * @access public
     4079         */
     4080        public function get_items() {
    43034081                $iquery = new BP_Group_Member_Query( array(
    4304                         'group_id' => $r['group_id'],
     4082                        'group_id' => $this->args['params']['group_id'],
    43054083                        'type'     => 'first_joined',
    4306                         'per_page' => $this->pag_num,
    4307                         'page'     => $this->pag_page,
     4084                        'per_page' => $this->per_page,
     4085                        'page'     => $this->page,
    43084086
    43094087                        // These filters ensure we get only pending invites
    43104088                        'is_confirmed' => false,
    4311                         'inviter_id'   => $r['user_id'],
     4089                        'inviter_id'   => $this->args['params']['user_id'],
    43124090                ) );
    4313                 $this->invite_data = $iquery->results;
    4314 
    4315                 $this->total_invite_count = $iquery->total_users;
    4316                 $this->invites            = array_values( wp_list_pluck( $this->invite_data, 'ID' ) );
    4317                 $this->invite_count       = count( $this->invites );
    4318 
    4319                 // If per_page is set to 0 (show all results), don't generate
    4320                 // pag_links
    4321                 if ( ! empty( $this->pag_num ) ) {
    4322                         $this->pag_links = paginate_links( array(
    4323                                 'base'      => add_query_arg( 'invitepage', '%#%' ),
    4324                                 'format'    => '',
    4325                                 'total'     => ceil( $this->total_invite_count / $this->pag_num ),
    4326                                 'current'   => $this->pag_page,
    4327                                 'prev_text' => '&larr;',
    4328                                 'next_text' => '&rarr;',
    4329                                 'mid_size'  => 1,
    4330                                 'add_args'  => array(),
    4331                         ) );
    4332                 } else {
    4333                         $this->pag_links = '';
    4334                 }
    4335         }
    4336 
    4337         function has_invites() {
    4338                 if ( $this->invite_count )
    4339                         return true;
    4340 
    4341                 return false;
    4342         }
    4343 
    4344         function next_invite() {
    4345                 $this->current_invite++;
    4346                 $this->invite = $this->invites[$this->current_invite];
    43474091
    4348                 return $this->invite;
    4349         }
     4092                // This will be use in BP_Groups_Invite_Template->the_invite()
     4093                $this->invite_data = $iquery->results;
    43504094
    4351         function rewind_invites() {
    4352                 $this->current_invite = -1;
    4353                 if ( $this->invite_count > 0 )
    4354                         $this->invite = $this->invites[0];
     4095                return array( 'invites' => array_values( wp_list_pluck( $this->invite_data, 'ID' ) ), 'total' => $iquery->total_users );
    43554096        }
    43564097
    4357         function invites() {
    4358                 if ( $this->current_invite + 1 < $this->invite_count ) {
    4359                         return true;
    4360                 } elseif ( $this->current_invite + 1 == $this->invite_count ) {
    4361                         do_action('loop_end');
    4362                         // Do some cleaning up after the loop
    4363                         $this->rewind_invites();
    4364                 }
    4365 
    4366                 $this->in_the_loop = false;
    4367                 return false;
     4098        /**
     4099         * Override parent method as we need to do custom work.
     4100         *
     4101         * @since BuddyPress (2.3.0)
     4102         *
     4103         * @uses  BP_Groups_Invite_Template->the_invite()
     4104         */
     4105        public function the_item() {
     4106                $this->the_invite();
    43684107        }
    43694108
    4370         function the_invite() {
     4109        /**
     4110         * Set up the current invite inside the loop.
     4111         */
     4112        public function the_invite() {
    43714113                global $group_id;
    43724114                $this->in_the_loop      = true;
    4373                 $user_id                = $this->next_invite();
     4115                $user_id                = $this->next_item();
    43744116
    43754117                $this->invite           = new stdClass;
    43764118                $this->invite->user     = $this->invite_data[ $user_id ];
    class BP_Groups_Invite_Template { 
    43954137                        $this->invite->user->total_groups = sprintf( _n( '%d group', '%d groups', $total_groups, 'buddypress' ), $total_groups );
    43964138                }
    43974139
    4398                 if ( bp_is_active( 'friends' ) ) {
    4399                         $this->invite->user->total_friends = BP_Friends_Friendship::total_friend_count( $user_id );
    4400                 }
    4401 
    44024140                $this->invite->user->total_blogs = null;
    44034141
    44044142                $this->invite->group_id = $group_id; // Globaled in bp_group_has_invites()
    44054143
    4406                 if ( 0 == $this->current_invite ) // loop has just started
    4407                         do_action('loop_start');
     4144                if ( 0 == $this->current_invite ) {
     4145                        do_action( 'group_invite_loop_start' );
     4146                }
    44084147        }
    44094148}
    44104149
    function bp_group_has_invites( $args = '' ) { 
    44364175        }
    44374176
    44384177        $invites_template = new BP_Groups_Invite_Template( $r );
    4439         return apply_filters( 'bp_group_has_invites', $invites_template->has_invites(), $invites_template );
     4178        return apply_filters( 'bp_group_has_invites', $invites_template->has_items(), $invites_template );
    44404179}
    44414180
    44424181function bp_group_invites() {
    44434182        global $invites_template;
    44444183
    4445         return $invites_template->invites();
     4184        return $invites_template->items();
    44464185}
    44474186
    44484187function bp_group_the_invite() {
    44494188        global $invites_template;
    44504189
    4451         return $invites_template->the_invite();
     4190        return $invites_template->the_item();
    44524191}
    44534192
    44544193function bp_group_invite_item_id() {
    function bp_group_invite_pagination_count() { 
    45424281        function bp_get_group_invite_pagination_count() {
    45434282                global $invites_template;
    45444283
    4545                 $start_num = intval( ( $invites_template->pag_page - 1 ) * $invites_template->pag_num ) + 1;
     4284                $start_num = intval( ( $invites_template->page - 1 ) * $invites_template->per_page ) + 1;
    45464285                $from_num  = bp_core_number_format( $start_num );
    4547                 $to_num    = bp_core_number_format( ( $start_num + ( $invites_template->pag_num - 1 ) > $invites_template->total_invite_count ) ? $invites_template->total_invite_count : $start_num + ( $invites_template->pag_num - 1 ) );
     4286                $to_num    = bp_core_number_format( ( $start_num + ( $invites_template->per_page - 1 ) > $invites_template->total_invite_count ) ? $invites_template->total_invite_count : $start_num + ( $invites_template->per_page - 1 ) );
    45484287                $total     = bp_core_number_format( $invites_template->total_invite_count );
    45494288
    45504289                return apply_filters( 'bp_get_groups_pagination_count', sprintf( _n( 'Viewing 1 invitation', 'Viewing %1$s - %2$s of %3$s invitations', $total, 'buddypress' ), $from_num, $to_num, $total ), $from_num, $to_num, $total );
  • src/bp-members/bp-members-template.php

    diff --git src/bp-members/bp-members-template.php src/bp-members/bp-members-template.php
    index feb679f..c95d85f 100644
    function bp_activate_slug() { 
    180180 *
    181181 * Responsible for loading a group of members into a loop for display.
    182182 */
    183 class BP_Core_Members_Template {
     183class BP_Core_Members_Template extends BP_Template_Loop {
    184184
    185185        /**
    186          * The loop iterator.
     186         * Default arguments for Members loop.
    187187         *
    188          * @access public
    189          * @var int
    190          */
    191         var $current_member = -1;
    192 
    193         /**
    194          * The number of members returned by the paged query.
     188         * @since BuddyPress (2.3.0)
    195189         *
    196          * @access public
    197          * @var int
    198          */
    199         var $member_count;
    200 
    201         /**
    202          * Array of members located by the query.
    203          *
    204          * @access public
     190         * @access protected
    205191         * @var array
    206192         */
    207         var $members;
    208 
    209         /**
    210          * The member object currently being iterated on.
    211          *
    212          * @access public
    213          * @var object
    214          */
    215         var $member;
    216 
    217         /**
    218          * A flag for whether the loop is currently being iterated.
    219          *
    220          * @access public
    221          * @var bool
    222          */
    223         var $in_the_loop;
    224 
    225         /**
    226          * The page number being requested.
    227          *
    228          * @access public
    229          * @var public
    230          */
    231         var $pag_page;
    232 
    233         /**
    234          * The number of items being requested per page.
    235          *
    236          * @access public
    237          * @var public
    238          */
    239         var $pag_num;
    240 
    241         /**
    242          * An HTML string containing pagination links.
    243          *
    244          * @access public
    245          * @var string
    246          */
    247         var $pag_links;
    248 
    249         /**
    250          * The total number of members matching the query parameters.
    251          *
    252          * @access public
    253          * @var int
    254          */
    255         var $total_member_count;
     193        protected $default_args = array(
     194                'labels' => array(
     195                        'item'              => 'member',
     196                        'item_plural'       => 'members',
     197                        'item_total_count'  => 'total_member_count',
     198                        'item_count'        => 'member_count',
     199                        'current_item'      => 'current_member',
     200                        'hook_prefix'       => 'member',
     201                ),
     202                'params' => array(
     203                        'page_arg'          => 'upage',
     204                        'page'              => 1,
     205                        'per_page'          => 20,
     206                        'max'               => 0,
     207                )
     208        );
    256209
    257210        /**
    258211         * Constructor method.
    class BP_Core_Members_Template { 
    275228         * @param array|string $member_type     Array or comma-separated string of member types to limit results to.
    276229         */
    277230        function __construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude, $meta_key, $meta_value, $page_arg = 'upage', $member_type = '' ) {
     231                $params = array( 'params' => array(
     232                        'type'            => $type,
     233                        'per_page'        => $per_page,
     234                        'page'            => $page_number,
     235                        'user_id'         => $user_id,
     236                        'include'         => $include,
     237                        'search_terms'    => $search_terms,
     238                        'populate_extras' => $populate_extras,
     239                        'exclude'         => $exclude,
     240                        'meta_key'        => $meta_key,
     241                        'meta_value'      => $meta_value,
     242                        'member_type'     => $member_type,
     243                        'page_arg'        => $page_arg,
     244                        'max'             => $max,
     245                ) );
    278246
    279                 $this->pag_page = !empty( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : (int) $page_number;
    280                 $this->pag_num  = !empty( $_REQUEST['num'] )   ? intval( $_REQUEST['num'] )   : (int) $per_page;
    281                 $this->type     = $type;
    282 
    283                 if ( !empty( $_REQUEST['letter'] ) )
    284                         $this->members = BP_Core_User::get_users_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page, $populate_extras, $exclude );
    285                 else
    286                         $this->members = bp_core_get_users( array( 'type' => $this->type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'include' => $include, 'search_terms' => $search_terms, 'populate_extras' => $populate_extras, 'exclude' => $exclude, 'meta_key' => $meta_key, 'meta_value' => $meta_value, 'member_type' => $member_type ) );
    287 
    288                 if ( !$max || $max >= (int) $this->members['total'] )
    289                         $this->total_member_count = (int) $this->members['total'];
    290                 else
    291                         $this->total_member_count = (int) $max;
    292 
    293                 $this->members = $this->members['users'];
    294 
    295                 if ( $max ) {
    296                         if ( $max >= count( $this->members ) ) {
    297                                 $this->member_count = count( $this->members );
    298                         } else {
    299                                 $this->member_count = (int) $max;
    300                         }
    301                 } else {
    302                         $this->member_count = count( $this->members );
    303                 }
    304 
    305                 if ( (int) $this->total_member_count && (int) $this->pag_num ) {
    306                         $pag_args = array(
    307                                 $page_arg => '%#%',
    308                         );
    309 
    310                         if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
    311                                 $base = remove_query_arg( 's', wp_get_referer() );
    312                         } else {
    313                                 $base = '';
    314                         }
    315 
    316                         /**
    317                          * Defaults to an empty array to make sure paginate_links()
    318                          * won't add the $page_arg to the links which would break
    319                          * pagination in case javascript is disabled.
    320                          */
    321                         $add_args = array();
    322 
    323                         if ( ! empty( $search_terms ) ) {
    324                                 $add_args['s'] = urlencode( $search_terms );
    325                         }
    326 
    327                         $this->pag_links = paginate_links( array(
    328                                 'base'      => add_query_arg( $pag_args, $base ),
    329                                 'format'    => '',
    330                                 'total'     => ceil( (int) $this->total_member_count / (int) $this->pag_num ),
    331                                 'current'   => (int) $this->pag_page,
    332                                 'prev_text' => _x( '&larr;', 'Member pagination previous text', 'buddypress' ),
    333                                 'next_text' => _x( '&rarr;', 'Member pagination next text', 'buddypress' ),
    334                                 'mid_size'  => 1,
    335                                 'add_args'  => $add_args,
    336                         ) );
    337                 }
     247                $this->set_loop( $params );
    338248        }
    339249
    340250        /**
    341          * Whether there are members available in the loop.
     251         * Get Members.
    342252         *
    343          * @see bp_has_members()
     253         * @since BuddyPress (2.3.0)
    344254         *
    345          * @return bool True if there are items in the loop, otherwise false.
     255         * @access public
    346256         */
    347         function has_members() {
    348                 if ( $this->member_count )
    349                         return true;
     257        public function get_items() {
     258                if ( ! empty( $_REQUEST['letter'] ) ) {
     259                        $members = BP_Core_User::get_users_by_letter(
     260                                $_REQUEST['letter'],
     261                                $this->args['params']['per_page'],
     262                                $this->args['params']['page'],
     263                                $this->args['params']['populate_extras'],
     264                                $this->args['params']['exclude']
     265                        );
     266
     267                } else {
     268                        $members = bp_core_get_users( $this->args['params'] );
     269                }
    350270
    351                 return false;
     271                return array( 'members' => $members['users'] , 'total' => $members['total'] );
    352272        }
    353273
    354274        /**
    355          * Set up the next member and iterate index.
     275         * Set Members pagination.
    356276         *
    357          * @return object The next member to iterate over.
     277         * @since BuddyPress (2.3.0)
     278         *
     279         * @access public
    358280         */
    359         function next_member() {
    360                 $this->current_member++;
    361                 $this->member = $this->members[$this->current_member];
    362 
    363                 return $this->member;
    364         }
     281        public function set_pag_links() {
     282                $pag_args = array(
     283                        $this->page_arg => '%#%'
     284                );
    365285
    366         /**
    367          * Rewind the members and reset member index.
    368          */
    369         function rewind_members() {
    370                 $this->current_member = -1;
    371                 if ( $this->member_count > 0 ) {
    372                         $this->member = $this->members[0];
     286                if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
     287                        $base = remove_query_arg( 's', wp_get_referer() );
     288                } else {
     289                        $base = '';
    373290                }
    374         }
    375291
    376         /**
    377          * Whether there are members left in the loop to iterate over.
    378          *
    379          * This method is used by {@link bp_members()} as part of the while loop
    380          * that controls iteration inside the members loop, eg:
    381          *     while ( bp_members() ) { ...
    382          *
    383          * @see bp_members()
    384          *
    385          * @return bool True if there are more members to show, otherwise false.
    386          */
    387         function members() {
    388                 if ( $this->current_member + 1 < $this->member_count ) {
    389                         return true;
    390                 } elseif ( $this->current_member + 1 == $this->member_count ) {
     292                /**
     293                 * Defaults to an empty array to make sure paginate_links()
     294                 * won't add the $page_arg to the links which would break
     295                 * pagination in case javascript is disabled.
     296                 */
     297                $add_args = array();
    391298
    392                         /**
    393                          * Fires right before the rewinding of members listing.
    394                          *
    395                          * @since BuddyPress (1.5.0)
    396                          */
    397                         do_action('member_loop_end');
    398                         // Do some cleaning up after the loop
    399                         $this->rewind_members();
     299                if ( ! empty( $this->search_terms ) ) {
     300                        $add_args['s'] = urlencode( $this->search_terms );
    400301                }
    401302
    402                 $this->in_the_loop = false;
    403                 return false;
     303                $this->pag_links = paginate_links( array(
     304                        'base'      => add_query_arg( $pag_args, $base ),
     305                        'format'    => '',
     306                        'total'     => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ),
     307                        'current'   => (int) $this->page,
     308                        'prev_text' => _x( '&larr;', 'Member pagination previous text', 'buddypress' ),
     309                        'next_text' => _x( '&rarr;', 'Member pagination next text', 'buddypress' ),
     310                        'mid_size'  => 1,
     311                        'add_args'  => $add_args,
     312                ) );
    404313        }
    405314
    406315        /**
    407          * Set up the current member inside the loop.
    408          *
    409          * Used by {@link bp_the_member()} to set up the current member data
    410          * while looping, so that template tags used during that iteration make
    411          * reference to the current member.
     316         * Rewind Members is an alias of rewind_items()
    412317         *
    413          * @see bp_the_member()
     318         * @access public
    414319         */
    415         function the_member() {
    416 
    417                 $this->in_the_loop = true;
    418                 $this->member      = $this->next_member();
    419 
    420                 // loop has just started
    421                 if ( 0 == $this->current_member ) {
    422 
    423                         /**
    424                          * Fires if the current member is the first in the loop.
    425                          *
    426                          * @since BuddyPress (1.5.0)
    427                          */
    428                         do_action( 'member_loop_start' );
    429                 }
    430 
     320        public function rewind_members() {
     321                _doing_it_wrong( __FUNCTION__, __( 'BP_Core_Members_Template->rewind_members() should not be used directly, please use bp_rewind_members()', 'buddypress' ), '2.3.0' );
     322                $this->rewind_items();
    431323        }
    432324}
    433325
    class BP_Core_Members_Template { 
    437329function bp_rewind_members() {
    438330        global $members_template;
    439331
    440         return $members_template->rewind_members();
     332        return $members_template->rewind_items();
    441333}
    442334
    443335/**
    function bp_has_members( $args = '' ) { 
    559451         * @param bool  $value            Whether or not there are members to iterate over.
    560452         * @param array $members_template Populated $members_template global.
    561453         */
    562         return apply_filters( 'bp_has_members', $members_template->has_members(), $members_template );
     454        return apply_filters( 'bp_has_members', $members_template->has_items(), $members_template );
    563455}
    564456
    565457/**
    function bp_has_members( $args = '' ) { 
    569461 */
    570462function bp_the_member() {
    571463        global $members_template;
    572         return $members_template->the_member();
     464        return $members_template->the_item();
    573465}
    574466
    575467/**
    function bp_the_member() { 
    579471 */
    580472function bp_members() {
    581473        global $members_template;
    582         return $members_template->members();
     474        return $members_template->items();
    583475}
    584476
    585477/**
    function bp_members_pagination_count() { 
    596488        function bp_get_members_pagination_count() {
    597489                global $members_template;
    598490
    599                 if ( empty( $members_template->type ) )
     491                if ( empty( $members_template->type ) ) {
    600492                        $members_template->type = '';
     493                }
    601494
    602                 $start_num = intval( ( $members_template->pag_page - 1 ) * $members_template->pag_num ) + 1;
     495                $start_num = intval( ( $members_template->page - 1 ) * $members_template->per_page ) + 1;
    603496                $from_num  = bp_core_number_format( $start_num );
    604                 $to_num    = bp_core_number_format( ( $start_num + ( $members_template->pag_num - 1 ) > $members_template->total_member_count ) ? $members_template->total_member_count : $start_num + ( $members_template->pag_num - 1 ) );
     497                $to_num    = bp_core_number_format( ( $start_num + ( $members_template->per_page - 1 ) > $members_template->total_member_count ) ? $members_template->total_member_count : $start_num + ( $members_template->per_page - 1 ) );
    605498                $total     = bp_core_number_format( $members_template->total_member_count );
    606499
    607                 if ( 'active' == $members_template->type )
     500                if ( 'active' == $members_template->type ){
    608501                        $pag = sprintf( _n( 'Viewing 1 active member', 'Viewing %1$s - %2$s of %3$s active members', $members_template->total_member_count, 'buddypress' ), $from_num, $to_num, $total );
    609                 else if ( 'popular' == $members_template->type )
     502                } else if ( 'popular' == $members_template->type ) {
    610503                        $pag = sprintf( _n( 'Viewing 1 member with friends', 'Viewing %1$s - %2$s of %3$s members with friends', $members_template->total_member_count, 'buddypress' ), $from_num, $to_num, $total );
    611                 else if ( 'online' == $members_template->type )
     504                } else if ( 'online' == $members_template->type ) {
    612505                        $pag = sprintf( _n( 'Viewing 1 online member', 'Viewing %1$s - %2$s of %3$s online members', $members_template->total_member_count, 'buddypress' ), $from_num, $to_num, $total );
    613                 else
     506                } else {
    614507                        $pag = sprintf( _n( 'Viewing 1 member', 'Viewing %1$s - %2$s of %3$s members', $members_template->total_member_count, 'buddypress' ), $from_num, $to_num, $total );
     508                }
    615509
    616510                /**
    617511                 * Filters the members pagination count.
  • src/bp-messages/bp-messages-template.php

    diff --git src/bp-messages/bp-messages-template.php src/bp-messages/bp-messages-template.php
    index fe9e1bd..4ab9c7d 100644
    if ( !defined( 'ABSPATH' ) ) exit; 
    1313/**
    1414 * Message Box Template Class
    1515 */
    16 class BP_Messages_Box_Template {
     16class BP_Messages_Box_Template extends BP_Template_Loop {
    1717
    1818        /**
    19          * The loop iterator.
     19         * Default arguments for Messages Box loop.
    2020         *
    21          * @access public
    22          * @var int
    23          */
    24         public $current_thread = -1;
    25 
    26         /**
    27          * The number of threads returned by the paged query.
     21         * @since BuddyPress (2.3.0)
    2822         *
    29          * @access public
    30          * @var int
    31          */
    32         public $current_thread_count = 0;
    33 
    34         /**
    35          * Total number of threads matching the query params.
    36          *
    37          * @access public
    38          * @var int
    39          */
    40         public $total_thread_count = 0;
    41 
    42         /**
    43          * Array of threads located by the query.
    44          *
    45          * @access public
     23         * @access protected
    4624         * @var array
    4725         */
    48         public $threads = array();
    49 
    50         /**
    51          * The thread object currently being iterated on.
    52          *
    53          * @access public
    54          * @var object
    55          */
    56         public $thread = false;
    57 
    58         /**
    59          * A flag for whether the loop is currently being iterated.
    60          *
    61          * @access public
    62          * @var bool
    63          */
    64         public $in_the_loop = false;
    65 
    66         /**
    67          * User ID of the current inbox.
    68          *
    69          * @access public
    70          * @var int
    71          */
    72         public $user_id = 0;
    73 
    74         /**
    75          * The current "box" view ('notices', 'sentbox', 'inbox')
    76          *
    77          * @access public
    78          * @var string
    79          */
    80         public $box = 'inbox';
    81 
    82         /**
    83          * The page number being requested.
    84          *
    85          * @access public
    86          * @var int
    87          */
    88         public $pag_page = 1;
    89 
    90         /**
    91          * The number of items being requested per page.
    92          *
    93          * @access public
    94          * @var int
    95          */
    96         public $pag_num = 10;
    97 
    98         /**
    99          * An HTML string containing pagination links.
    100          *
    101          * @access public
    102          * @var string
    103          */
    104         public $pag_links = '';
    105 
    106         /**
    107          * Search terms for limiting the thread query.
    108          *
    109          * @access public
    110          * @var string
    111          */
    112         public $search_terms = '';
     26        protected $default_args = array(
     27                'labels' => array(
     28                        'item'              => 'thread',
     29                        'item_plural'       => 'threads',
     30                        'item_total_count'  => 'total_thread_count',
     31                        'item_count'        => 'thread_count',
     32                        'current_item'      => 'current_thread',
     33                        'hook_prefix'       => 'messages_box',
     34                ),
     35                'params' => array(
     36                        'page_arg'          => 'mpage',
     37                        'page'              => 1,
     38                        'per_page'          => 20,
     39                        'max'               => 0,
     40                )
     41        );
    11342
    11443        /**
    11544         * Constructor method.
    class BP_Messages_Box_Template { 
    12756         *        parameter. Default: 'mpage'.
    12857         */
    12958        public function __construct( $user_id, $box, $per_page, $max, $type, $search_terms, $page_arg = 'mpage' ) {
    130                 $this->pag_page = isset( $_GET[$page_arg] ) ? intval( $_GET[$page_arg] ) : 1;
    131                 $this->pag_num  = isset( $_GET['num'] )   ? intval( $_GET['num'] )   : $per_page;
    132 
    133                 $this->user_id      = $user_id;
    134                 $this->box          = $box;
    135                 $this->type         = $type;
    136                 $this->search_terms = $search_terms;
    137 
    138                 if ( 'notices' == $this->box ) {
    139                         $this->threads = BP_Messages_Notice::get_notices( array(
    140                                 'pag_num'  => $this->pag_num,
    141                                 'pag_page' => $this->pag_page
    142                         ) );
    143                 } else {
    144                         $threads = BP_Messages_Thread::get_current_threads_for_user( $this->user_id, $this->box, $this->type, $this->pag_num, $this->pag_page, $this->search_terms );
    145 
    146                         $this->threads            = $threads['threads'];
    147                         $this->total_thread_count = $threads['total'];
    148                 }
    149 
    150                 if ( !$this->threads ) {
    151                         $this->thread_count       = 0;
    152                         $this->total_thread_count = 0;
    153                 } else {
    154                         $total_notice_count = BP_Messages_Notice::get_total_notice_count();
     59                $page = 1;
    15560
    156                         if ( !$max || $max >= (int) $total_notice_count ) {
    157                                 if ( 'notices' == $this->box ) {
    158                                         $this->total_thread_count = (int) $total_notice_count;
    159                                 }
    160                         } else {
    161                                 $this->total_thread_count = (int) $max;
    162                         }
    163 
    164                         if ( $max ) {
    165                                 if ( $max >= count( $this->threads ) ) {
    166                                         $this->thread_count = count( $this->threads );
    167                                 } else {
    168                                         $this->thread_count = (int) $max;
    169                                 }
    170                         } else {
    171                                 $this->thread_count = count( $this->threads );
    172                         }
     61                if ( ! empty( $_GET[ $page_arg ] ) ) {
     62                        $page = $_GET[ $page_arg ];
    17363                }
    17464
    175                 if ( (int) $this->total_thread_count && (int) $this->pag_num ) {
    176                         $pag_args = array(
    177                                 $page_arg => '%#%',
    178                         );
    179 
    180                         if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
    181                                 $base = remove_query_arg( 's', wp_get_referer() );
    182                         } else {
    183                                 $base = '';
    184                         }
    185 
    186                         $add_args = array();
    187 
    188                         if ( ! empty( $this->search_terms ) ) {
    189                                 $add_args['s'] = $this->search_terms;
    190                         }
     65                $params = array( 'params' => array(
     66                        'user_id'         => $user_id,
     67                        'box'             => $box,
     68                        'per_page'        => $per_page,
     69                        'max'             => $max,
     70                        'type'            => $type,
     71                        'search_terms'    => $search_terms,
     72                        'page_arg'        => $page_arg,
     73                        'page'            => $page,
     74                ) );
    19175
    192                         $this->pag_links = paginate_links( array(
    193                                 'base'      => add_query_arg( $pag_args, $base ),
    194                                 'format'    => '',
    195                                 'total'     => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),
    196                                 'current'   => $this->pag_page,
    197                                 'prev_text' => _x( '&larr;', 'Message pagination previous text', 'buddypress' ),
    198                                 'next_text' => _x( '&rarr;', 'Message pagination next text', 'buddypress' ),
    199                                 'mid_size'  => 1,
    200                                 'add_args'  => $add_args,
    201                         ) );
    202                 }
     76                $this->set_loop( $params );
    20377        }
    20478
    20579        /**
    206          * Whether there are threads available in the loop.
     80         * Get Threads.
    20781         *
    208          * @see bp_has_message_threads()
     82         * @since BuddyPress (2.3.0)
    20983         *
    210          * @return bool True if there are items in the loop, otherwise false.
     84         * @access public
    21185         */
    212         public function has_threads() {
    213                 if ( $this->thread_count ) {
    214                         return true;
     86        public function get_items() {
     87                if ( 'notices' == $this->args['params']['box'] ) {
     88                        $threads = array(
     89                                'threads' => BP_Messages_Notice::get_notices( array(
     90                                        'pag_num'  => $this->per_page,
     91                                        'pag_page' => $this->page
     92                                ) ),
     93                                'total' => BP_Messages_Notice::get_total_notice_count(),
     94                        );
     95
     96                } else {
     97                        $threads = BP_Messages_Thread::get_current_threads_for_user(
     98                                $this->args['params']['user_id'],
     99                                $this->args['params']['box'],
     100                                $this->args['params']['type'],
     101                                $this->per_page,
     102                                $this->page,
     103                                $this->args['params']['search_terms']
     104                        );
    215105                }
    216106
    217                 return false;
     107                return $threads;
    218108        }
    219109
    220110        /**
    221          * Set up the next member and iterate index.
     111         * Set Threads pagination.
     112         *
     113         * @since BuddyPress (2.3.0)
    222114         *
    223          * @return object The next member to iterate over.
     115         * @access public
    224116         */
    225         public function next_thread() {
    226                 $this->current_thread++;
    227                 $this->thread = $this->threads[$this->current_thread];
     117        public function set_pag_links() {
     118                $pag_args = array(
     119                        $this->page_arg => '%#%'
     120                );
    228121
    229                 return $this->thread;
    230         }
     122                if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
     123                        $base = remove_query_arg( 's', wp_get_referer() );
     124                } else {
     125                        $base = '';
     126                }
    231127
    232         /**
    233          * Rewind the threads and reset thread index.
    234          */
    235         public function rewind_threads() {
    236                 $this->current_thread = -1;
    237                 if ( $this->thread_count > 0 ) {
    238                         $this->thread = $this->threads[0];
     128                /**
     129                 * Defaults to an empty array to make sure paginate_links()
     130                 * won't add the $page_arg to the links which would break
     131                 * pagination in case javascript is disabled.
     132                 */
     133                $add_args = array();
     134
     135                if ( ! empty( $this->search_terms ) ) {
     136                        $add_args['s'] = urlencode( $this->search_terms );
    239137                }
     138
     139                $this->pag_links = paginate_links( array(
     140                        'base'      => add_query_arg( $pag_args, $base ),
     141                        'format'    => '',
     142                        'total'     => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ),
     143                        'current'   => (int) $this->page,
     144                        'prev_text' => _x( '&larr;', 'Message pagination previous text', 'buddypress' ),
     145                        'next_text' => _x( '&rarr;', 'Message pagination next text', 'buddypress' ),
     146                        'mid_size'  => 1,
     147                        'add_args'  => $add_args,
     148                ) );
    240149        }
    241150
    242151        /**
    243          * Whether there are threads left in the loop to iterate over.
    244          *
    245          * This method is used by {@link bp_message_threads()} as part of the
    246          * while loop that controls iteration inside the threads loop, eg:
    247          *     while ( bp_message_threads() ) { ...
     152         * Override parent method as we need to do custom work.
    248153         *
    249          * @see bp_message_threads()
     154         * @since BuddyPress (2.3.0)
    250155         *
    251          * @return bool True if there are more threads to show, otherwise false.
     156         * @uses  BP_Messages_Box_Template->the_message_thread()
    252157         */
    253         function message_threads() {
    254                 if ( $this->current_thread + 1 < $this->thread_count ) {
    255                         return true;
    256                 } elseif ( $this->current_thread + 1 == $this->thread_count ) {
    257 
    258                         /**
    259                          * Fires when at the end of threads to iterate over.
    260                          *
    261                          * @since BuddyPress (1.5.0)
    262                          */
    263                         do_action( 'messages_box_loop_end' );
    264                         // Do some cleaning up after the loop
    265                         $this->rewind_threads();
    266                 }
    267 
    268                 $this->in_the_loop = false;
    269                 return false;
     158        public function the_item() {
     159                $this->the_message_thread();
    270160        }
    271161
    272162        /**
    class BP_Messages_Box_Template { 
    281171        public function the_message_thread() {
    282172
    283173                $this->in_the_loop = true;
    284                 $this->thread      = $this->next_thread();
     174                $this->thread      = $this->next_item();
    285175
    286176                if ( ! bp_is_current_action( 'notices' ) ) {
    287177                        $last_message_index     = count( $this->thread->messages ) - 1;
    288178                        $this->thread->messages = array_reverse( (array) $this->thread->messages );
    289179
    290180                        // Set up the last message data
    291                         if ( count($this->thread->messages) > 1 ) {
     181                        if ( count( $this->thread->messages ) > 1 ) {
    292182                                if ( 'inbox' == $this->box ) {
    293183                                        foreach ( (array) $this->thread->messages as $key => $message ) {
    294184                                                if ( bp_loggedin_user_id() != $message->sender_id ) {
    function bp_has_message_threads( $args = '' ) { 
    401291         * @param BP_Messages_Box_Template $messages_template Current message box template object.
    402292         * @param array                    $r                 Array of parsed arguments passed into function.
    403293         */
    404         return apply_filters( 'bp_has_message_threads', $messages_template->has_threads(), $messages_template, $r );
     294        return apply_filters( 'bp_has_message_threads', $messages_template->has_items(), $messages_template, $r );
    405295}
    406296
    407297/**
    function bp_has_message_threads( $args = '' ) { 
    411301 */
    412302function bp_message_threads() {
    413303        global $messages_template;
    414         return $messages_template->message_threads();
     304        return $messages_template->items();
    415305}
    416306
    417307/**
    function bp_message_threads() { 
    421311 */
    422312function bp_message_thread() {
    423313        global $messages_template;
    424         return $messages_template->the_message_thread();
     314        return $messages_template->the_item();
    425315}
    426316
    427317/**
    function bp_messages_pagination() { 
    1055945function bp_messages_pagination_count() {
    1056946        global $messages_template;
    1057947
    1058         $start_num = intval( ( $messages_template->pag_page - 1 ) * $messages_template->pag_num ) + 1;
     948        $start_num = intval( ( $messages_template->page - 1 ) * $messages_template->per_page ) + 1;
    1059949        $from_num  = bp_core_number_format( $start_num );
    1060         $to_num    = bp_core_number_format( ( $start_num + ( $messages_template->pag_num - 1 ) > $messages_template->total_thread_count ) ? $messages_template->total_thread_count : $start_num + ( $messages_template->pag_num - 1 ) );
     950        $to_num    = bp_core_number_format( ( $start_num + ( $messages_template->per_page - 1 ) > $messages_template->total_thread_count ) ? $messages_template->total_thread_count : $start_num + ( $messages_template->per_page - 1 ) );
    1061951        $total     = bp_core_number_format( $messages_template->total_thread_count );
    1062952
    1063953        echo sprintf( _n( 'Viewing 1 message', 'Viewing %1$s - %2$s of %3$s messages', $total, 'buddypress' ), $from_num, $to_num, number_format_i18n( $total ) );
    function bp_message_get_recipient_usernames() { 
    16851575                return apply_filters( 'bp_get_message_get_recipient_usernames', $recipients );
    16861576        }
    16871577
    1688 
    16891578/**
    16901579 * Message Thread Template Class
    16911580 */
    1692 class BP_Messages_Thread_Template {
     1581class BP_Messages_Thread_Template extends BP_Template_Loop {
    16931582
    16941583        /**
    1695          * The loop iterator.
     1584         * Default arguments for Messages loop.
    16961585         *
    1697          * @access public
    1698          * @var int
    1699          */
    1700         public $current_message = -1;
    1701 
    1702         /**
    1703          * Number of messages returned by the paged query.
    1704          *
    1705          * @access public
    1706          * @var int
    1707          */
    1708         public $message_count = 0;
    1709 
    1710         /**
    1711          * The message object currently being iterated on.
    1712          *
    1713          * @access public
    1714          * @var object
    1715          */
    1716         public $message;
    1717 
    1718         /**
    1719          * Thread that the current messages belong to.
    1720          *
    1721          * @access public
    1722          * @var BP_Messages_Thread
    1723          */
    1724         public $thread;
    1725 
    1726         /**
    1727          * A flag for whether the loop is currently being iterated.
     1586         * @since BuddyPress (2.3.0)
    17281587         *
    1729          * @access public
    1730          * @var bool
     1588         * @access protected
     1589         * @var array
    17311590         */
    1732         public $in_the_loop = false;
     1591        protected $default_args = array(
     1592                'labels' => array(
     1593                        'item'              => 'message',
     1594                        'item_plural'       => 'messages',
     1595                        'item_total_count'  => 'total_message_count',
     1596                        'item_count'        => 'message_count',
     1597                        'current_item'      => 'current_message',
     1598                        'hook_prefix'       => 'thread',
     1599                ),
     1600                'params' => array(
     1601                        'order' => 'ASC',
     1602                )
     1603        );
    17331604
    17341605        /**
    1735          * The page number being requested.
     1606         * Constructor method.
    17361607         *
    1737          * @access public
    1738          * @var int
     1608         * @see BP_Messages_Thread::populate() for full parameter info
    17391609         */
    1740         public $pag_page = 1;
     1610        public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) {
     1611                $params = array( 'params' => array(
     1612                        'thread_id'   => $thread_id,
     1613                        'order'       => $order,
     1614                        'thread_args' => $args,
     1615                ) );
    17411616
    1742         /**
    1743          * The number of items being requested per page.
    1744          *
    1745          * @access public
    1746          * @var int
    1747          */
    1748         public $pag_num = 10;
     1617                $this->set_loop( $params );
     1618        }
    17491619
    17501620        /**
    1751          * An HTML string containing pagination links.
     1621         * Get Messages.
    17521622         *
    1753          * @access public
    1754          * @var string
    1755          */
    1756         public $pag_links = '';
    1757 
    1758         /**
    1759          * The total number of messages matching the query.
     1623         * @since BuddyPress (2.3.0)
    17601624         *
    17611625         * @access public
    1762          * @var int
    1763          */
    1764         public $total_message_count = 0;
    1765 
    1766         /**
    1767          * Constructor method.
    1768          *
    1769          * @see BP_Messages_Thread::populate() for full parameter info
    17701626         */
    1771         public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) {
    1772                 $this->thread        = new BP_Messages_Thread( $thread_id, $order, $args );
    1773                 $this->message_count = count( $this->thread->messages );
     1627        public function get_items() {
     1628                $this->thread = new BP_Messages_Thread(
     1629                        $this->args['params']['thread_id'],
     1630                        $this->args['params']['order'],
     1631                        $this->args['params']['thread_args']
     1632                );
    17741633
    1775                 $last_message_index                 = $this->message_count - 1;
     1634                $last_message_index                 = count( $this->thread->messages ) - 1;
    17761635                $this->thread->last_message_id      = $this->thread->messages[ $last_message_index ]->id;
    17771636                $this->thread->last_message_date    = $this->thread->messages[ $last_message_index ]->date_sent;
    17781637                $this->thread->last_sender_id       = $this->thread->messages[ $last_message_index ]->sender_id;
    17791638                $this->thread->last_message_subject = $this->thread->messages[ $last_message_index ]->subject;
    17801639                $this->thread->last_message_content = $this->thread->messages[ $last_message_index ]->message;
    1781         }
    1782 
    1783         /**
    1784          * Whether there are messages available in the loop.
    1785          *
    1786          * @see bp_thread_has_messages()
    1787          *
    1788          * @return bool True if there are items in the loop, otherwise false.
    1789          */
    1790         public function has_messages() {
    1791                 if ( ! empty( $this->message_count ) ) {
    1792                         return true;
    1793                 }
    1794 
    1795                 return false;
    1796         }
    1797 
    1798         /**
    1799          * Set up the next member and iterate index.
    1800          *
    1801          * @return object The next member to iterate over.
    1802          */
    1803         public function next_message() {
    1804                 $this->current_message++;
    1805                 $this->message = $this->thread->messages[ $this->current_message ];
    1806 
    1807                 return $this->message;
    1808         }
    1809 
    1810         /**
    1811          * Rewind the messages and reset message index.
    1812          */
    1813         public function rewind_messages() {
    1814                 $this->current_message = -1;
    1815                 if ( $this->message_count > 0 ) {
    1816                         $this->message = $this->thread->messages[0];
    1817                 }
    1818         }
    1819 
    1820         /**
    1821          * Whether there are messages left in the loop to iterate over.
    1822          *
    1823          * This method is used by {@link bp_thread_messages()} as part of the
    1824          * while loop that controls iteration inside the messages loop, eg:
    1825          *     while ( bp_thread_messages() ) { ...
    1826          *
    1827          * @see bp_thread_messages()
    1828          *
    1829          * @return bool True if there are more messages to show, otherwise false.
    1830          */
    1831         public function messages() {
    1832                 if ( ( $this->current_message + 1 ) < $this->message_count ) {
    1833                         return true;
    1834                 } elseif ( ( $this->current_message + 1 ) === $this->message_count ) {
    1835 
    1836                         /**
    1837                          * Fires when at the end of messages to iterate over.
    1838                          *
    1839                          * @since BuddyPress (1.1.0)
    1840                          */
    1841                         do_action( 'thread_loop_end' );
    1842                         // Do some cleaning up after the loop
    1843                         $this->rewind_messages();
    1844                 }
    1845 
    1846                 $this->in_the_loop = false;
    1847                 return false;
    1848         }
    18491640
    1850         /**
    1851          * Set up the current message inside the loop.
    1852          *
    1853          * Used by {@link bp_thread_the_message()} to set up the current
    1854          * message data while looping, so that template tags used during
    1855          * that iteration make reference to the current message.
    1856          *
    1857          * @see bp_thread_the_message()
    1858          */
    1859         public function the_message() {
    1860                 $this->in_the_loop = true;
    1861                 $this->message     = $this->next_message();
    1862 
    1863                 // loop has just started
    1864                 if ( 0 === $this->current_message ) {
    1865 
    1866                         /**
    1867                          * Fires if at the start of the message loop.
    1868                          *
    1869                          * @since BuddyPress (1.1.0)
    1870                          */
    1871                         do_action( 'thread_loop_start' );
    1872                 }
     1641                return array( 'messages' => $this->thread->messages, 'total' => 0 );
    18731642        }
    18741643}
    18751644
    function bp_thread_has_messages( $args = '' ) { 
    19061675
    19071676        $thread_template = new BP_Messages_Thread_Template( $r['thread_id'], $r['order'], $extra_args );
    19081677
    1909         return $thread_template->has_messages();
     1678        return $thread_template->has_items();
    19101679}
    19111680
    19121681/**
    function bp_thread_messages_order() { 
    19331702function bp_thread_messages() {
    19341703        global $thread_template;
    19351704
    1936         return $thread_template->messages();
     1705        return $thread_template->items();
    19371706}
    19381707
    19391708/**
    function bp_thread_messages() { 
    19441713function bp_thread_the_message() {
    19451714        global $thread_template;
    19461715
    1947         return $thread_template->the_message();
     1716        return $thread_template->the_item();
    19481717}
    19491718
    19501719/**
  • src/bp-notifications/bp-notifications-template.php

    diff --git src/bp-notifications/bp-notifications-template.php src/bp-notifications/bp-notifications-template.php
    index 8ff04ac..f2e76ac 100644
    function bp_notifications_read_permalink() { 
    9898 *
    9999 * @since BuddyPress (1.9.0)
    100100 */
    101 class BP_Notifications_Template {
     101class BP_Notifications_Template extends BP_Template_Loop {
    102102
    103103        /**
    104          * The loop iterator.
     104         * Default arguments for Notifications loop.
    105105         *
    106          * @since BuddyPress (1.9.0)
    107          * @access public
    108          * @var int
    109          */
    110         public $current_notification = -1;
    111 
    112         /**
    113          * The number of notifications returned by the paged query.
    114          *
    115          * @since BuddyPress (1.9.0)
    116          * @access public
    117          * @var int
    118          */
    119         public $current_notification_count;
    120 
    121         /**
    122          * Total number of notifications matching the query.
    123          *
    124          * @since BuddyPress (1.9.0)
    125          * @access public
    126          * @var int
    127          */
    128         public $total_notification_count;
    129 
    130         /**
    131          * Array of notifications located by the query.
     106         * @since BuddyPress (2.3.0)
    132107         *
    133          * @since BuddyPress (1.9.0)
    134          * @access public
     108         * @access protected
    135109         * @var array
    136110         */
    137         public $notifications;
    138 
    139         /**
    140          * The notification object currently being iterated on.
    141          *
    142          * @since BuddyPress (1.9.0)
    143          * @access public
    144          * @var object
    145          */
    146         public $notification;
    147 
    148         /**
    149          * A flag for whether the loop is currently being iterated.
    150          *
    151          * @since BuddyPress (1.9.0)
    152          * @access public
    153          * @var bool
    154          */
    155         public $in_the_loop;
    156 
    157         /**
    158          * The ID of the user to whom the displayed notifications belong.
    159          *
    160          * @since BuddyPress (1.9.0)
    161          * @access public
    162          * @var int
    163          */
    164         public $user_id;
    165 
    166         /**
    167          * The page number being requested.
    168          *
    169          * @since BuddyPress (1.9.0)
    170          * @access public
    171          * @var int
    172          */
    173         public $pag_page;
    174 
    175         /**
    176          * The number of items to display per page of results.
    177          *
    178          * @since BuddyPress (1.9.0)
    179          * @access public
    180          * @var int
    181          */
    182         public $pag_num;
    183 
    184         /**
    185          * An HTML string containing pagination links.
    186          *
    187          * @since BuddyPress (1.9.0)
    188          * @access public
    189          * @var string
    190          */
    191         public $pag_links;
    192 
    193         /**
    194          * A string to match against.
    195          *
    196          * @since BuddyPress (1.9.0)
    197          * @access public
    198          * @var string
    199          */
    200         public $search_terms;
    201 
    202         /**
    203          * A database column to order the results by.
    204          *
    205          * @since BuddyPress (1.9.0)
    206          * @access public
    207          * @var string
    208          */
    209         public $order_by;
    210 
    211         /**
    212          * The direction to sort the results (ASC or DESC)
    213          *
    214          * @since BuddyPress (1.9.0)
    215          * @access public
    216          * @var string
    217          */
    218         public $sort_order;
     111        protected $default_args = array(
     112                'labels' => array(
     113                        'item'              => 'notification',
     114                        'item_plural'       => 'notifications',
     115                        'item_total_count'  => 'total_notification_count',
     116                        'item_count'        => 'notification_count',
     117                        'current_item'      => 'current_notification',
     118                        'hook_prefix'       => 'notifications',
     119                ),
     120                'params' => array(
     121                        'page_arg'          => 'npage',
     122                        'page'              => 1,
     123                        'per_page'          => 20,
     124                        'max'               => 0,
     125                )
     126        );
    219127
    220128        /**
    221129         * Constructor method.
    class BP_Notifications_Template { 
    248156                        'page_arg'          => 'npage',
    249157                ) );
    250158
    251                 // Overrides
    252 
    253                 // Set which pagination page
    254                 if ( isset( $_GET[ $r['page_arg'] ] ) ) {
    255                         $r['page'] = intval( $_GET[ $r['page_arg'] ] );
    256                 }
    257 
    258                 // Set the number to show per page
    259                 if ( isset( $_GET['num'] ) ) {
    260                         $r['per_page'] = intval( $_GET['num'] );
    261                 } else {
    262                         $r['per_page'] = intval( $r['per_page'] );
    263                 }
    264 
    265159                // Sort order direction
    266160                $orders = array( 'ASC', 'DESC' );
    267161                if ( ! empty( $_GET['sort_order'] ) && in_array( $_GET['sort_order'], $orders ) ) {
    class BP_Notifications_Template { 
    270164                        $r['sort_order'] = in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC';
    271165                }
    272166
    273                 // Setup variables
    274                 $this->pag_page     = $r['page'];
    275                 $this->pag_num      = $r['per_page'];
    276                 $this->user_id      = $r['user_id'];
    277                 $this->is_new       = $r['is_new'];
    278                 $this->search_terms = $r['search_terms'];
    279                 $this->page_arg     = $r['page_arg'];
    280                 $this->order_by     = $r['order_by'];
    281                 $this->sort_order   = $r['sort_order'];
    282 
    283                 // Setup the notifications to loop through
    284                 $this->notifications            = BP_Notifications_Notification::get( $r );
    285                 $this->total_notification_count = BP_Notifications_Notification::get_total_count( $r );
    286 
    287                 if ( empty( $this->notifications ) ) {
    288                         $this->notification_count       = 0;
    289                         $this->total_notification_count = 0;
    290 
    291                 } else {
    292                         if ( ! empty( $r['max'] ) ) {
    293                                 if ( $r['max'] >= count( $this->notifications ) ) {
    294                                         $this->notification_count = count( $this->notifications );
    295                                 } else {
    296                                         $this->notification_count = (int) $r['max'];
    297                                 }
    298                         } else {
    299                                 $this->notification_count = count( $this->notifications );
    300                         }
    301                 }
    302 
    303                 if ( (int) $this->total_notification_count && (int) $this->pag_num ) {
    304                         $add_args = array(
    305                                 'sort_order' => $this->sort_order,
    306                         );
    307 
    308                         $this->pag_links = paginate_links( array(
    309                                 'base'      => add_query_arg( $this->page_arg, '%#%' ),
    310                                 'format'    => '',
    311                                 'total'     => ceil( (int) $this->total_notification_count / (int) $this->pag_num ),
    312                                 'current'   => $this->pag_page,
    313                                 'prev_text' => _x( '&larr;', 'Notifications pagination previous text', 'buddypress' ),
    314                                 'next_text' => _x( '&rarr;', 'Notifications pagination next text',     'buddypress' ),
    315                                 'mid_size'  => 1,
    316                                 'add_args'  => $add_args,
    317                         ) );
    318 
    319                         // Remove first page from pagination
    320                         $this->pag_links = str_replace( '?'      . $r['page_arg'] . '=1', '', $this->pag_links );
    321                         $this->pag_links = str_replace( '&#038;' . $r['page_arg'] . '=1', '', $this->pag_links );
    322                 }
    323         }
    324 
    325         /**
    326          * Whether there are notifications available in the loop.
    327          *
    328          * @since BuddyPress (1.9.0)
    329          *
    330          * @see bp_has_notifications()
    331          *
    332          * @return bool True if there are items in the loop, otherwise false.
    333          */
    334         public function has_notifications() {
    335                 if ( $this->notification_count ) {
    336                         return true;
    337                 }
    338 
    339                 return false;
     167                $this->set_loop( array( 'params' => $r ) );
    340168        }
    341169
    342170        /**
    343          * Set up the next notification and iterate index.
    344          *
    345          * @since BuddyPress (1.9.0)
     171         * Get Notifications.
    346172         *
    347          * @return object The next notification to iterate over.
    348          */
    349         public function next_notification() {
    350 
    351                 $this->current_notification++;
    352 
    353                 $this->notification = $this->notifications[ $this->current_notification ];
    354 
    355                 return $this->notification;
    356         }
    357 
    358         /**
    359          * Rewind the blogs and reset blog index.
     173         * @since BuddyPress (2.3.0)
    360174         *
    361          * @since BuddyPress (1.9.0)
     175         * @access public
    362176         */
    363         public function rewind_notifications() {
    364 
    365                 $this->current_notification = -1;
    366 
    367                 if ( $this->notification_count > 0 ) {
    368                         $this->notification = $this->notifications[0];
    369                 }
     177        public function get_items() {
     178                return array(
     179                        'notifications' => BP_Notifications_Notification::get( $this->args['params'] ),
     180                        'total'         => BP_Notifications_Notification::get_total_count( $this->args['params'] )
     181                );
    370182        }
    371183
    372184        /**
    373          * Whether there are notifications left in the loop to iterate over.
    374          *
    375          * This method is used by {@link bp_notifications()} as part of the
    376          * while loop that controls iteration inside the notifications loop, eg:
    377          *     while ( bp_notifications() ) { ...
    378          *
    379          * @since BuddyPress (1.9.0)
     185         * Set Notifications pagination.
    380186         *
    381          * @see bp_notifications()
     187         * @since BuddyPress (2.3.0)
    382188         *
    383          * @return bool True if there are more notifications to show,
    384          *         otherwise false.
     189         * @access public
    385190         */
    386         public function notifications() {
    387 
    388                 if ( $this->current_notification + 1 < $this->notification_count ) {
    389                         return true;
    390 
    391                 } elseif ( $this->current_notification + 1 == $this->notification_count ) {
    392                         do_action( 'notifications_loop_end');
    393 
    394                         $this->rewind_notifications();
    395                 }
    396 
    397                 $this->in_the_loop = false;
    398                 return false;
    399         }
     191        public function set_pag_links() {
     192                $add_args = array(
     193                        'sort_order' => $this->sort_order,
     194                );
    400195
    401         /**
    402          * Set up the current notification inside the loop.
    403          *
    404          * Used by {@link bp_the_notification()} to set up the current
    405          * notification data while looping, so that template tags used during
    406          * that iteration make reference to the current notification.
    407          *
    408          * @since BuddyPress (1.9.0)
    409          *
    410          * @see bp_the_notification()
    411          */
    412         public function the_notification() {
    413                 $this->in_the_loop  = true;
    414                 $this->notification = $this->next_notification();
     196                $this->pag_links = paginate_links( array(
     197                        'base'      => add_query_arg( $this->page_arg, '%#%' ),
     198                        'format'    => '',
     199                        'total'     => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ),
     200                        'current'   => (int) $this->page,
     201                        'prev_text' => _x( '&larr;', 'Notifications pagination previous text', 'buddypress' ),
     202                        'next_text' => _x( '&rarr;', 'Notifications pagination next text',     'buddypress' ),
     203                        'mid_size'  => 1,
     204                        'add_args'  => $add_args,
     205                ) );
    415206
    416                 // loop has just started
    417                 if ( 0 === $this->current_notification ) {
    418                         do_action( 'notifications_loop_start' );
    419                 }
     207                // Remove first page from pagination
     208                $this->pag_links = str_replace( '?'      . $this->page_arg . '=1', '', $this->pag_links );
     209                $this->pag_links = str_replace( '&#038;' . $this->page_arg . '=1', '', $this->pag_links );
    420210        }
    421211}
    422212
    function bp_has_notifications( $args = '' ) { 
    489279        // Setup the global query loop
    490280        buddypress()->notifications->query_loop = $query_loop;
    491281
    492         return apply_filters( 'bp_has_notifications', $query_loop->has_notifications(), $query_loop );
     282        return apply_filters( 'bp_has_notifications', $query_loop->has_items(), $query_loop );
    493283}
    494284
    495285/**
    function bp_has_notifications( $args = '' ) { 
    500290 * @return array List of notifications.
    501291 */
    502292function bp_the_notifications() {
    503         return buddypress()->notifications->query_loop->notifications();
     293        return buddypress()->notifications->query_loop->items();
    504294}
    505295
    506296/**
    function bp_the_notifications() { 
    511301 * @return object The current notification within the loop.
    512302 */
    513303function bp_the_notification() {
    514         return buddypress()->notifications->query_loop->the_notification();
     304        return buddypress()->notifications->query_loop->the_item();
    515305}
    516306
    517307/** Loop Output ***************************************************************/
    function bp_notifications_pagination_count() { 
    991781         */
    992782        function bp_get_notifications_pagination_count() {
    993783                $query_loop = buddypress()->notifications->query_loop;
    994                 $start_num  = intval( ( $query_loop->pag_page - 1 ) * $query_loop->pag_num ) + 1;
     784                $start_num  = intval( ( $query_loop->page - 1 ) * $query_loop->per_page ) + 1;
    995785                $from_num   = bp_core_number_format( $start_num );
    996                 $to_num     = bp_core_number_format( ( $start_num + ( $query_loop->pag_num - 1 ) > $query_loop->total_notification_count ) ? $query_loop->total_notification_count : $start_num + ( $query_loop->pag_num - 1 ) );
     786                $to_num     = bp_core_number_format( ( $start_num + ( $query_loop->per_page - 1 ) > $query_loop->total_notification_count ) ? $query_loop->total_notification_count : $start_num + ( $query_loop->per_page - 1 ) );
    997787                $total      = bp_core_number_format( $query_loop->total_notification_count );
    998788                $pag        = sprintf( _n( 'Viewing 1 notification', 'Viewing %1$s - %2$s of %3$s notifications', $total, 'buddypress' ), $from_num, $to_num, $total );
    999789
  • src/bp-templates/bp-legacy/buddypress/members/register.php

    diff --git src/bp-templates/bp-legacy/buddypress/members/register.php src/bp-templates/bp-legacy/buddypress/members/register.php
    index e0f8f5b..8c84606 100644
     
    6565                                        <?php /* Use the profile field loop to render input fields for the 'base' profile field group */ ?>
    6666                                        <?php if ( bp_is_active( 'xprofile' ) ) : if ( bp_has_profile( array( 'profile_group_id' => 1, 'fetch_field_data' => false ) ) ) : while ( bp_profile_groups() ) : bp_the_profile_group(); ?>
    6767
    68                                         <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
     68                                                <?php if ( bp_profile_group_has_fields() ) : ?>
    6969
    70                                                 <div<?php bp_field_css_class( 'editfield' ); ?>>
     70                                                        <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
    7171
    72                                                         <?php
    73                                                         $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
    74                                                         $field_type->edit_field_html();
     72                                                                <div<?php bp_field_css_class( 'editfield' ); ?>>
    7573
    76                                                         do_action( 'bp_custom_profile_edit_fields_pre_visibility' );
     74                                                                        <?php
     75                                                                        $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
     76                                                                        $field_type->edit_field_html();
    7777
    78                                                         if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
    79                                                                 <p class="field-visibility-settings-toggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
    80                                                                         <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> <a href="#" class="visibility-toggle-link"><?php _ex( 'Change', 'Change profile field visibility level', 'buddypress' ); ?></a>
    81                                                                 </p>
     78                                                                        do_action( 'bp_custom_profile_edit_fields_pre_visibility' );
    8279
    83                                                                 <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
    84                                                                         <fieldset>
    85                                                                                 <legend><?php _e( 'Who can see this field?', 'buddypress' ) ?></legend>
     80                                                                        if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
     81                                                                                <p class="field-visibility-settings-toggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
     82                                                                                        <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> <a href="#" class="visibility-toggle-link"><?php _ex( 'Change', 'Change profile field visibility level', 'buddypress' ); ?></a>
     83                                                                                </p>
    8684
    87                                                                                 <?php bp_profile_visibility_radio_buttons() ?>
     85                                                                                <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
     86                                                                                        <fieldset>
     87                                                                                                <legend><?php _e( 'Who can see this field?', 'buddypress' ) ?></legend>
    8888
    89                                                                         </fieldset>
    90                                                                         <a class="field-visibility-settings-close" href="#"><?php _e( 'Close', 'buddypress' ) ?></a>
     89                                                                                                <?php bp_profile_visibility_radio_buttons() ?>
    9190
    92                                                                 </div>
    93                                                         <?php else : ?>
    94                                                                 <p class="field-visibility-settings-notoggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
    95                                                                         <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?>
    96                                                                 </p>
    97                                                         <?php endif ?>
     91                                                                                        </fieldset>
     92                                                                                        <a class="field-visibility-settings-close" href="#"><?php _e( 'Close', 'buddypress' ) ?></a>
     93
     94                                                                                </div>
     95                                                                        <?php else : ?>
     96                                                                                <p class="field-visibility-settings-notoggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
     97                                                                                        <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?>
     98                                                                                </p>
     99                                                                        <?php endif ?>
    98100
    99                                                         <?php do_action( 'bp_custom_profile_edit_fields' ); ?>
     101                                                                        <?php do_action( 'bp_custom_profile_edit_fields' ); ?>
    100102
    101                                                         <p class="description"><?php bp_the_profile_field_description(); ?></p>
     103                                                                        <p class="description"><?php bp_the_profile_field_description(); ?></p>
     104
     105                                                                </div>
    102106
    103                                                 </div>
     107                                                        <?php endwhile; ?>
    104108
    105                                         <?php endwhile; ?>
     109                                                <?php endif ;?>
    106110
    107111                                        <input type="hidden" name="signup_profile_field_ids" id="signup_profile_field_ids" value="<?php bp_the_profile_field_ids(); ?>" />
    108112
  • src/bp-templates/bp-legacy/buddypress/members/single/profile/edit.php

    diff --git src/bp-templates/bp-legacy/buddypress/members/single/profile/edit.php src/bp-templates/bp-legacy/buddypress/members/single/profile/edit.php
    index 7fe800f..ae2b58c 100644
    if ( bp_has_profile( 'profile_group_id=' . bp_get_current_profile_group_id() ) ) 
    1919
    2020                <div class="clear"></div>
    2121
    22                 <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
     22                <?php if ( bp_profile_group_has_fields() ) : ?>
    2323
    24                         <div<?php bp_field_css_class( 'editfield' ); ?>>
     24                        <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
    2525
    26                                 <?php
    27                                 $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
    28                                 $field_type->edit_field_html();
     26                                <div<?php bp_field_css_class( 'editfield' ); ?>>
    2927
    30                                 do_action( 'bp_custom_profile_edit_fields_pre_visibility' );
    31                                 ?>
     28                                        <?php
     29                                        $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
     30                                        $field_type->edit_field_html();
    3231
    33                                 <?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
    34                                         <p class="field-visibility-settings-toggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
    35                                                 <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> <a href="#" class="visibility-toggle-link"><?php _e( 'Change', 'buddypress' ); ?></a>
    36                                         </p>
     32                                        do_action( 'bp_custom_profile_edit_fields_pre_visibility' );
     33                                        ?>
    3734
    38                                         <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
    39                                                 <fieldset>
    40                                                         <legend><?php _e( 'Who can see this field?', 'buddypress' ) ?></legend>
     35                                        <?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
     36                                                <p class="field-visibility-settings-toggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
     37                                                        <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> <a href="#" class="visibility-toggle-link"><?php _e( 'Change', 'buddypress' ); ?></a>
     38                                                </p>
    4139
    42                                                         <?php bp_profile_visibility_radio_buttons() ?>
     40                                                <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
     41                                                        <fieldset>
     42                                                                <legend><?php _e( 'Who can see this field?', 'buddypress' ) ?></legend>
    4343
    44                                                 </fieldset>
    45                                                 <a class="field-visibility-settings-close" href="#"><?php _e( 'Close', 'buddypress' ) ?></a>
    46                                         </div>
    47                                 <?php else : ?>
    48                                         <div class="field-visibility-settings-notoggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
    49                                                 <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?>
    50                                         </div>
    51                                 <?php endif ?>
     44                                                                <?php bp_profile_visibility_radio_buttons() ?>
    5245
    53                                 <?php do_action( 'bp_custom_profile_edit_fields' ); ?>
     46                                                        </fieldset>
     47                                                        <a class="field-visibility-settings-close" href="#"><?php _e( 'Close', 'buddypress' ) ?></a>
     48                                                </div>
     49                                        <?php else : ?>
     50                                                <div class="field-visibility-settings-notoggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
     51                                                        <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?>
     52                                                </div>
     53                                        <?php endif ?>
    5454
    55                                 <p class="description"><?php bp_the_profile_field_description(); ?></p>
    56                         </div>
     55                                        <?php do_action( 'bp_custom_profile_edit_fields' ); ?>
    5756
    58                 <?php endwhile; ?>
     57                                        <p class="description"><?php bp_the_profile_field_description(); ?></p>
     58                                </div>
     59
     60                        <?php endwhile; ?>
     61
     62                <?php endif ;?>
    5963
    6064        <?php do_action( 'bp_after_profile_field_content' ); ?>
    6165
  • src/bp-templates/bp-legacy/buddypress/members/single/settings/profile.php

    diff --git src/bp-templates/bp-legacy/buddypress/members/single/settings/profile.php src/bp-templates/bp-legacy/buddypress/members/single/settings/profile.php
    index 0fdae6f..cd4d040 100644
     
    66
    77                <?php while ( bp_profile_groups() ) : bp_the_profile_group(); ?>
    88
    9                         <?php if ( bp_profile_fields() ) : ?>
     9                        <?php if ( bp_profile_group_has_fields() ) : ?>
    1010
    1111                                <table class="profile-settings" id="xprofile-settings-<?php bp_the_profile_group_slug(); ?>">
    1212                                        <thead>
  • src/bp-xprofile/bp-xprofile-admin.php

    diff --git src/bp-xprofile/bp-xprofile-admin.php src/bp-xprofile/bp-xprofile-admin.php
    index 24d36e1..c9fb625 100644
    class BP_XProfile_User_Admin { 
    756756
    757757                        <?php endif; ?>
    758758
     759                        <?php if ( ! bp_profile_group_has_fields() ) { continue ; }  ?>
     760
    759761                        <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
    760762
    761763                                <div<?php bp_field_css_class( 'bp-profile-field' ); ?>>
  • src/bp-xprofile/bp-xprofile-template.php

    diff --git src/bp-xprofile/bp-xprofile-template.php src/bp-xprofile/bp-xprofile-template.php
    index 5c9e53f..7381f57 100644
     
    1010// Exit if accessed directly
    1111if ( !defined( 'ABSPATH' ) ) exit;
    1212
    13 class BP_XProfile_Data_Template {
    14         var $current_group = -1;
    15         var $group_count;
    16         var $groups;
    17         var $group;
     13/**
     14 * The main xProfile Groups template loop class.
     15 *
     16 * Responsible for loading a group of fields into a loop for display.
     17 */
     18class BP_XProfile_Data_Template extends BP_Template_Loop {
     19
     20        /**
     21         * Default arguments for xProfile Groups loop.
     22         *
     23         * @since BuddyPress (2.3.0)
     24         *
     25         * @access protected
     26         * @var array
     27         */
     28        protected $default_args = array(
     29                'labels' => array(
     30                        'item'              => 'group',
     31                        'item_plural'       => 'groups',
     32                        'item_total_count'  => 'total_group_count',
     33                        'item_count'        => 'group_count',
     34                        'current_item'      => 'current_group',
     35                        'hook_prefix'       => 'xprofile_template',
     36                ),
     37                'params' => array()
     38        );
    1839
    19         var $current_field = -1;
    20         var $field_count;
    21         var $field_has_data;
    22         var $field;
     40        /**
     41         * @deprecated BuddyPress (2.3.0).
     42         *
     43         * @access public
     44         * @var int
     45         */
     46        public $current_field = -1;
    2347
    24         var $in_the_loop;
    25         var $user_id;
     48        /**
     49         * @deprecated BuddyPress (2.3.0).
     50         *
     51         * @access public
     52         * @var object
     53         */
     54        public $field;
    2655
     56        /**
     57         * Constructor method.
     58         *
     59         * @see BP_XProfile_Group::get() for an in-depth description of arguments.
     60         *
     61         * @param int $user_id
     62         * @param int $profile_group_id
     63         * @param bool $hide_empty_groups
     64         * @param bool $fetch_fields
     65         * @param bool $fetch_field_data
     66         * @param bool|string $exclude_groups Comma-separated list of profile field group IDs to exclude
     67         * @param bool|string $exclude_fields Comma-separated list of profile field IDs to exclude
     68         * @param bool $hide_empty_fields
     69         * @param bool $fetch_visibility_level
     70         * @param bool $update_meta_cache
     71         */
    2772        function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false, $fetch_visibility_level = false, $update_meta_cache = true ) {
    28                 $this->groups = bp_xprofile_get_groups( array(
    29                         'profile_group_id'    => $profile_group_id,
    30                         'user_id'             => $user_id,
    31                         'hide_empty_groups'   => $hide_empty_groups,
    32                         'hide_empty_fields'   => $hide_empty_fields,
    33                         'fetch_fields'        => $fetch_fields,
    34                         'fetch_field_data'    => $fetch_field_data,
     73                $params = array( 'params' => array(
     74                        'profile_group_id'       => $profile_group_id,
     75                        'user_id'                => $user_id,
     76                        'hide_empty_groups'      => $hide_empty_groups,
     77                        'hide_empty_fields'      => $hide_empty_fields,
     78                        'fetch_fields'           => $fetch_fields,
     79                        'fetch_field_data'       => $fetch_field_data,
    3580                        'fetch_visibility_level' => $fetch_visibility_level,
    36                         'exclude_groups'      => $exclude_groups,
    37                         'exclude_fields'      => $exclude_fields,
    38                         'update_meta_cache'   => $update_meta_cache,
     81                        'exclude_groups'         => $exclude_groups,
     82                        'exclude_fields'         => $exclude_fields,
     83                        'update_meta_cache'      => $update_meta_cache,
    3984                ) );
    4085
    41                 $this->group_count = count($this->groups);
    42                 $this->user_id = $user_id;
     86                $this->set_loop( $params );
    4387        }
    4488
    45         function has_groups() {
    46                 if ( $this->group_count )
    47                         return true;
     89        /**
     90         * Get xProfile Groups.
     91         *
     92         * @since BuddyPress (2.3.0)
     93         *
     94         * @access public
     95         */
     96        public function get_items() {
     97                $groups = bp_xprofile_get_groups( $this->args['params'] );
    4898
    49                 return false;
     99                return array( 'groups' => $groups, 'total' => count( $groups ) );
    50100        }
    51101
    52         function next_group() {
     102        /**
     103         * Alias of BP_XProfile_Data_Template->next_item().
     104         */
     105        public function next_group() {
     106                $this->next_item();
     107        }
     108
     109        /**
     110         * Override parent method as we need to do custom work.
     111         *
     112         * @since BuddyPress (2.3.0)
     113         */
     114        function next_item() {
    53115                $this->current_group++;
    54116
    55117                $this->group       = $this->groups[$this->current_group];
    class BP_XProfile_Data_Template { 
    63125                return $this->group;
    64126        }
    65127
    66         function rewind_groups() {
    67                 $this->current_group = -1;
    68                 if ( $this->group_count > 0 ) {
    69                         $this->group = $this->groups[0];
    70                 }
    71         }
    72 
    73         function profile_groups() {
    74                 if ( $this->current_group + 1 < $this->group_count ) {
    75                         return true;
    76                 } elseif ( $this->current_group + 1 == $this->group_count ) {
    77                         do_action('xprofile_template_loop_end');
    78                         // Do some cleaning up after the loop
    79                         $this->rewind_groups();
    80                 }
    81 
    82                 $this->in_the_loop = false;
    83                 return false;
    84         }
    85 
    86         function the_profile_group() {
     128        /**
     129         * Override parent method as we need to do custom work.
     130         * Set up the current xProfile Group inside the loop.
     131         *
     132         * @since BuddyPress (2.3.0)
     133         *
     134         * @global $group
     135         */
     136        public function the_item() {
    87137                global $group;
    88138
    89                 $this->in_the_loop = true;
    90                 $group = $this->next_group();
     139                parent::the_item();
    91140
    92                 if ( 0 == $this->current_group ) // loop has just started
    93                         do_action('xprofile_template_loop_start');
     141                if ( ! empty( $this->group ) ) {
     142                        $group = $this->group;
     143                }
    94144        }
    95145
    96         /**** FIELDS ****/
     146        /**** FIELDS : backcompat methods *********************************************/
    97147
    98         function next_field() {
     148        /**
     149         * These methods are used to ensure back compatibility for themes using
     150         * bp_profile_fields() without first initiating the loop thanks to bp_profile_group_has_fields()
     151         */
     152
     153        /**
     154         * @deprecated BuddyPress (2.3.0).
     155         *
     156         * @access public
     157         */
     158        public function next_field() {
    99159                $this->current_field++;
    100160
    101161                $this->field = $this->group->fields[$this->current_field];
    102162                return $this->field;
    103163        }
    104164
    105         function rewind_fields() {
     165        /**
     166         * @deprecated BuddyPress (2.3.0).
     167         *
     168         * @access public
     169         */
     170        public function rewind_fields() {
    106171                $this->current_field = -1;
    107172                if ( $this->field_count > 0 ) {
    108173                        $this->field = $this->group->fields[0];
    109174                }
    110175        }
    111176
    112         function has_fields() {
    113                 $has_data = false;
    114 
    115                 for ( $i = 0, $count = count( $this->group->fields ); $i < $count; ++$i ) {
    116                         $field = &$this->group->fields[$i];
    117 
    118                         if ( !empty( $field->data ) && $field->data->value != null ) {
    119                                 $has_data = true;
    120                         }
     177        /**
     178         * @deprecated BuddyPress (2.3.0).
     179         *
     180         * @access public
     181         */
     182        public function profile_fields() {
     183                if ( empty( $this->doing_it_wrong ) ) {
     184                        _doing_it_wrong( __FUNCTION__, __( 'bp_profile_group_has_fields() should always be used before bp_profile_fields(), please update your templates.', 'buddypress' ), '2.3.0' );
    121185                }
     186                // Display the doing it wrong message once only
     187                $this->doing_it_wrong = true;
    122188
    123                 if ( $has_data )
    124                         return true;
    125 
    126                 return false;
    127         }
    128 
    129         function profile_fields() {
    130189                if ( $this->current_field + 1 < $this->field_count ) {
    131190                        return true;
    132191                } elseif ( $this->current_field + 1 == $this->field_count ) {
    class BP_XProfile_Data_Template { 
    137196                return false;
    138197        }
    139198
    140         function the_profile_field() {
     199        /**
     200         * @deprecated BuddyPress (2.3.0).
     201         *
     202         * @access public
     203         */
     204        public function the_profile_field() {
    141205                global $field;
    142206
    143207                $field = $this->next_field();
    144 
    145                 // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731
    146                 if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || '0' == $field->data->value ) ) {
    147                         $value = maybe_unserialize( $field->data->value );
    148                 } else {
    149                         $value = false;
    150                 }
    151 
    152                 if ( ! empty( $value ) || '0' == $value ) {
    153                         $this->field_has_data = true;
    154                 } else {
    155                         $this->field_has_data = false;
    156                 }
    157208        }
    158209}
    159210
    function bp_has_profile( $args = '' ) { 
    185236        );
    186237
    187238        $r = bp_parse_args( $args, $defaults, 'has_profile' );
    188         extract( $r, EXTR_SKIP );
    189239
    190         $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields, $fetch_visibility_level, $update_meta_cache );
    191         return apply_filters( 'bp_has_profile', $profile_template->has_groups(), $profile_template );
     240        $profile_template = new BP_XProfile_Data_Template(
     241                $r['user_id'],
     242                $r['profile_group_id'],
     243                $r['hide_empty_groups'],
     244                $r['fetch_fields'],
     245                $r['fetch_field_data'],
     246                $r['exclude_groups'],
     247                $r['exclude_fields'],
     248                $r['hide_empty_fields'],
     249                $r['fetch_visibility_level'],
     250                $r['update_meta_cache']
     251        );
     252
     253        return apply_filters( 'bp_has_profile', $profile_template->has_items(), $profile_template );
    192254}
    193255
    194256function bp_profile_groups() {
    195257        global $profile_template;
    196         return $profile_template->profile_groups();
     258        return $profile_template->items();
    197259}
    198260
    199261function bp_the_profile_group() {
    200262        global $profile_template;
    201         return $profile_template->the_profile_group();
     263        return $profile_template->the_item();
    202264}
    203265
    204 function bp_profile_group_has_fields() {
     266/**
     267 * xProfile Fields template loop class.
     268 *
     269 * Responsible for loading fields into a loop for display.
     270 *
     271 * @since BuddyPress (2.3.0)
     272 */
     273class BP_XProfile_Fields_Template extends BP_Template_Loop {
     274
     275        /**
     276         * Default arguments for xProfile Fields loop.
     277         *
     278         * @since BuddyPress (2.3.0)
     279         *
     280         * @access protected
     281         * @var array
     282         */
     283        protected $default_args = array(
     284                'labels' => array(
     285                        'item'              => 'field',
     286                        'item_plural'       => 'fields',
     287                        'item_total_count'  => 'total_field_count',
     288                        'item_count'        => 'field_count',
     289                        'current_item'      => 'current_field',
     290                        'hook_prefix'       => 'xprofile_field',
     291        ) );
     292
     293        /**
     294         * Constructor method.
     295         *
     296         * @since BuddyPress (2.3.0)
     297         *
     298         * @param array $fields a list of all field objects
     299         * @param int $group_id the profile group id to loop the fields on.
     300         * @uses wp_list_filter()
     301         */
     302        public function __construct( $fields = null, $group_id = 0 ) {
     303                if ( empty( $fields ) || empty( $group_id ) ) {
     304                        return false;
     305                }
     306
     307                $fields = wp_list_filter( $fields, array( 'group_id' => $group_id ) );
     308
     309                $this->set_loop( array( 'fields' => $fields ) );
     310        }
     311
     312        /**
     313         * Get xProfile Fields.
     314         *
     315         * @since BuddyPress (2.3.0)
     316         *
     317         * @access public
     318         */
     319        public function get_items() {
     320                return array(
     321                        'fields' => $this->args['fields'],
     322                        'total'  => count( $this->args['fields'] ),
     323                );
     324        }
     325
     326        /**
     327         * Override parent method as we need to do custom work.
     328         *
     329         * @since BuddyPress (2.3.0)
     330         *
     331         * @global $field
     332         */
     333        public function the_item() {
     334                global $field;
     335
     336                parent::the_item();
     337
     338                if ( empty( $this->field ) ) {
     339                        return;
     340                }
     341
     342                $field = $this->field;
     343        }
     344}
     345
     346/**
     347 * Initialize the Fields loop for a given xProfile Group
     348 *
     349 * @global $profile_template
     350 * @param array $fields a list of all field objects
     351 * @param int $group_id the profile group id to loop the fields on.
     352 * @uses  BP_XProfile_Fields_Template
     353 * @return bool true if fields were found, false otherwise
     354 */
     355function bp_profile_group_has_fields( $fields = array(), $group_id = 0 ) {
    205356        global $profile_template;
    206         return $profile_template->has_fields();
     357
     358        if ( empty( $fields ) ) {
     359                if ( ! empty( $profile_template->group->fields ) ) {
     360                        $fields =& $profile_template->group->fields;
     361
     362                // We really need the group fields to init the loop
     363                } else {
     364                        return false;
     365                }
     366        }
     367
     368        if ( empty( $group_id ) && ! empty( $profile_template->group->id ) ) {
     369                $group_id =& $profile_template->group->id;
     370        }
     371
     372        if ( empty( $profile_template ) ) {
     373                $profile_template = new stdClass();
     374        }
     375
     376        $profile_template->group_fields = new BP_XProfile_Fields_Template( $fields, $group_id );
     377
     378        return apply_filters( 'bp_profile_group_has_fields', $profile_template->group_fields->has_items(), $profile_template->group_fields );
    207379}
    208380
    209381function bp_field_css_class( $class = false ) {
    function bp_field_css_class( $class = false ) { 
    214386
    215387                $css_classes = array();
    216388
     389                /**
     390                 * Backcompat check in case bp_profile_group_has_fields()
     391                 * wasn't used before bp_profile_fields()
     392                 */
     393                if ( empty( $profile_template->group_fields ) ) {
     394                        $group_field = $profile_template;
     395                } else {
     396                        $group_field = $profile_template->group_fields;
     397                }
     398
    217399                if ( $class )
    218400                        $css_classes[] = sanitize_title( esc_attr( $class ) );
    219401
    220402                // Set a class with the field ID
    221                 $css_classes[] = 'field_' . $profile_template->field->id;
     403                $css_classes[] = 'field_' . $group_field->field->id;
    222404
    223405                // Set a class with the field name (sanitized)
    224                 $css_classes[] = 'field_' . sanitize_title( $profile_template->field->name );
     406                $css_classes[] = 'field_' . sanitize_title( $group_field->field->name );
    225407
    226408                // Set a class indicating whether the field is required or optional
    227                 if ( ! empty( $profile_template->field->is_required ) ) {
     409                if ( ! empty( $group_field->field->is_required ) ) {
    228410                        $css_classes[] = 'required-field';
    229411                } else {
    230412                        $css_classes[] = 'optional-field';
    function bp_field_css_class( $class = false ) { 
    233415                // Add the field visibility level
    234416                $css_classes[] = 'visibility-' . esc_attr( bp_get_the_profile_field_visibility_level() );
    235417
    236                 if ( $profile_template->current_field % 2 == 1 )
     418                if ( $group_field->current_field % 2 == 1 ) {
    237419                        $css_classes[] = 'alt';
     420                }
    238421
    239                 $css_classes[] = 'field_type_' . sanitize_title( $profile_template->field->type );
     422                $css_classes[] = 'field_type_' . sanitize_title( $group_field->field->type );
    240423                $css_classes = apply_filters_ref_array( 'bp_field_css_classes', array( &$css_classes ) );
    241424
    242425                return apply_filters( 'bp_get_field_css_class', ' class="' . implode( ' ', $css_classes ) . '"' );
    function bp_the_profile_field_ids() { 
    346529
    347530function bp_profile_fields() {
    348531        global $profile_template;
    349         return $profile_template->profile_fields();
     532
     533        /**
     534         * Backcompat check in case bp_profile_group_has_fields()
     535         * wasn't used before bp_profile_fields()
     536         */
     537        if ( empty( $profile_template->group_fields ) ) {
     538                return $profile_template->profile_fields();
     539        }
     540
     541        return $profile_template->group_fields->items();
    350542}
    351543
    352544function bp_the_profile_field() {
    353545        global $profile_template;
    354         return $profile_template->the_profile_field();
     546
     547        /**
     548         * Backcompat check in case bp_profile_group_has_fields()
     549         * wasn't used before bp_profile_fields()
     550         */
     551        if ( empty( $profile_template->group_fields ) ) {
     552                $field = $profile_template->the_profile_field();
     553        } else {
     554                $field = $profile_template->group_fields->the_item();
     555        }
     556
     557        // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731
     558        if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || '0' == $field->data->value ) ) {
     559                $value = maybe_unserialize( $field->data->value );
     560        } else {
     561                $value = false;
     562        }
     563
     564        if ( ! empty( $value ) || '0' == $value ) {
     565                $profile_template->field_has_data = true;
     566        } else {
     567                $profile_template->field_has_data = false;
     568        }
     569
     570        return $field;
    355571}
    356572
    357573function bp_the_profile_field_id() {