Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/20/2025 07:10:28 PM (17 months ago)
Author:
dcavins
Message:

Restrict bulk notification management to owner (14.0 branch).

When attempting to manage notifications in bulk, ensure that the current user is either a site admin or owns all of the notifications specified.

Many thanks to Brian Mungah for responsibly reporting the problem.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/12.0/src/bp-notifications/bp-notifications-functions.php

    r13499 r14107  
    669669
    670670/**
     671 * Mark a batch of notifications as read or unread or delete them.
     672 *
     673 * @since 12.5.3 (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 */
     679function 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/**
    671721 * Check if a user has access to a specific notification.
    672722 *
Note: See TracChangeset for help on using the changeset viewer.