Skip to:
Content

bbPress.org

Ticket #2488: 2488.4.diff

File 2488.4.diff, 10.4 KB (added by thebrandonallen, 7 years ago)
  • src/includes/extend/buddypress/groups.php

    diff --git src/includes/extend/buddypress/groups.php src/includes/extend/buddypress/groups.php
    index 408bf1e..8cd02f9 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                // Remove subscriptions/favorites when a user is banned from a group.
     104                add_action( 'groups_ban_member',              array( $this, 'leave_group_unsubscribe' ), 10, 2  );
     105
     106                // Remove subscriptions/favorites when a user is removed from a group.
     107                add_action( 'groups_remove_member',           array( $this, 'leave_group_unsubscribe' ), 10, 2  );
     108
     109                // Remove subscriptions/favorites when a user leaves a group.
     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 2.6.0 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
     1527         *                      deleted.
     1528         *
     1529         * @return bool True if any have been deleted.
     1530         */
     1531        public function leave_group_unsubscribe( $group_id = 0, $user_id = 0 ) {
     1532
     1533                // We need both ids to be successful.
     1534                if ( empty( $group_id ) || empty( $user_id ) ) {
     1535                        return false;
     1536                }
     1537
     1538                // Get the current filter for later use.
     1539                $current_filter = current_filter();
     1540
     1541                // Bail early if the user is leaving, not removing. Wait for round two.
     1542                // See https://buddypress.trac.wordpress.org/ticket/6597.
     1543                if ( 'groups_remove_member' === $current_filter && 'leave-group' === bp_current_action() ) {
     1544                        return false;
     1545                }
     1546
     1547                // Is a user leaving, being removed, or getting banned?
     1548                if ( 'groups_remove_member' === $current_filter ) {
     1549                        $action = 'remove';
     1550                } elseif ( 'groups_ban_member' === $current_filter ) {
     1551                        $action = 'ban';
     1552                } else {
     1553                        $action = 'leave';
     1554                }
     1555
     1556                /**
     1557                 * Fires before group forum/topic subscriptions and favorites are
     1558                 * removed for a user.
     1559                 *
     1560                 * Allows plugin developers to short-circuit the unsubscribe process.
     1561                 *
     1562                 * @since 2.6.0 bbPress (rXXXX)
     1563                 *
     1564                 * @param bool $unsubscribe Should we unsubscribe?
     1565                 * @param int  $group_id    The group id the user is leaving.
     1566                 * @param int  $user_id     The user id.
     1567                 * @param int  $action      The action taken by or against the user.
     1568                 */
     1569                $unsubscribe = apply_filters( 'bbp_leave_group_unsubscribe', true, $group_id, $user_id, $action );
     1570                if ( ! $unsubscribe ) {
     1571                        return false;
     1572                }
     1573
     1574                // Get the forum associated with the group. If there are none, bail.
     1575                $forum_ids = bbp_get_group_forum_ids( $group_id );
     1576                if ( empty( $forum_ids ) ) {
     1577                        return false;
     1578                }
     1579
     1580                // Get the user's forum and topic subcriptions.
     1581                $forum_subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     1582                $topic_subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     1583
     1584                // Get the user's topic favorites.
     1585                $topic_favorites = bbp_get_user_favorites_topic_ids( $user_id );
     1586
     1587                // If no subscriptions, bail.
     1588                if ( empty( $forum_subscriptions ) && empty( $topic_subscriptions ) && empty( $topic_favorites ) ) {
     1589                        return false;
     1590                }
     1591
     1592                // Set default return value.
     1593                $retval = false;
     1594
     1595                // Loop over all of the forums to get all the topics.
     1596                foreach ( $forum_ids as $forum_id ) {
     1597
     1598                        // Validate the forum id.
     1599                        $forum_id = bbp_get_forum_id( $forum_id );
     1600
     1601                        // Maybe unsubscribe forum.
     1602                        if ( in_array( $forum_id, $forum_subscriptions, true ) ) {
     1603                                bbp_remove_user_forum_subscription( $user_id, $forum_id );
     1604                                $retval = true;
     1605                        }
     1606
     1607                        // Get the forum's topics, and bail if there are none.
     1608                        $topics = bbp_get_all_child_ids( $forum_id, bbp_get_topic_post_type() );
     1609                        if ( empty( $topics ) ) {
     1610                                continue;
     1611                        }
     1612
     1613                        // Get topic subscriptions to be removed, and unsubscribe.
     1614                        $group_topics_subscribed = array_intersect( $topics, $topic_subscriptions );
     1615                        foreach ( $group_topics_subscribed as $topic_id ) {
     1616                                bbp_remove_user_topic_subscription( $user_id, $topic_id );
     1617                                $retval = true;
     1618                        }
     1619
     1620                        // Get topic favorites to be removed, and unfavorite.
     1621                        $group_topics_favorited = array_intersect( $topics, $topic_favorites );
     1622                        foreach ( $group_topics_favorited as $topic_id ) {
     1623                                bbp_remove_user_favorite( $user_id, $topic_id );
     1624                                $retval = true;
     1625                        }
     1626                }
     1627
     1628                return $retval;
     1629        }
     1630
    15081631}
    15091632endif;
  • 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..4cd3b61
    - +  
     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();
     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
     60                $old_current_user = get_current_user_id();
     61                $u1 = $this->group->creator_id;
     62                $u2 = $this->factory->user->create( array(
     63                        'user_login' => 'bbPress Groups User',
     64                        'user_pass'  => 'password',
     65                        'user_email' => 'bbp_user@example.org',
     66                ) );
     67                wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) );
     68                BP_UnitTestCase::set_current_user( $u1 );
     69                BP_UnitTestCase::add_user_to_group( $u2, $this->group_id );
     70
     71                $f = $this->factory->forum->create();
     72                $t = $this->factory->topic->create( array(
     73                        'post_parent' => $f,
     74                        'post_author' => $u1,
     75                ) );
     76                $this->attach_forum_to_group( $f, $this->group_id );
     77
     78                bbp_add_user_forum_subscription( $u2, $f );
     79                bbp_add_user_topic_subscription( $u2, $t );
     80                bbp_add_user_favorite( $u2, $t );
     81
     82                $this->set_current_filter( 'groups_ban_member' );
     83
     84                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     85
     86                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     87                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     88                $this->assertFalse( bbp_is_user_favorite( $u2, $t ) );
     89
     90                $this->restore_current_filter();
     91
     92                // Restore old user
     93                BP_UnitTestCase::set_current_user( $old_current_user );
     94        }
     95
     96        /**
     97         * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe
     98         */
     99        public function test_leave_group_unsubscribe_when_user_removed_from_group() {
     100
     101                $old_current_user = get_current_user_id();
     102                $u1 = $this->group->creator_id;
     103                $u2 = $this->factory->user->create( array(
     104                        'user_login' => 'bbPress Groups User',
     105                        'user_pass'  => 'password',
     106                        'user_email' => 'bbp_user@example.org',
     107                ) );
     108                wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) );
     109                BP_UnitTestCase::set_current_user( $u1 );
     110                BP_UnitTestCase::add_user_to_group( $u2, $this->group_id );
     111
     112                $f = $this->factory->forum->create();
     113                $t = $this->factory->topic->create( array(
     114                        'post_parent' => $f,
     115                        'post_author' => $u1,
     116                ) );
     117                $this->attach_forum_to_group( $f, $this->group_id );
     118
     119                bbp_add_user_forum_subscription( $u2, $f );
     120                bbp_add_user_topic_subscription( $u2, $t );
     121                bbp_add_user_favorite( $u2, $t );
     122
     123                $this->set_current_filter( 'groups_remove_member' );
     124
     125                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     126
     127                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     128                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     129                $this->assertFalse( bbp_is_user_favorite( $u2, $t ) );
     130
     131                $this->restore_current_filter();
     132
     133                // Restore old user
     134                BP_UnitTestCase::set_current_user( $old_current_user );
     135        }
     136
     137        /**
     138         * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe
     139         */
     140        public function test_leave_group_unsubscribe_when_user_leaves_group() {
     141
     142                // See https://buddypress.trac.wordpress.org/ticket/6597.
     143                buddypress()->current_action = 'leave-group';
     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                BP_UnitTestCase::set_current_user( $u2 );
     153                BP_UnitTestCase::add_user_to_group( $u2, $this->group_id );
     154
     155                $f = $this->factory->forum->create();
     156                $t = $this->factory->topic->create( array(
     157                        'post_parent' => $f,
     158                        'post_author' => $u1,
     159                ) );
     160                $this->attach_forum_to_group( $f, $this->group_id );
     161
     162                bbp_add_user_forum_subscription( $u2, $f );
     163                bbp_add_user_topic_subscription( $u2, $t );
     164                bbp_add_user_favorite( $u2, $t );
     165
     166                $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 );
     167
     168                $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) );
     169                $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) );
     170                $this->assertFalse( bbp_is_user_favorite( $u2, $t ) );
     171
     172                $this->restore_current_filter();
     173
     174                // Reset BP current action
     175                buddypress()->current_action = '';
     176
     177                // Restore old user
     178                BP_UnitTestCase::set_current_user( $old_current_user );
     179        }
     180}