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