Skip to:
Content

BuddyPress.org

Changeset 10970


Ignore:
Timestamp:
07/30/2016 11:36:02 AM (6 years ago)
Author:
imath
Message:

Only delete the "new_blog" activity when its author is removed from the Blog.

Before this commit, as soon as any contributor was removed from the blog, the "new_blog" activity was also removed although the removed user was not the author of the activity.
It is no more the case. This activity will only be deleted if the user who created the blog is removed from it.
This commit also make sure this activity is not deleted twice as some duplicate code has been left when moving the activity code inside the bp-blogs-activity.php file during the 2.6 dev-cycle.

See #7199 (branch 2.6)

Location:
branches/2.6
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/src/bp-blogs/bp-blogs-activity.php

    r10854 r10970  
    640640 * @param int $blog_id Site ID.
    641641 */
    642 function bp_blogs_delete_new_blog_activity_for_site( $blog_id ) {
    643     bp_blogs_delete_activity( array(
     642function bp_blogs_delete_new_blog_activity_for_site( $blog_id, $user_id = 0 ) {
     643    $args = array(
    644644        'item_id'   => $blog_id,
    645645        'component' => buddypress()->blogs->id,
    646646        'type'      => 'new_blog'
    647     ) );
    648 }
    649 add_action( 'bp_blogs_remove_blog',          'bp_blogs_delete_new_blog_activity_for_site' );
    650 add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_delete_new_blog_activity_for_site' );
     647    );
     648
     649    /**
     650     * In the case a user is removed, make sure he is the author of the 'new_blog' activity
     651     * when trying to delete it.
     652     */
     653    if ( ! empty( $user_id ) ) {
     654        $args['user_id'] = $user_id;
     655    }
     656
     657    bp_blogs_delete_activity( $args );
     658}
     659add_action( 'bp_blogs_remove_blog',          'bp_blogs_delete_new_blog_activity_for_site', 10, 1 );
     660add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_delete_new_blog_activity_for_site', 10, 2 );
    651661
    652662/**
  • branches/2.6/src/bp-blogs/bp-blogs-functions.php

    r10895 r10970  
    921921
    922922    /**
    923      * Delete activity stream item only if the Activity component is active
    924      *
    925      * @see https://buddypress.trac.wordpress.org/ticket/6937
    926      */
    927     if ( bp_is_active( 'activity' ) ) {
    928         bp_blogs_delete_activity( array(
    929             'item_id'   => $blog_id,
    930             'component' => buddypress()->blogs->id,
    931             'type'      => 'new_blog'
    932         ) );
    933     }
    934 
    935     /**
    936923     * Fires after a blog has been removed from the tracker for a specific user.
    937924     *
  • branches/2.6/tests/phpunit/testcases/blogs/functions.php

    r10815 r10970  
    10171017    }
    10181018
     1019    /**
     1020     * @group bp_blogs_remove_blog
     1021     */
     1022    public function test_bp_blogs_remove_blog() {
     1023        if ( ! is_multisite() ) {
     1024            return;
     1025        }
     1026
     1027        $reset_post = $_POST;
     1028        $old_user = get_current_user_id();
     1029
     1030        // Simulate a new "BuddyPress generated" blog
     1031        $_POST['blog_public'] = 1;
     1032
     1033        $u = $this->factory->user->create();
     1034        $this->set_current_user( $u );
     1035
     1036        // Create three sites.
     1037        $b = $this->factory->blog->create( array(
     1038            'user_id' => $u
     1039        ) );
     1040
     1041        $activity = bp_activity_get( array(
     1042            'filter' => array(
     1043                'object'     => 'blogs',
     1044                'action'     => 'new_blog',
     1045                'primary_id' => $b,
     1046            ),
     1047        ) );
     1048
     1049        $new_blog = array_map( 'intval', wp_list_pluck( $activity['activities'], 'item_id', 'id' ) );
     1050        $this->assertSame( $b, reset( $new_blog ) );
     1051
     1052        // Removing the blog should delete the activity and the blog association.
     1053        wpmu_delete_blog( $b );
     1054
     1055        $deleted = bp_activity_get( array(
     1056            'in' => array_keys( $new_blog ),
     1057        ) );
     1058
     1059        $this->assertEmpty( $deleted['activities'] );
     1060        $this->assertEmpty( BP_Blogs_Blog::is_recorded( $b ) );
     1061
     1062        $_POST = $reset_post;
     1063        $this->set_current_user( $old_user );
     1064    }
     1065
     1066    /**
     1067     * @group bp_blogs_remove_blog_for_user
     1068     */
     1069    public function test_bp_blogs_remove_blog_for_user_is_contributor() {
     1070        if ( ! is_multisite() ) {
     1071            return;
     1072        }
     1073
     1074        $reset_post = $_POST;
     1075        $old_user = get_current_user_id();
     1076
     1077        // Simulate a new "BuddyPress generated" blog
     1078        $_POST['blog_public'] = 1;
     1079
     1080        $u = $this->factory->user->create();
     1081        $this->set_current_user( $u );
     1082
     1083        // Create three sites.
     1084        $b = $this->factory->blog->create( array(
     1085            'user_id' => $u
     1086        ) );
     1087
     1088        $u2 = $this->factory->user->create();
     1089        add_user_to_blog( $b, $u2, 'contributor' );
     1090
     1091        $u2_blogs = BP_Blogs_Blog::get_blog_ids_for_user( $u2 );
     1092        $this->assertContains( $b, $u2_blogs, 'The user should be associated to the blog as he is a contributor' );
     1093
     1094        remove_user_from_blog( $u2, $b );
     1095        $u2_blogs = BP_Blogs_Blog::get_blog_ids_for_user( $u2 );
     1096        $this->assertNotContains( $b, $u2_blogs, 'The user should not be associated anymore to the blog' );
     1097
     1098        $activity = bp_activity_get( array(
     1099            'filter' => array(
     1100                'object'     => 'blogs',
     1101                'action'     => 'new_blog',
     1102                'primary_id' => $b,
     1103            ),
     1104        ) );
     1105
     1106        $new_blog = array_map( 'intval', wp_list_pluck( $activity['activities'], 'item_id', 'id' ) );
     1107        $this->assertSame( $b, reset( $new_blog ), 'The new_blog activity should not be deleted when a contributor is removed from the blog.' );
     1108
     1109        $_POST = $reset_post;
     1110        $this->set_current_user( $old_user );
     1111    }
     1112
    10191113    protected function activity_exists_for_post( $post_id ) {
    10201114        $a = bp_activity_get( array(
Note: See TracChangeset for help on using the changeset viewer.