Changeset 9485 for trunk/src/bp-notifications/bp-notifications-classes.php
- Timestamp:
- 02/15/2015 12:48:56 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-notifications/bp-notifications-classes.php
r9352 r9485 1 1 <?php 2 3 2 /** 4 3 * BuddyPress Notifications Classes … … 15 14 defined( 'ABSPATH' ) || exit; 16 15 17 /** 18 * BuddyPress Notification items. 19 * 20 * Use this class to create, access, edit, or delete BuddyPress Notifications. 21 * 22 * @since BuddyPress (1.9.0) 23 */ 24 class BP_Notifications_Notification { 25 26 /** 27 * The notification ID. 28 * 29 * @since BuddyPress (1.9.0) 30 * @access public 31 * @var int 32 */ 33 public $id; 34 35 /** 36 * The ID of the item associated with the notification. 37 * 38 * @since BuddyPress (1.9.0) 39 * @access public 40 * @var int 41 */ 42 public $item_id; 43 44 /** 45 * The ID of the secondary item associated with the notification. 46 * 47 * @since BuddyPress (1.9.0) 48 * @access public 49 * @var int 50 */ 51 public $secondary_item_id = null; 52 53 /** 54 * The ID of the user the notification is associated with. 55 * 56 * @since BuddyPress (1.9.0) 57 * @access public 58 * @var int 59 */ 60 public $user_id; 61 62 /** 63 * The name of the component that the notification is for. 64 * 65 * @since BuddyPress (1.9.0) 66 * @access public 67 * @var string 68 */ 69 public $component_name; 70 71 /** 72 * The component action which the notification is related to. 73 * 74 * @since BuddyPress (1.9.0) 75 * @access public 76 * @var string 77 */ 78 public $component_action; 79 80 /** 81 * The date the notification was created. 82 * 83 * @since BuddyPress (1.9.0) 84 * @access public 85 * @var string 86 */ 87 public $date_notified; 88 89 /** 90 * Is the notification new, or has it already been read. 91 * 92 * @since BuddyPress (1.9.0) 93 * @access public 94 * @var bool 95 */ 96 public $is_new; 97 98 /** Public Methods ****************************************************/ 99 100 /** 101 * Constructor method. 102 * 103 * @since BuddyPress (1.9.0) 104 * 105 * @param int $id Optional. Provide an ID to access an existing 106 * notification item. 107 */ 108 public function __construct( $id = 0 ) { 109 if ( ! empty( $id ) ) { 110 $this->id = $id; 111 $this->populate(); 112 } 113 } 114 115 /** 116 * Update or insert notification details into the database. 117 * 118 * @since BuddyPress (1.9.0) 119 * 120 * @global wpdb $wpdb WordPress database object. 121 * 122 * @return bool True on success, false on failure. 123 */ 124 public function save() { 125 126 // Return value 127 $retval = false; 128 129 // Default data and format 130 $data = array( 131 'user_id' => $this->user_id, 132 'item_id' => $this->item_id, 133 'secondary_item_id' => $this->secondary_item_id, 134 'component_name' => $this->component_name, 135 'component_action' => $this->component_action, 136 'date_notified' => $this->date_notified, 137 'is_new' => $this->is_new, 138 ); 139 $data_format = array( '%d', '%d', '%d', '%s', '%s', '%s', '%d' ); 140 141 /** 142 * Fires before the current notification item gets saved. 143 * 144 * Please use this hook to filter the properties above. Each part will be passed in. 145 * 146 * @since BuddyPress (2.0.0) 147 * 148 * @param BP_Notifications_Notification Current instance of the notification item being saved. Passed by reference. 149 */ 150 do_action_ref_array( 'bp_notification_before_save', array( &$this ) ); 151 152 // Update 153 if ( ! empty( $this->id ) ) { 154 $result = self::_update( $data, array( 'ID' => $this->id ), $data_format, array( '%d' ) ); 155 156 // Insert 157 } else { 158 $result = self::_insert( $data, $data_format ); 159 } 160 161 // Set the notification ID if successful 162 if ( ! empty( $result ) && ! is_wp_error( $result ) ) { 163 global $wpdb; 164 165 $this->id = $wpdb->insert_id; 166 $retval = $wpdb->insert_id; 167 } 168 169 /** 170 * Fires after the current notification item gets saved. 171 * 172 * @since BuddyPress (2.0.0) 173 * 174 * @param BP_Notifications_Notification Current instance of the notification item being saved. Passed by reference. 175 */ 176 do_action_ref_array( 'bp_notification_after_save', array( &$this ) ); 177 178 // Return the result 179 return $retval; 180 } 181 182 /** 183 * Fetch data for an existing notification from the database. 184 * 185 * @since BuddyPress (1.9.0) 186 * 187 * @global BuddyPress $bp The one true BuddyPress instance. 188 * @global wpdb $wpdb WordPress database object. 189 */ 190 public function populate() { 191 global $wpdb; 192 193 $bp = buddypress(); 194 195 // Look for a notification 196 $notification = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->notifications->table_name} WHERE id = %d", $this->id ) ); 197 198 // Setup the notification data 199 if ( ! empty( $notification ) && ! is_wp_error( $notification ) ) { 200 $this->item_id = $notification->item_id; 201 $this->secondary_item_id = $notification->secondary_item_id; 202 $this->user_id = $notification->user_id; 203 $this->component_name = $notification->component_name; 204 $this->component_action = $notification->component_action; 205 $this->date_notified = $notification->date_notified; 206 $this->is_new = $notification->is_new; 207 } 208 } 209 210 /** Protected Static Methods ******************************************/ 211 212 /** 213 * Create a notification entry. 214 * 215 * @since BuddyPress (1.9.0) 216 * 217 * @param array $data { 218 * Array of notification data, passed to {@link wpdb::insert()}. 219 * @type int $user_id ID of the associated user. 220 * @type int $item_id ID of the associated item. 221 * @type int $secondary_item_id ID of the secondary associated item. 222 * @type string $component_name Name of the associated component. 223 * @type string $component_action Name of the associated component 224 * action. 225 * @type string $date_notified Timestamp of the notification. 226 * @type bool $is_new True if the notification is unread, otherwise 227 * false. 228 * } 229 * @param array $data_format See {@link wpdb::insert()}. 230 * @return int|false The number of rows inserted, or false on error. 231 */ 232 protected static function _insert( $data = array(), $data_format = array() ) { 233 global $wpdb; 234 return $wpdb->insert( buddypress()->notifications->table_name, $data, $data_format ); 235 } 236 237 /** 238 * Update notifications. 239 * 240 * @since BuddyPress (1.9.0) 241 * 242 * @see wpdb::update() for further description of paramater formats. 243 * 244 * @param array $data Array of notification data to update, passed to 245 * {@link wpdb::update()}. Accepts any property of a 246 * BP_Notification_Notification object. 247 * @param array $where The WHERE params as passed to wpdb::update(). 248 * Typically consists of array( 'ID' => $id ) to specify the ID 249 * of the item being updated. See {@link wpdb::update()}. 250 * @param array $data_format See {@link wpdb::insert()}. 251 * @param array $where_format See {@link wpdb::insert()}. 252 * @return int|false The number of rows updated, or false on error. 253 */ 254 protected static function _update( $data = array(), $where = array(), $data_format = array(), $where_format = array() ) { 255 global $wpdb; 256 return $wpdb->update( buddypress()->notifications->table_name, $data, $where, $data_format, $where_format ); 257 } 258 259 /** 260 * Delete notifications. 261 * 262 * @since BuddyPress (1.9.0) 263 * 264 * @see wpdb::update() for further description of paramater formats. 265 * 266 * @param array $where Array of WHERE clauses to filter by, passed to 267 * {@link wpdb::delete()}. Accepts any property of a 268 * BP_Notification_Notification object. 269 * @param array $where_format See {@link wpdb::insert()}. 270 * @return int|false The number of rows updated, or false on error. 271 */ 272 protected static function _delete( $where = array(), $where_format = array() ) { 273 global $wpdb; 274 return $wpdb->delete( buddypress()->notifications->table_name, $where, $where_format ); 275 } 276 277 /** 278 * Assemble the WHERE clause of a get() SQL statement. 279 * 280 * Used by BP_Notifications_Notification::get() to create its WHERE 281 * clause. 282 * 283 * @since BuddyPress (1.9.0) 284 * 285 * @param array $args See {@link BP_Notifications_Notification::get()} 286 * for more details. 287 * @return string WHERE clause. 288 */ 289 protected static function get_where_sql( $args = array() ) { 290 global $wpdb; 291 292 $where_conditions = array(); 293 $where = ''; 294 295 // id 296 if ( ! empty( $args['id'] ) ) { 297 $id_in = implode( ',', wp_parse_id_list( $args['id'] ) ); 298 $where_conditions['id'] = "id IN ({$id_in})"; 299 } 300 301 // user_id 302 if ( ! empty( $args['user_id'] ) ) { 303 $user_id_in = implode( ',', wp_parse_id_list( $args['user_id'] ) ); 304 $where_conditions['user_id'] = "user_id IN ({$user_id_in})"; 305 } 306 307 // item_id 308 if ( ! empty( $args['item_id'] ) ) { 309 $item_id_in = implode( ',', wp_parse_id_list( $args['item_id'] ) ); 310 $where_conditions['item_id'] = "item_id IN ({$item_id_in})"; 311 } 312 313 // secondary_item_id 314 if ( ! empty( $args['secondary_item_id'] ) ) { 315 $secondary_item_id_in = implode( ',', wp_parse_id_list( $args['secondary_item_id'] ) ); 316 $where_conditions['secondary_item_id'] = "secondary_item_id IN ({$secondary_item_id_in})"; 317 } 318 319 // component_name 320 if ( ! empty( $args['component_name'] ) ) { 321 if ( ! is_array( $args['component_name'] ) ) { 322 $component_names = explode( ',', $args['component_name'] ); 323 } else { 324 $component_names = $args['component_name']; 325 } 326 327 $cn_clean = array(); 328 foreach ( $component_names as $cn ) { 329 $cn_clean[] = $wpdb->prepare( '%s', $cn ); 330 } 331 332 $cn_in = implode( ',', $cn_clean ); 333 $where_conditions['component_name'] = "component_name IN ({$cn_in})"; 334 } 335 336 // component_action 337 if ( ! empty( $args['component_action'] ) ) { 338 if ( ! is_array( $args['component_action'] ) ) { 339 $component_actions = explode( ',', $args['component_action'] ); 340 } else { 341 $component_actions = $args['component_action']; 342 } 343 344 $ca_clean = array(); 345 foreach ( $component_actions as $ca ) { 346 $ca_clean[] = $wpdb->prepare( '%s', $ca ); 347 } 348 349 $ca_in = implode( ',', $ca_clean ); 350 $where_conditions['component_action'] = "component_action IN ({$ca_in})"; 351 } 352 353 // is_new 354 if ( ! empty( $args['is_new'] ) && 'both' !== $args['is_new'] ) { 355 $where_conditions['is_new'] = "is_new = 1"; 356 } elseif ( isset( $args['is_new'] ) && ( 0 === $args['is_new'] || false === $args['is_new'] ) ) { 357 $where_conditions['is_new'] = "is_new = 0"; 358 } 359 360 // search_terms 361 if ( ! empty( $args['search_terms'] ) ) { 362 $search_terms_like = '%' . bp_esc_like( $args['search_terms'] ) . '%'; 363 $where_conditions['search_terms'] = $wpdb->prepare( "( component_name LIKE %s OR component_action LIKE %s )", $search_terms_like, $search_terms_like ); 364 } 365 366 // Custom WHERE 367 if ( ! empty( $where_conditions ) ) { 368 $where = 'WHERE ' . implode( ' AND ', $where_conditions ); 369 } 370 371 return $where; 372 } 373 374 /** 375 * Assemble the ORDER BY clause of a get() SQL statement. 376 * 377 * Used by BP_Notifications_Notification::get() to create its ORDER BY 378 * clause. 379 * 380 * @since BuddyPress (1.9.0) 381 * 382 * @param array $args See {@link BP_Notifications_Notification::get()} 383 * for more details. 384 * @return string ORDER BY clause. 385 */ 386 protected static function get_order_by_sql( $args = array() ) { 387 388 // Setup local variable 389 $conditions = array(); 390 $retval = ''; 391 392 // Order by 393 if ( ! empty( $args['order_by'] ) ) { 394 $order_by = implode( ', ', (array) $args['order_by'] ); 395 $conditions['order_by'] = "{$order_by}"; 396 } 397 398 // Sort order direction 399 if ( ! empty( $args['sort_order'] ) && in_array( $args['sort_order'], array( 'ASC', 'DESC' ) ) ) { 400 $sort_order = $args['sort_order']; 401 $conditions['sort_order'] = "{$sort_order}"; 402 } 403 404 // Custom ORDER BY 405 if ( ! empty( $conditions ) ) { 406 $retval = 'ORDER BY ' . implode( ' ', $conditions ); 407 } 408 409 return $retval; 410 } 411 412 /** 413 * Assemble the LIMIT clause of a get() SQL statement. 414 * 415 * Used by BP_Notifications_Notification::get() to create its LIMIT clause. 416 * 417 * @since BuddyPress (1.9.0) 418 * 419 * @param array $args See {@link BP_Notifications_Notification::get()} 420 * for more details. 421 * @return string LIMIT clause. 422 */ 423 protected static function get_paged_sql( $args = array() ) { 424 global $wpdb; 425 426 // Setup local variable 427 $retval = ''; 428 429 // Custom LIMIT 430 if ( ! empty( $args['page'] ) && ! empty( $args['per_page'] ) ) { 431 $page = absint( $args['page'] ); 432 $per_page = absint( $args['per_page'] ); 433 $offset = $per_page * ( $page - 1 ); 434 $retval = $wpdb->prepare( "LIMIT %d, %d", $offset, $per_page ); 435 } 436 437 return $retval; 438 } 439 440 /** 441 * Assemble query clauses, based on arguments, to pass to $wpdb methods. 442 * 443 * The insert(), update(), and delete() methods of {@link wpdb} expect 444 * arguments of the following forms: 445 * 446 * - associative arrays whose key/value pairs are column => value, to 447 * be used in WHERE, SET, or VALUES clauses 448 * - arrays of "formats", which tell $wpdb->prepare() which type of 449 * value to expect when sanitizing (eg, array( '%s', '%d' )) 450 * 451 * This utility method can be used to assemble both kinds of params, 452 * out of a single set of associative array arguments, such as: 453 * 454 * $args = array( 455 * 'user_id' => 4, 456 * 'component_name' => 'groups', 457 * ); 458 * 459 * This will be converted to: 460 * 461 * array( 462 * 'data' => array( 463 * 'user_id' => 4, 464 * 'component_name' => 'groups', 465 * ), 466 * 'format' => array( 467 * '%d', 468 * '%s', 469 * ), 470 * ) 471 * 472 * which can easily be passed as arguments to the $wpdb methods. 473 * 474 * @since BuddyPress (1.9.0) 475 * 476 * @param $args Associative array of filter arguments. 477 * See {@BP_Notifications_Notification::get()} for a breakdown. 478 * @return array Associative array of 'data' and 'format' args. 479 */ 480 protected static function get_query_clauses( $args = array() ) { 481 $where_clauses = array( 482 'data' => array(), 483 'format' => array(), 484 ); 485 486 // id 487 if ( ! empty( $args['id'] ) ) { 488 $where_clauses['data']['id'] = absint( $args['id'] ); 489 $where_clauses['format'][] = '%d'; 490 } 491 492 // user_id 493 if ( ! empty( $args['user_id'] ) ) { 494 $where_clauses['data']['user_id'] = absint( $args['user_id'] ); 495 $where_clauses['format'][] = '%d'; 496 } 497 498 // item_id 499 if ( ! empty( $args['item_id'] ) ) { 500 $where_clauses['data']['item_id'] = absint( $args['item_id'] ); 501 $where_clauses['format'][] = '%d'; 502 } 503 504 // secondary_item_id 505 if ( ! empty( $args['secondary_item_id'] ) ) { 506 $where_clauses['data']['secondary_item_id'] = absint( $args['secondary_item_id'] ); 507 $where_clauses['format'][] = '%d'; 508 } 509 510 // component_name 511 if ( ! empty( $args['component_name'] ) ) { 512 $where_clauses['data']['component_name'] = $args['component_name']; 513 $where_clauses['format'][] = '%s'; 514 } 515 516 // component_action 517 if ( ! empty( $args['component_action'] ) ) { 518 $where_clauses['data']['component_action'] = $args['component_action']; 519 $where_clauses['format'][] = '%s'; 520 } 521 522 // is_new 523 if ( isset( $args['is_new'] ) ) { 524 $where_clauses['data']['is_new'] = ! empty( $args['is_new'] ) ? 1 : 0; 525 $where_clauses['format'][] = '%d'; 526 } 527 528 return $where_clauses; 529 } 530 531 /** Public Static Methods *********************************************/ 532 533 /** 534 * Check that a specific notification is for a specific user. 535 * 536 * @since BuddyPress (1.9.0) 537 * 538 * @param int $user_id ID of the user being checked. 539 * @param int $notification_id ID of the notification being checked. 540 * @return bool True if the notification belongs to the user, otherwise 541 * false. 542 */ 543 public static function check_access( $user_id, $notification_id ) { 544 global $wpdb; 545 546 $bp = buddypress(); 547 548 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->core->table_name_notifications} WHERE id = %d AND user_id = %d", $notification_id, $user_id ) ); 549 } 550 551 /** 552 * Get notifications, based on provided filter parameters. 553 * 554 * @since BuddyPress (1.9.0) 555 * 556 * @param array $args { 557 * Associative array of arguments. All arguments but $page and 558 * $per_page can be treated as filter values for get_where_sql() 559 * and get_query_clauses(). All items are optional. 560 * @type int|array $id ID of notification being updated. Can be an 561 * array of IDs. 562 * @type int|array $user_id ID of user being queried. Can be an 563 * array of user IDs. 564 * @type int|array $item_id ID of associated item. Can be an array 565 * of multiple item IDs. 566 * @type int|array $secondary_item_id ID of secondary associated 567 * item. Can be an array of multiple IDs. 568 * @type string|array $component_name Name of the component to 569 * filter by. Can be an array of component names. 570 * @type string|array $component_action Name of the action to 571 * filter by. Can be an array of actions. 572 * @type bool $is_new Whether to limit to new notifications. True 573 * returns only new notifications, false returns only non-new 574 * notifications. 'both' returns all. Default: true. 575 * @type string $search_terms Term to match against component_name 576 * or component_action fields. 577 * @type string $order_by Database column to order notifications by. 578 * @type string $sort_order Either 'ASC' or 'DESC'. 579 * @type string $order_by Field to order results by. 580 * @type string $sort_order ASC or DESC. 581 * @type int $page Number of the current page of results. Default: 582 * false (no pagination - all items). 583 * @type int $per_page Number of items to show per page. Default: 584 * false (no pagination - all items). 585 * } 586 * @return array Located notifications. 587 */ 588 public static function get( $args = array() ) { 589 global $wpdb; 590 591 // Parse the arguments 592 $r = wp_parse_args( $args, array( 593 'id' => false, 594 'user_id' => false, 595 'item_id' => false, 596 'secondary_item_id' => false, 597 'component_name' => bp_notifications_get_registered_components(), 598 'component_action' => false, 599 'is_new' => true, 600 'search_terms' => '', 601 'order_by' => false, 602 'sort_order' => false, 603 'page' => false, 604 'per_page' => false, 605 ) ); 606 607 // SELECT 608 $select_sql = "SELECT *"; 609 610 // FROM 611 $from_sql = "FROM " . buddypress()->notifications->table_name; 612 613 // WHERE 614 $where_sql = self::get_where_sql( array( 615 'id' => $r['id'], 616 'user_id' => $r['user_id'], 617 'item_id' => $r['item_id'], 618 'secondary_item_id' => $r['secondary_item_id'], 619 'component_name' => $r['component_name'], 620 'component_action' => $r['component_action'], 621 'is_new' => $r['is_new'], 622 'search_terms' => $r['search_terms'], 623 ) ); 624 625 // ORDER BY 626 $order_sql = self::get_order_by_sql( array( 627 'order_by' => $r['order_by'], 628 'sort_order' => $r['sort_order'] 629 ) ); 630 631 // LIMIT %d, %d 632 $pag_sql = self::get_paged_sql( array( 633 'page' => $r['page'], 634 'per_page' => $r['per_page'], 635 ) ); 636 637 $sql = "{$select_sql} {$from_sql} {$where_sql} {$order_sql} {$pag_sql}"; 638 639 return $wpdb->get_results( $sql ); 640 } 641 642 /** 643 * Get a count of total notifications matching a set of arguments. 644 * 645 * @since BuddyPress (1.9.0) 646 * 647 * @see BP_Notifications_Notification::get() for a description of 648 * arguments. 649 * 650 * @param array $args See {@link BP_Notifications_Notification::get()}. 651 * @return int Count of located items. 652 */ 653 public static function get_total_count( $args ) { 654 global $wpdb; 655 656 /** 657 * Default component_name to active_components 658 * 659 * @see http://buddypress.trac.wordpress.org/ticket/5300 660 */ 661 $args = wp_parse_args( $args, array( 662 'component_name' => bp_notifications_get_registered_components() 663 ) ); 664 665 // Load BuddyPress 666 $bp = buddypress(); 667 668 // Build the query 669 $select_sql = "SELECT COUNT(*)"; 670 $from_sql = "FROM {$bp->notifications->table_name}"; 671 $where_sql = self::get_where_sql( $args ); 672 $sql = "{$select_sql} {$from_sql} {$where_sql}"; 673 674 // Return the queried results 675 return $wpdb->get_var( $sql ); 676 } 677 678 /** 679 * Update notifications. 680 * 681 * @since BuddyPress (1.9.0) 682 * 683 * @see BP_Notifications_Notification::get() for a description of 684 * accepted update/where arguments. 685 * 686 * @param array $update_args Associative array of fields to update, 687 * and the values to update them to. Of the format 688 * array( 'user_id' => 4, 'component_name' => 'groups', ) 689 * @param array $where_args Associative array of columns/values, to 690 * determine which rows should be updated. Of the format 691 * array( 'item_id' => 7, 'component_action' => 'members', ) 692 * @return int|bool Number of rows updated on success, false on failure. 693 */ 694 public static function update( $update_args = array(), $where_args = array() ) { 695 $update = self::get_query_clauses( $update_args ); 696 $where = self::get_query_clauses( $where_args ); 697 698 // make sure we delete the notification cache for the user on update 699 if ( ! empty( $where_args['user_id'] ) ) { 700 wp_cache_delete( 'all_for_user_' . $where_args['user_id'], 'bp_notifications' ); 701 } 702 703 return self::_update( $update['data'], $where['data'], $update['format'], $where['format'] ); 704 } 705 706 /** 707 * Delete notifications. 708 * 709 * @since BuddyPress (1.9.0) 710 * 711 * @see BP_Notifications_Notification::get() for a description of 712 * accepted update/where arguments. 713 * 714 * @param array $args Associative array of columns/values, to determine 715 * which rows should be deleted. Of the format 716 * array( 'item_id' => 7, 'component_action' => 'members', ) 717 * @return int|bool Number of rows deleted on success, false on failure. 718 */ 719 public static function delete( $args = array() ) { 720 $where = self::get_query_clauses( $args ); 721 722 /** 723 * Fires before the deletion of a notification item. 724 * 725 * @since BuddyPress (2.0.0) 726 * 727 * @param array $args Associative array of columns/values, to determine 728 * which rows should be deleted. Of the format 729 * array( 'item_id' => 7, 'component_action' => 'members' ). 730 */ 731 do_action( 'bp_notification_before_delete', $args ); 732 733 return self::_delete( $where['data'], $where['format'] ); 734 } 735 736 /** Convenience methods ***********************************************/ 737 738 /** 739 * Delete a single notification by ID. 740 * 741 * @since BuddyPress (1.9.0) 742 * 743 * @see BP_Notifications_Notification::delete() for explanation of 744 * return value. 745 * 746 * @param int $id ID of the notification item to be deleted. 747 * @return bool True on success, false on failure. 748 */ 749 public static function delete_by_id( $id ) { 750 return self::delete( array( 751 'id' => $id, 752 ) ); 753 } 754 755 /** 756 * Fetch all the notifications in the database for a specific user. 757 * 758 * @since BuddyPress (1.9.0) 759 * 760 * @param int $user_id ID of the user whose notifications are being 761 * fetched. 762 * @param string $status Optional. Status of notifications to fetch. 763 * 'is_new' to get only unread items, 'all' to get all. 764 * @return array Associative array of notification items. 765 */ 766 public static function get_all_for_user( $user_id, $status = 'is_new' ) { 767 return self::get( array( 768 'user_id' => $user_id, 769 'is_new' => 'is_new' === $status, 770 ) ); 771 } 772 773 /** 774 * Fetch all the unread notifications in the database for a specific user. 775 * 776 * @since BuddyPress (1.9.0) 777 * 778 * @param int $user_id ID of the user whose notifications are being 779 * fetched. 780 * @return array Associative array of unread notification items. 781 */ 782 public static function get_unread_for_user( $user_id = 0 ) { 783 return self::get( array( 784 'user_id' => $user_id, 785 'is_new' => true, 786 ) ); 787 } 788 789 /** 790 * Fetch all the read notifications in the database for a specific user. 791 * 792 * @since BuddyPress (1.9.0) 793 * 794 * @param int $user_id ID of the user whose notifications are being 795 * fetched. 796 * @return array Associative array of unread notification items. 797 */ 798 public static function get_read_for_user( $user_id = 0 ) { 799 return self::get( array( 800 'user_id' => $user_id, 801 'is_new' => false, 802 ) ); 803 } 804 805 /** 806 * Get unread notifications for a user, in a pagination-friendly format. 807 * 808 * @since BuddyPress (1.9.0) 809 * 810 * @param array $args { 811 * Array of arguments. 812 * @type int $user_id ID of the user for whom the notifications are 813 * being fetched. Default: logged-in user ID. 814 * @type bool $is_new Whether to limit the query to unread 815 * notifications. Default: true. 816 * @type int $page Number of the page to return. Default: 1. 817 * @type int $per_page Number of results to display per page. 818 * Default: 10. 819 * @type string $search_terms Optional. A term to search against in 820 * the 'component_name' and 'component_action' columns. 821 * } 822 * @return array { 823 * @type array $notifications Array of notification results. 824 * @type int $total Count of all located notifications matching 825 * the query params. 826 * } 827 */ 828 public static function get_current_notifications_for_user( $args = array() ) { 829 $r = wp_parse_args( $args, array( 830 'user_id' => bp_loggedin_user_id(), 831 'is_new' => true, 832 'page' => 1, 833 'per_page' => 25, 834 'search_terms' => '', 835 ) ); 836 837 $notifications = self::get( $r ); 838 839 // Bail if no notifications 840 if ( empty( $notifications ) ) { 841 return false; 842 } 843 844 $total_count = self::get_total_count( $r ); 845 846 return array( 'notifications' => &$notifications, 'total' => $total_count ); 847 } 848 849 /** Mark **************************************************************/ 850 851 /** 852 * Mark all user notifications as read. 853 * 854 * @since BuddyPress (1.9.0) 855 * 856 * @param int $user_id The ID of the user who the notifications 857 * are for. 858 * @param int $is_new Mark as read (1) or unread (0). 859 */ 860 public static function mark_all_for_user( $user_id, $is_new = 0, $item_id = 0, $component_name = '', $component_action = '', $secondary_item_id = 0 ) { 861 862 // Values to be updated 863 $update_args = array( 864 'is_new' => $is_new, 865 ); 866 867 // WHERE clauses 868 $where_args = array( 869 'user_id' => $user_id, 870 ); 871 872 if ( ! empty( $item_id ) ) { 873 $where_args['item_id'] = $item_id; 874 } 875 876 if ( ! empty( $component_name ) ) { 877 $where_args['component_name'] = $component_name; 878 } 879 880 if ( ! empty( $component_action ) ) { 881 $where_args['component_action'] = $component_action; 882 } 883 884 if ( ! empty( $secondary_item_id ) ) { 885 $where_args['secondary_item_id'] = $secondary_item_id; 886 } 887 888 return self::update( $update_args, $where_args ); 889 } 890 891 /** 892 * Mark all notifications from a user as read. 893 * 894 * @since BuddyPress (1.9.0) 895 * 896 * @param int $user_id The ID of the user who the notifications are from. 897 * @param int $is_new Mark as read (1) or unread (0). 898 */ 899 public static function mark_all_from_user( $user_id, $is_new = 0, $component_name = '', $component_action = '', $secondary_item_id = 0 ) { 900 901 // Values to be updated 902 $update_args = array( 903 'is_new' => $is_new, 904 ); 905 906 // WHERE clauses 907 $where_args = array( 908 'item_id' => $user_id, 909 ); 910 911 if ( ! empty( $component_name ) ) { 912 $where_args['component_name'] = $component_name; 913 } 914 915 if ( ! empty( $component_action ) ) { 916 $where_args['component_action'] = $component_action; 917 } 918 919 if ( ! empty( $secondary_item_id ) ) { 920 $where_args['secondary_item_id'] = $secondary_item_id; 921 } 922 923 return self::update( $update_args, $where_args ); 924 } 925 926 /** 927 * Mark all notifications for all users as read by item id, and optional 928 * secondary item id, and component name and action. 929 * 930 * @since BuddyPress (1.9.0) 931 * 932 * @param int $item_id The ID of the item associated with the 933 * notifications. 934 * @param string $component_name The component that the notifications 935 * are associated with. 936 * @param string $component_action The action that the notifications 937 * are associated with. 938 * @param string $secondary_item_id Optional. ID of the secondary 939 * associated item. 940 */ 941 public static function mark_all_by_type( $item_id, $is_new = 0, $component_name = '', $component_action = '', $secondary_item_id = 0 ) { 942 943 // Values to be updated 944 $update_args = array( 945 'is_new' => $is_new, 946 ); 947 948 // WHERE clauses 949 $where_args = array( 950 'item_id' => $item_id, 951 ); 952 953 if ( ! empty( $component_name ) ) { 954 $where_args['component_name'] = $component_name; 955 } 956 957 if ( ! empty( $component_action ) ) { 958 $where_args['component_action'] = $component_action; 959 } 960 961 if ( ! empty( $secondary_item_id ) ) { 962 $where_args['secondary_item_id'] = $secondary_item_id; 963 } 964 965 return self::update( $update_args, $where_args ); 966 } 967 } 16 require __DIR__ . '/classes/class-bp-notifications-notification.php';
Note: See TracChangeset
for help on using the changeset viewer.