[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Groups Template loop class.
   4   *
   5   * @package BuddyPress
   6   * @since 1.2.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * The main Groups template loop class.
  14   *
  15   * Responsible for loading a group of groups into a loop for display.
  16   *
  17   * @since 1.2.0
  18   */
  19  class BP_Groups_Template {
  20  
  21      /**
  22       * The loop iterator.
  23       *
  24       * @var int
  25       * @since 1.2.0
  26       */
  27      public $current_group = -1;
  28  
  29      /**
  30       * The number of groups returned by the paged query.
  31       *
  32       * @var int
  33       * @since 1.2.0
  34       */
  35      public $group_count;
  36  
  37      /**
  38       * Array of groups located by the query.
  39       *
  40       * @var array
  41       * @since 1.2.0
  42       */
  43      public $groups;
  44  
  45      /**
  46       * The group object currently being iterated on.
  47       *
  48       * @var object
  49       * @since 1.2.0
  50       */
  51      public $group;
  52  
  53      /**
  54       * A flag for whether the loop is currently being iterated.
  55       *
  56       * @var bool
  57       * @since 1.2.0
  58       */
  59      public $in_the_loop;
  60  
  61      /**
  62       * The page number being requested.
  63       *
  64       * @var string
  65       * @since 1.2.0
  66       */
  67      public $pag_page;
  68  
  69      /**
  70       * The number of items being requested per page.
  71       *
  72       * @var string
  73       * @since 1.2.0
  74       */
  75      public $pag_num;
  76  
  77      /**
  78       * An HTML string containing pagination links.
  79       *
  80       * @var string
  81       * @since 1.2.0
  82       */
  83      public $pag_links;
  84  
  85      /**
  86       * The total number of groups matching the query parameters.
  87       *
  88       * @var int
  89       * @since 1.2.0
  90       */
  91      public $total_group_count;
  92  
  93      /**
  94       * Whether the template loop is for a single group page.
  95       *
  96       * @var bool
  97       * @since 1.2.0
  98       */
  99      public $single_group = false;
 100  
 101      /**
 102       * Field to sort by.
 103       *
 104       * @var string
 105       * @since 1.2.0
 106       */
 107      public $sort_by;
 108  
 109      /**
 110       * Sort order.
 111       *
 112       * @var string
 113       * @since 1.2.0
 114       */
 115      public $order;
 116  
 117      /**
 118       * Constructor method.
 119       *
 120       * @see BP_Groups_Group::get() for an in-depth description of arguments.
 121       *
 122       * @param array $args {
 123       *     Array of arguments. Accepts all arguments accepted by
 124       *     {@link BP_Groups_Group::get()}. In cases where the default
 125       *     values of the params differ, they have been discussed below.
 126       *     @type int $per_page Default: 20.
 127       *     @type int $page Default: 1.
 128       * }
 129       */
 130  	function __construct( ...$args ){
 131          // Backward compatibility with old method of passing arguments.
 132          if ( ! is_array( $args[0] ) || count( $args ) > 1 ) {
 133              _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__ ) );
 134  
 135              $old_args_keys = array(
 136                  0  => 'user_id',
 137                  1  => 'type',
 138                  2  => 'page',
 139                  3  => 'per_page',
 140                  4  => 'max',
 141                  5  => 'slug',
 142                  6  => 'search_terms',
 143                  7  => 'populate_extras',
 144                  8  => 'include',
 145                  9  => 'exclude',
 146                  10 => 'show_hidden',
 147                  11 => 'page_arg',
 148              );
 149  
 150              $args = bp_core_parse_args_array( $old_args_keys, $args );
 151          } else {
 152              $args = reset( $args );
 153          }
 154  
 155          $defaults = array(
 156              'page'               => 1,
 157              'per_page'           => 20,
 158              'page_arg'           => 'grpage',
 159              'max'                => false,
 160              'type'               => 'active',
 161              'order'              => 'DESC',
 162              'orderby'            => 'date_created',
 163              'show_hidden'        => false,
 164              'user_id'            => 0,
 165              'slug'               => false,
 166              'include'            => false,
 167              'exclude'            => false,
 168              'parent_id'          => null,
 169              'search_terms'       => '',
 170              'search_columns'     => array(),
 171              'group_type'         => '',
 172              'group_type__in'     => '',
 173              'group_type__not_in' => '',
 174              'status'             => array(),
 175              'meta_query'         => false,
 176              'date_query'         => false,
 177              'update_meta_cache'  => true,
 178              'update_admin_cache' => false,
 179          );
 180  
 181          $r = bp_parse_args(
 182              $args,
 183              $defaults,
 184              'groups_template'
 185          );
 186  
 187          extract( $r );
 188  
 189          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 190          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 191          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 192  
 193          if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) ) {
 194              $show_hidden = true;
 195          }
 196  
 197          if ( 'invites' == $type ) {
 198              $this->groups = groups_get_invites_for_user( $user_id, $this->pag_num, $this->pag_page, $exclude );
 199          } elseif ( 'single-group' == $type ) {
 200              $this->single_group = true;
 201  
 202              if ( groups_get_current_group() ) {
 203                  $group = groups_get_current_group();
 204  
 205              } else {
 206                  $group = groups_get_group( BP_Groups_Group::get_id_from_slug( $r['slug'] ) );
 207              }
 208  
 209              // Backwards compatibility - the 'group_id' variable is not part of the
 210              // BP_Groups_Group object, but we add it here for devs doing checks against it
 211              //
 212              // @see https://buddypress.trac.wordpress.org/changeset/3540
 213              //
 214              // this is subject to removal in a future release; devs should check against
 215              // $group->id instead.
 216              $group->group_id = $group->id;
 217  
 218              $this->groups = array( $group );
 219  
 220          } else {
 221              $this->groups = groups_get_groups(
 222                      array(
 223                      'type'               => $type,
 224                      'order'              => $order,
 225                      'orderby'            => $orderby,
 226                      'per_page'           => $this->pag_num,
 227                      'page'               => $this->pag_page,
 228                      'user_id'            => $user_id,
 229                      'search_terms'       => $search_terms,
 230                      'search_columns'     => $search_columns,
 231                      'meta_query'         => $meta_query,
 232                      'date_query'         => $date_query,
 233                      'group_type'         => $group_type,
 234                      'group_type__in'     => $group_type__in,
 235                      'group_type__not_in' => $group_type__not_in,
 236                      'status'             => $status,
 237                      'include'            => $include,
 238                      'exclude'            => $exclude,
 239                      'parent_id'          => $parent_id,
 240                      'update_meta_cache'  => $update_meta_cache,
 241                      'update_admin_cache' => $update_admin_cache,
 242                      'show_hidden'        => $show_hidden,
 243                  )
 244              );
 245          }
 246  
 247          if ( 'invites' == $type ) {
 248              $this->total_group_count = (int) $this->groups['total'];
 249              $this->group_count       = (int) $this->groups['total'];
 250              $this->groups            = $this->groups['groups'];
 251          } elseif ( 'single-group' == $type ) {
 252              if ( empty( $group->id ) ) {
 253                  $this->total_group_count = 0;
 254                  $this->group_count       = 0;
 255              } else {
 256                  $this->total_group_count = 1;
 257                  $this->group_count       = 1;
 258              }
 259          } else {
 260              if ( empty( $max ) || $max >= (int) $this->groups['total'] ) {
 261                  $this->total_group_count = (int) $this->groups['total'];
 262              } else {
 263                  $this->total_group_count = (int) $max;
 264              }
 265  
 266              $this->groups = $this->groups['groups'];
 267  
 268              if ( !empty( $max ) ) {
 269                  if ( $max >= count( $this->groups ) ) {
 270                      $this->group_count = count( $this->groups );
 271                  } else {
 272                      $this->group_count = (int) $max;
 273                  }
 274              } else {
 275                  $this->group_count = count( $this->groups );
 276              }
 277          }
 278  
 279          // Build pagination links.
 280          if ( (int) $this->total_group_count && (int) $this->pag_num ) {
 281              $pag_args = array(
 282                  $this->pag_arg => '%#%'
 283              );
 284  
 285              if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
 286                  $base = remove_query_arg( 's', wp_get_referer() );
 287              } else {
 288                  $base = '';
 289              }
 290  
 291              $add_args = array(
 292                  'num'     => $this->pag_num,
 293                  'sortby'  => $this->sort_by,
 294                  'order'   => $this->order,
 295              );
 296  
 297              if ( ! empty( $search_terms ) ) {
 298                  $query_arg = bp_core_get_component_search_query_arg( 'groups' );
 299                  $add_args[ $query_arg ] = urlencode( $search_terms );
 300              }
 301  
 302              $this->pag_links = paginate_links( array(
 303                  'base'      => add_query_arg( $pag_args, $base ),
 304                  'format'    => '',
 305                  'total'     => ceil( (int) $this->total_group_count / (int) $this->pag_num ),
 306                  'current'   => $this->pag_page,
 307                  'prev_text' => _x( '&larr;', 'Group pagination previous text', 'buddypress' ),
 308                  'next_text' => _x( '&rarr;', 'Group pagination next text', 'buddypress' ),
 309                  'mid_size'  => 1,
 310                  'add_args'  => $add_args,
 311              ) );
 312          }
 313      }
 314  
 315      /**
 316       * Whether there are groups available in the loop.
 317       *
 318       * @since 1.2.0
 319       *
 320       * @see bp_has_groups()
 321       *
 322       * @return bool True if there are items in the loop, otherwise false.
 323       */
 324  	function has_groups() {
 325          if ( $this->group_count ) {
 326              return true;
 327          }
 328  
 329          return false;
 330      }
 331  
 332      /**
 333       * Set up the next group and iterate index.
 334       *
 335       * @since 1.2.0
 336       *
 337       * @return object The next group to iterate over.
 338       */
 339  	function next_group() {
 340          $this->current_group++;
 341          $this->group = $this->groups[$this->current_group];
 342  
 343          return $this->group;
 344      }
 345  
 346      /**
 347       * Rewind the groups and reset member index.
 348       *
 349       * @since 1.2.0
 350       */
 351  	function rewind_groups() {
 352          $this->current_group = -1;
 353          if ( $this->group_count > 0 ) {
 354              $this->group = $this->groups[0];
 355          }
 356      }
 357  
 358      /**
 359       * Whether there are groups left in the loop to iterate over.
 360       *
 361       * This method is used by {@link bp_groups()} as part of the while loop
 362       * that controls iteration inside the groups loop, eg:
 363       *     while ( bp_groups() ) { ...
 364       *
 365       * @since 1.2.0
 366       *
 367       * @see bp_groups()
 368       *
 369       * @return bool True if there are more groups to show, otherwise false.
 370       */
 371  	function groups() {
 372          if ( $this->current_group + 1 < $this->group_count ) {
 373              return true;
 374          } elseif ( $this->current_group + 1 == $this->group_count ) {
 375  
 376              /**
 377               * Fires right before the rewinding of groups list.
 378               *
 379               * @since 1.5.0
 380               */
 381              do_action('group_loop_end');
 382              // Do some cleaning up after the loop.
 383              $this->rewind_groups();
 384          }
 385  
 386          $this->in_the_loop = false;
 387          return false;
 388      }
 389  
 390      /**
 391       * Set up the current group inside the loop.
 392       *
 393       * Used by {@link bp_the_group()} to set up the current group data
 394       * while looping, so that template tags used during that iteration make
 395       * reference to the current member.
 396       *
 397       * @since 1.2.0
 398       *
 399       * @see bp_the_group()
 400       */
 401  	function the_group() {
 402          $this->in_the_loop = true;
 403          $this->group       = $this->next_group();
 404  
 405          if ( 0 == $this->current_group ) {
 406  
 407              /**
 408               * Fires if the current group item is the first in the loop.
 409               *
 410               * @since 1.1.0
 411               */
 412              do_action( 'group_loop_start' );
 413          }
 414      }
 415  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1