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


Generated: Sun Sep 22 01:01:44 2019 Cross-referenced by PHPXref 0.7.1