Skip to:
Content

BuddyPress.org

Ticket #5857: 5857.03.patch

File 5857.03.patch, 2.8 KB (added by DJPaul, 9 years ago)
  • src/bp-blogs/bp-blogs-functions.php

    diff --git a/src/bp-blogs/bp-blogs-functions.php b/src/bp-blogs/bp-blogs-functions.php
    index c10af26..35e4f4d 100644
    a b function bp_blogs_remove_data( $user_id ) { 
    14221422add_action( 'wpmu_delete_user',  'bp_blogs_remove_data' );
    14231423add_action( 'delete_user',       'bp_blogs_remove_data' );
    14241424add_action( 'bp_make_spam_user', 'bp_blogs_remove_data' );
     1425
     1426/**
     1427 * Restore all blog associations for a given user
     1428 *
     1429 * @since BuddyPress (2.2.0)
     1430 *
     1431 * @param  int $user_id ID whose blog data should be restored.
     1432 * @uses   get_blogs_of_user() Get the blogs a user belongs to.
     1433 * @uses   bp_blogs_add_user_to_blog() Record a user's association with a blog.
     1434 * @return bool|null Returns false on failure.
     1435 */
     1436function bp_blogs_restore_data( $user_id = 0 ) {
     1437        if ( ! is_multisite() ) {
     1438                return false;
     1439        }
     1440
     1441        // Get the user's blogs
     1442        $user_blogs = get_blogs_of_user( $user_id );
     1443
     1444        if ( empty( $user_blogs ) ) {
     1445                return false;
     1446        }
     1447
     1448        $blogs = array_keys( $user_blogs );
     1449
     1450        foreach ( $blogs as $blog_id ) {
     1451                bp_blogs_add_user_to_blog( $user_id, false, $blog_id );
     1452        }
     1453}
     1454add_action( 'bp_make_ham_user', 'bp_blogs_restore_data', 10, 1 );
  • tests/phpunit/testcases/blogs/functions.php

    diff --git a/tests/phpunit/testcases/blogs/functions.php b/tests/phpunit/testcases/blogs/functions.php
    index b2ee4eb..7dd7eb2 100644
    a b class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    280280        }
    281281
    282282        /**
     283         * @group bp_blogs_restore_data
     284         */
     285        public function test_bp_blogs_restore_data() {
     286                if ( ! is_multisite() ) {
     287                        return;
     288                }
     289
     290                // Create a regular member
     291                $u = $this->factory->user->create();
     292
     293                // Create blogs
     294                $b1 = $this->factory->blog->create( array( 'user_id' => $u ) );
     295                $b2 = $this->factory->blog->create( array( 'user_id' => $u ) );
     296
     297                add_user_to_blog( $b2, $u, 'contributor' );
     298
     299                $expected = array(
     300                        $b1 => $b1,
     301                        $b2 => $b2
     302                );
     303
     304                bp_blogs_record_existing_blogs();
     305
     306                // Mark the user as spam
     307                bp_core_process_spammer_status( $u, 'spam' );
     308
     309                // get all blogs for user
     310                $blogs = bp_blogs_get_blogs_for_user( $u, true );
     311                $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );
     312
     313                $this->assertNotEquals( $expected, array_map( 'intval', $blog_ids ), 'User marked as spam should not have any blog registered' );
     314
     315                // Ham the user
     316                bp_core_process_spammer_status( $u, 'ham' );
     317
     318                // get all blogs for user
     319                $blogs = bp_blogs_get_blogs_for_user( $u, true );
     320                $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );
     321
     322                $this->assertEquals( $expected, array_map( 'intval', $blog_ids ) );
     323        }
     324
     325        /**
    283326         * @group bp_blogs_catch_transition_post_status
    284327         */
    285328        public function test_transition_post_status_publish_to_publish() {