Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2016 04:55:58 AM (9 years ago)
Author:
boonebgorges
Message:

Move bp-groups classes to their own files.

See #6870.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-template.php

    r10454 r10520  
    1111defined( 'ABSPATH' ) || exit;
    1212
     13require dirname( __FILE__ ) . '/classes/class-bp-groups-template.php';
     14require dirname( __FILE__ ) . '/classes/class-bp-groups-group-members-template.php';
     15require dirname( __FILE__ ) . '/classes/class-bp-groups-memberships-requests-template.php';
     16require dirname( __FILE__ ) . '/classes/class-bp-groups-invite-template.php';
     17
    1318/**
    1419 * Output the groups component slug.
     
    9196        return apply_filters( 'bp_get_groups_directory_permalink', trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() ) );
    9297    }
    93 
    94 /**
    95  * The main Groups template loop class.
    96  *
    97  * Responsible for loading a group of groups into a loop for display.
    98  *
    99  * @since 1.2.0
    100  */
    101 class BP_Groups_Template {
    102 
    103     /**
    104      * The loop iterator.
    105      *
    106      * @var int
    107      * @since 1.2.0
    108      */
    109     public $current_group = -1;
    110 
    111     /**
    112      * The number of groups returned by the paged query.
    113      *
    114      * @var int
    115      * @since 1.2.0
    116      */
    117     public $group_count;
    118 
    119     /**
    120      * Array of groups located by the query.
    121      *
    122      * @var array
    123      * @since 1.2.0
    124      */
    125     public $groups;
    126 
    127     /**
    128      * The group object currently being iterated on.
    129      *
    130      * @var object
    131      * @since 1.2.0
    132      */
    133     public $group;
    134 
    135     /**
    136      * A flag for whether the loop is currently being iterated.
    137      *
    138      * @var bool
    139      * @since 1.2.0
    140      */
    141     public $in_the_loop;
    142 
    143     /**
    144      * The page number being requested.
    145      *
    146      * @var string
    147      * @since 1.2.0
    148      */
    149     public $pag_page;
    150 
    151     /**
    152      * The number of items being requested per page.
    153      *
    154      * @var string
    155      * @since 1.2.0
    156      */
    157     public $pag_num;
    158 
    159     /**
    160      * An HTML string containing pagination links.
    161      *
    162      * @var string
    163      * @since 1.2.0
    164      */
    165     public $pag_links;
    166 
    167     /**
    168      * The total number of groups matching the query parameters.
    169      *
    170      * @var int
    171      * @since 1.2.0
    172      */
    173     public $total_group_count;
    174 
    175     /**
    176      * Whether the template loop is for a single group page.
    177      *
    178      * @var bool
    179      * @since 1.2.0
    180      */
    181     public $single_group = false;
    182 
    183     /**
    184      * Field to sort by.
    185      *
    186      * @var string
    187      * @since 1.2.0
    188      */
    189     public $sort_by;
    190 
    191     /**
    192      * Sort order.
    193      *
    194      * @var string
    195      * @since 1.2.0
    196      */
    197     public $order;
    198 
    199     /**
    200      * Constructor method.
    201      *
    202      * @see BP_Groups_Group::get() for an in-depth description of arguments.
    203      *
    204      * @param array $args {
    205      *     Array of arguments. Accepts all arguments accepted by
    206      *     {@link BP_Groups_Group::get()}. In cases where the default
    207      *     values of the params differ, they have been discussed below.
    208      *     @type int $per_page Default: 20.
    209      *     @type int $page Default: 1.
    210      * }
    211      */
    212     function __construct( $args = array() ){
    213 
    214         // Backward compatibility with old method of passing arguments.
    215         if ( ! is_array( $args ) || func_num_args() > 1 ) {
    216             _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__ ) );
    217 
    218             $old_args_keys = array(
    219                 0  => 'user_id',
    220                 1  => 'type',
    221                 2  => 'page',
    222                 3  => 'per_page',
    223                 4  => 'max',
    224                 5  => 'slug',
    225                 6  => 'search_terms',
    226                 7  => 'populate_extras',
    227                 8  => 'include',
    228                 9  => 'exclude',
    229                 10 => 'show_hidden',
    230                 11 => 'page_arg',
    231             );
    232 
    233             $func_args = func_get_args();
    234             $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
    235         }
    236 
    237         $defaults = array(
    238             'page'              => 1,
    239             'per_page'          => 20,
    240             'page_arg'          => 'grpage',
    241             'max'               => false,
    242             'type'              => 'active',
    243             'order'             => 'DESC',
    244             'orderby'           => 'date_created',
    245             'show_hidden'       => false,
    246             'user_id'           => 0,
    247             'slug'              => false,
    248             'include'           => false,
    249             'exclude'           => false,
    250             'search_terms'      => '',
    251             'meta_query'        => false,
    252             'populate_extras'   => true,
    253             'update_meta_cache' => true,
    254         );
    255 
    256         $r = wp_parse_args( $args, $defaults );
    257         extract( $r );
    258 
    259         $this->pag_arg  = sanitize_key( $r['page_arg'] );
    260         $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    261         $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
    262 
    263         if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) ) {
    264             $show_hidden = true;
    265         }
    266 
    267         if ( 'invites' == $type ) {
    268             $this->groups = groups_get_invites_for_user( $user_id, $this->pag_num, $this->pag_page, $exclude );
    269         } elseif ( 'single-group' == $type ) {
    270             $this->single_group = true;
    271 
    272             if ( groups_get_current_group() ) {
    273                 $group = groups_get_current_group();
    274 
    275             } else {
    276                 $group = groups_get_group( array(
    277                     'group_id'        => BP_Groups_Group::get_id_from_slug( $r['slug'] ),
    278                     'populate_extras' => $r['populate_extras'],
    279                 ) );
    280             }
    281 
    282             // Backwards compatibility - the 'group_id' variable is not part of the
    283             // BP_Groups_Group object, but we add it here for devs doing checks against it
    284             //
    285             // @see https://buddypress.trac.wordpress.org/changeset/3540
    286             //
    287             // this is subject to removal in a future release; devs should check against
    288             // $group->id instead.
    289             $group->group_id = $group->id;
    290 
    291             $this->groups = array( $group );
    292 
    293         } else {
    294             $this->groups = groups_get_groups( array(
    295                 'type'              => $type,
    296                 'order'             => $order,
    297                 'orderby'           => $orderby,
    298                 'per_page'          => $this->pag_num,
    299                 'page'              => $this->pag_page,
    300                 'user_id'           => $user_id,
    301                 'search_terms'      => $search_terms,
    302                 'meta_query'        => $meta_query,
    303                 'include'           => $include,
    304                 'exclude'           => $exclude,
    305                 'populate_extras'   => $populate_extras,
    306                 'update_meta_cache' => $update_meta_cache,
    307                 'show_hidden'       => $show_hidden
    308             ) );
    309         }
    310 
    311         if ( 'invites' == $type ) {
    312             $this->total_group_count = (int) $this->groups['total'];
    313             $this->group_count       = (int) $this->groups['total'];
    314             $this->groups            = $this->groups['groups'];
    315         } elseif ( 'single-group' == $type ) {
    316             if ( empty( $group->id ) ) {
    317                 $this->total_group_count = 0;
    318                 $this->group_count       = 0;
    319             } else {
    320                 $this->total_group_count = 1;
    321                 $this->group_count       = 1;
    322             }
    323         } else {
    324             if ( empty( $max ) || $max >= (int) $this->groups['total'] ) {
    325                 $this->total_group_count = (int) $this->groups['total'];
    326             } else {
    327                 $this->total_group_count = (int) $max;
    328             }
    329 
    330             $this->groups = $this->groups['groups'];
    331 
    332             if ( !empty( $max ) ) {
    333                 if ( $max >= count( $this->groups ) ) {
    334                     $this->group_count = count( $this->groups );
    335                 } else {
    336                     $this->group_count = (int) $max;
    337                 }
    338             } else {
    339                 $this->group_count = count( $this->groups );
    340             }
    341         }
    342 
    343         // Build pagination links.
    344         if ( (int) $this->total_group_count && (int) $this->pag_num ) {
    345             $pag_args = array(
    346                 $this->pag_arg => '%#%'
    347             );
    348 
    349             if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
    350                 $base = remove_query_arg( 's', wp_get_referer() );
    351             } else {
    352                 $base = '';
    353             }
    354 
    355             $add_args = array(
    356                 'num'     => $this->pag_num,
    357                 'sortby'  => $this->sort_by,
    358                 'order'   => $this->order,
    359             );
    360 
    361             if ( ! empty( $search_terms ) ) {
    362                 $query_arg = bp_core_get_component_search_query_arg( 'groups' );
    363                 $add_args[ $query_arg ] = urlencode( $search_terms );
    364             }
    365 
    366             $this->pag_links = paginate_links( array(
    367                 'base'      => add_query_arg( $pag_args, $base ),
    368                 'format'    => '',
    369                 'total'     => ceil( (int) $this->total_group_count / (int) $this->pag_num ),
    370                 'current'   => $this->pag_page,
    371                 'prev_text' => _x( '←', 'Group pagination previous text', 'buddypress' ),
    372                 'next_text' => _x( '→', 'Group pagination next text', 'buddypress' ),
    373                 'mid_size'  => 1,
    374                 'add_args'  => $add_args,
    375             ) );
    376         }
    377     }
    378 
    379     /**
    380      * Whether there are groups available in the loop.
    381      *
    382      * @since 1.2.0
    383      *
    384      * @see bp_has_groups()
    385      *
    386      * @return bool True if there are items in the loop, otherwise false.
    387      */
    388     function has_groups() {
    389         if ( $this->group_count ) {
    390             return true;
    391         }
    392 
    393         return false;
    394     }
    395 
    396     /**
    397      * Set up the next group and iterate index.
    398      *
    399      * @since 1.2.0
    400      *
    401      * @return object The next group to iterate over.
    402      */
    403     function next_group() {
    404         $this->current_group++;
    405         $this->group = $this->groups[$this->current_group];
    406 
    407         return $this->group;
    408     }
    409 
    410     /**
    411      * Rewind the groups and reset member index.
    412      *
    413      * @since 1.2.0
    414      */
    415     function rewind_groups() {
    416         $this->current_group = -1;
    417         if ( $this->group_count > 0 ) {
    418             $this->group = $this->groups[0];
    419         }
    420     }
    421 
    422     /**
    423      * Whether there are groups left in the loop to iterate over.
    424      *
    425      * This method is used by {@link bp_groups()} as part of the while loop
    426      * that controls iteration inside the groups loop, eg:
    427      *     while ( bp_groups() ) { ...
    428      *
    429      * @since 1.2.0
    430      *
    431      * @see bp_groups()
    432      *
    433      * @return bool True if there are more groups to show, otherwise false.
    434      */
    435     function groups() {
    436         if ( $this->current_group + 1 < $this->group_count ) {
    437             return true;
    438         } elseif ( $this->current_group + 1 == $this->group_count ) {
    439 
    440             /**
    441              * Fires right before the rewinding of groups list.
    442              *
    443              * @since 1.5.0
    444              */
    445             do_action('group_loop_end');
    446             // Do some cleaning up after the loop.
    447             $this->rewind_groups();
    448         }
    449 
    450         $this->in_the_loop = false;
    451         return false;
    452     }
    453 
    454     /**
    455      * Set up the current group inside the loop.
    456      *
    457      * Used by {@link bp_the_group()} to set up the current group data
    458      * while looping, so that template tags used during that iteration make
    459      * reference to the current member.
    460      *
    461      * @since 1.2.0
    462      *
    463      * @see bp_the_group()
    464      */
    465     function the_group() {
    466         $this->in_the_loop = true;
    467         $this->group       = $this->next_group();
    468 
    469         if ( 0 == $this->current_group ) {
    470 
    471             /**
    472              * Fires if the current group item is the first in the loop.
    473              *
    474              * @since 1.1.0
    475              */
    476             do_action( 'group_loop_start' );
    477         }
    478     }
    479 }
    48098
    48199/**
     
    39743592
    39753593/**
    3976  * Class BP_Groups_Group_Members_Template
    3977  *
    3978  * @since 1.0.0
    3979  */
    3980 class BP_Groups_Group_Members_Template {
    3981 
    3982     /**
    3983      * @since 1.0.0
    3984      * @var int
    3985      */
    3986     public $current_member = -1;
    3987 
    3988     /**
    3989      * @since 1.0.0
    3990      * @var int
    3991      */
    3992     public $member_count;
    3993 
    3994     /**
    3995      * @since 1.0.0
    3996      * @var array
    3997      */
    3998     public $members;
    3999 
    4000     /**
    4001      * @since 1.0.0
    4002      * @var object
    4003      */
    4004     public $member;
    4005 
    4006     /**
    4007      * @since 1.0.0
    4008      * @var bool
    4009      */
    4010     public $in_the_loop;
    4011 
    4012     /**
    4013      * @since 1.0.0
    4014      * @var int
    4015      */
    4016     public $pag_page;
    4017 
    4018     /**
    4019      * @since 1.0.0
    4020      * @var int
    4021      */
    4022     public $pag_num;
    4023 
    4024     /**
    4025      * @since 1.0.0
    4026      * @var array|string|void
    4027      */
    4028     public $pag_links;
    4029 
    4030     /**
    4031      * @since 1.0.0
    4032      * @var int
    4033      */
    4034     public $total_group_count;
    4035 
    4036     /**
    4037      * Constructor.
    4038      *
    4039      * @since 1.5.0
    4040      *
    4041      * @param array $args {
    4042      *     An array of optional arguments.
    4043      *     @type int      $group_id           ID of the group whose members are being
    4044      *                                        queried. Default: current group ID.
    4045      *     @type int      $page               Page of results to be queried. Default: 1.
    4046      *     @type int      $per_page           Number of items to return per page of
    4047      *                                        results. Default: 20.
    4048      *     @type int      $max                Optional. Max number of items to return.
    4049      *     @type array    $exclude            Optional. Array of user IDs to exclude.
    4050      *     @type bool|int $exclude_admin_mods True (or 1) to exclude admins and mods from
    4051      *                                        results. Default: 1.
    4052      *     @type bool|int $exclude_banned     True (or 1) to exclude banned users from results.
    4053      *                                        Default: 1.
    4054      *     @type array    $group_role         Optional. Array of group roles to include.
    4055      *     @type string   $search_terms       Optional. Search terms to match.
    4056      * }
    4057      */
    4058     public function __construct( $args = array() ) {
    4059 
    4060         // Backward compatibility with old method of passing arguments.
    4061         if ( ! is_array( $args ) || func_num_args() > 1 ) {
    4062             _deprecated_argument( __METHOD__, '2.0.0', 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__ ) );
    4063 
    4064             $old_args_keys = array(
    4065                 0 => 'group_id',
    4066                 1 => 'per_page',
    4067                 2 => 'max',
    4068                 3 => 'exclude_admins_mods',
    4069                 4 => 'exclude_banned',
    4070                 5 => 'exclude',
    4071                 6 => 'group_role',
    4072             );
    4073 
    4074             $func_args = func_get_args();
    4075             $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
    4076         }
    4077 
    4078         $r = wp_parse_args( $args, array(
    4079             'group_id'            => bp_get_current_group_id(),
    4080             'page'                => 1,
    4081             'per_page'            => 20,
    4082             'page_arg'            => 'mlpage',
    4083             'max'                 => false,
    4084             'exclude'             => false,
    4085             'exclude_admins_mods' => 1,
    4086             'exclude_banned'      => 1,
    4087             'group_role'          => false,
    4088             'search_terms'        => false,
    4089             'type'                => 'last_joined',
    4090         ) );
    4091 
    4092         $this->pag_arg  = sanitize_key( $r['page_arg'] );
    4093         $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    4094         $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
    4095 
    4096         /**
    4097          * Check the current group is the same as the supplied group ID.
    4098          * It can differ when using {@link bp_group_has_members()} outside the Groups screens.
    4099          */
    4100         $current_group = groups_get_current_group();
    4101         if ( empty( $current_group ) || ( $current_group && $current_group->id !== bp_get_current_group_id() ) ) {
    4102             $current_group = groups_get_group( array( 'group_id' => $r['group_id'] ) );
    4103         }
    4104 
    4105         // Assemble the base URL for pagination.
    4106         $base_url = trailingslashit( bp_get_group_permalink( $current_group ) . bp_current_action() );
    4107         if ( bp_action_variable() ) {
    4108             $base_url = trailingslashit( $base_url . bp_action_variable() );
    4109         }
    4110 
    4111         $members_args = $r;
    4112 
    4113         $members_args['page']     = $this->pag_page;
    4114         $members_args['per_page'] = $this->pag_num;
    4115 
    4116         // Get group members for this loop.
    4117         $this->members = groups_get_group_members( $members_args );
    4118 
    4119         if ( empty( $r['max'] ) || ( $r['max'] >= (int) $this->members['count'] ) ) {
    4120             $this->total_member_count = (int) $this->members['count'];
    4121         } else {
    4122             $this->total_member_count = (int) $r['max'];
    4123         }
    4124 
    4125         // Reset members array for subsequent looping.
    4126         $this->members = $this->members['members'];
    4127 
    4128         if ( empty( $r['max'] ) || ( $r['max'] >= count( $this->members ) ) ) {
    4129             $this->member_count = (int) count( $this->members );
    4130         } else {
    4131             $this->member_count = (int) $r['max'];
    4132         }
    4133 
    4134         $this->pag_links = paginate_links( array(
    4135             'base'      => add_query_arg( array( $this->pag_arg => '%#%' ), $base_url ),
    4136             'format'    => '',
    4137             'total'     => ! empty( $this->pag_num ) ? ceil( $this->total_member_count / $this->pag_num ) : $this->total_member_count,
    4138             'current'   => $this->pag_page,
    4139             'prev_text' => '&larr;',
    4140             'next_text' => '&rarr;',
    4141             'mid_size'  => 1,
    4142             'add_args'  => array(),
    4143         ) );
    4144     }
    4145 
    4146     /**
    4147      * Whether or not there are members to display.
    4148      *
    4149      * @since 1.0.0
    4150      *
    4151      * @return bool
    4152      */
    4153     public function has_members() {
    4154         if ( ! empty( $this->member_count ) ) {
    4155             return true;
    4156         }
    4157 
    4158         return false;
    4159     }
    4160 
    4161     /**
    4162      * Increments to the next member to display.
    4163      *
    4164      * @since 1.0.0
    4165      *
    4166      * @return object
    4167      */
    4168     public function next_member() {
    4169         $this->current_member++;
    4170         $this->member = $this->members[ $this->current_member ];
    4171 
    4172         return $this->member;
    4173     }
    4174 
    4175     /**
    4176      * Rewinds to the first member to display.
    4177      *
    4178      * @since 1.0.0
    4179      */
    4180     public function rewind_members() {
    4181         $this->current_member = -1;
    4182         if ( $this->member_count > 0 ) {
    4183             $this->member = $this->members[0];
    4184         }
    4185     }
    4186 
    4187     /**
    4188      * Finishes up the members for display.
    4189      *
    4190      * @since 1.0.0
    4191      *
    4192      * @return bool
    4193      */
    4194     public function members() {
    4195         $tick = intval( $this->current_member + 1 );
    4196         if ( $tick < $this->member_count ) {
    4197             return true;
    4198         } elseif ( $tick == $this->member_count ) {
    4199 
    4200             /**
    4201              * Fires right before the rewinding of members list.
    4202              *
    4203              * @since 1.0.0
    4204              * @since 2.3.0 `$this` parameter added.
    4205              *
    4206              * @param BP_Groups_Group_Members_Template $this Instance of the current Members template.
    4207              */
    4208             do_action( 'loop_end', $this );
    4209 
    4210             // Do some cleaning up after the loop.
    4211             $this->rewind_members();
    4212         }
    4213 
    4214         $this->in_the_loop = false;
    4215         return false;
    4216     }
    4217 
    4218     /**
    4219      * Sets up the member to display.
    4220      *
    4221      * @since 1.0.0
    4222      */
    4223     public function the_member() {
    4224         $this->in_the_loop = true;
    4225         $this->member      = $this->next_member();
    4226 
    4227         // Loop has just started.
    4228         if ( 0 == $this->current_member ) {
    4229 
    4230             /**
    4231              * Fires if the current member item is the first in the members list.
    4232              *
    4233              * @since 1.0.0
    4234              * @since 2.3.0 `$this` parameter added.
    4235              *
    4236              * @param BP_Groups_Group_Members_Template $this Instance of the current Members template.
    4237              */
    4238             do_action( 'loop_start', $this );
    4239         }
    4240     }
    4241 }
    4242 
    4243 /**
    42443594 * Initialize a group member query loop.
    42453595 *
     
    58675217
    58685218/**
    5869  * Class BP_Groups_Membership_Requests_Template
    5870  *
    5871  * @since 1.0.0
    5872  */
    5873 class BP_Groups_Membership_Requests_Template {
    5874 
    5875     /**
    5876      * @since 1.0.0
    5877      * @var int
    5878      */
    5879     public $current_request = -1;
    5880 
    5881     /**
    5882      * @since 1.0.0
    5883      * @var int
    5884      */
    5885     public $request_count;
    5886 
    5887     /**
    5888      * @since 1.0.0
    5889      * @var array
    5890      */
    5891     public $requests;
    5892 
    5893     /**
    5894      * @since 1.0.0
    5895      * @var object
    5896      */
    5897     public $request;
    5898 
    5899     /**
    5900      * @sine 1.0.0
    5901      * @var bool
    5902      */
    5903     public $in_the_loop;
    5904 
    5905     /**
    5906      * @since 1.0.0
    5907      * @var int
    5908      */
    5909     public $pag_page;
    5910 
    5911     /**
    5912      * @since 1.0.0
    5913      * @var int
    5914      */
    5915     public $pag_num;
    5916 
    5917     /**
    5918      * @since 1.0.0
    5919      * @var array|string|void
    5920      */
    5921     public $pag_links;
    5922 
    5923     /**
    5924      * @since 1.0.0
    5925      * @var int
    5926      */
    5927     public $total_request_count;
    5928 
    5929     /**
    5930      * Constructor method.
    5931      *
    5932      * @since 1.5.0
    5933      *
    5934      * @param array $args {
    5935      *     @type int $group_id ID of the group whose membership requests
    5936      *                         are being queried. Default: current group id.
    5937      *     @type int $per_page Number of records to return per page of
    5938      *                         results. Default: 10.
    5939      *     @type int $page     Page of results to show. Default: 1.
    5940      *     @type int $max      Max items to return. Default: false (show all)
    5941      * }
    5942      */
    5943     public function __construct( $args = array() ) {
    5944 
    5945         // Backward compatibility with old method of passing arguments.
    5946         if ( ! is_array( $args ) || func_num_args() > 1 ) {
    5947             _deprecated_argument( __METHOD__, '2.0.0', 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__ ) );
    5948 
    5949             $old_args_keys = array(
    5950                 0 => 'group_id',
    5951                 1 => 'per_page',
    5952                 2 => 'max',
    5953             );
    5954 
    5955             $func_args = func_get_args();
    5956             $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
    5957         }
    5958 
    5959         $r = wp_parse_args( $args, array(
    5960             'page'     => 1,
    5961             'per_page' => 10,
    5962             'page_arg' => 'mrpage',
    5963             'max'      => false,
    5964             'type'     => 'first_joined',
    5965             'group_id' => bp_get_current_group_id(),
    5966         ) );
    5967 
    5968         $this->pag_arg  = sanitize_key( $r['page_arg'] );
    5969         $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    5970         $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
    5971 
    5972         $mquery = new BP_Group_Member_Query( array(
    5973             'group_id' => $r['group_id'],
    5974             'type'     => $r['type'],
    5975             'per_page' => $this->pag_num,
    5976             'page'     => $this->pag_page,
    5977 
    5978             // These filters ensure we only get pending requests.
    5979             'is_confirmed' => false,
    5980             'inviter_id'   => 0,
    5981         ) );
    5982 
    5983         $this->requests      = array_values( $mquery->results );
    5984         $this->request_count = count( $this->requests );
    5985 
    5986         // Compatibility with legacy format of request data objects.
    5987         foreach ( $this->requests as $rk => $rv ) {
    5988             // For legacy reasons, the 'id' property of each
    5989             // request must match the membership id, not the ID of
    5990             // the user (as it's returned by BP_Group_Member_Query).
    5991             $this->requests[ $rk ]->user_id = $rv->ID;
    5992             $this->requests[ $rk ]->id      = $rv->membership_id;
    5993 
    5994             // Miscellaneous values.
    5995             $this->requests[ $rk ]->group_id   = $r['group_id'];
    5996         }
    5997 
    5998         if ( empty( $r['max'] ) || ( $r['max'] >= (int) $mquery->total_users ) ) {
    5999             $this->total_request_count = (int) $mquery->total_users;
    6000         } else {
    6001             $this->total_request_count = (int) $r['max'];
    6002         }
    6003 
    6004         if ( empty( $r['max'] ) || ( $r['max'] >= count( $this->requests ) ) ) {
    6005             $this->request_count = count( $this->requests );
    6006         } else {
    6007             $this->request_count = (int) $r['max'];
    6008         }
    6009 
    6010         $this->pag_links = paginate_links( array(
    6011             'base'      => add_query_arg( $this->pag_arg, '%#%' ),
    6012             'format'    => '',
    6013             'total'     => ceil( $this->total_request_count / $this->pag_num ),
    6014             'current'   => $this->pag_page,
    6015             'prev_text' => '&larr;',
    6016             'next_text' => '&rarr;',
    6017             'mid_size'  => 1,
    6018             'add_args'  => array(),
    6019         ) );
    6020     }
    6021 
    6022     /**
    6023      * Whether or not there are requests to show.
    6024      *
    6025      * @since 1.0.0
    6026      *
    6027      * @return bool
    6028      */
    6029     public function has_requests() {
    6030         if ( ! empty( $this->request_count ) ) {
    6031             return true;
    6032         }
    6033 
    6034         return false;
    6035     }
    6036 
    6037     /**
    6038      * Moves up to the next request.
    6039      *
    6040      * @since 1.0.0
    6041      *
    6042      * @return object
    6043      */
    6044     public function next_request() {
    6045         $this->current_request++;
    6046         $this->request = $this->requests[ $this->current_request ];
    6047 
    6048         return $this->request;
    6049     }
    6050 
    6051     /**
    6052      * Rewinds the requests to the first in the list.
    6053      *
    6054      * @since 1.0.0
    6055      */
    6056     public function rewind_requests() {
    6057         $this->current_request = -1;
    6058 
    6059         if ( $this->request_count > 0 ) {
    6060             $this->request = $this->requests[0];
    6061         }
    6062     }
    6063 
    6064     /**
    6065      * Finishes up the requests to display.
    6066      *
    6067      * @since 1.0.0
    6068      *
    6069      * @return bool
    6070      */
    6071     public function requests() {
    6072         $tick = intval( $this->current_request + 1 );
    6073         if ( $tick < $this->request_count ) {
    6074             return true;
    6075         } elseif ( $tick == $this->request_count ) {
    6076 
    6077             /**
    6078              * Fires right before the rewinding of group membership requests list.
    6079              *
    6080              * @since 1.5.0
    6081              */
    6082             do_action( 'group_request_loop_end' );
    6083             // Do some cleaning up after the loop.
    6084             $this->rewind_requests();
    6085         }
    6086 
    6087         $this->in_the_loop = false;
    6088         return false;
    6089     }
    6090 
    6091     /**
    6092      * Sets up the request to display.
    6093      *
    6094      * @since 1.0.0
    6095      */
    6096     public function the_request() {
    6097         $this->in_the_loop = true;
    6098         $this->request     = $this->next_request();
    6099 
    6100         // Loop has just started.
    6101         if ( 0 == $this->current_request ) {
    6102 
    6103             /**
    6104              * Fires if the current group membership request item is the first in the loop.
    6105              *
    6106              * @since 1.1.0
    6107              */
    6108             do_action( 'group_request_loop_start' );
    6109         }
    6110     }
    6111 }
    6112 
    6113 /**
    61145219 * Initialize a group membership request template loop.
    61155220 *
     
    63645469
    63655470/** Group Invitations *********************************************************/
    6366 
    6367 /**
    6368  * Class BP_Groups_Invite_Template
    6369  *
    6370  * @since 1.1.0
    6371  */
    6372 class BP_Groups_Invite_Template {
    6373 
    6374     /**
    6375      * @since 1.1.0
    6376      * @var int
    6377      */
    6378     public $current_invite = -1;
    6379 
    6380     /**
    6381      * @since 1.1.0
    6382      * @var int
    6383      */
    6384     public $invite_count;
    6385 
    6386     /**
    6387      * @since 1.1.0
    6388      * @var array
    6389      */
    6390     public $invites;
    6391 
    6392     /**
    6393      * @since 1.1.0
    6394      * @var object
    6395      */
    6396     public $invite;
    6397 
    6398     /**
    6399      * @since 1.1.0
    6400      * @var bool
    6401      */
    6402     public $in_the_loop;
    6403 
    6404     /**
    6405      * @since 1.1.0
    6406      * @var int
    6407      */
    6408     public $pag_page;
    6409 
    6410     /**
    6411      * @since 1.1.0
    6412      * @var int
    6413      */
    6414     public $pag_num;
    6415 
    6416     /**
    6417      * @since 1.1.0
    6418      * @var string
    6419      */
    6420     public $pag_links;
    6421 
    6422     /**
    6423      * @since 1.1.0
    6424      * @var int
    6425      */
    6426     public $total_invite_count;
    6427 
    6428     /**
    6429      * BP_Groups_Invite_Template constructor.
    6430      *
    6431      * @since 1.5.0
    6432      *
    6433      * @param array $args
    6434      */
    6435     public function __construct( $args = array() ) {
    6436 
    6437         // Backward compatibility with old method of passing arguments.
    6438         if ( ! is_array( $args ) || func_num_args() > 1 ) {
    6439             _deprecated_argument( __METHOD__, '2.0.0', 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__ ) );
    6440 
    6441             $old_args_keys = array(
    6442                 0  => 'user_id',
    6443                 1  => 'group_id',
    6444             );
    6445 
    6446             $func_args = func_get_args();
    6447             $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
    6448         }
    6449 
    6450         $r = wp_parse_args( $args, array(
    6451             'page'     => 1,
    6452             'per_page' => 10,
    6453             'page_arg' => 'invitepage',
    6454             'user_id'  => bp_loggedin_user_id(),
    6455             'group_id' => bp_get_current_group_id(),
    6456         ) );
    6457 
    6458         $this->pag_arg  = sanitize_key( $r['page_arg'] );
    6459         $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    6460         $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
    6461 
    6462         $iquery = new BP_Group_Member_Query( array(
    6463             'group_id' => $r['group_id'],
    6464             'type'     => 'first_joined',
    6465             'per_page' => $this->pag_num,
    6466             'page'     => $this->pag_page,
    6467 
    6468             // These filters ensure we get only pending invites.
    6469             'is_confirmed' => false,
    6470             'inviter_id'   => $r['user_id'],
    6471         ) );
    6472 
    6473         $this->invite_data        = $iquery->results;
    6474         $this->total_invite_count = $iquery->total_users;
    6475         $this->invites            = array_values( wp_list_pluck( $this->invite_data, 'ID' ) );
    6476         $this->invite_count       = count( $this->invites );
    6477 
    6478         // If per_page is set to 0 (show all results), don't generate
    6479         // pag_links.
    6480         if ( ! empty( $this->pag_num ) ) {
    6481             $this->pag_links = paginate_links( array(
    6482                 'base'      => add_query_arg( $this->pag_arg, '%#%' ),
    6483                 'format'    => '',
    6484                 'total'     => ceil( $this->total_invite_count / $this->pag_num ),
    6485                 'current'   => $this->pag_page,
    6486                 'prev_text' => '&larr;',
    6487                 'next_text' => '&rarr;',
    6488                 'mid_size'  => 1,
    6489                 'add_args'  => array(),
    6490             ) );
    6491         } else {
    6492             $this->pag_links = '';
    6493         }
    6494     }
    6495 
    6496     /**
    6497      * Whether or not there are invites to show.
    6498      *
    6499      * @since 1.1.0
    6500      *
    6501      * @return bool
    6502      */
    6503     public function has_invites() {
    6504         if ( ! empty( $this->invite_count ) ) {
    6505             return true;
    6506         }
    6507 
    6508         return false;
    6509     }
    6510 
    6511     /**
    6512      * Increments up to the next invite to show.
    6513      *
    6514      * @since 1.1.0
    6515      *
    6516      * @return object
    6517      */
    6518     public function next_invite() {
    6519         $this->current_invite++;
    6520         $this->invite = $this->invites[ $this->current_invite ];
    6521 
    6522         return $this->invite;
    6523     }
    6524 
    6525     /**
    6526      * Rewinds to the first invite to show.
    6527      *
    6528      * @since 1.1.0
    6529      */
    6530     public function rewind_invites() {
    6531         $this->current_invite = -1;
    6532         if ( $this->invite_count > 0 ) {
    6533             $this->invite = $this->invites[0];
    6534         }
    6535     }
    6536 
    6537     /**
    6538      * Finishes up the invites to show.
    6539      *
    6540      * @since 1.1.0
    6541      *
    6542      * @return bool
    6543      */
    6544     public function invites() {
    6545         $tick = intval( $this->current_invite + 1 );
    6546         if ( $tick < $this->invite_count ) {
    6547             return true;
    6548         } elseif ( $tick == $this->invite_count ) {
    6549 
    6550             /**
    6551              * Fires right before the rewinding of invites list.
    6552              *
    6553              * @since 1.1.0
    6554              * @since 2.3.0 `$this` parameter added.
    6555              *
    6556              * @param BP_Groups_Invite_Template $this Instance of the current Invites template.
    6557              */
    6558             do_action( 'loop_end', $this );
    6559 
    6560             // Do some cleaning up after the loop
    6561             $this->rewind_invites();
    6562         }
    6563 
    6564         $this->in_the_loop = false;
    6565         return false;
    6566     }
    6567 
    6568     /**
    6569      * Sets up the invite to show.
    6570      *
    6571      * @since 1.1.0
    6572      */
    6573     public function the_invite() {
    6574         global $group_id;
    6575 
    6576         $this->in_the_loop  = true;
    6577         $user_id            = $this->next_invite();
    6578 
    6579         $this->invite       = new stdClass;
    6580         $this->invite->user = $this->invite_data[ $user_id ];
    6581 
    6582         // This method previously populated the user object with
    6583         // BP_Core_User. We manually configure BP_Core_User data for
    6584         // backward compatibility.
    6585         if ( bp_is_active( 'xprofile' ) ) {
    6586             $this->invite->user->profile_data = BP_XProfile_ProfileData::get_all_for_user( $user_id );
    6587         }
    6588 
    6589         $this->invite->user->avatar       = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'full',  'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->invite->user->fullname ) ) );
    6590         $this->invite->user->avatar_thumb = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->invite->user->fullname ) ) );
    6591         $this->invite->user->avatar_mini  = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->invite->user->fullname ), 'width' => 30, 'height' => 30 ) );
    6592         $this->invite->user->email        = $this->invite->user->user_email;
    6593         $this->invite->user->user_url     = bp_core_get_user_domain( $user_id, $this->invite->user->user_nicename, $this->invite->user->user_login );
    6594         $this->invite->user->user_link    = "<a href='{$this->invite->user->user_url}' title='{$this->invite->user->fullname}'>{$this->invite->user->fullname}</a>";
    6595         $this->invite->user->last_active  = bp_core_get_last_activity( $this->invite->user->last_activity, __( 'active %s', 'buddypress' ) );
    6596 
    6597         if ( bp_is_active( 'groups' ) ) {
    6598             $total_groups = BP_Groups_Member::total_group_count( $user_id );
    6599             $this->invite->user->total_groups = sprintf( _n( '%d group', '%d groups', $total_groups, 'buddypress' ), $total_groups );
    6600         }
    6601 
    6602         if ( bp_is_active( 'friends' ) ) {
    6603             $this->invite->user->total_friends = BP_Friends_Friendship::total_friend_count( $user_id );
    6604         }
    6605 
    6606         $this->invite->user->total_blogs = null;
    6607 
    6608         // Global'ed in bp_group_has_invites()
    6609         $this->invite->group_id = $group_id;
    6610 
    6611         // loop has just started
    6612         if ( 0 == $this->current_invite ) {
    6613 
    6614             /**
    6615              * Fires if the current invite item is the first in the loop.
    6616              *
    6617              * @since 1.1.0
    6618              * @since 2.3.0 `$this` parameter added.
    6619              *
    6620              * @param BP_Groups_Invite_Template $this Instance of the current Invites template.
    6621              */
    6622             do_action( 'loop_start', $this );
    6623         }
    6624     }
    6625 }
    66265471
    66275472/**
Note: See TracChangeset for help on using the changeset viewer.