[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Notifications Template Loop Class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage TonificationsTemplate
   7   * @since 1.9.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * The main notifications template loop class.
  15   *
  16   * Responsible for loading a group of notifications into a loop for display.
  17   *
  18   * @since 1.9.0
  19   */
  20  class BP_Notifications_Template {
  21  
  22      /**
  23       * The loop iterator.
  24       *
  25       * @since 1.9.0
  26       * @var int
  27       */
  28      public $current_notification = -1;
  29  
  30      /**
  31       * The number of notifications returned by the paged query.
  32       *
  33       * @since 1.9.0
  34       * @var int
  35       */
  36      public $current_notification_count;
  37  
  38      /**
  39       * Total number of notifications matching the query.
  40       *
  41       * @since 1.9.0
  42       * @var int
  43       */
  44      public $total_notification_count;
  45  
  46      /**
  47       * Array of notifications located by the query.
  48       *
  49       * @since 1.9.0
  50       * @var array
  51       */
  52      public $notifications;
  53  
  54      /**
  55       * The notification object currently being iterated on.
  56       *
  57       * @since 1.9.0
  58       * @var object
  59       */
  60      public $notification;
  61  
  62      /**
  63       * A flag for whether the loop is currently being iterated.
  64       *
  65       * @since 1.9.0
  66       * @var bool
  67       */
  68      public $in_the_loop;
  69  
  70      /**
  71       * The ID of the user to whom the displayed notifications belong.
  72       *
  73       * @since 1.9.0
  74       * @var int
  75       */
  76      public $user_id;
  77  
  78      /**
  79       * The page number being requested.
  80       *
  81       * @since 1.9.0
  82       * @var int
  83       */
  84      public $pag_page;
  85  
  86      /**
  87       * The $_GET argument used in URLs for determining pagination.
  88       *
  89       * @since 1.9.0
  90       * @var int
  91       */
  92      public $pag_arg;
  93  
  94      /**
  95       * The number of items to display per page of results.
  96       *
  97       * @since 1.9.0
  98       * @var int
  99       */
 100      public $pag_num;
 101  
 102      /**
 103       * An HTML string containing pagination links.
 104       *
 105       * @since 1.9.0
 106       * @var string
 107       */
 108      public $pag_links;
 109  
 110      /**
 111       * A string to match against.
 112       *
 113       * @since 1.9.0
 114       * @var string
 115       */
 116      public $search_terms;
 117  
 118      /**
 119       * A database column to order the results by.
 120       *
 121       * @since 1.9.0
 122       * @var string
 123       */
 124      public $order_by;
 125  
 126      /**
 127       * The direction to sort the results (ASC or DESC).
 128       *
 129       * @since 1.9.0
 130       * @var string
 131       */
 132      public $sort_order;
 133  
 134      /**
 135       * Array of variables used in this notification query.
 136       *
 137       * @since 2.2.2
 138       * @var array
 139       */
 140      public $query_vars;
 141  
 142      /**
 143       * Constructor method.
 144       *
 145       * @since 1.9.0
 146       *
 147       * @param array $args {
 148       *     An array of arguments. See {@link bp_has_notifications()}
 149       *     for more details.
 150       * }
 151       */
 152  	public function __construct( $args = array() ) {
 153  
 154          // Parse arguments.
 155          $r = bp_parse_args(
 156              $args,
 157              array(
 158                  'id'                => false,
 159                  'user_id'           => 0,
 160                  'item_id'           => false,
 161                  'secondary_item_id' => false,
 162                  'component_name'    => bp_notifications_get_registered_components(),
 163                  'component_action'  => false,
 164                  'is_new'            => true,
 165                  'search_terms'      => '',
 166                  'order_by'          => 'date_notified',
 167                  'sort_order'        => 'DESC',
 168                  'page_arg'          => 'npage',
 169                  'page'              => 1,
 170                  'per_page'          => 25,
 171                  'max'               => null,
 172                  'meta_query'        => false,
 173                  'date_query'        => false,
 174              )
 175          );
 176  
 177          // Sort order direction.
 178          if ( ! empty( $_GET['sort_order'] ) ) {
 179              $r['sort_order'] = $_GET['sort_order'];
 180          }
 181  
 182          // Setup variables.
 183          $this->pag_arg      = sanitize_key( $r['page_arg'] );
 184          $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] );
 185          $this->pag_num      = bp_sanitize_pagination_arg( 'num', $r['per_page'] );
 186          $this->sort_order   = bp_esc_sql_order( $r['sort_order'] );
 187          $this->user_id      = $r['user_id'];
 188          $this->is_new       = $r['is_new'];
 189          $this->search_terms = $r['search_terms'];
 190          $this->order_by     = $r['order_by'];
 191          $this->query_vars   = array(
 192              'id'                => $r['id'],
 193              'user_id'           => $this->user_id,
 194              'item_id'           => $r['item_id'],
 195              'secondary_item_id' => $r['secondary_item_id'],
 196              'component_name'    => $r['component_name'],
 197              'component_action'  => $r['component_action'],
 198              'meta_query'        => $r['meta_query'],
 199              'date_query'        => $r['date_query'],
 200              'is_new'            => $this->is_new,
 201              'search_terms'      => $this->search_terms,
 202              'order_by'          => $this->order_by,
 203              'sort_order'        => $this->sort_order,
 204              'page'              => $this->pag_page,
 205              'per_page'          => $this->pag_num,
 206          );
 207  
 208          // Setup the notifications to loop through.
 209          $this->notifications            = BP_Notifications_Notification::get( $this->query_vars );
 210          $this->total_notification_count = BP_Notifications_Notification::get_total_count( $this->query_vars );
 211  
 212          if ( empty( $this->notifications ) ) {
 213              $this->notification_count       = 0;
 214              $this->total_notification_count = 0;
 215  
 216          } else {
 217              if ( ! empty( $r['max'] ) ) {
 218                  if ( $r['max'] >= count( $this->notifications ) ) {
 219                      $this->notification_count = count( $this->notifications );
 220                  } else {
 221                      $this->notification_count = (int) $r['max'];
 222                  }
 223              } else {
 224                  $this->notification_count = count( $this->notifications );
 225              }
 226          }
 227  
 228          if ( (int) $this->total_notification_count && (int) $this->pag_num ) {
 229              $add_args = array(
 230                  'sort_order' => $this->sort_order,
 231              );
 232  
 233              $this->pag_links = paginate_links( array(
 234                  'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 235                  'format'    => '',
 236                  'total'     => ceil( (int) $this->total_notification_count / (int) $this->pag_num ),
 237                  'current'   => $this->pag_page,
 238                  'prev_text' => _x( '&larr;', 'Notifications pagination previous text', 'buddypress' ),
 239                  'next_text' => _x( '&rarr;', 'Notifications pagination next text', 'buddypress' ),
 240                  'mid_size'  => 1,
 241                  'add_args'  => $add_args,
 242              ) );
 243          }
 244      }
 245  
 246      /**
 247       * Whether there are notifications available in the loop.
 248       *
 249       * @since 1.9.0
 250       *
 251       * @see bp_has_notifications()
 252       *
 253       * @return bool True if there are items in the loop, otherwise false.
 254       */
 255  	public function has_notifications() {
 256          return ! empty( $this->notification_count );
 257      }
 258  
 259      /**
 260       * Set up the next notification and iterate index.
 261       *
 262       * @since 1.9.0
 263       *
 264       * @return BP_Notifications_Notification The next notification to iterate over.
 265       */
 266  	public function next_notification() {
 267  
 268          $this->current_notification++;
 269  
 270          $this->notification = $this->notifications[ $this->current_notification ];
 271  
 272          return $this->notification;
 273      }
 274  
 275      /**
 276       * Rewind the blogs and reset blog index.
 277       *
 278       * @since 1.9.0
 279       */
 280  	public function rewind_notifications() {
 281  
 282          $this->current_notification = -1;
 283  
 284          if ( $this->notification_count > 0 ) {
 285              $this->notification = $this->notifications[0];
 286          }
 287      }
 288  
 289      /**
 290       * Whether there are notifications left in the loop to iterate over.
 291       *
 292       * This method is used by {@link bp_notifications()} as part of the
 293       * while loop that controls iteration inside the notifications loop, eg:
 294       *     while ( bp_notifications() ) { ...
 295       *
 296       * @since 1.9.0
 297       *
 298       * @see bp_notifications()
 299       *
 300       * @return bool True if there are more notifications to show,
 301       *              otherwise false.
 302       */
 303  	public function notifications() {
 304  
 305          if ( $this->current_notification + 1 < $this->notification_count ) {
 306              return true;
 307  
 308          } elseif ( $this->current_notification + 1 === $this->notification_count ) {
 309  
 310              /**
 311               * Fires right before the rewinding of notification posts.
 312               *
 313               * @since 1.9.0
 314               */
 315              do_action( 'notifications_loop_end' );
 316  
 317              $this->rewind_notifications();
 318          }
 319  
 320          $this->in_the_loop = false;
 321          return false;
 322      }
 323  
 324      /**
 325       * Set up the current notification inside the loop.
 326       *
 327       * Used by {@link bp_the_notification()} to set up the current
 328       * notification data while looping, so that template tags used during
 329       * that iteration make reference to the current notification.
 330       *
 331       * @since 1.9.0
 332       *
 333       * @see bp_the_notification()
 334       */
 335  	public function the_notification() {
 336          $this->in_the_loop  = true;
 337          $this->notification = $this->next_notification();
 338  
 339          // Loop has just started.
 340          if ( 0 === $this->current_notification ) {
 341  
 342              /**
 343               * Fires if the current notification item is the first in the notification loop.
 344               *
 345               * @since 1.9.0
 346               */
 347              do_action( 'notifications_loop_start' );
 348          }
 349      }
 350  }


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