-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbp-rsed.php
204 lines (168 loc) · 4.88 KB
/
bp-rsed.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
<?php
/*
Plugin Name: BP Restrict Signup By Email Domain
Description: Only allow users with email addresses from certain domains to register in BuddyPress.
Version: 1.0.0
Author: r-a-y
Author URI: http://profiles.wordpress.org/r-a-y
License: GPLv2 or later
Text Domain: bp-rsed
*/
/**
* BP Restrict Signup By Email Domain Core.
*
* @package BP_RSED
* @subpackage Core
*/
add_action( 'bp_include', array( 'BP_RSED', 'init' ) );
/**
* BP Restrict Signup By Email Domain.
*
* @since 1.0.0
*/
class BP_RSED {
/**
* Static initializer.
*/
public static function init() {
return new self();
}
/**
* Constructor.
*/
protected function __construct() {
// This plugin only works on BuddyPress 1.6+.
if ( false === function_exists( 'bp_version' ) ) {
return;
}
// Translations for everybody!
load_plugin_textdomain( 'bp-rsed' );
// Hooks.
add_action( 'bp_signup_validate', array( $this, 'validate' ) );
add_action( 'bp_before_account_details_fields', array( $this, 'registration_msg' ) );
// Set up admin area if in the WP dashboard.
if ( defined( 'WP_NETWORK_ADMIN' ) ) {
require dirname( __FILE__ ) . '/bp-rsed-admin.php';
BP_RSED_Admin_Settings::init();
}
}
/**
* Validate email address against custom whitelisted email domains.
*/
public function validate() {
global $bp;
$allowed_domains = self::allowed_domains();
// make sure we have some whitelisted domains added
if ( empty( $allowed_domains ) || empty( $bp->signup->email ) ) {
return;
}
// get full email domain of email address
$email_domain = substr(
$bp->signup->email,
strpos( $bp->signup->email, '@' ) + 1
);
$okay = false;
foreach ( $allowed_domains as $allowed ) {
// See if email address matches a whitelisted domain.
if ( self::string_ends_with( $email_domain, $allowed ) ) {
// First character doesn't contain a dot; check length.
if ( strpos( $allowed, '.' ) !== 0 ) {
if ( strlen( $allowed ) == strlen( $email_domain ) ) {
$okay = true;
break;
}
} else {
$okay = true;
break;
}
}
}
// Email address doesn't match any whitelisted domain, so throw up an error.
if ( ! $okay ) {
$settings = self::get_settings();
$bp->signup->errors['signup_email'] = sprintf(
str_replace( '{{domains}}', '%s', wp_kses_data( $settings['error_msg'] ) ),
implode( ', ', $allowed_domains )
);
}
}
/**
* Show custom registration blurb if available.
*
* Only shown if whitelisted email domains are set.
*/
public function registration_msg() {
$allowed_domains = self::allowed_domains();
// Make sure we have some whitelisted domains added.
if ( empty( $allowed_domains ) ) {
return;
}
$settings = self::get_settings();
if ( empty( $settings['registration_msg'] ) ) {
return;
}
// Allow default KSES elements through.
add_filter( 'comment_text', 'wp_kses_post' );
echo apply_filters( 'comment_text',
sprintf(
str_replace(
array( '{{domains}}', '{{adminemail}}' ),
array( '%1$s', '%2$s' ),
$settings['registration_msg']
),
'<strong>' . implode( ', ', $allowed_domains ) . '</strong>',
antispambot( bp_get_option( 'admin_email' ) )
)
);
// Remove our filter.
remove_filter( 'comment_text', 'wp_kses_post' );
}
/**
* Only allow certain email domains to register in BuddyPress.
*
* @return array
*/
public static function allowed_domains() {
$settings = self::get_settings();
if ( empty( $settings['whitelist_domains'] ) ) {
return false;
}
$domains = preg_split( '/\R/', $settings['whitelist_domains'] );
return apply_filters( 'bp_rsed_allowed_domains', $domains );
}
/**
* Get our settings.
*
* Sets defaults if not set.
*
* @return array
*/
public static function get_settings() {
$settings = bp_get_option( 'bp_rsed' );
// Set defaults.
if ( is_string( $settings ) ) {
$settings = array();
// Can be overriden on settings page.
if ( empty( $settings['registration_msg'] ) ) {
$settings['registration_msg'] = __( 'Registrations are currently only allowed for email addresses from these domains - {{domains}}. To register with a different address, please write to {{adminemail}} to request an account.', 'bp-rsed' );
}
}
// Always have an error message.
if ( empty( $settings['error_msg'] ) ) {
$settings['error_msg'] = __( 'This email address is not allowed. Please use an email address from one of these domains - {{domains}}', 'bp-rsed' );
}
return $settings;
}
/**
* Static method to find out if a string ends with a certain string.
*
* @link http://stackoverflow.com/a/619725
*
* @param string $str The string we want to test against
* @param string $test The string we're attempting to find
* @bool
*/
public static function string_ends_with( $str, $test ) {
return substr_compare( $str, $test, strlen( $str )-strlen( $test ), strlen( $test ) ) === 0;
}
}