[ 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              _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__ ) );
  93  
  94              $old_args_keys = array(
  95                  0 => 'group_id',
  96                  1 => 'per_page',
  97                  2 => 'max',
  98              );
  99  
 100              $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 101          }
 102  
 103          $r = bp_parse_args( $args, array(
 104              'page'     => 1,
 105              'per_page' => 10,
 106              'page_arg' => 'mrpage',
 107              'max'      => false,
 108              'type'     => 'first_joined',
 109              'group_id' => bp_get_current_group_id(),
 110          ), 'groups_membership_requests_template' );
 111  
 112          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 113          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 114          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 115  
 116          $mquery = new BP_Group_Member_Query( array(
 117              'group_id' => $r['group_id'],
 118              'type'     => $r['type'],
 119              'per_page' => $this->pag_num,
 120              'page'     => $this->pag_page,
 121  
 122              // These filters ensure we only get pending requests.
 123              'is_confirmed' => false,
 124              'inviter_id'   => 0,
 125          ) );
 126  
 127          $this->requests      = array_values( $mquery->results );
 128          $this->request_count = count( $this->requests );
 129  
 130          // Compatibility with legacy format of request data objects.
 131          foreach ( $this->requests as $rk => $rv ) {
 132              // For legacy reasons, the 'id' property of each
 133              // request must match the membership id, not the ID of
 134              // the user (as it's returned by BP_Group_Member_Query).
 135              $this->requests[ $rk ]->user_id = $rv->ID;
 136              $this->requests[ $rk ]->id      = $rv->membership_id;
 137  
 138              // Miscellaneous values.
 139              $this->requests[ $rk ]->group_id   = $r['group_id'];
 140          }
 141  
 142          if ( empty( $r['max'] ) || ( $r['max'] >= (int) $mquery->total_users ) ) {
 143              $this->total_request_count = (int) $mquery->total_users;
 144          } else {
 145              $this->total_request_count = (int) $r['max'];
 146          }
 147  
 148          if ( empty( $r['max'] ) || ( $r['max'] >= count( $this->requests ) ) ) {
 149              $this->request_count = count( $this->requests );
 150          } else {
 151              $this->request_count = (int) $r['max'];
 152          }
 153  
 154          $this->pag_links = paginate_links( array(
 155              'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 156              'format'    => '',
 157              'total'     => ceil( $this->total_request_count / $this->pag_num ),
 158              'current'   => $this->pag_page,
 159              'prev_text' => '&larr;',
 160              'next_text' => '&rarr;',
 161              'mid_size'  => 1,
 162              'add_args'  => array(),
 163          ) );
 164      }
 165  
 166      /**
 167       * Whether or not there are requests to show.
 168       *
 169       * @since 1.0.0
 170       *
 171       * @return bool
 172       */
 173  	public function has_requests() {
 174          if ( ! empty( $this->request_count ) ) {
 175              return true;
 176          }
 177  
 178          return false;
 179      }
 180  
 181      /**
 182       * Moves up to the next request.
 183       *
 184       * @since 1.0.0
 185       *
 186       * @return object
 187       */
 188  	public function next_request() {
 189          $this->current_request++;
 190          $this->request = $this->requests[ $this->current_request ];
 191  
 192          return $this->request;
 193      }
 194  
 195      /**
 196       * Rewinds the requests to the first in the list.
 197       *
 198       * @since 1.0.0
 199       */
 200  	public function rewind_requests() {
 201          $this->current_request = -1;
 202  
 203          if ( $this->request_count > 0 ) {
 204              $this->request = $this->requests[0];
 205          }
 206      }
 207  
 208      /**
 209       * Finishes up the requests to display.
 210       *
 211       * @since 1.0.0
 212       *
 213       * @return bool
 214       */
 215  	public function requests() {
 216          $tick = intval( $this->current_request + 1 );
 217          if ( $tick < $this->request_count ) {
 218              return true;
 219          } elseif ( $tick == $this->request_count ) {
 220  
 221              /**
 222               * Fires right before the rewinding of group membership requests list.
 223               *
 224               * @since 1.5.0
 225               */
 226              do_action( 'group_request_loop_end' );
 227              // Do some cleaning up after the loop.
 228              $this->rewind_requests();
 229          }
 230  
 231          $this->in_the_loop = false;
 232          return false;
 233      }
 234  
 235      /**
 236       * Sets up the request to display.
 237       *
 238       * @since 1.0.0
 239       */
 240  	public function the_request() {
 241          $this->in_the_loop = true;
 242          $this->request     = $this->next_request();
 243  
 244          // Loop has just started.
 245          if ( 0 == $this->current_request ) {
 246  
 247              /**
 248               * Fires if the current group membership request item is the first in the loop.
 249               *
 250               * @since 1.1.0
 251               */
 252              do_action( 'group_request_loop_start' );
 253          }
 254      }
 255  }


Generated: Sat Dec 14 01:01:40 2019 Cross-referenced by PHPXref 0.7.1