Skip to:
Content

bbPress.org

Ticket #2488: 2488.3.patch

File 2488.3.patch, 15.1 KB (added by thebrandonallen, 8 years ago)
  • src/includes/extend/buddypress/groups.php

    diff --git src/includes/extend/buddypress/groups.php src/includes/extend/buddypress/groups.php
    index c7b5167..57db56d 100644
    class BBP_Forums_Group_Extension extends BP_Group_Extension { 
    9999
    100100                // Adds a hidden input value to the "Group Settings" page
    101101                add_action( 'bp_before_group_settings_admin', array( $this, 'group_settings_hidden_field'     ) );
     102
     103                // When a user is banned from a group, remove forum/topic subscriptions and favorites.
     104                add_action( 'groups_ban_member',              array( $this, 'leave_group_unsubscribe' ), 10, 2  );
     105
     106                // When a user is removed from a group, remove forum/topic subscriptions and favorites.
     107                add_action( 'groups_remove_member',           array( $this, 'leave_group_unsubscribe' ), 10, 2  );
     108
     109                // When a user leaves a group, remove forum/topic subscriptions and favorites.
     110                add_action( 'groups_leave_group',             array( $this, 'leave_group_unsubscribe' ), 10, 2  );
    102111        }
    103112
    104113        /**
    class BBP_Forums_Group_Extension extends BP_Group_Extension { 
    15051514
    15061515                return $args;
    15071516        }
     1517
     1518        /** Subscriptions/Favorites ***********************************************/
     1519
     1520        /**
     1521         * Unsubscribe user from all forums and topics when leaving a group
     1522         *
     1523         * @since x.x.x bbPress (rXXXX)
     1524         *
     1525         * @param int $group_id ID of the group.
     1526         * @param int $user_id  The user whose subscriptions/favorites need to be deleted.
     1527         *
     1528         * @uses current_filter() To get the current action.
     1529         * @uses bp_current_action() To get the current BP action.
     1530         * @uses bbp_parse_args() Parse unfavorite/unsubscribe arguments.
     1531         * @uses groups_get_group() To get the group.
     1532         * @uses bbp_get_group_forum_ids() To get the group forum ids.
     1533         * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscribed forum ids.
     1534         * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscribed topic ids.
     1535         * @uses bbp_get_user_favorites_topic_ids() To get the user's favorited topic ids.
     1536         * @uses bbp_remove_user_forum_subscription() To remove the user's forum subscription.
     1537         * @uses bbp_get_topic_post_type() To get the topic post type.
     1538         * @uses bbp_get_all_child_ids() To get the forum's child ids.
     1539         * @uses bbp_remove_user_topic_subscription() To remove the user's topic subscription.
     1540         * @uses bbp_remove_user_favorite() To remove the user's topic favorite.
     1541         *
     1542         * @return bool True if any have been deleted.
     1543         */
     1544        public function leave_group_unsubscribe( $group_id = 0, $user_id = 0 ) {
     1545
     1546                // We need both ids to be successful.
     1547                if ( empty( $group_id ) || empty( $user_id ) ) {
     1548                        return false;
     1549                }
     1550
     1551                // Get the current filter for later use.
     1552                $current_filter = current_filter();
     1553
     1554                // Bail early if the user is leaving, not removing. Wait for round two.
     1555                // See #BP6597.
     1556                if ( 'groups_remove_member' === $current_filter && 'leave-group' === bp_current_action() ) {
     1557                        return false;
     1558                }
     1559
     1560                // Is a user leaving, being removed, or getting banned.
     1561                if ( 'groups_remove_member' === $current_filter ) {
     1562                        $action = 'remove';
     1563                } elseif ( 'groups_ban_member' === $current_filter ) {
     1564                        $action = 'ban';
     1565                } else {
     1566                        $action = 'leave';
     1567                }
     1568
     1569                // Set return value. Default to false.
     1570                $retval = false;
     1571
     1572                // By default, everything is unfavorited and unsubscribed when someone leaves,
     1573                // gets banned from, or removed from a group. All arguments are required, and
     1574                // are expected to have a value of true or false.
     1575                $defaults = array(
     1576                        'leave' => array(
     1577                                'hidden'  => true,
     1578                                'private' => true,
     1579                                'public'  => true,
     1580                        ),
     1581                        'remove' => array(
     1582                                'hidden'  => true,
     1583                                'private' => true,
     1584                                'public'  => true,
     1585                        ),
     1586                        'ban' => array(
     1587                                'hidden'  => true,
     1588                                'private' => true,
     1589                                'public'  => true,
     1590                        ),
     1591                );
     1592
     1593                // Parse our arguments. See `bbp_parse_args()` for filters.
     1594                $args = bbp_parse_args( array(), $defaults, 'group_unsubscribe'  );
     1595
     1596                // Get the group statuses we should update.
     1597                $statuses = array_keys( $args[ $action ], true, true );
     1598
     1599                // What is the group status?
     1600                $group_status = groups_get_group( array( 'group_id' => $group_id  ) )->status;
     1601
     1602                // If the group status isn't a status we should act upon, bail.
     1603                if ( ! in_array( $group_status, $statuses ) ) {
     1604                        return false;
     1605                }
     1606
     1607                // Get the forum associated with the group. If there's none, bail.
     1608                $forum_ids = bbp_get_group_forum_ids( $group_id );
     1609                if ( empty( $forum_ids ) ) {
     1610                        return false;
     1611                }
     1612
     1613                // Get the user's forum and topic subcriptions.
     1614                $forum_subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     1615                $topic_subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     1616
     1617                // Get the user's topic favorites.
     1618                $topic_favorites = bbp_get_user_favorites_topic_ids( $user_id );
     1619
     1620                // If no subscriptions, bail.
     1621                if ( empty( $forum_subscriptions ) && empty( $topic_subscriptions ) && empty( $topic_favorites ) ) {
     1622                        return false;
     1623                }
     1624
     1625                // Loop over all of the forums to get all the topics.
     1626                foreach ( $forum_ids as $forum_id ) {
     1627
     1628                        // Maybe unsubscribe forum.
     1629                        if ( in_array( $forum_id, $forum_subscriptions ) ) {
     1630                                        bbp_remove_user_forum_subscription( $user_id, $forum_id );
     1631                                        $retval = true;
     1632                        }
     1633
     1634                        // Get the forum's topics.
     1635                        $topics = bbp_get_all_child_ids( $forum_id, bbp_get_topic_post_type() );
     1636
     1637                        // Get the intersection of child topics and the user's topic subscriptions.
     1638                        $group_topics_subscribed = array_intersect( $topics, $topic_subscriptions );
     1639
     1640                        // Maybe unsubscribe topics.
     1641                        if ( ! empty( $group_topics_subscribed ) ) {
     1642                                foreach ( $group_topics_subscribed as $topic_id ) {
     1643                                        bbp_remove_user_topic_subscription( $user_id, $topic_id );
     1644                                        $retval = true;
     1645                                }
     1646                        }
     1647
     1648                        // Get the intersetion of child topics and the user's topic favorites.
     1649                        $group_topics_favorited = array_intersect( $topics, $topic_favorites );
     1650
     1651                        // Maybe unfavorite topics.
     1652                        if ( ! empty( $group_topics_favorited ) ) {
     1653                                foreach( $group_topics_favorited as $topic_id ) {
     1654                                        bbp_remove_user_favorite( $user_id, $topic_id );
     1655                                        $retval = true;
     1656                                }
     1657                        }
     1658                }
     1659
     1660                return $retval;
     1661        }
     1662
    15081663}
    15091664endif;
  • new file tests/phpunit/testcases/extend/buddypress/groups/subscriptions.php

    diff --git tests/phpunit/testcases/extend/buddypress/groups/subscriptions.php tests/phpunit/testcases/extend/buddypress/groups/subscriptions.php
    new file mode 100644
    index 0000000..3a5f292
    - +  
     1<?php
     2
     3/**
     4 * @group extend
     5 * @group buddypress
     6 * @group groups
     7 * @group subscriptions
     8 */
     9class BBP_Tests_Extend_BuddyPress_Groups_Subscriptions extends BBP_UnitTestCase {
     10
     11        protected $group_extension = null;
     12        protected $old_current_filter = null;
     13        protected $current_filter = null;
     14        protected $group;
     15        protected $group_id;
     16
     17        public function setUp() {
     18                parent::setUp();
     19
     20                if ( ! function_exists( 'buddypress' ) ) {
     21                        return;
     22                }
     23
     24                $this->group_extension = new BBP_Forums_Group_Extension;
     25
     26                $this->group_id = $this->bp_factory->group->create( array( 'status' => 'public' ) );
     27                $this->group = groups_get_group( array( 'group_id' => $this->group_id ) );
     28        }
     29
     30        public function tearDown() {
     31                parent::tearDown();
     32
     33                $this->group_extension = null;
     34        }
     35
     36        protected function set_current_filter( $filter = array() ) {
     37                global $wp_current_filter;
     38                $this->old_current_filter = $wp_current_filter;
     39                $wp_current_filter[] = $filter;
     40        }
     41
     42        protected function restore_current_filter() {
     43                global $wp_current_filter;
     44                $wp_current_filter = $this->old_current_filter;
     45        }
     46
     47        /**
     48         * Copied from `BBP_Forums_Group_Extension::new_forum()`.
     49         */
     50        private function attach_forum_to_group( $forum_id, $group_id ) {
     51                bbp_add_forum_id_to_group( $group_id, $forum_id );
     52                bbp_add_group_id_to_forum( $forum_id, $group_id );
     53        }
     54
     55        /**
     56         * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe
     57         */
     58        public function test_leave_group_unsubscribe_when_user_banned_from_group() {
     59                $this->group->status = 'public';
     60                $this->group->save();
     61
     62                $old_current_user = get_current_user_id();
     63                $u1 = $this->group->creator_id;
     64                $u2 = $this->factory->user->create( array(
     65                        'user_login' => 'bbPress Groups User',
     66                        'user_pass'  => 'password',
     67                        'user_email' => 'bbp_user@example.org',
     68                ) );
     69                wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) );
     70                BP_UnitTestCase::set_current_user( $u1 );
     71                BP_UnitTestCase::add_user_to_group( $u2, $this->group_id );
     72
     73                $f = $this->factory->forum->create();
     74                $t = $this->factory->topic->create( array(
     75                        'post_parent' => $f,
     76                        'post_author' => $u1,
     77                ) );
     78                $this->attach_forum_to_group( $f, $this->group_id );
     79
     80                bbp_add_user_forum_subscription( $u2, $f );
     81                bbp_add_user_topic_subscription( $u2, $t );
     82
     83                $this->set_current_filter( 'groups_ban_member' );
     84
     85                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     86
     87                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     88                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     89
     90                $this->group->status = 'private';
     91                $this->group->save();
     92
     93                bbp_add_user_forum_subscription( $u2, $f );
     94                bbp_add_user_topic_subscription( $u2, $t );
     95
     96                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     97
     98                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     99                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     100
     101                $this->group->status = 'hidden';
     102                $this->group->save();
     103
     104                bbp_add_user_forum_subscription( $u2, $f );
     105                bbp_add_user_topic_subscription( $u2, $t );
     106
     107                add_filter( 'bbp_before_group_unsubscribe_parse_args', create_function( '', "
     108                        return array(
     109                                'leave' => array(
     110                                        'hidden'  => true,
     111                                        'private' => true,
     112                                        'public'  => true,
     113                                ),
     114                                'remove' => array(
     115                                        'hidden'  => true,
     116                                        'private' => true,
     117                                        'public'  => true,
     118                                ),
     119                                'ban' => array(
     120                                        'hidden'  => false,
     121                                        'private' => true,
     122                                        'public'  => true,
     123                                ),
     124                        );"
     125                ) );
     126
     127                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     128
     129                $this->restore_current_filter();
     130
     131                $this->assertTrue( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     132                $this->assertTrue( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     133
     134                // Restore old user
     135                BP_UnitTestCase::set_current_user( $old_current_user );
     136        }
     137
     138        /**
     139         * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe
     140         */
     141        public function test_leave_group_unsubscribe_when_user_removed_from_group() {
     142                $this->group->status = 'public';
     143                $this->group->save();
     144
     145                $old_current_user = get_current_user_id();
     146                $u1 = $this->group->creator_id;
     147                $u2 = $this->factory->user->create( array(
     148                        'user_login' => 'bbPress Groups User',
     149                        'user_pass'  => 'password',
     150                        'user_email' => 'bbp_user@example.org',
     151                ) );
     152                wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) );
     153                BP_UnitTestCase::set_current_user( $u1 );
     154                BP_UnitTestCase::add_user_to_group( $u2, $this->group_id );
     155
     156                $f = $this->factory->forum->create();
     157                $t = $this->factory->topic->create( array(
     158                        'post_parent' => $f,
     159                        'post_author' => $u1,
     160                ) );
     161                $this->attach_forum_to_group( $f, $this->group_id );
     162
     163                bbp_add_user_forum_subscription( $u2, $f );
     164                bbp_add_user_topic_subscription( $u2, $t );
     165
     166                $this->set_current_filter( 'groups_remove_member' );
     167
     168                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     169
     170                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     171                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     172
     173                $this->group->status = 'private';
     174                $this->group->save();
     175
     176                bbp_add_user_forum_subscription( $u2, $f );
     177                bbp_add_user_topic_subscription( $u2, $t );
     178
     179                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     180
     181                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     182                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     183
     184                $this->group->status = 'hidden';
     185                $this->group->save();
     186
     187                bbp_add_user_forum_subscription( $u2, $f );
     188                bbp_add_user_topic_subscription( $u2, $t );
     189
     190                add_filter( 'bbp_before_group_unsubscribe_parse_args', create_function( '', "
     191                        return array(
     192                                'leave' => array(
     193                                        'hidden'  => true,
     194                                        'private' => true,
     195                                        'public'  => true,
     196                                ),
     197                                'remove' => array(
     198                                        'hidden'  => false,
     199                                        'private' => true,
     200                                        'public'  => true,
     201                                ),
     202                                'ban' => array(
     203                                        'hidden'  => true,
     204                                        'private' => true,
     205                                        'public'  => true,
     206                                ),
     207                        );"
     208                ) );
     209
     210                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     211
     212                $this->restore_current_filter();
     213
     214                $this->assertTrue( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     215                $this->assertTrue( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     216
     217                // Restore old user
     218                BP_UnitTestCase::set_current_user( $old_current_user );
     219        }
     220
     221        /**
     222         * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe
     223         */
     224        public function test_leave_group_unsubscribe_when_user_leaves_group() {
     225
     226                // See #BP6597.
     227                buddypress()->current_action = 'leave-group';
     228
     229                $g = $this->bp_factory->group->create( array( 'status' => 'public' ) );
     230                $group = groups_get_group( array( 'group_id' => $g ) );
     231
     232                $old_current_user = get_current_user_id();
     233                $u1 = $group->creator_id;
     234                $u2 = $this->factory->user->create( array(
     235                        'user_login' => 'bbPress Groups User',
     236                        'user_pass'  => 'password',
     237                        'user_email' => 'bbp_user@example.org',
     238                ) );
     239                BP_UnitTestCase::set_current_user( $u2 );
     240                BP_UnitTestCase::add_user_to_group( $u2, $g );
     241
     242                $f = $this->factory->forum->create();
     243                $t = $this->factory->topic->create( array(
     244                        'post_parent' => $f,
     245                        'post_author' => $u1,
     246                ) );
     247                $this->attach_forum_to_group( $f, $g );
     248
     249                bbp_add_user_forum_subscription( $u2, $f );
     250                bbp_add_user_topic_subscription( $u2, $t );
     251
     252                $this->group_extension->leave_group_unsubscribe( $g, $u2 );
     253
     254                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     255                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     256
     257                $group->status = 'private';
     258                $group->save();
     259
     260                bbp_add_user_forum_subscription( $u2, $f );
     261                bbp_add_user_topic_subscription( $u2, $t );
     262
     263                $this->group_extension->leave_group_unsubscribe( $g, $u2 );
     264
     265                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     266                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     267
     268                $group->status = 'hidden';
     269                $group->save();
     270
     271                bbp_add_user_forum_subscription( $u2, $f );
     272                bbp_add_user_topic_subscription( $u2, $t );
     273
     274                add_filter( 'bbp_before_group_unsubscribe_parse_args', create_function( '', "
     275                        return array(
     276                                'leave' => array(
     277                                        'hidden'  => false,
     278                                        'private' => true,
     279                                        'public'  => true,
     280                                ),
     281                                'remove' => array(
     282                                        'hidden'  => true,
     283                                        'private' => true,
     284                                        'public'  => true,
     285                                ),
     286                                'ban' => array(
     287                                        'hidden'  => true,
     288                                        'private' => true,
     289                                        'public'  => true,
     290                                ),
     291                        );"
     292                ) );
     293
     294                $this->group_extension->leave_group_unsubscribe( $g, $u2 );
     295
     296                $this->assertTrue( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     297                $this->assertTrue( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     298
     299                // Reset BP current action
     300                buddypress()->current_action = '';
     301
     302                // Restore old user
     303                BP_UnitTestCase::set_current_user( $old_current_user );
     304        }
     305}