This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
forked from buddydev/limit-groups-per-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limit-groups-per-user.php
executable file
·280 lines (228 loc) · 5.86 KB
/
limit-groups-per-user.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Plugin name: Limit Groups per User
* description: Limit the number of groups a user can create.
* Author: BuddyDev
* Author URI: https://buddydev.com
* Plugin URI: https://buddydev.com/buddypress/limit-groups-per-user-plugin-for-buddypress/
* Version: 2.0.1
* License: GPL
*/
// Exit if file access directly over web.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class BP_Limit_Groups_Per_User_Helper
*/
class BP_Limit_Groups_Per_User_Helper {
/**
* Plugin directory path
*
* @var string
*/
private $path;
/**
* Class instance
*
* @var BP_Limit_Groups_Per_User_Helper
*/
private static $instance;
/**
* BP_Limit_Groups_Per_User_Helper constructor.
*/
private function __construct() {
$this->path = plugin_dir_path( __FILE__ );
$this->setup();
}
/**
* Get singleton instance.
*
* @return BP_Limit_Groups_Per_User_Helper
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Callbacks to necessaries actions
*/
public function setup() {
register_activation_hook(
__FILE__,
array( $this, 'on_activation' )
);
add_filter(
'bp_user_can_create_groups',
array( $this, 'filter_create_groups_permission' )
);
add_action( 'bp_actions', array( $this, 'groups_action_create_redirect' ), 9 );
add_action( 'plugins_loaded', array( $this, 'load_admin' ), 9996 );
}
/**
* On activation
*/
public function on_activation() {
// delete old option.
delete_site_option( 'limit-groups-creation-per-user' );
if ( is_multisite() ) {
// yes, it is written correctly. Don't feel strange.
delete_option( 'limit-groups-creation-per-user' );
}
}
/**
* Filter permission.
*
* @param bool $can is allowed tpo create.
*
* @return bool
*/
public function filter_create_groups_permission( $can ) {
if ( ! is_user_logged_in() || is_super_admin() || ( bp_is_group_create() && ! bp_is_action_variable( 'group-details', 1 ) ) ) {
return $can;
}
$user_id = get_current_user_id();
// if the user is restricted and has exceeded limit, do not allow.
if ( self::is_user_restricted( $user_id ) && self::has_exceeded_limit( $user_id ) ) {
$can = false;
}
return $can;
}
/**
* Get the total allowed no. of groups for user
*
* @param int $user_id User id.
*
* @return int
*/
public static function get_allowed_group_count( $user_id ) {
$allowed_count = apply_filters( 'limit_groups_get_allowed_group_count', self::get_user_limit( $user_id ), $user_id );
return absint( $allowed_count );
}
/**
* Modify group redirect
*/
public function groups_action_create_redirect() {
// If we're not at domain.org/groups/create/ then return false.
if ( ! bp_is_groups_component() || ! bp_is_current_action( 'create' ) ) {
return false;
}
if ( ! is_user_logged_in() ) {
return false;
}
$redirect_url = trim( self::get_option( 'redirect_url' ) );
if ( ! bp_user_can_create_groups() && $redirect_url ) {
bp_core_add_message( __( 'Sorry, you are not allowed to create groups.', 'buddypress' ), 'error' );
bp_core_redirect( $redirect_url );
}
}
/**
* Load admin
*/
public function load_admin() {
if ( is_admin() && ! wp_doing_ajax() ) {
require_once $this->path . 'admin/pt-settings/pt-settings-loader.php';
require_once $this->path . 'admin/class-limit-groups-per-user-admin-helper.php';
$admin_helper = new Limit_Groups_Per_User_Admin_Helper();
$admin_helper->setup();
}
}
/**
* Get setting value
*
* @param string $key Option key.
*
* @return mixed
*/
public static function get_option( $key ) {
$settings = get_option( 'limit-groups-per-user-settings' );
if ( isset( $settings[ $key ] ) ) {
return $settings[ $key ];
}
return null;
}
/**
* Check if user group creation limited exceeded or not
*
* @param int $user_id User id.
*
* @return bool
*/
public static function has_exceeded_limit( $user_id ) {
$has_exceeded = false;
$allowed_limit = self::get_allowed_group_count( $user_id );
$count_created = self::get_user_groups_created_count( $user_id );
// In case of new group creation, do not prevent steps.
if ( bp_get_new_group_id() ) {
$count_created--;
}
if ( $count_created >= $allowed_limit ) {
$has_exceeded = true;
}
return $has_exceeded;
}
/**
* Get groups count created by user
*
* @param int $user_id User id.
*
* @return int
*/
public static function get_user_groups_created_count( $user_id ) {
global $wpdb;
$table = $wpdb->prefix . 'bp_groups';
if ( empty( $user_id ) ) {
return 0;
}
$query = $wpdb->prepare( "SELECT COUNT('*') FROM {$table} WHERE creator_id = %d", absint( $user_id ) );
$count = $wpdb->get_var( $query );
return (int) $count;
}
/**
* Get user group creation limit
*
* @param int $user_id User id.
*
* @return int
*/
public static function get_user_limit( $user_id ) {
$user = get_user_by( 'id', $user_id );
if ( ! $user ) {
return 0;
}
$allowed_count = 0;
foreach ( $user->roles as $role ) {
$role_allowed_count = self::get_option( "{$role}_threshold_limit" );
// Increase user threshold to max of his role threshold.
if ( $role_allowed_count > $allowed_count ) {
$allowed_count = $role_allowed_count;
}
}
return absint( $allowed_count );
}
/**
* Check if user is restricted or not
*
* @param int $user_id user id.
*
* @return boolean
*/
public static function is_user_restricted( $user_id ) {
$is_restricted = true;
$user = get_user_by( 'id', $user_id );
if ( empty( $user ) ) {
return $is_restricted;
}
foreach ( $user->roles as $role ) {
// if any role is not restricted, do not restrict.
if ( ! self::get_option( "restrict_role_{$role}" ) ) {
$is_restricted = false;
break;
}
}
return $is_restricted;
}
}
BP_Limit_Groups_Per_User_Helper::get_instance();