[ 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          $orders = array( 'ASC', 'DESC' );
 179          if ( ! empty( $_GET['sort_order'] ) && in_array( $_GET['sort_order'], $orders, true ) ) {
 180              $r['sort_order'] = $_GET['sort_order'];
 181          } else {
 182              $r['sort_order'] = in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC';
 183          }
 184  
 185          // Setup variables.
 186          $this->pag_arg      = sanitize_key( $r['page_arg'] );
 187          $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] );
 188          $this->pag_num      = bp_sanitize_pagination_arg( 'num', $r['per_page'] );
 189          $this->user_id      = $r['user_id'];
 190          $this->is_new       = $r['is_new'];
 191          $this->search_terms = $r['search_terms'];
 192          $this->order_by     = $r['order_by'];
 193          $this->sort_order   = $r['sort_order'];
 194          $this->query_vars   = array(
 195              'id'                => $r['id'],
 196              'user_id'           => $this->user_id,
 197              'item_id'           => $r['item_id'],
 198              'secondary_item_id' => $r['secondary_item_id'],
 199              'component_name'    => $r['component_name'],
 200              'component_action'  => $r['component_action'],
 201              'meta_query'        => $r['meta_query'],
 202              'date_query'        => $r['date_query'],
 203              'is_new'            => $this->is_new,
 204              'search_terms'      => $this->search_terms,
 205              'order_by'          => $this->order_by,
 206              'sort_order'        => $this->sort_order,
 207              'page'              => $this->pag_page,
 208              'per_page'          => $this->pag_num,
 209          );
 210  
 211          // Setup the notifications to loop through.
 212          $this->notifications            = BP_Notifications_Notification::get( $this->query_vars );
 213          $this->total_notification_count = BP_Notifications_Notification::get_total_count( $this->query_vars );
 214  
 215          if ( empty( $this->notifications ) ) {
 216              $this->notification_count       = 0;
 217              $this->total_notification_count = 0;
 218  
 219          } else {
 220              if ( ! empty( $r['max'] ) ) {
 221                  if ( $r['max'] >= count( $this->notifications ) ) {
 222                      $this->notification_count = count( $this->notifications );
 223                  } else {
 224                      $this->notification_count = (int) $r['max'];
 225                  }
 226              } else {
 227                  $this->notification_count = count( $this->notifications );
 228              }
 229          }
 230  
 231          if ( (int) $this->total_notification_count && (int) $this->pag_num ) {
 232              $add_args = array(
 233                  'sort_order' => $this->sort_order,
 234              );
 235  
 236              $this->pag_links = paginate_links( array(
 237                  'base'      => add_query_arg( $this->pag_arg, '%#%' ),
 238                  'format'    => '',
 239                  'total'     => ceil( (int) $this->total_notification_count / (int) $this->pag_num ),
 240                  'current'   => $this->pag_page,
 241                  'prev_text' => _x( '&larr;', 'Notifications pagination previous text', 'buddypress' ),
 242                  'next_text' => _x( '&rarr;', 'Notifications pagination next text', 'buddypress' ),
 243                  'mid_size'  => 1,
 244                  'add_args'  => $add_args,
 245              ) );
 246          }
 247      }
 248  
 249      /**
 250       * Whether there are notifications available in the loop.
 251       *
 252       * @since 1.9.0
 253       *
 254       * @see bp_has_notifications()
 255       *
 256       * @return bool True if there are items in the loop, otherwise false.
 257       */
 258  	public function has_notifications() {
 259          return ! empty( $this->notification_count );
 260      }
 261  
 262      /**
 263       * Set up the next notification and iterate index.
 264       *
 265       * @since 1.9.0
 266       *
 267       * @return BP_Notifications_Notification The next notification to iterate over.
 268       */
 269  	public function next_notification() {
 270  
 271          $this->current_notification++;
 272  
 273          $this->notification = $this->notifications[ $this->current_notification ];
 274  
 275          return $this->notification;
 276      }
 277  
 278      /**
 279       * Rewind the blogs and reset blog index.
 280       *
 281       * @since 1.9.0
 282       */
 283  	public function rewind_notifications() {
 284  
 285          $this->current_notification = -1;
 286  
 287          if ( $this->notification_count > 0 ) {
 288              $this->notification = $this->notifications[0];
 289          }
 290      }
 291  
 292      /**
 293       * Whether there are notifications left in the loop to iterate over.
 294       *
 295       * This method is used by {@link bp_notifications()} as part of the
 296       * while loop that controls iteration inside the notifications loop, eg:
 297       *     while ( bp_notifications() ) { ...
 298       *
 299       * @since 1.9.0
 300       *
 301       * @see bp_notifications()
 302       *
 303       * @return bool True if there are more notifications to show,
 304       *              otherwise false.
 305       */
 306  	public function notifications() {
 307  
 308          if ( $this->current_notification + 1 < $this->notification_count ) {
 309              return true;
 310  
 311          } elseif ( $this->current_notification + 1 === $this->notification_count ) {
 312  
 313              /**
 314               * Fires right before the rewinding of notification posts.
 315               *
 316               * @since 1.9.0
 317               */
 318              do_action( 'notifications_loop_end' );
 319  
 320              $this->rewind_notifications();
 321          }
 322  
 323          $this->in_the_loop = false;
 324          return false;
 325      }
 326  
 327      /**
 328       * Set up the current notification inside the loop.
 329       *
 330       * Used by {@link bp_the_notification()} to set up the current
 331       * notification data while looping, so that template tags used during
 332       * that iteration make reference to the current notification.
 333       *
 334       * @since 1.9.0
 335       *
 336       * @see bp_the_notification()
 337       */
 338  	public function the_notification() {
 339          $this->in_the_loop  = true;
 340          $this->notification = $this->next_notification();
 341  
 342          // Loop has just started.
 343          if ( 0 === $this->current_notification ) {
 344  
 345              /**
 346               * Fires if the current notification item is the first in the notification loop.
 347               *
 348               * @since 1.9.0
 349               */
 350              do_action( 'notifications_loop_start' );
 351          }
 352      }
 353  }


Generated: Tue Sep 21 01:01:37 2021 Cross-referenced by PHPXref 0.7.1