1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bulk Delete Users by User Role. |
4
|
|
|
* |
5
|
|
|
* @since 5.5 |
6
|
|
|
* |
7
|
|
|
* @author Sudar |
8
|
|
|
* |
9
|
|
|
* @package BulkDelete\Users\Modules |
10
|
|
|
*/ |
11
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Bulk Delete Users by User Role. |
15
|
|
|
* |
16
|
|
|
* @since 5.5 |
17
|
|
|
*/ |
18
|
|
|
class Bulk_Delete_Users_By_User_Role extends BD_User_Meta_Box_Module { |
19
|
|
|
/** |
20
|
|
|
* Make this class a "hybrid Singleton". |
21
|
|
|
* |
22
|
|
|
* @static |
23
|
|
|
* |
24
|
|
|
* @since 5.5 |
25
|
|
|
*/ |
26
|
|
|
public static function factory() { |
27
|
|
|
static $instance = false; |
28
|
|
|
|
29
|
|
|
if ( ! $instance ) { |
30
|
|
|
$instance = new self; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $instance; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialize and setup variables. |
38
|
|
|
* |
39
|
|
|
* @since 5.5 |
40
|
|
|
*/ |
41
|
|
|
protected function initialize() { |
42
|
|
|
$this->item_type = 'users'; |
43
|
|
|
$this->field_slug = 'u_role'; |
44
|
|
|
$this->meta_box_slug = 'bd_users_by_role'; |
45
|
|
|
$this->meta_box_hook = "bd_add_meta_box_for_{$this->item_type}"; |
46
|
|
|
$this->delete_action = 'delete_users_by_role'; |
47
|
|
|
$this->cron_hook = 'do-bulk-delete-users-by-role'; |
48
|
|
|
$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-users-by-role/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ur'; |
49
|
|
|
$this->messages = array( |
50
|
|
|
'box_label' => __( 'By User Role', 'bulk-delete' ), |
51
|
|
|
'scheduled' => __( 'Users from the selected userrole are scheduled for deletion.', 'bulk-delete' ), |
52
|
|
|
'deleted_single' => __( 'Deleted %d user from the selected roles', 'bulk-delete' ), |
53
|
|
|
'deleted_plural' => __( 'Deleted %d users from the selected roles', 'bulk-delete' ), |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Render delete users box. |
59
|
|
|
* |
60
|
|
|
* @since 5.5 |
61
|
|
|
*/ |
62
|
|
|
public function render() { |
63
|
|
|
?> |
64
|
|
|
<!-- Users Start--> |
65
|
|
|
<h4><?php _e( 'Select the user roles from which you want to delete users', 'bulk-delete' ); ?></h4> |
66
|
|
|
|
67
|
|
|
<fieldset class="options"> |
68
|
|
|
<table class="optiontable"> |
69
|
|
|
<?php |
70
|
|
|
$users_count = count_users(); |
71
|
|
|
foreach ( $users_count['avail_roles'] as $role => $count ) { |
72
|
|
|
?> |
73
|
|
|
<tr> |
74
|
|
|
<td scope="row" > |
75
|
|
|
<input name="smbd_u_roles[]" value = "<?php echo $role; ?>" type = "checkbox"> |
76
|
|
|
<label for="smbd_u_roles"><?php echo $role; ?> (<?php echo $count . ' '; _e( 'Users', 'bulk-delete' ); ?>)</label> |
77
|
|
|
</td> |
78
|
|
|
</tr> |
79
|
|
|
<?php |
80
|
|
|
} |
81
|
|
|
?> |
82
|
|
|
</table> |
83
|
|
|
|
84
|
|
|
<table class="optiontable"> |
85
|
|
|
<?php |
86
|
|
|
$this->render_filtering_table_header(); |
87
|
|
|
$this->render_user_login_restrict_settings(); |
88
|
|
|
$this->render_user_with_no_posts_settings(); |
89
|
|
|
$this->render_limit_settings(); |
90
|
|
|
$this->render_cron_settings(); |
91
|
|
|
?> |
92
|
|
|
</table> |
93
|
|
|
</fieldset> |
94
|
|
|
<!-- Users end--> |
95
|
|
|
<?php |
96
|
|
|
$this->render_submit_button(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Process the request for deleting users by role. |
101
|
|
|
* |
102
|
|
|
* @since 5.5 |
103
|
|
|
*/ |
104
|
|
|
public function process() { |
105
|
|
|
$delete_options = array(); |
106
|
|
|
|
107
|
|
|
// Nonce verification is handled in `request_handler()`. |
108
|
|
|
$delete_options['selected_roles'] = bd_array_get( $_POST, 'smbd_u_roles' ); // WPCS: XSS ok. |
109
|
|
|
|
110
|
|
|
$this->process_user_delete( $delete_options ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Delete users by user role. |
115
|
|
|
* |
116
|
|
|
* @since 5.5 |
117
|
|
|
* |
118
|
|
|
* @param array $delete_options Delete Options |
119
|
|
|
* |
120
|
|
|
* @return int Number of users deleted |
121
|
|
|
*/ |
122
|
|
|
public function delete( $delete_options ) { |
123
|
|
|
if ( ! function_exists( 'wp_delete_user' ) ) { |
124
|
|
|
require_once ABSPATH . 'wp-admin/includes/user.php'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$count = 0; |
128
|
|
|
|
129
|
|
|
foreach ( $delete_options['selected_roles'] as $role ) { |
130
|
|
|
$options = array('role' => $role); |
131
|
|
|
if ( $delete_options['limit_to'] > 0 ) { |
132
|
|
|
$options['number'] = $delete_options['limit_to']; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$count += $this->delete_users( $options, $delete_options ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $count; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Filter JS Array and add validation hooks. |
143
|
|
|
* |
144
|
|
|
* @since 5.5 |
145
|
|
|
* |
146
|
|
|
* @param array $js_array JavaScript Array |
147
|
|
|
* |
148
|
|
|
* @return array Modified JavaScript Array |
149
|
|
|
*/ |
150
|
|
|
public function filter_js_array( $js_array ) { |
151
|
|
|
$js_array['dt_iterators'][] = '_' . $this->field_slug; |
152
|
|
|
|
153
|
|
|
$js_array['pre_action_msg'][ $this->delete_action ] = 'deleteUsersWarning'; |
154
|
|
|
$js_array['msg']['deleteUsersWarning'] = __( 'Are you sure you want to delete all the users from the selected user role?', 'bulk-delete' ); |
155
|
|
|
|
156
|
|
|
$js_array['error_msg'][ $this->delete_action ] = 'selectOneUserRole'; |
157
|
|
|
$js_array['msg']['selectOneUserRole'] = __( 'Select at least one user role from which users should be deleted', 'bulk-delete' ); |
158
|
|
|
|
159
|
|
|
return $js_array; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
Bulk_Delete_Users_By_User_Role::factory(); |
164
|
|
|
?> |
165
|
|
|
|