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


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1