| 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) ); |
| 1474 | |
| 1475 | /** |
| 1476 | * Unsubscribe user from all forums and topics when leaving a group |
| 1477 | * |
| 1478 | * @since |
| 1479 | * |
| 1480 | * @param int $group_id ID of the group. |
| 1481 | * @param int $user_id The user whose subscriptions need to be deleted. |
| 1482 | * @return bool true if any have been deleted |
| 1483 | */ |
| 1484 | public function leave_group_unsubscribe( $group_id, $user_id ){ |
| 1485 | |
| 1486 | //get the forum associated with the group |
| 1487 | $forums = groups_get_groupmeta( $group_id, 'forum_id' ); |
| 1488 | if( empty( $forums ) ){ |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
| 1492 | foreach( $forums as $forum ){ |
| 1493 | |
| 1494 | //get a user's forum and topic subscriptions |
| 1495 | $forum_subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); |
| 1496 | $topic_subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); |
| 1497 | |
| 1498 | //bail if no subscriptions |
| 1499 | if( empty( $forum_subscriptions ) && empty( $topic_subscriptions ) ){ |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
| 1503 | $group_forums_subscribed = array_intersect( (array) $forum, $forum_subscriptions ); |
| 1504 | //unsubscribe if subscribed to the forum |
| 1505 | if( !empty( $group_forums_subscribed ) ){ |
| 1506 | foreach( $group_forums_subscribed as $group_forum_subscribed ){ |
| 1507 | bbp_remove_user_subscription( $user_id, $group_forum_subscribed ); |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | //get all the child topics for the forum, no matter what they are |
| 1512 | $topics = (array) bbp_get_all_child_ids( $forum, 'topic' ); |
| 1513 | |
| 1514 | //are any of the forum topics in the user's topic subscriptions? |
| 1515 | $group_topics_subscribed = array_intersect( $topics, $topic_subscriptions ); |
| 1516 | |
| 1517 | //if they are, unsubscribe them |
| 1518 | if( !empty( $group_topics_subscribed ) ){ |
| 1519 | foreach( $group_topics_subscribed as $group_topic_subscribed ){ |
| 1520 | bbp_remove_user_subscription( $user_id, $group_topic_subscribed ); |
| 1521 | } |
| 1522 | |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | $unsubs = ( !empty( $group_forums_subscribed ) || !empty( $group_topics_subscribed ) ) ? true : false; |
| 1527 | return $unsubs; |
| 1528 | } |