Skip to:
Content

bbPress.org

Ticket #2488: 2488.2.diff

File 2488.2.diff, 5.0 KB (added by tharsheblows, 9 years ago)

unsub / unfav when groups are left

  • src/includes/extend/buddypress/groups.php

     
    9898
    9999                // Adds a hidden input value to the "Group Settings" page
    100100                add_action( 'bp_before_group_settings_admin', array( $this, 'group_settings_hidden_field'    ) );
     101
     102                // When a member is banned from a group, unsubscribe from forum and its topics
     103                add_action( 'groups_ban_member',                         array( $this, 'leave_group_unsubscribe'), 10, 2        );     
     104
     105                // When a member is removed from a group, unsubscribe from forum and its topics
     106                add_action( 'groups_remove_member',             array( $this, 'leave_group_unsubscribe'), 10, 2         );
     107
     108                // When a member leaves a group, unsubscribe from forum and its topics
     109                add_action( 'groups_leave_group',                       array( $this, 'leave_group_unsubscribe'), 10, 2         );
    101110        }
    102111
    103112        /**
     
    14621471
    14631472                return $args;
    14641473        }
     1474
     1475
     1476/**
     1477         * Unsubscribe user from all forums and topics when leaving a group
     1478         *
     1479         * @since
     1480         *
     1481         * @param int $group_id  ID of the group.
     1482         * @param int $user_id The user whose subscriptions need to be deleted.
     1483         * @return bool true if any have been deleted
     1484         */
     1485        public function leave_group_unsubscribe( $group_id, $user_id ){
     1486
     1487                $unsub = false;
     1488
     1489                // By default, everything is unfavorited and unsubscribed when someone leaves or gets banned or removed
     1490                // true means "unsubscribe and unfavorite", false means "leave as is"
     1491                // eg 'ban_public' => true means if someone is banned from a public group,
     1492                // they will be unsubscribed and their favorites will be removed.
     1493                $defaults = array(
     1494                        'leave_hidden' => true,
     1495                        'leave_private' => true,
     1496                        'leave_public' => true,
     1497                        'remove_hidden' => true,
     1498                        'remove_private' => true,
     1499                        'remove_public' => true,
     1500                        'ban_hidden' => true,
     1501                        'ban_private' => true,
     1502                        'ban_public' => true
     1503                );
     1504
     1505               
     1506                $args = array();
     1507               
     1508                // You can change the defaults by using add_filter( ' bbp_before_group_unsubscribe_parse_args', 'your_function')
     1509                // your_function() should return an array with whatever you'd like to change.
     1510                $args = bbp_parse_args( $args, $defaults, $filter_key = 'group_unsubscribe'  );
     1511
     1512                // Is a user leaving / being removed / getting banned
     1513                $current_filter = current_filter();
     1514               
     1515                if( $current_filter == 'groups_remove_member'){
     1516                        $action = 'remove';
     1517                } elseif( $current_filter == 'groups_ban_member'){
     1518                        $action = 'ban';
     1519                } else {
     1520                        $action = 'leave';
     1521                }
     1522
     1523                $statuses = array();
     1524
     1525                // Make an array of the statuses to act upon for the action (leave / remove / ban)
     1526                foreach( $args as $key => $value ){
     1527                        if ( strpos( $key, $action ) !== false && $value == true ){
     1528                                $status = explode( '_', $key );
     1529                                $statuses[] = $status[1];
     1530                        }
     1531                }
     1532
     1533                // What is the group status?
     1534                $group_status = groups_get_group( array( 'group_id' => $group_id  ) ) -> status;
     1535
     1536                // If this status is not in the array of statuses, bail
     1537                if( ! in_array( $group_status, $statuses ) ){
     1538                        return false;
     1539                }
     1540
     1541                // Get the forum associated with the group, if there's none, bail
     1542                $forums = groups_get_groupmeta( $group_id, 'forum_id' );
     1543                if( empty( $forums ) ){
     1544                        return false;
     1545                }
     1546
     1547                // Get the user's forum and topic subcriptions
     1548                $forum_subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     1549                $topic_subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     1550
     1551                // Get the user's topic favorites
     1552                $topic_favorites = bbp_get_user_favorites_topic_ids( $user_id );
     1553               
     1554                //  If no subscriptions, bail
     1555                if( empty( $forum_subscriptions ) && empty( $topic_subscriptions) && empty( $topic_favorites ) ){
     1556                        return false;
     1557                }
     1558       
     1559                // Loop over all of the forums to get all the topics
     1560                foreach( $forums as $forum ){
     1561       
     1562                        // Unsubscribe if subscribed to the forum
     1563                        if( in_array( $forum, $forum_subscriptions ) ){                 
     1564                                        bbp_remove_user_subscription( $user_id, $forum );
     1565                                        $unsub = true;
     1566                        }
     1567               
     1568                        // Get all the child topics for the forum
     1569                        $topics = bbp_get_all_child_ids( $forum, 'topic' );
     1570               
     1571                        // Get the child topics which are also in the user's topic subscriptions
     1572                        $group_topics_subscribed = array_intersect( $topics, $topic_subscriptions );
     1573
     1574                        // Get the child topics which are also in the user's topic favorites
     1575                        $group_topics_favorited = array_intersect( $topics, $topic_favorites );
     1576
     1577       
     1578                        // Unsubscribe them after checking if there are any
     1579                        if( !empty( $group_topics_subscribed ) ){
     1580                                foreach( $group_topics_subscribed as $group_topic_subscribed ){
     1581                                        bbp_remove_user_subscription( $user_id, $group_topic_subscribed );
     1582                                        $unsub = true;
     1583                                }
     1584                        }
     1585
     1586                        // Unfavorite (defavorite?) them after checking if there are any
     1587                        if( !empty( $group_topics_favorited ) ){
     1588                                foreach( $group_topics_favorited as $group_topic_favorited ){
     1589                                        bbp_remove_user_favorite( $user_id, $group_topic_subscribed );
     1590                                        $unsub = true;
     1591                                }
     1592                        }
     1593                }
     1594
     1595                return $unsub;
     1596        }
     1597
    14651598}
    14661599endif;