- Timestamp:
- 02/05/2016 05:26:45 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-notifications/bp-notifications-template.php
r10420 r10523 11 11 defined( 'ABSPATH' ) || exit; 12 12 13 require dirname( __FILE__ ) . '/classes/class-bp-notifications-template.php'; 14 13 15 /** 14 16 * Output the notifications component slug. … … 121 123 return apply_filters( 'bp_get_notifications_unread_permalink', $retval ); 122 124 } 123 124 /** Main Loop *****************************************************************/125 126 /**127 * The main notifications template loop class.128 *129 * Responsible for loading a group of notifications into a loop for display.130 *131 * @since 1.9.0132 */133 class BP_Notifications_Template {134 135 /**136 * The loop iterator.137 *138 * @since 1.9.0139 * @var int140 */141 public $current_notification = -1;142 143 /**144 * The number of notifications returned by the paged query.145 *146 * @since 1.9.0147 * @var int148 */149 public $current_notification_count;150 151 /**152 * Total number of notifications matching the query.153 *154 * @since 1.9.0155 * @var int156 */157 public $total_notification_count;158 159 /**160 * Array of notifications located by the query.161 *162 * @since 1.9.0163 * @var array164 */165 public $notifications;166 167 /**168 * The notification object currently being iterated on.169 *170 * @since 1.9.0171 * @var object172 */173 public $notification;174 175 /**176 * A flag for whether the loop is currently being iterated.177 *178 * @since 1.9.0179 * @var bool180 */181 public $in_the_loop;182 183 /**184 * The ID of the user to whom the displayed notifications belong.185 *186 * @since 1.9.0187 * @var int188 */189 public $user_id;190 191 /**192 * The page number being requested.193 *194 * @since 1.9.0195 * @var int196 */197 public $pag_page;198 199 /**200 * The $_GET argument used in URLs for determining pagination.201 *202 * @since 1.9.0203 * @var int204 */205 public $pag_arg;206 207 /**208 * The number of items to display per page of results.209 *210 * @since 1.9.0211 * @var int212 */213 public $pag_num;214 215 /**216 * An HTML string containing pagination links.217 *218 * @since 1.9.0219 * @var string220 */221 public $pag_links;222 223 /**224 * A string to match against.225 *226 * @since 1.9.0227 * @var string228 */229 public $search_terms;230 231 /**232 * A database column to order the results by.233 *234 * @since 1.9.0235 * @var string236 */237 public $order_by;238 239 /**240 * The direction to sort the results (ASC or DESC).241 *242 * @since 1.9.0243 * @var string244 */245 public $sort_order;246 247 /**248 * Array of variables used in this notification query.249 *250 * @since 2.2.2251 * @var array252 */253 public $query_vars;254 255 /**256 * Constructor method.257 *258 * @see bp_has_notifications() For information on the array format.259 *260 * @since 1.9.0261 *262 * @param array $args {263 * An array of arguments. See {@link bp_has_notifications()}264 * for more details.265 * }266 */267 public function __construct( $args = array() ) {268 269 // Parse arguments.270 $r = wp_parse_args( $args, array(271 'id' => false,272 'user_id' => 0,273 'item_id' => false,274 'secondary_item_id' => false,275 'component_name' => bp_notifications_get_registered_components(),276 'component_action' => false,277 'is_new' => true,278 'search_terms' => '',279 'order_by' => 'date_notified',280 'sort_order' => 'DESC',281 'page_arg' => 'npage',282 'page' => 1,283 'per_page' => 25,284 'max' => null,285 'meta_query' => false,286 'date_query' => false287 ) );288 289 // Sort order direction.290 $orders = array( 'ASC', 'DESC' );291 if ( ! empty( $_GET['sort_order'] ) && in_array( $_GET['sort_order'], $orders ) ) {292 $r['sort_order'] = $_GET['sort_order'];293 } else {294 $r['sort_order'] = in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC';295 }296 297 // Setup variables.298 $this->pag_arg = sanitize_key( $r['page_arg'] );299 $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] );300 $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] );301 $this->user_id = $r['user_id'];302 $this->is_new = $r['is_new'];303 $this->search_terms = $r['search_terms'];304 $this->order_by = $r['order_by'];305 $this->sort_order = $r['sort_order'];306 $this->query_vars = array(307 'id' => $r['id'],308 'user_id' => $this->user_id,309 'item_id' => $r['item_id'],310 'secondary_item_id' => $r['secondary_item_id'],311 'component_name' => $r['component_name'],312 'component_action' => $r['component_action'],313 'meta_query' => $r['meta_query'],314 'date_query' => $r['date_query'],315 'is_new' => $this->is_new,316 'search_terms' => $this->search_terms,317 'order_by' => $this->order_by,318 'sort_order' => $this->sort_order,319 'page' => $this->pag_page,320 'per_page' => $this->pag_num,321 );322 323 // Setup the notifications to loop through.324 $this->notifications = BP_Notifications_Notification::get( $this->query_vars );325 $this->total_notification_count = BP_Notifications_Notification::get_total_count( $this->query_vars );326 327 if ( empty( $this->notifications ) ) {328 $this->notification_count = 0;329 $this->total_notification_count = 0;330 331 } else {332 if ( ! empty( $r['max'] ) ) {333 if ( $r['max'] >= count( $this->notifications ) ) {334 $this->notification_count = count( $this->notifications );335 } else {336 $this->notification_count = (int) $r['max'];337 }338 } else {339 $this->notification_count = count( $this->notifications );340 }341 }342 343 if ( (int) $this->total_notification_count && (int) $this->pag_num ) {344 $add_args = array(345 'sort_order' => $this->sort_order,346 );347 348 $this->pag_links = paginate_links( array(349 'base' => add_query_arg( $this->pag_arg, '%#%' ),350 'format' => '',351 'total' => ceil( (int) $this->total_notification_count / (int) $this->pag_num ),352 'current' => $this->pag_page,353 'prev_text' => _x( '←', 'Notifications pagination previous text', 'buddypress' ),354 'next_text' => _x( '→', 'Notifications pagination next text', 'buddypress' ),355 'mid_size' => 1,356 'add_args' => $add_args,357 ) );358 }359 }360 361 /**362 * Whether there are notifications available in the loop.363 *364 * @since 1.9.0365 *366 * @see bp_has_notifications()367 *368 * @return bool True if there are items in the loop, otherwise false.369 */370 public function has_notifications() {371 if ( $this->notification_count ) {372 return true;373 }374 375 return false;376 }377 378 /**379 * Set up the next notification and iterate index.380 *381 * @since 1.9.0382 *383 * @return object The next notification to iterate over.384 */385 public function next_notification() {386 387 $this->current_notification++;388 389 $this->notification = $this->notifications[ $this->current_notification ];390 391 return $this->notification;392 }393 394 /**395 * Rewind the blogs and reset blog index.396 *397 * @since 1.9.0398 */399 public function rewind_notifications() {400 401 $this->current_notification = -1;402 403 if ( $this->notification_count > 0 ) {404 $this->notification = $this->notifications[0];405 }406 }407 408 /**409 * Whether there are notifications left in the loop to iterate over.410 *411 * This method is used by {@link bp_notifications()} as part of the412 * while loop that controls iteration inside the notifications loop, eg:413 * while ( bp_notifications() ) { ...414 *415 * @since 1.9.0416 *417 * @see bp_notifications()418 *419 * @return bool True if there are more notifications to show,420 * otherwise false.421 */422 public function notifications() {423 424 if ( $this->current_notification + 1 < $this->notification_count ) {425 return true;426 427 } elseif ( $this->current_notification + 1 == $this->notification_count ) {428 429 /**430 * Fires right before the rewinding of notification posts.431 *432 * @since 1.9.0433 */434 do_action( 'notifications_loop_end');435 436 $this->rewind_notifications();437 }438 439 $this->in_the_loop = false;440 return false;441 }442 443 /**444 * Set up the current notification inside the loop.445 *446 * Used by {@link bp_the_notification()} to set up the current447 * notification data while looping, so that template tags used during448 * that iteration make reference to the current notification.449 *450 * @since 1.9.0451 *452 * @see bp_the_notification()453 */454 public function the_notification() {455 $this->in_the_loop = true;456 $this->notification = $this->next_notification();457 458 // Loop has just started.459 if ( 0 === $this->current_notification ) {460 461 /**462 * Fires if the current notification item is the first in the notification loop.463 *464 * @since 1.9.0465 */466 do_action( 'notifications_loop_start' );467 }468 }469 }470 125 471 126 /** The Loop ******************************************************************/
Note: See TracChangeset
for help on using the changeset viewer.