Ticket #5558: 5558.01.patch
File 5558.01.patch, 9.3 KB (added by , 11 years ago) |
---|
-
bp-notifications/bp-notifications-classes.php
542 542 * notifications. 'both' returns all. Default: true. 543 543 * @type string $search_terms Term to match against component_name 544 544 * or component_action fields. 545 * @type string $order_by Database column to order notifications by. 546 * @type string $sort_order Either 'ASC' or 'DESC'. 545 547 * @type string $order_by Field to order results by. 546 548 * @type string $sort_order ASC or DESC. 547 549 * @type int $page Number of the current page of results. Default: -
bp-notifications/bp-notifications-template.php
220 220 /** 221 221 * Constructor method. 222 222 * 223 * @see bp_has_notifications() For information on the array format. 224 * 223 225 * @since BuddyPress (1.9.0) 224 226 * 225 227 * @param array $args { 226 * @type int $user_id ID of the user to whom the displayed 227 * notifications belong. 228 * @type bool $is_new Whether to limit the query to unread 229 * notifications. Default: true. 230 * @type int $page Number of the page of results to return. 231 * Will be overridden by URL parameter. Default: 1. 232 * @type int $per_page Number of results to return per page. 233 * Will be overridden by URL parameter. Default: 25. 234 * @type int $max Optional. Max results to display. 235 * @type string $search_terms Optional. Term to match against 236 * component_name and component_action. 237 * @type string $page_arg URL argument to use for pagination. 238 * Default: 'npage'. 228 * An array of arguments. See {@link bp_has_notifications()} 229 * for more details. 239 230 * } 240 231 */ 241 232 public function __construct( $args = array() ) { 242 233 243 234 // Parse arguments 244 235 $r = wp_parse_args( $args, array( 245 'user_id' => 0, 246 'is_new' => true, 247 'page' => 1, 248 'per_page' => 25, 249 'order_by' => 'date_notified', 250 'sort_order' => 'DESC', 251 'max' => null, 252 'search_terms' => '', 253 'page_arg' => 'npage', 236 'id' => false, 237 'user_id' => 0, 238 'secondary_item_id' => false, 239 'component_name' => bp_notifications_get_registered_components(), 240 'component_action' => false, 241 'is_new' => true, 242 'search_terms' => '', 243 'order_by' => 'date_notified', 244 'sort_order' => 'DESC', 245 'page' => 1, 246 'per_page' => 25, 247 'max' => null, 248 'page_arg' => 'npage', 254 249 ) ); 255 250 256 251 // Overrides 257 252 258 253 // Set which pagination page 259 254 if ( isset( $_GET[ $r['page_arg'] ] ) ) { 260 $pag_page = intval( $_GET[ $r['page_arg'] ] ); 261 } else { 262 $pag_page = $r['page']; 255 $r['page'] = intval( $_GET[ $r['page_arg'] ] ); 263 256 } 264 257 265 258 // Set the number to show per page 266 259 if ( isset( $_GET['num'] ) ) { 267 $ pag_num= intval( $_GET['num'] );260 $r['per_page'] = intval( $_GET['num'] ); 268 261 } else { 269 $ pag_num= intval( $r['per_page'] );262 $r['per_page'] = intval( $r['per_page'] ); 270 263 } 271 264 272 265 // Sort order direction 273 266 $orders = array( 'ASC', 'DESC' ); 274 267 if ( ! empty( $_GET['sort_order'] ) && in_array( $_GET['sort_order'], $orders ) ) { 275 $ sort_order= $_GET['sort_order'];268 $r['sort_order'] = $_GET['sort_order']; 276 269 } else { 277 $ sort_order= in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC';270 $r['sort_order'] = in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC'; 278 271 } 279 272 280 273 // Setup variables 281 $this->pag_page = $ pag_page;282 $this->pag_num = $ pag_num;274 $this->pag_page = $r['page']; 275 $this->pag_num = $r['per_page']; 283 276 $this->user_id = $r['user_id']; 284 277 $this->is_new = $r['is_new']; 285 278 $this->search_terms = $r['search_terms']; 286 279 $this->page_arg = $r['page_arg']; 287 280 $this->order_by = $r['order_by']; 288 $this->sort_order = $sort_order; 289 290 // Get the notifications 291 $notifications = BP_Notifications_Notification::get_current_notifications_for_user( array( 292 'user_id' => $this->user_id, 293 'is_new' => $this->is_new, 294 'page' => $this->pag_page, 295 'per_page' => $this->pag_num, 296 'search_terms' => $this->search_terms, 297 'order_by' => $this->order_by, 298 'sort_order' => $this->sort_order, 299 ) ); 281 $this->sort_order = $r['sort_order']; 300 282 301 283 // Setup the notifications to loop through 302 $this->notifications = $notifications['notifications'];303 $this->total_notification_count = $notifications['total'];284 $this->notifications = BP_Notifications_Notification::get( $r ); 285 $this->total_notification_count = BP_Notifications_Notification::get_total_count( $r ); 304 286 305 287 if ( empty( $this->notifications ) ) { 306 288 $this->notification_count = 0; 307 289 $this->total_notification_count = 0; 308 290 309 291 } else { 310 if ( ! empty( $ max) ) {311 if ( $ max>= count( $this->notifications ) ) {292 if ( ! empty( $r['max'] ) ) { 293 if ( $r['max'] >= count( $this->notifications ) ) { 312 294 $this->notification_count = count( $this->notifications ); 313 295 } else { 314 $this->notification_count = (int) $ max;296 $this->notification_count = (int) $r['max']; 315 297 } 316 298 } else { 317 299 $this->notification_count = count( $this->notifications ); … … 447 429 * @param array $args { 448 430 * Arguments for limiting the contents of the notifications loop. Can be 449 431 * passed as an associative array, or as a URL query string. 450 * @type int $user_id ID of the user to whom notifications belong. Default: 451 * ID of the logged-in user. 452 * @type bool $is_new Whether to limit query to unread notifications. 453 * Default: when viewing the 'unread' tab, defaults to true; when 454 * viewing the 'read' tab, defaults to false. 455 * @type int $page The page of notifications being fetched. Default: 1. 456 * @type int $per_page Number of items to display on a page. Default: 25. 432 * 433 * See {@link BP_Notifications_Notification::get()} for detailed 434 * information on the arguments. In addition, also supports: 435 * 457 436 * @type int $max Optional. Max items to display. Default: false. 458 * @type string $search_terms Optional. Term to match against459 * component_name and component_action.460 437 * @type string $page_arg URL argument to use for pagination. 461 438 * Default: 'npage'. 462 439 * } … … 468 445 $is_new = 1; 469 446 } elseif ( bp_is_current_action( 'read' ) ) { 470 447 $is_new = 0; 448 449 // not on a notifications page? default to fetch new notifications 450 } else { 451 $is_new = 1; 471 452 } 472 453 473 454 // Get the user ID … … 479 460 480 461 // Parse the args 481 462 $r = bp_parse_args( $args, array( 482 'user_id' => $user_id, 483 'is_new' => $is_new, 484 'page' => 1, 485 'per_page' => 25, 486 'max' => false, 487 'search_terms' => isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '', 488 'page_arg' => 'npage', 463 'id' => false, 464 'user_id' => $user_id, 465 'secondary_item_id' => false, 466 'component_name' => bp_notifications_get_registered_components(), 467 'component_action' => false, 468 'is_new' => $is_new, 469 'search_terms' => isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '', 470 'order_by' => 'date_notified', 471 'sort_order' => 'DESC', 472 'page' => 1, 473 'per_page' => 25, 474 475 // these are additional arguments that are not available in 476 // BP_Notifications_Notification::get() 477 'max' => false, 478 'page_arg' => 'npage', 489 479 ), 'has_notifications' ); 490 480 491 481 // Get the notifications -
tests/testcases/notifications/functions.php
120 120 // assert 121 121 $this->assertEquals( 0, $n ); 122 122 } 123 124 /** 125 * @group bp_has_notifications 126 */ 127 public function test_bp_has_notifications_filtering() { 128 $u1 = $this->create_user(); 129 $u2 = $this->create_user(); 130 131 // create a mixture of different notifications 132 $n1 = $this->factory->notification->create( array( 133 'component_name' => 'messages', 134 'component_action' => 'new_message', 135 'item_id' => 99, 136 'user_id' => $u2, 137 'secondary_item_id' => $u1, 138 'is_new' => true, 139 ) ); 140 141 $n2 = $this->factory->notification->create( array( 142 'component_name' => 'activity', 143 'component_action' => 'new_at_mention', 144 'item_id' => 99, 145 'user_id' => $u2, 146 'secondary_item_id' => $u1, 147 'is_new' => true, 148 ) ); 149 150 $n3 = $this->factory->notification->create( array( 151 'component_name' => 'activity', 152 'component_action' => 'new_at_mention', 153 'item_id' => 100, 154 'user_id' => $u2, 155 'secondary_item_id' => $u1, 156 'is_new' => true, 157 ) ); 158 159 // now fetch only activity notifications 160 bp_has_notifications( array( 161 'component_name' => 'activity', 162 ) ); 163 164 // assert 165 $this->assertEquals( 2, buddypress()->notifications->query_loop->total_notification_count ); 166 } 123 167 }