Changeset 14116
- Timestamp:
- 03/20/2025 08:24:38 PM (3 months ago)
- Location:
- branches/11.0
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/11.0/src/bp-notifications/actions/bulk-manage.php
r13112 r14116 41 41 42 42 // Delete, mark as read or unread depending on the user 'action'. 43 $result = bp_notifications_bulk_manage_notifications( $action, $notifications ); 44 45 // Set message depending on the user 'action'. 43 46 switch ( $action ) { 44 47 case 'delete': 45 bp_notifications_delete_notifications_by_ids( $notifications );46 48 bp_core_add_message( __( 'Notifications deleted.', 'buddypress' ) ); 47 49 break; 48 50 49 51 case 'read': 50 bp_notifications_mark_notifications_by_ids( $notifications, false );51 52 bp_core_add_message( __( 'Notifications marked as read', 'buddypress' ) ); 52 53 break; 53 54 54 55 case 'unread': 55 bp_notifications_mark_notifications_by_ids( $notifications, true );56 56 bp_core_add_message( __( 'Notifications marked as unread.', 'buddypress' ) ); 57 57 break; -
branches/11.0/src/bp-notifications/bp-notifications-functions.php
r13112 r14116 669 669 670 670 /** 671 * Mark a batch of notifications as read or unread or delete them. 672 * 673 * @since 11.4.4 (Backported from 14.3.4) 674 * 675 * @param string $user_id Action to run on notifications. 676 * @param array $notification_ids IDs of the notifications to change. 677 * @return bool True if the action run returned true. 678 */ 679 function bp_notifications_bulk_manage_notifications( $action, $notification_ids = array() ) { 680 $notification_ids = wp_parse_id_list( $notification_ids ); 681 if ( empty( $notification_ids ) ) { 682 return false; 683 } 684 685 if ( ! current_user_can( 'bp_manage' ) ) { 686 // Regular users can only manage their own notifications. 687 $all_user_notifications = BP_Notifications_Notification::get( 688 array( 689 'user_id' => bp_loggedin_user_id(), 690 'is_new' => 'both', // Allow unread and read notices to be found. 691 'update_meta_cache' => false, 692 ) 693 ); 694 $all_user_notifications_ids = wp_list_pluck( $all_user_notifications, 'id' ); 695 $notification_ids = array_intersect( $notification_ids, $all_user_notifications_ids ); 696 if ( empty( $notification_ids ) ) { 697 return false; 698 } 699 } 700 701 // Delete, mark as read or unread depending on the 'action'. 702 $result = false; 703 switch ( $action ) { 704 case 'delete': 705 $result = bp_notifications_delete_notifications_by_ids( $notification_ids ); 706 break; 707 708 case 'read': 709 $result = bp_notifications_mark_notifications_by_ids( $notification_ids, false ); 710 break; 711 712 case 'unread': 713 $result = bp_notifications_mark_notifications_by_ids( $notification_ids, true ); 714 break; 715 } 716 717 return ( bool ) $result; 718 } 719 720 /** 671 721 * Check if a user has access to a specific notification. 672 722 * -
branches/11.0/tests/phpunit/testcases/notifications/functions.php
r13243 r14116 501 501 $this->assertTrue( 1 === (int) $n_obj->is_new ); 502 502 } 503 504 /** 505 * @group bulk_manage_notifications 506 */ 507 public function test_bp_notifications_bulk_manage_notifications_user_must_own_items() { 508 $u1 = self::factory()->user->create(); 509 $u2 = self::factory()->user->create(); 510 511 // Create notifications 512 $n1 = self::factory()->notification->create( array( 513 'component_name' => 'messages', 514 'component_action' => 'new_message', 515 'item_id' => 99, 516 'user_id' => $u1, 517 ) ); 518 $n2 = self::factory()->notification->create( array( 519 'component_name' => 'messages', 520 'component_action' => 'new_message', 521 'item_id' => 100, 522 'user_id' => $u1, 523 ) ); 524 $n3 = self::factory()->notification->create( array( 525 'component_name' => 'messages', 526 'component_action' => 'new_message', 527 'item_id' => 101, 528 'user_id' => $u2, 529 ) ); 530 531 wp_set_current_user( $u2 ); 532 // Attempt to mark all as read. 533 bp_notifications_bulk_manage_notifications( 'read', array( $n1, $n2, $n3 ) ); 534 535 // Check status of $n2 (which shouldn't be affected). 536 $n_get = BP_Notifications_Notification::get( 537 array( 538 'id' => $n2, 539 'component_name' => 'messages', 540 'component_action' => 'new_message', 541 'is_new' => 'both', 542 ) 543 ); 544 $n_obj = reset( $n_get ); 545 $this->assertTrue( 1 === (int) $n_obj->is_new ); 546 547 // Check status of $n3 (which should be affected). 548 $n_get = BP_Notifications_Notification::get( 549 array( 550 'id' => $n3, 551 'component_name' => 'messages', 552 'component_action' => 'new_message', 553 'is_new' => 'both', 554 ) 555 ); 556 $n_obj = reset( $n_get ); 557 $this->assertTrue( 0 === (int) $n_obj->is_new ); 558 } 559 503 560 }
Note: See TracChangeset
for help on using the changeset viewer.