[ 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          $function_args = func_get_args();
 143  
 144          // Backward compatibility with old method of passing arguments.
 145          if ( !is_array( $args ) || count( $function_args ) > 1 ) {
 146              _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__ ) );
 147  
 148              $old_args_keys = array(
 149                  0 => 'page',
 150                  1 => 'per_page',
 151                  2 => 'max',
 152                  3 => 'include',
 153                  4 => 'sort',
 154                  5 => 'filter',
 155                  6 => 'search_terms',
 156                  7 => 'display_comments',
 157                  8 => 'show_hidden',
 158                  9 => 'exclude',
 159                  10 => 'in',
 160                  11 => 'spam',
 161                  12 => 'page_arg'
 162              );
 163  
 164              $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 165          }
 166  
 167          $defaults = array(
 168              'page'              => 1,
 169              'per_page'          => 20,
 170              'page_arg'          => 'acpage',
 171              'max'               => false,
 172              'fields'            => 'all',
 173              'count_total'       => false,
 174              'sort'              => false,
 175              'include'           => false,
 176              'exclude'           => false,
 177              'in'                => false,
 178              'filter'            => false,
 179              'scope'             => false,
 180              'search_terms'      => false,
 181              'meta_query'        => false,
 182              'date_query'        => false,
 183              'filter_query'      => false,
 184              'display_comments'  => 'threaded',
 185              'show_hidden'       => false,
 186              'spam'              => 'ham_only',
 187              'update_meta_cache' => true,
 188          );
 189  
 190          $r = bp_parse_args(
 191              $args,
 192              $defaults
 193          );
 194  
 195          extract( $r );
 196  
 197          $this->pag_arg  = sanitize_key( $r['page_arg'] );
 198          $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 199          $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 200  
 201          // Check if post/comment replies are disabled.
 202          $this->disable_blogforum_replies = (bool) bp_core_get_root_option( 'bp-disable-blogforum-comments' );
 203  
 204          // Get an array of the logged in user's favorite activities.
 205          $this->my_favs = bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true );
 206  
 207          // Fetch specific activity items based on ID's.
 208          if ( !empty( $include ) ) {
 209              $this->activities = bp_activity_get_specific( array(
 210                  'activity_ids'      => explode( ',', $include ),
 211                  'max'               => $max,
 212                  'count_total'       => $count_total,
 213                  'page'              => $this->pag_page,
 214                  'per_page'          => $this->pag_num,
 215                  'sort'              => $sort,
 216                  'display_comments'  => $display_comments,
 217                  'show_hidden'       => $show_hidden,
 218                  'spam'              => $spam,
 219                  'update_meta_cache' => $update_meta_cache,
 220              ) );
 221  
 222          // Fetch all activity items.
 223          } else {
 224              $this->activities = bp_activity_get( array(
 225                  'display_comments'  => $display_comments,
 226                  'max'               => $max,
 227                  'count_total'       => $count_total,
 228                  'per_page'          => $this->pag_num,
 229                  'page'              => $this->pag_page,
 230                  'sort'              => $sort,
 231                  'search_terms'      => $search_terms,
 232                  'meta_query'        => $meta_query,
 233                  'date_query'        => $date_query,
 234                  'filter_query'      => $filter_query,
 235                  'filter'            => $filter,
 236                  'scope'             => $scope,
 237                  'show_hidden'       => $show_hidden,
 238                  'exclude'           => $exclude,
 239                  'in'                => $in,
 240                  'spam'              => $spam,
 241                  'update_meta_cache' => $update_meta_cache,
 242              ) );
 243          }
 244  
 245          // The total_activity_count property will be set only if a
 246          // 'count_total' query has taken place.
 247          if ( ! is_null( $this->activities['total'] ) ) {
 248              if ( ! $max || $max >= (int) $this->activities['total'] ) {
 249                  $this->total_activity_count = (int) $this->activities['total'];
 250              } else {
 251                  $this->total_activity_count = (int) $max;
 252              }
 253          }
 254  
 255          $this->has_more_items = $this->activities['has_more_items'];
 256  
 257          $this->activities = $this->activities['activities'];
 258  
 259          if ( $max ) {
 260              if ( $max >= count($this->activities) ) {
 261                  $this->activity_count = count( $this->activities );
 262              } else {
 263                  $this->activity_count = (int) $max;
 264              }
 265          } else {
 266              $this->activity_count = count( $this->activities );
 267          }
 268  
 269          $this->full_name = bp_get_displayed_user_fullname();
 270  
 271          // Fetch parent content for activity comments so we do not have to query in the loop.
 272          foreach ( (array) $this->activities as $activity ) {
 273              if ( 'activity_comment' != $activity->type ) {
 274                  continue;
 275              }
 276  
 277              $parent_ids[] = $activity->item_id;
 278          }
 279  
 280          if ( !empty( $parent_ids ) ) {
 281              $activity_parents = bp_activity_get_specific( array( 'activity_ids' => $parent_ids ) );
 282          }
 283  
 284          if ( !empty( $activity_parents['activities'] ) ) {
 285              foreach( $activity_parents['activities'] as $parent ) {
 286                  $this->activity_parents[$parent->id] = $parent;
 287              }
 288  
 289              unset( $activity_parents );
 290          }
 291  
 292          if ( (int) $this->total_activity_count && (int) $this->pag_num ) {
 293              $this->pag_links = paginate_links( array(
 294                  'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 295                  'format'    => '',
 296                  'total'     => ceil( (int) $this->total_activity_count / (int) $this->pag_num ),
 297                  'current'   => (int) $this->pag_page,
 298                  'prev_text' => _x( '&larr;', 'Activity pagination previous text', 'buddypress' ),
 299                  'next_text' => _x( '&rarr;', 'Activity pagination next text', 'buddypress' ),
 300                  'mid_size'  => 1,
 301                  'add_args'  => array(),
 302              ) );
 303          }
 304      }
 305  
 306      /**
 307       * Whether there are activity items available in the loop.
 308       *
 309       * @since 1.0.0
 310       *
 311       * @see bp_has_activities()
 312       *
 313       * @return bool True if there are items in the loop, otherwise false.
 314       */
 315  	function has_activities() {
 316          if ( $this->activity_count ) {
 317              return true;
 318          }
 319  
 320          return false;
 321      }
 322  
 323      /**
 324       * Set up the next activity item and iterate index.
 325       *
 326       * @since 1.0.0
 327       *
 328       * @return object The next activity item to iterate over.
 329       */
 330  	public function next_activity() {
 331          $this->current_activity++;
 332          $this->activity = $this->activities[ $this->current_activity ];
 333  
 334          return $this->activity;
 335      }
 336  
 337      /**
 338       * Rewind the posts and reset post index.
 339       *
 340       * @since 1.0.0
 341       */
 342  	public function rewind_activities() {
 343          $this->current_activity = -1;
 344          if ( $this->activity_count > 0 ) {
 345              $this->activity = $this->activities[0];
 346          }
 347      }
 348  
 349      /**
 350       * Whether there are activity items left in the loop to iterate over.
 351       *
 352       * This method is used by {@link bp_activities()} as part of the while loop
 353       * that controls iteration inside the activities loop, eg:
 354       *     while ( bp_activities() ) { ...
 355       *
 356       * @since 1.0.0
 357       *
 358       * @see bp_activities()
 359       *
 360       * @return bool True if there are more activity items to show,
 361       *              otherwise false.
 362       */
 363  	public function user_activities() {
 364          if ( ( $this->current_activity + 1 ) < $this->activity_count ) {
 365              return true;
 366          } elseif ( ( $this->current_activity + 1 ) == $this->activity_count ) {
 367  
 368              /**
 369               * Fires right before the rewinding of activity posts.
 370               *
 371               * @since 1.1.0
 372               */
 373              do_action( 'activity_loop_end' );
 374  
 375              // Do some cleaning up after the loop.
 376              $this->rewind_activities();
 377          }
 378  
 379          $this->in_the_loop = false;
 380  
 381          return false;
 382      }
 383  
 384      /**
 385       * Set up the current activity item inside the loop.
 386       *
 387       * Used by {@link bp_the_activity()} to set up the current activity item
 388       * data while looping, so that template tags used during that iteration
 389       * make reference to the current activity item.
 390       *
 391       * @since 1.0.0
 392       *
 393       * @see bp_the_activity()
 394       */
 395  	public function the_activity() {
 396  
 397          $this->in_the_loop = true;
 398          $this->activity    = $this->next_activity();
 399  
 400          if ( is_array( $this->activity ) ) {
 401              $this->activity = (object) $this->activity;
 402          }
 403  
 404          // Loop has just started.
 405          if ( $this->current_activity == 0 ) {
 406  
 407              /**
 408               * Fires if the current activity item is the first in the activity loop.
 409               *
 410               * @since 1.1.0
 411               */
 412              do_action('activity_loop_start');
 413          }
 414      }
 415  }


Generated: Tue Apr 23 01:01:07 2024 Cross-referenced by PHPXref 0.7.1