[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Activity Template.
   4   *
   5   * @package BuddyPress
   6   * @subpackage ActivityTemplate
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * The main activity template loop class.
  15   *
  16   * This is responsible for loading a group of activity items and displaying them.
  17   *
  18   * @since 1.0.0
  19   */
  20  class BP_Activity_Template {
  21  
  22      /**
  23       * The loop iterator.
  24       *
  25       * @since 1.0.0
  26       * @var int
  27       */
  28      public $current_activity = -1;
  29  
  30      /**
  31       * The activity count.
  32       *
  33       * @since 1.0.0
  34       * @var int
  35       */
  36      public $activity_count;
  37  
  38      /**
  39       * The total activity count.
  40       *
  41       * @since 1.0.0
  42       * @var int
  43       */
  44      public $total_activity_count;
  45  
  46      /**
  47       * Array of activities located by the query.
  48       *
  49       * @since 1.0.0
  50       * @var array
  51       */
  52      public $activities;
  53  
  54      /**
  55       * The activity object currently being iterated on.
  56       *
  57       * @since 1.0.0
  58       * @var object
  59       */
  60      public $activity;
  61  
  62      /**
  63       * A flag for whether the loop is currently being iterated.
  64       *
  65       * @since 1.0.0
  66       * @var bool
  67       */
  68      public $in_the_loop;
  69  
  70      /**
  71       * URL parameter key for activity pagination. Default: 'acpage'.
  72       *
  73       * @since 2.1.0
  74       * @var string
  75       */
  76      public $pag_arg;
  77  
  78      /**
  79       * The page number being requested.
  80       *
  81       * @since 1.0.0
  82       * @var int
  83       */
  84      public $pag_page;
  85  
  86      /**
  87       * The number of items being requested per page.
  88       *
  89       * @since 1.0.0
  90       * @var int
  91       */
  92      public $pag_num;
  93  
  94      /**
  95       * An HTML string containing pagination links.
  96       *
  97       * @since 1.0.0
  98       * @var string
  99       */
 100      public $pag_links;
 101  
 102      /**
 103       * The displayed user's full name.
 104       *
 105       * @since 1.0.0
 106       * @var string
 107       */
 108      public $full_name;
 109  
 110      /**
 111       * Constructor method.
 112       *
 113       * The arguments passed to this class constructor are of the same
 114       * format as {@link BP_Activity_Activity::get()}.
 115       *
 116       * @since 1.5.0
 117       *
 118       * @see BP_Activity_Activity::get() for a description of the argument
 119       *      structure, as well as default values.
 120       *
 121       * @param array $args {
 122       *     Array of arguments. Supports all arguments from
 123       *     BP_Activity_Activity::get(), as well as 'page_arg' and
 124       *     'include'. Default values for 'per_page' and 'display_comments'
 125       *     differ from the originating function, and are described below.
 126       *     @type string      $page_arg         The string used as a query parameter in
 127       *                                         pagination links. Default: 'acpage'.
 128       *     @type array|bool  $include          Pass an array of activity IDs to
 129       *                                         retrieve only those items, or false to noop the 'include'
 130       *                                         parameter. 'include' differs from 'in' in that 'in' forms
 131       *                                         an IN clause that works in conjunction with other filters
 132       *                                         passed to the function, while 'include' is interpreted as
 133       *                                         an exact list of items to retrieve, which skips all other
 134       *                                         filter-related parameters. Default: false.
 135       *     @type int|bool    $per_page         Default: 20.
 136       *     @type string|bool $display_comments Default: 'threaded'.
 137       * }
 138       */
 139  	public function __construct( $args ) {
 140          $bp = buddypress();
 141  
 142          // Backward compatibility with old method of passing arguments.
 143          if ( !is_array( $args ) || func_num_args() > 1 ) {
 144              _deprecated_argument( __METHOD__, '1.6', 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__ ) );
 145  
 146              $old_args_keys = array(
 147                  0 => 'page',
 148                  1 => 'per_page',
 149                  2 => 'max',
 150                  3 => 'include',
 151                  4 => 'sort',
 152                  5 => 'filter',
 153                  6 => 'search_terms',
 154                  7 => 'display_comments',
 155                  8 => 'show_hidden',
 156                  9 => 'exclude',
 157                  10 => 'in',
 158                  11 => 'spam',
 159                  12 => 'page_arg'
 160              );
 161  
 162              $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
 163          }
 164  
 165          $defaults = array(
 166              'page'              => 1,
 167              'per_page'          => 20,
 168              'page_arg'          => 'acpage',
 169              'max'               => false,
 170              'fields'            => 'all',
 171              'count_total'       => false,
 172              'sort'              => false,
 173              'include'           => false,
 174              'exclude'           => false,
 175              'in'                => false,
 176              'filter'            => false,
 177              'scope'             => false,
 178              'search_terms'      => false,
 179              'meta_query'        => false,
 180              'date_query'        => false,
 181              'filter_query'      => false,
 182              'display_comments'  => 'threaded',
 183              'show_hidden'       => false,
 184              'spam'              => 'ham_only',
 185              'update_meta_cache' => true,
 186          );
 187          $r = wp_parse_args( $args, $defaults );
 188          extract( $r );
 189  
 190          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 191          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 192          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 193  
 194          // Check if post/comment replies are disabled.
 195          $this->disable_blogforum_replies = (bool) bp_core_get_root_option( 'bp-disable-blogforum-comments' );
 196  
 197          // Get an array of the logged in user's favorite activities.
 198          $this->my_favs = bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true );
 199  
 200          // Fetch specific activity items based on ID's.
 201          if ( !empty( $include ) ) {
 202              $this->activities = bp_activity_get_specific( array(
 203                  'activity_ids'      => explode( ',', $include ),
 204                  'max'               => $max,
 205                  'count_total'       => $count_total,
 206                  'page'              => $this->pag_page,
 207                  'per_page'          => $this->pag_num,
 208                  'sort'              => $sort,
 209                  'display_comments'  => $display_comments,
 210                  'show_hidden'       => $show_hidden,
 211                  'spam'              => $spam,
 212                  'update_meta_cache' => $update_meta_cache,
 213              ) );
 214  
 215          // Fetch all activity items.
 216          } else {
 217              $this->activities = bp_activity_get( array(
 218                  'display_comments'  => $display_comments,
 219                  'max'               => $max,
 220                  'count_total'       => $count_total,
 221                  'per_page'          => $this->pag_num,
 222                  'page'              => $this->pag_page,
 223                  'sort'              => $sort,
 224                  'search_terms'      => $search_terms,
 225                  'meta_query'        => $meta_query,
 226                  'date_query'        => $date_query,
 227                  'filter_query'      => $filter_query,
 228                  'filter'            => $filter,
 229                  'scope'             => $scope,
 230                  'show_hidden'       => $show_hidden,
 231                  'exclude'           => $exclude,
 232                  'in'                => $in,
 233                  'spam'              => $spam,
 234                  'update_meta_cache' => $update_meta_cache,
 235              ) );
 236          }
 237  
 238          // The total_activity_count property will be set only if a
 239          // 'count_total' query has taken place.
 240          if ( ! is_null( $this->activities['total'] ) ) {
 241              if ( ! $max || $max >= (int) $this->activities['total'] ) {
 242                  $this->total_activity_count = (int) $this->activities['total'];
 243              } else {
 244                  $this->total_activity_count = (int) $max;
 245              }
 246          }
 247  
 248          $this->has_more_items = $this->activities['has_more_items'];
 249  
 250          $this->activities = $this->activities['activities'];
 251  
 252          if ( $max ) {
 253              if ( $max >= count($this->activities) ) {
 254                  $this->activity_count = count( $this->activities );
 255              } else {
 256                  $this->activity_count = (int) $max;
 257              }
 258          } else {
 259              $this->activity_count = count( $this->activities );
 260          }
 261  
 262          $this->full_name = bp_get_displayed_user_fullname();
 263  
 264          // Fetch parent content for activity comments so we do not have to query in the loop.
 265          foreach ( (array) $this->activities as $activity ) {
 266              if ( 'activity_comment' != $activity->type ) {
 267                  continue;
 268              }
 269  
 270              $parent_ids[] = $activity->item_id;
 271          }
 272  
 273          if ( !empty( $parent_ids ) ) {
 274              $activity_parents = bp_activity_get_specific( array( 'activity_ids' => $parent_ids ) );
 275          }
 276  
 277          if ( !empty( $activity_parents['activities'] ) ) {
 278              foreach( $activity_parents['activities'] as $parent ) {
 279                  $this->activity_parents[$parent->id] = $parent;
 280              }
 281  
 282              unset( $activity_parents );
 283          }
 284  
 285          if ( (int) $this->total_activity_count && (int) $this->pag_num ) {
 286              $this->pag_links = paginate_links( array(
 287                  'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 288                  'format'    => '',
 289                  'total'     => ceil( (int) $this->total_activity_count / (int) $this->pag_num ),
 290                  'current'   => (int) $this->pag_page,
 291                  'prev_text' => _x( '&larr;', 'Activity pagination previous text', 'buddypress' ),
 292                  'next_text' => _x( '&rarr;', 'Activity pagination next text', 'buddypress' ),
 293                  'mid_size'  => 1,
 294                  'add_args'  => array(),
 295              ) );
 296          }
 297      }
 298  
 299      /**
 300       * Whether there are activity items available in the loop.
 301       *
 302       * @since 1.0.0
 303       *
 304       * @see bp_has_activities()
 305       *
 306       * @return bool True if there are items in the loop, otherwise false.
 307       */
 308  	function has_activities() {
 309          if ( $this->activity_count ) {
 310              return true;
 311          }
 312  
 313          return false;
 314      }
 315  
 316      /**
 317       * Set up the next activity item and iterate index.
 318       *
 319       * @since 1.0.0
 320       *
 321       * @return object The next activity item to iterate over.
 322       */
 323  	public function next_activity() {
 324          $this->current_activity++;
 325          $this->activity = $this->activities[ $this->current_activity ];
 326  
 327          return $this->activity;
 328      }
 329  
 330      /**
 331       * Rewind the posts and reset post index.
 332       *
 333       * @since 1.0.0
 334       */
 335  	public function rewind_activities() {
 336          $this->current_activity = -1;
 337          if ( $this->activity_count > 0 ) {
 338              $this->activity = $this->activities[0];
 339          }
 340      }
 341  
 342      /**
 343       * Whether there are activity items left in the loop to iterate over.
 344       *
 345       * This method is used by {@link bp_activities()} as part of the while loop
 346       * that controls iteration inside the activities loop, eg:
 347       *     while ( bp_activities() ) { ...
 348       *
 349       * @since 1.0.0
 350       *
 351       * @see bp_activities()
 352       *
 353       * @return bool True if there are more activity items to show,
 354       *              otherwise false.
 355       */
 356  	public function user_activities() {
 357          if ( ( $this->current_activity + 1 ) < $this->activity_count ) {
 358              return true;
 359          } elseif ( ( $this->current_activity + 1 ) == $this->activity_count ) {
 360  
 361              /**
 362               * Fires right before the rewinding of activity posts.
 363               *
 364               * @since 1.1.0
 365               */
 366              do_action( 'activity_loop_end' );
 367  
 368              // Do some cleaning up after the loop.
 369              $this->rewind_activities();
 370          }
 371  
 372          $this->in_the_loop = false;
 373  
 374          return false;
 375      }
 376  
 377      /**
 378       * Set up the current activity item inside the loop.
 379       *
 380       * Used by {@link bp_the_activity()} to set up the current activity item
 381       * data while looping, so that template tags used during that iteration
 382       * make reference to the current activity item.
 383       *
 384       * @since 1.0.0
 385       *
 386       * @see bp_the_activity()
 387       */
 388  	public function the_activity() {
 389  
 390          $this->in_the_loop = true;
 391          $this->activity    = $this->next_activity();
 392  
 393          if ( is_array( $this->activity ) ) {
 394              $this->activity = (object) $this->activity;
 395          }
 396  
 397          // Loop has just started.
 398          if ( $this->current_activity == 0 ) {
 399  
 400              /**
 401               * Fires if the current activity item is the first in the activity loop.
 402               *
 403               * @since 1.1.0
 404               */
 405              do_action('activity_loop_start');
 406          }
 407      }
 408  }


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1