-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathusername-availability-checker.php
executable file
·299 lines (242 loc) · 8.73 KB
/
username-availability-checker.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Plugin Name: WordPress Username Availability Checker
* Version: 1.1.8
* Author: BuddyDev
* Author URI: https://buddydev.com
* Plugin URI: https://buddydev.com/buddydev-username-availability-checker/
* Description: Check the availability of Username on WordPress/BuddyPress registration/add new user screens
* License : GPL
*/
// No direct access over web.
if ( ! defined( 'ABSPATH' ) ) {
exit( 0 );
}
class BuddyDev_Username_Availability_Checker {
/**
* Singleton instance.
*
* @var BuddyDev_Username_Availability_Checker
*/
private static $instance = null;
/**
* Absolute path to this plugin directory.
*
* @var string
*/
private $path;
/**
* Absolute url to this plugin directory.
*
* @var string
*/
private $url;
/**
* Plugin version.
*
* @var string
*/
private $version = '1.1.8';
/**
* Constructor.
*/
private function __construct() {
$this->path = plugin_dir_path( __FILE__ );
$this->url = plugin_dir_url( __FILE__ );
$this->setup();
}
/**
* Get the singleton instance.
*
* @return BuddyDev_Username_Availability_Checker
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Setup hooks.
*/
private function setup() {
// load translations.
add_action( 'init', array( $this, 'load_textdomain' ) );
// load css/js on front end.
add_action( 'wp_head', array( $this, 'add_ajax_url' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'load_js' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'load_css' ) );
// on wp-login.php for action=register.
add_action( 'login_enqueue_scripts', array( $this, 'load_js' ) );
add_action( 'login_enqueue_scripts', array( $this, 'load_css' ) );
add_action( 'login_head', array( $this, 'add_ajax_url' ) );
// load assets on admin Add new user screen.
add_action( 'admin_enqueue_scripts', array( $this, 'load_js' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'load_css' ) );
// ajax check.
// hook to ajax action.
add_action( 'wp_ajax_check_username', array( $this, 'ajax_check_username' ) );
// hook to ajax action.
add_action( 'wp_ajax_nopriv_check_username', array( $this, 'ajax_check_username' ) );
}
/**
* Load translations.
*/
public function load_textdomain() {
load_plugin_textdomain( 'bpdev-username-availability-checker', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Check username via ajax.
*/
public function ajax_check_username() {
if ( empty( $_POST['user_name'] ) ) {
// if username is empty, the execution will stop here.
wp_send_json( array(
'code' => 'error',
'message' => __( 'Username Can not be empty!', 'bpdev-username-availability-checker' ),
) );
}
$user_name = sanitize_user( $_POST['user_name'] );
if ( username_exists( $user_name ) ) {
$message = array(
'code' => 'taken',
'message' => __( 'This usename is taken, please choose another one.', 'bpdev-username-availability-checker' ),
);
} elseif ( is_multisite() ) {
// for multisite.
global $wpdb;
// check for the username in the signups table.
$user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name ) );
if ( ! empty( $user ) ) {
$message = array(
'code' => 'registered',
'message' => __( 'This username is registered but not activated. It may be available within few days if not activated. Please check back again for the availability.', 'bpdev-username-availability-checker' ),
);
}
}
if ( empty( $message ) ) {
// so all is well, but now let us validate.
$check = $this->validate_username( $user_name );
if ( empty( $check ) ) {
$message = array(
'code' => 'success',
'message' => __( 'Congrats! The username is available.', 'bpdev-username-availability-checker' ),
);
} else {
$message = array(
'code' => 'error',
'message' => $check,
);
}
}
wp_send_json( $message );
}
/**
* Load required js
*/
public function load_js() {
if ( $this->should_load_asset() ) {
wp_enqueue_script( 'username-availability-checker-js', $this->url . 'assets/username-availability-checker.js', array( 'jquery' ), $this->version );
$data = array(
'selectors' => apply_filters( 'buddydev_uachecker_selectors', 'input#signup_username, form#createuser input#user_login, #registerform input#user_login, .lwa-register input#user_login, .woocommerce-form #reg_username, .woocommerce-checkout #account_username' ),
);
wp_localize_script( 'username-availability-checker-js', '_BDUAChecker', $data );
}
}
/**
* Load css.
*/
public function load_css() {
if ( $this->should_load_asset() ) {
wp_enqueue_style( 'username-availability-checker-css', $this->url . 'assets/username-availability-checker.css', array(), $this->version );
}
}
/**
* Add ajax end point.
*/
public function add_ajax_url() {
?>
<script type="text/javascript">
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' );?>";
</script>
<?php
}
/**
* Check whether to load assets or not?
*
* @return boolean whether to load assets or not
*/
public function should_load_asset() {
global $pagenow;
$load = false;
if ( function_exists( 'bp_is_register_page' ) && bp_is_register_page() ) {
$load = true;
} elseif ( is_admin() && function_exists( 'get_current_screen' ) && get_current_screen()->id == 'user' && get_current_screen()->action == 'add' ) {
$load = true;
} elseif ( $pagenow === 'wp-login.php' && isset( $_GET['action'] ) && $_GET['action'] == 'register' ) {
$load = true;
} elseif ( class_exists( 'LoginWithAjax' ) && ! is_user_logged_in() ) {
$load = true;
} elseif ( function_exists( 'is_account_page' ) && is_account_page() || function_exists( 'is_checkout' ) && is_checkout() ) {
$load = true;
}
// sorry I should have renamed it buddydev_uachecker__load_assets but now I can not, my hads are tied.
return apply_filters( 'buddydev_username_availability_checker_load_assets', $load );
}
/**
* Helper function to check the username is valid or not,
* Thanks to @apeatling, taken from bp-core/bp-core-signup.php and modified for checking only the username
* original: bp_core_validate_user_signup()
*
* @return string nothing if validated else error string
* */
private function validate_username( $user_name ) {
$errors = new WP_Error();
$user_name = sanitize_user( $user_name, true );
if ( empty( $user_name ) ) {
// must not be empty.
$errors->add( 'user_name', __( 'Please enter a valid username.', 'bpdev-username-availability-checker' ) );
}
if ( function_exists( 'buddypress' ) ) {
$user_name = preg_replace( '/\s+/', '', $user_name );
}
// check blacklist.
$illegal_names = get_site_option( 'illegal_names' );
if ( in_array( $user_name, (array) $illegal_names ) ) {
$errors->add( 'user_name', __( 'That username is not allowed.', 'bpdev-username-availability-checker' ) );
}
// see if passed validity check.
if ( ! validate_username( $user_name ) ) {
$errors->add( 'user_name', __( 'Usernames can contain only letters, numbers, ., -, and @', 'bpdev-username-availability-checker' ) );
}
if ( strlen( $user_name ) < 4 ) {
$errors->add( 'user_name', __( 'Username must be at least 4 characters', 'bpdev-username-availability-checker' ) );
} elseif ( mb_strlen( $user_name ) > 60 ) {
$errors->add( 'user_login_too_long', __( 'Username may not be longer than 60 characters.', 'bpdev-username-availability-checker' ) );
}
if ( strpos( ' ' . $user_name, '_' ) != false ) {
$errors->add( 'user_name', __( 'Sorry, usernames may not contain the character "_"!', 'bpdev-username-availability-checker' ) );
}
/* Is the user_name all numeric? */
$match = array();
preg_match( '/[0-9]*/', $user_name, $match );
if ( $match[0] == $user_name ) {
$errors->add( 'user_name', __( 'Sorry, usernames must have letters too!', 'bpdev-username-availability-checker' ) );
}
/**
* Filters the list of blacklisted usernames.
*
* @param array $usernames Array of blacklisted usernames.
*/
$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) {
$errors->add( 'invalid_username', __( 'Sorry, that username is not allowed.', 'bpdev-username-availability-checker' ) );
}
// Let others dictate us
// the divine message to show the users in case of failure
// success is empty, never forget that.
return apply_filters( 'buddydev_uachecker_username_error', $errors->has_errors() ? $errors->get_error_message() : '', $user_name );
}
}
// instantiate.
BuddyDev_Username_Availability_Checker::get_instance();