Skip to:
Content

BuddyPress.org

Changeset 10437


Ignore:
Timestamp:
01/05/2016 03:23:11 PM (6 years ago)
Author:
boonebgorges
Message:

Delete a user's notifications when the user is deleted.

Props henry.wright, Mamaduka.
Fixes #6681.

Location:
trunk
Files:
2 edited

Legend:

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

    r10417 r10437  
    422422}
    423423
     424/**
     425 * Delete a user's notifications when the user is deleted.
     426 *
     427 * @since 2.5.0
     428 *
     429 * @param int $user_id ID of the user who is about to be deleted.
     430 * @return int|bool The number of rows deleted, or false on error.
     431 */
     432function bp_notifications_delete_notifications_on_user_delete( $user_id ) {
     433    return BP_Notifications_Notification::delete( array(
     434        'user_id'           => $user_id,
     435        'item_id'           => false,
     436        'secondary_item_id' => false,
     437        'component_action'  => false,
     438        'component_name'    => false,
     439    ) );
     440}
     441add_action( 'wpmu_delete_user', 'bp_notifications_delete_notifications_on_user_delete' );
     442add_action( 'delete_user', 'bp_notifications_delete_notifications_on_user_delete' );
     443
    424444/** Mark **********************************************************************/
    425445
  • trunk/tests/phpunit/testcases/notifications/functions.php

    r9888 r10437  
    234234        $this->assertEquals( 2, buddypress()->notifications->query_loop->total_notification_count );
    235235    }
     236
     237    /**
     238     * @group bp_notifications_delete_notifications_on_user_delete
     239     * @ticket BP6681
     240     */
     241    public function test_bp_notifications_delete_notifications_on_user_delete_should_delete_all_notifications() {
     242        $u = $this->factory->user->create();
     243
     244        // Create notifications
     245        $n1 = $this->factory->notification->create( array(
     246            'component_name'    => 'messages',
     247            'component_action'  => 'new_message',
     248            'item_id'           => 99,
     249            'user_id'           => $u,
     250        ) );
     251
     252        $n2 = $this->factory->notification->create( array(
     253            'component_name'    => 'activity',
     254            'component_action'  => 'new_at_mention',
     255            'item_id'           => 99,
     256            'user_id'           => $u,
     257        ) );
     258
     259        $n3 = $this->factory->notification->create( array(
     260            'component_name' => 'groups',
     261            'user_id'        => $u,
     262        ) );
     263
     264        $n4 = $this->factory->notification->create( array(
     265            'component_name'   => 'friends',
     266            'component_action' => 'friendship_request',
     267            'user_id'          => $u,
     268        ) );
     269
     270        // Create notification for non-core component
     271        $n5 = $this->factory->notification->create( array(
     272            'component_name'    => 'foo',
     273            'component_action'  => 'bar',
     274            'item_id'           => 99,
     275            'user_id'           => $u,
     276        ) );
     277
     278        global $wpdb, $bp;
     279
     280        /**
     281         * Can't use BP_Notifications_Notification::get(), because class::parse_args,
     282         * checks against bp_notifications_get_registered_components()
     283         * and if component is disabled it will be ignored.
     284         */
     285        $query = $wpdb->prepare( "SELECT id FROM {$bp->notifications->table_name} WHERE user_id = %d and is_new = 1", $u );
     286
     287        // Make sure notifications have been added.
     288        $found1 = $wpdb->get_col( $query );
     289        $this->assertEqualSets( array( $n1, $n2, $n3, $n4, $n5 ), $found1 );
     290
     291        wp_delete_user( $u );
     292
     293        // Check if notifications are deleted.
     294        $found2 = $wpdb->get_col( $query );
     295        $this->assertEmpty( $found2 );
     296    }
    236297}
Note: See TracChangeset for help on using the changeset viewer.