[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/classes/ -> class-bp-groups-membership-requests-template.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Groups membership request template loop class.
   4   *
   5   * @package BuddyPress
   6   * @since 1.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * Membership request template loop class.
  14   *
  15   * @since 1.0.0
  16   */
  17  class BP_Groups_Membership_Requests_Template {
  18  
  19      /**
  20       * @since 1.0.0
  21       * @var int
  22       */
  23      public $current_request = -1;
  24  
  25      /**
  26       * @since 1.0.0
  27       * @var int
  28       */
  29      public $request_count;
  30  
  31      /**
  32       * @since 1.0.0
  33       * @var array
  34       */
  35      public $requests;
  36  
  37      /**
  38       * @since 1.0.0
  39       * @var object
  40       */
  41      public $request;
  42  
  43      /**
  44       * @sine 1.0.0
  45       * @var bool
  46       */
  47      public $in_the_loop;
  48  
  49      /**
  50       * @since 1.0.0
  51       * @var int
  52       */
  53      public $pag_page;
  54  
  55      /**
  56       * @since 1.0.0
  57       * @var int
  58       */
  59      public $pag_num;
  60  
  61      /**
  62       * @since 1.0.0
  63       * @var array|string|void
  64       */
  65      public $pag_links;
  66  
  67      /**
  68       * @since 1.0.0
  69       * @var int
  70       */
  71      public $total_request_count;
  72  
  73      /**
  74       * Constructor method.
  75       *
  76       * @since 1.5.0
  77       *
  78       * @param array $args {
  79       *     @type int $group_id ID of the group whose membership requests
  80       *                         are being queried. Default: current group id.
  81       *     @type int $per_page Number of records to return per page of
  82       *                         results. Default: 10.
  83       *     @type int $page     Page of results to show. Default: 1.
  84       *     @type int $max      Max items to return. Default: false (show all)
  85       * }
  86       */
  87  	public function __construct( $args = array() ) {
  88          $function_args = func_get_args();
  89  
  90          // Backward compatibility with old method of passing arguments.
  91          if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
  92              /* translators: 1: the name of the method. 2: the name of the file. */
  93              _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__ ) );
  94  
  95              $old_args_keys = array(
  96                  0 => 'group_id',
  97                  1 => 'per_page',
  98                  2 => 'max',
  99              );
 100  
 101              $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 102          }
 103  
 104          $r = bp_parse_args( $args, array(
 105              'page'     => 1,
 106              'per_page' => 10,
 107              'page_arg' => 'mrpage',
 108              'max'      => false,
 109              'type'     => 'first_joined',
 110              'group_id' => bp_get_current_group_id(),
 111          ), 'groups_membership_requests_template' );
 112  
 113          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 114          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 115          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 116  
 117          $mquery = new BP_Group_Member_Query( array(
 118              'group_id' => $r['group_id'],
 119              'type'     => $r['type'],
 120              'per_page' => $this->pag_num,
 121              'page'     => $this->pag_page,
 122  
 123              // These filters ensure we only get pending requests.
 124              'is_confirmed' => false,
 125              'inviter_id'   => 0,
 126          ) );
 127  
 128          $this->requests      = array_values( $mquery->results );
 129          $this->request_count = count( $this->requests );
 130  
 131          // Compatibility with legacy format of request data objects.
 132          foreach ( $this->requests as $rk => $rv ) {
 133              // For legacy reasons, the 'id' property of each
 134              // request must match the membership id, not the ID of
 135              // the user (as it's returned by BP_Group_Member_Query).
 136              $this->requests[ $rk ]->user_id = $rv->ID;
 137              $this->requests[ $rk ]->id      = $rv->membership_id;
 138  
 139              // Miscellaneous values.
 140              $this->requests[ $rk ]->group_id   = $r['group_id'];
 141          }
 142  
 143          if ( empty( $r['max'] ) || ( $r['max'] >= (int) $mquery->total_users ) ) {
 144              $this->total_request_count = (int) $mquery->total_users;
 145          } else {
 146              $this->total_request_count = (int) $r['max'];
 147          }
 148  
 149          if ( empty( $r['max'] ) || ( $r['max'] >= count( $this->requests ) ) ) {
 150              $this->request_count = count( $this->requests );
 151          } else {
 152              $this->request_count = (int) $r['max'];
 153          }
 154  
 155          $this->pag_links = paginate_links( array(
 156              'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 157              'format'    => '',
 158              'total'     => ceil( $this->total_request_count / $this->pag_num ),
 159              'current'   => $this->pag_page,
 160              'prev_text' => '&larr;',
 161              'next_text' => '&rarr;',
 162              'mid_size'  => 1,
 163              'add_args'  => array(),
 164          ) );
 165      }
 166  
 167      /**
 168       * Whether or not there are requests to show.
 169       *
 170       * @since 1.0.0
 171       *
 172       * @return bool
 173       */
 174  	public function has_requests() {
 175          if ( ! empty( $this->request_count ) ) {
 176              return true;
 177          }
 178  
 179          return false;
 180      }
 181  
 182      /**
 183       * Moves up to the next request.
 184       *
 185       * @since 1.0.0
 186       *
 187       * @return object
 188       */
 189  	public function next_request() {
 190          $this->current_request++;
 191          $this->request = $this->requests[ $this->current_request ];
 192  
 193          return $this->request;
 194      }
 195  
 196      /**
 197       * Rewinds the requests to the first in the list.
 198       *
 199       * @since 1.0.0
 200       */
 201  	public function rewind_requests() {
 202          $this->current_request = -1;
 203  
 204          if ( $this->request_count > 0 ) {
 205              $this->request = $this->requests[0];
 206          }
 207      }
 208  
 209      /**
 210       * Finishes up the requests to display.
 211       *
 212       * @since 1.0.0
 213       *
 214       * @return bool
 215       */
 216  	public function requests() {
 217          $tick = intval( $this->current_request + 1 );
 218          if ( $tick < $this->request_count ) {
 219              return true;
 220          } elseif ( $tick == $this->request_count ) {
 221  
 222              /**
 223               * Fires right before the rewinding of group membership requests list.
 224               *
 225               * @since 1.5.0
 226               */
 227              do_action( 'group_request_loop_end' );
 228              // Do some cleaning up after the loop.
 229              $this->rewind_requests();
 230          }
 231  
 232          $this->in_the_loop = false;
 233          return false;
 234      }
 235  
 236      /**
 237       * Sets up the request to display.
 238       *
 239       * @since 1.0.0
 240       */
 241  	public function the_request() {
 242          $this->in_the_loop = true;
 243          $this->request     = $this->next_request();
 244  
 245          // Loop has just started.
 246          if ( 0 == $this->current_request ) {
 247  
 248              /**
 249               * Fires if the current group membership request item is the first in the loop.
 250               *
 251               * @since 1.1.0
 252               */
 253              do_action( 'group_request_loop_start' );
 254          }
 255      }
 256  }


Generated: Mon Sep 21 01:01:31 2020 Cross-referenced by PHPXref 0.7.1