Skip to:
Content

BuddyPress.org

Ticket #7199: 7199.patch

File 7199.patch, 5.1 KB (added by imath, 9 years ago)
  • src/bp-blogs/bp-blogs-activity.php

    diff --git src/bp-blogs/bp-blogs-activity.php src/bp-blogs/bp-blogs-activity.php
    index b3fe3ee..5c91e83 100644
    add_action( 'bp_blogs_new_blog', 'bp_blogs_record_activity_on_site_creation', 10 
    639639 *
    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         ) );
     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 );
    648658}
    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' );
     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/**
    653663 * Delete all 'blogs' activity items for a site when the site is deleted.
  • src/bp-blogs/bp-blogs-functions.php

    diff --git src/bp-blogs/bp-blogs-functions.php src/bp-blogs/bp-blogs-functions.php
    index 6fe24a6..a9895e7 100644
    function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) { 
    920920        BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
    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         *
    938925         * @since 1.0.0
  • tests/phpunit/testcases/blogs/functions.php

    diff --git tests/phpunit/testcases/blogs/functions.php tests/phpunit/testcases/blogs/functions.php
    index dcbed4c..25b3cab 100644
    class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    10161016                $this->set_current_user( $old_user );
    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(
    10211115                        'component' => buddypress()->blogs->id,