-
Notifications
You must be signed in to change notification settings - Fork 0
/
restrict-email-domain.php
281 lines (234 loc) · 7.81 KB
/
restrict-email-domain.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
281
<?php
/*
Plugin Name: Restrict Email Domain
Plugin URI: https://github.com/dale0525/restrict-email-domain
Description: Prevent people from registering on Wordpress with blacklisted email domains from a remote list.
Version: 1.0.0
Author: Logic Tan
Author URI: https://www.logiconsole.com
Network: true
Text Domain: restrict-email-domain
Copyright 2021 Logiconsole (email: [email protected])
This file is part of Restrict Email Domain, a plugin for WordPress.
Restrict Email Domain is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Restrict Email Domain is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WordPress. If not, see <http://www.gnu.org/licenses/>.
*/
class RestrictEmailDomain {
// Holds option data.
public $option_name = 'restrict_email_domain_options';
public $option_defaults;
public $options;
// Instance
public static $instance;
/**
* Construct
*
* @since 1.0
* @access public
*/
public function __construct() {
//allow this instance to be called from outside the class
self::$instance = $this;
add_action( 'init', array( &$this, 'init' ) );
add_action( 'admin_init', array( &$this, 'admin_init' ) );
//add admin panel
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
// Setting plugin defaults here:
$this->option_defaults = array(
'message' => __( '<strong>ERROR</strong>: Sorry, but the email you used is not allowed.', 'restrict-email-domain' ),
'urllist' => 'https://raw.githubusercontent.com/disposable/disposable-email-domains/master/domains.txt'
);
// Fetch and set up options.
$this->options = wp_parse_args( get_site_option( $this->option_name ), $this->option_defaults, false );
}
/**
* Init
*
* @since 1.0
* @access public
*/
public function init() {
// The magic sauce
add_action( 'register_post', array( &$this, 'restrict_email_domain_drop' ), 1, 3 );
}
/**
* Admin init Callback
*
* @since 1.0
*/
public function admin_init() {
// Settings links
$plugin = plugin_basename( __FILE__ );
add_filter( 'plugin_action_links_$plugin', array( &$this, 'settings_link' ) );
// Register Settings
$this->register_settings();
}
/**
* Admin Notices
*
* Display admin notices as needed.
*
* @since 1.0
*/
public function admin_notices() {
printf( '<div class="notice notice-%1$s"><p>%2$s</p></div>', esc_attr( $this->notice_class ), wp_kses_post( $this->notice_message ) );
}
/**
* Upgrade options
*
* @since 1.0
*/
public function upgrade() {
update_site_option( $this->option_name, $this->options );
}
/**
* Admin Menu
*
* @since 1.0
* @access public
*/
public function admin_menu() {
add_management_page( __( 'Restrict Email Domain', 'restrict-email-domain' ), __( 'Restrict Email Domain', 'restrict-email-domain' ), 'moderate_comments', 'restrict-email-domain', array( &$this, 'options' ) );
}
/**
* Register Admin Settings
*
* @since 1.0
*/
public function register_settings() {
register_setting( 'restrict-email-domain', 'restrict_email_domain_options', array( &$this, 'restrict_email_domain_sanitize' ) );
// The main section
add_settings_section( 'restrict-email-domain-settings', '', array( &$this, 'restrict_email_domain_settings_callback' ), 'restrict-email-domain-settings' );
// The Fields
add_settings_field( 'message', __( 'Blocked Notice', 'restrict-email-domain' ), array( &$this, 'message_callback' ), 'restrict-email-domain-settings', 'restrict-email-domain-settings' );
add_settings_field( 'listurl', __( 'The Blacklist URL', 'restrict-email-domain' ), array( &$this, 'listurl_callback' ), 'restrict-email-domain-settings', 'restrict-email-domain-settings' );
}
/**
* Restrict Email Domain Settings Callback
*
* @since 1.0
*/
public function restrict_email_domain_settings_callback() {
?>
<p><?php esc_html_e( 'Customize your Restrict Email Domain experience via the settings below.', 'restrict-email-domain' ); ?></p>
<?php
}
/**
* Message Callback
*
* @since 1.0
*/
public function message_callback() {
?>
<p><?php esc_html_e( 'The message below is displayed to users who\'s email domain is blocked. Remember to keep it simple.', 'restrict-email-domain' ); ?></p>
<p><textarea name="restrict_email_domain_options[message]" id="restrict_email_domain_options[message]" cols="80" rows="2"><?php echo esc_html( $this->options['message'] ); ?></textarea></p>
<?php
}
/**
* List URL Callback
*
* @since 1.0
*/
public function listurl_callback() {
?>
<p><?php esc_html_e( 'The URL containing the domain blacklist. You can provide your own URL, but make sure the content is in plain text and one line per domain.', 'restrict-email-domain' ); ?></p>
<p><input type="url" name="restrict_email_domain_options[urllist]" id="restrict_email_domain_options[urllist]" size="100" value="<?php echo esc_html( $this->options['urllist'] ); ?>" /></p>
<?php
}
/**
* Options
*
* The options page.
*
* @since 1.0
* @access public
*/
public function options() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'Restrict Email Domain', 'restrict-email-domain' ); ?></h1>
<?php
settings_errors();
?>
<form action="options.php" method="POST" >
<?php
settings_fields( 'restrict-email-domain' );
do_settings_sections( 'restrict-email-domain-settings' );
submit_button( '', 'primary', 'update' );
?>
</form>
</div>
<?php
}
/**
* Options sanitization and validation
*
* @param $input the input to be sanitized
* @since 1.0
*/
public function restrict_email_domain_sanitize( $input ) {
// Get current options
$options = $this->options;
// Message
if ( $input['message'] !== $options['message'] && wp_kses_post( $input['message'] === $input['message'] ) ) {
$input['message'] = wp_kses_post( $input['message'] );
} else {
$input['message'] = $options['message'];
}
return $input;
}
/**
* The basic plugin
*
* @since 1.0
* @access public
*/
public function restrict_email_domain_drop( $user_login, $user_email, $errors ) {
$bannedlist_string = file_get_contents($this->options['urllist']);
$bannedlist_array = explode( "\n", $bannedlist_string );
$bannedlist_size = count( $bannedlist_array );
// Go through bannedlist
for ( $i = 0; $i < $bannedlist_size; $i++ ) {
$bannedlist_current = trim( $bannedlist_array[ $i ] );
#region don't use wildcard
if ( stripos( $user_email, $bannedlist_current ) !== false ) {
$errors->add( 'invalid_email', $this->options['message'] );
return true;
}
#endregion
#region support wildcard
// if (function_exists('fnmatch'))
// {
// if ( fnmatch( $bannedlist_current, $user_email )) {
// $errors->add( 'invalid_email', $this->options['message'] );
// return true;
// }
// }
#endregion
}
}
/**
* Settings link
*
* Adds link to settings page on the plugins page
*
* @access public
*/
public function settings_link( $links ) {
$settings_link = admin_url( 'tools.php?page=restrict-email-domain' );
$settings_link = '<a href="' . $settings_link . '">' . __( 'Settings', 'restrict-email-domain' ) . '</a>';
if ( user_can( get_current_user_id(), 'moderate_comments' ) ) {
array_unshift( $links, $settings_link );
}
return $links;
}
}
new RestrictEmailDomain();