-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom-add-user.php
396 lines (336 loc) Β· 14 KB
/
custom-add-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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**
* Custom Add User Page.
*
* @link http://about.me/harshit
* @package Custom-Add-User
* @author Harshit Sanghvi {@link http://github.com/sanghviharshit}
* @license GNU General Public License (Version 3 - GPLv3) {@link http://www.gnu.org/licenses/gpl-3.0.html}
*
* @wordpress-plugin
* Plugin Name: Custom Add User
* Description: Completely customized add user page with single form for adding brand new users or existing users in multisite without sending them email notification.
* Plugin URI: https://github.com/sanghviharshit/custom-add-user
* Author: Harshit Sanghvi <[email protected]>
* Author URI: http://sanghviharshit.com
* License: GPL3
* Version: 2.0.2
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'CAU_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'CAU_PAGE_SLUG', 'custom-add-user.php');
class Custom_Add_User {
/** @var string $text_domain The text domain of the plugin */
var $text_domain = 'cau_trans';
/** @var string $plugin_name The plugin name */
var $plugin_name = 'custom_add_user';
/** @var string $plugin_dir The plugin directory path */
var $plugin_dir;
/** @var string $plugin_url The plugin directory URL */
var $plugin_url;
/** @var string $plugin_basename The plugin base name */
var $plugin_basename;
/** @var string $options_name The plugin options string */
var $options_name = 'custom_add_user_options';
/** @var array $settings The plugin site options */
var $settings;
/** @var array $settings The plugin network options */
var $network_settings;
/** @var array $settings The plugin network or site options depending on localization in admin page */
var $current_settings;
/**
* Constructor.
*/
function __construct() {
add_action( 'init', array( $this, 'init' ), 1 );
add_action( 'init', array( $this, 'init_vars' ), 1 );
}
/**
* Initiate variables.
*
* @return void
*/
function init_vars() {
global $wpdb;
/* Set plugin directory path */
$this->plugin_dir = CAU_PLUGIN_DIR;
/* Set plugin directory URL */
$this->plugin_url = plugin_dir_url(__FILE__);
/* Set plugin basename */
$this->plugin_basename = plugin_basename( __FILE__ );
$this->settings = $this->get_options();
$this->network_settings = $this->get_options(null, 'network');
// Makes sure the plugin is defined before trying to use it
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
$this->current_settings = is_plugin_active_for_network( $this->plugin_basename ) ? $this->network_settings : $this->settings;
}
/**
* Initiate plugin.
*
* @return void
*/
function init() {
/* Handle form from settings page */
add_action( 'admin_init', array( $this, 'handle_page_requests' ) );
/* Load Custom CSS only on user-new.php */
add_action( 'load-user-new.php', array($this, 'load_css' ) );
/* Create Admin menu */
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
/* Create Network Admin menu */
add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) );
/* Display instructions for adding users */
add_action( 'user_new_form', array( $this , 'custom_content_below_add_user_form' ) );
/* Handle 'createuser' action https://developer.wordpress.org/reference/hooks/admin_action__requestaction/ */
add_action( 'admin_action_createuser', array( $this , 'custom_createuser' ) );
/* Handle 'adduser' action https://developer.wordpress.org/reference/hooks/admin_action__requestaction/ */
add_action( 'admin_action_adduser', array( $this , 'custom_adduser' ) );
}
/**
* Add CSS
*
* @return void
*/
function load_css($hook) {
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'includes/custom-add-user.css', array(), $this->version, 'all' );
}
/**
* Loads the language file from the "languages" directory.
*
* @return void
*/
function load_plugin_textdomain() {
load_plugin_textdomain( $this->text_domain, null, dirname( plugin_basename( __FILE__ ) ) . '/includes/languages/' );
}
/**
* Add Custom Add User options page.
*
* @return void
*/
function admin_menu() {
add_submenu_page( 'options-general.php', 'Custom Add User Settings', 'Custom Add User', 'manage_options', 'custom-add-user-settings', array( &$this, 'output_site_settings_page' ) );
}
/**
* Add network admin menu
*
* @access public
* @return void
*/
function network_admin_menu() {
if (is_plugin_active_for_network( $this->plugin_basename ) ) {
add_submenu_page( 'settings.php', 'Custom Add User Settings', 'Custom Add User', 'manage_network', 'custom-add-user-settings', array( $this, 'output_network_settings_page' ) );
}
}
/**
* Network settings page for Custom New User
*
* @return void
*/
function output_network_settings_page() {
$this->output_site_settings_page( 'network' );
}
/**
* Admin options page output
*
* @return void
*/
function output_site_settings_page( $network = '' ) {
require_once( $this->plugin_dir . "includes/custom-add-user-settings.php" );
}
/**
* Adds Custom text on add user page below add user form.
*
* @access public
*/
public function custom_content_below_add_user_form($type) {
/*
$type can be βadd-existing-userβ (Multisite), and βadd-new-userβ (single site and network admin).
https://developer.wordpress.org/reference/hooks/user_new_form/
*/
$cau_instructions = '';
if ($type == 'add-new-user' && !empty($this->current_settings['cau_settings']['cau_instructions_content'])) {
$cau_instructions = stripslashes($this->current_settings['cau_settings']['cau_instructions_content']);
}
echo $cau_instructions;
}
/**
* Creates user without email confirmation.
*
* @access public
*/
public function custom_createuser() {
global $wpdb;
check_admin_referer( 'create-user', '_wpnonce_create-user' );
if ( ! current_user_can('create_users') )
wp_die(__('Cheatin’ uh?'));
if ( ! is_multisite() ) {
$user_id = edit_user();
if ( is_wp_error( $user_id ) ) {
$add_user_errors = $user_id;
} else {
if ( current_user_can( 'list_users' ) )
$redirect = 'users.php?update=add&id=' . $user_id;
else
$redirect = add_query_arg( 'update', 'add', 'user-new.php' );
wp_redirect( $redirect );
die();
}
} else {
/* Check if user already exists in the network */
$user_details = null;
/* Find user by user_login instead of email */
$user_login = wp_unslash( $_REQUEST['user_login'] );
//$user_email = wp_unslash( $_REQUEST['email'] );
$user_details = get_user_by( 'login', $user_login );
if ( !$user_details ) {
if ( ! current_user_can( 'create_users' ) ) {
wp_die(
'<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' .
'<p>' . __( 'Sorry, you are not allowed to create users.' ) . '</p>',
403
);
}
// Adding a new user to this site
$new_user_email = wp_unslash( $_REQUEST['email'] );
$user_details = wpmu_validate_user_signup( $_REQUEST['user_login'], $new_user_email );
if ( is_wp_error( $user_details[ 'errors' ] ) && !empty( $user_details[ 'errors' ]->errors ) ) {
$add_user_errors = $user_details[ 'errors' ];
} else {
$new_user_login = apply_filters( 'pre_user_login', sanitize_user( wp_unslash( $_REQUEST['user_login'] ), true ) );
add_filter( 'wpmu_signup_user_notification', '__return_false' ); // Disable confirmation email
/* Not Disabling the welcome email */
//add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email
wpmu_signup_user( $new_user_login, $new_user_email, array( 'add_to_blog' => $wpdb->blogid, 'new_role' => $_REQUEST[ 'role' ] ) );
$key = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s AND user_email = %s", $new_user_login, $_REQUEST[ 'email' ] ) );
$new_user = wpmu_activate_signup( $key );
if ( is_wp_error( $new_user ) ) {
$redirect = add_query_arg( array( 'update' => 'addnoconfirmation' ), 'user-new.php' );
} else {
$redirect = add_query_arg( array( 'update' => 'addnoconfirmation', 'user_id' => $new_user['user_id'] ), 'user-new.php' );
}
wp_redirect( $redirect );
die();
}
} else {
if ( ! current_user_can( 'promote_user', $user_details->ID ) ) {
wp_die(
'<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' .
'<p>' . __( 'Sorry, you are not allowed to add users to this network.' ) . '</p>',
403
);
}
//Adding an existing user to this blog
$new_user_email = $user_details->user_email;
$redirect = 'user-new.php';
$username = $user_details->user_login;
$user_id = $user_details->ID;
add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => $_REQUEST[ 'role' ] ) );
$redirect = add_query_arg( array('update' => 'addnoconfirmation'), 'user-new.php' );
wp_redirect( $redirect );
die();
}
}
}
/**
* Adds existing user without email confirmation.
*
* @access public
*/
public function custom_adduser() {
global $wpdb;
check_admin_referer( 'add-user', '_wpnonce_add-user' );
$user_details = null;
$user_email = wp_unslash( $_REQUEST['email'] );
if ( false !== strpos($user_email, '@') ) {
$user_details = get_user_by('email', $user_email);
} else {
if ( is_super_admin() ) {
$user_details = get_user_by('login', $user_email);
} else {
wp_redirect( add_query_arg( array('update' => 'enter_email'), 'user-new.php' ) );
die();
}
}
if ( !$user_details ) {
wp_redirect( add_query_arg( array('update' => 'does_not_exist'), 'user-new.php' ) );
die();
}
if ( ! current_user_can('promote_user', $user_details->ID) )
wp_die(__('Cheatin’ uh?'));
// Adding an existing user to this blog
$new_user_email = $user_details->user_email;
$redirect = 'user-new.php';
$username = $user_details->user_login;
$user_id = $user_details->ID;
add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => $_REQUEST[ 'role' ] ) );
$redirect = add_query_arg( array('update' => 'addnoconfirmation'), 'user-new.php' );
wp_redirect( $redirect );
die();
}
/**
* Update Custom Add User plugin settings into DB.
*
* @return void
*/
function handle_page_requests() {
if ( isset( $_POST['submit'] ) ) {
if ( wp_verify_nonce( $_POST['_wpnonce'], 'cau_submit_settings_network' ) ) {
//save network settings
$this->save_options( array('cau_settings' => $_POST), 'network' );
wp_redirect( add_query_arg( array( 'page' => 'custom-add-user-settings', 'dmsg' => urlencode( __( 'Changes were saved!', $this->text_domain ) ) ), 'settings.php' ) );
exit;
}
elseif ( wp_verify_nonce( $_POST['_wpnonce'], 'cau_submit_settings' ) ) {
//save settings
$this->save_options( array('cau_settings' => $_POST) );
wp_redirect( add_query_arg( array( 'page' => 'custom-add-user-settings', 'dmsg' => urlencode( __( 'Changes were saved!', $this->text_domain ) ) ), 'options-general.php' ) );
exit;
}
}
}
/**
* Save plugin options.
*
* @param array $params The $_POST array
* @return void
*/
function save_options( $params, $network = '' ) {
/* Remove unwanted parameters */
unset( $params['_wpnonce'], $params['_wp_http_referer'], $params['submit'] );
/* Update options by merging the old ones */
if ( '' == $network )
$options = get_option( $this->options_name );
else
$options = get_site_option( $this->options_name );
if(!is_array($options))
$options = array();
$options = array_merge( $options, $params );
if ( '' == $network )
update_option( $this->options_name, $options );
else
update_site_option( $this->options_name, $options );
}
/**
* Get plugin options.
*
* @param string|NULL $key The key for that plugin option.
* @return array $options Plugin options or empty array if no options are found
*/
function get_options( $key = null, $network = '' ) {
if ( '' == $network )
$options = get_option( $this->options_name );
else
$options = get_site_option( $this->options_name );
/* Check if specific plugin option is requested and return it */
if ( isset( $key ) && array_key_exists( $key, $options ) )
return $options[$key];
else
return $options;
}
}
global $custom_add_user;
$custom_add_user = new Custom_Add_User();