-
Notifications
You must be signed in to change notification settings - Fork 15
/
rcp-remove-group-member-role.php
82 lines (65 loc) · 2.36 KB
/
rcp-remove-group-member-role.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Plugin Name: Restrict Content Pro - Remove Group Member Role
* Description: Removes role from all group members when owner's membership expired and re-adds the role to all members if this expired membership is later renewed.
* Version: 1.0
* Author: Sandhills Development, LLC
* Author URI: https://sandhillsdev.com
* License: GPL2
*/
/**
* Removes granted user role from all group members when the owner's membership expires.
*
* @param string $old_status
* @param int $membership_id
*/
function ag_rcpga_remove_role_on_expiration( $old_status, $membership_id ) {
if ( ! function_exists( 'rcpga_get_group_by' ) || ! class_exists( 'RCPGA_Group' ) ) {
return;
}
$group = rcpga_get_group_by( 'membership_id', $membership_id );
if ( ! $group instanceof RCPGA_Group ) {
return;
}
/**
* This query is limited to 300 results for performance reasons. Increase at your own risk.
*/
foreach ( $group->get_members( array( 'number' => 300 ) ) as $member ) {
/**
* @var RCPGA_Group_Member $member
*/
if ( 'owner' !== $member->get_role() ) {
$user = get_userdata( $member->get_user_id() );
$user->remove_role( $group->get_membership_role() );
}
}
}
add_action( 'rcp_transition_membership_status_expired', 'ag_rcpga_remove_role_on_expiration', 10, 2 );
/**
* Re-adds role granted by the membership for all group members when a membership is renewed.
*
* @param string $expiration
* @param int $membership_id
* @param RCP_Membership $membership
*/
function ag_rcpga_readd_role_on_renewal( $expiration, $membership_id, $membership ) {
if ( ! function_exists( 'rcpga_get_group_by' ) || ! class_exists( 'RCPGA_Group' ) ) {
return;
}
$group = rcpga_get_group_by( 'membership_id', $membership_id );
if ( ! $group instanceof RCPGA_Group ) {
return;
}
/**
* This query is limited to 300 results for performance reasons. Increase at your own risk.
*/
foreach ( $group->get_members( array( 'number' => 300 ) ) as $member ) {
/**
* @var RCPGA_Group_Member $member
*/
if ( 'owner' !== $member->get_role() ) {
$member->set_membership_role();
}
}
}
add_action( 'rcp_membership_post_renew', 'ag_rcpga_readd_role_on_renewal', 10, 3 );