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


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1