|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Users\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Users\UsersModule; |
|
6
|
|
|
|
|
7
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Bulk Delete Users by User Role Module. |
|
11
|
|
|
* |
|
12
|
|
|
* @since 5.5 |
|
13
|
|
|
* @since 6.0.0 Renamed to DeleteUsersByUserRoleModule |
|
14
|
|
|
*/ |
|
15
|
|
|
class DeleteUsersByUserRoleModule extends UsersModule { |
|
16
|
|
|
/** |
|
17
|
|
|
* Initialize and setup variables. |
|
18
|
|
|
* |
|
19
|
|
|
* @since 5.5 |
|
20
|
|
|
*/ |
|
21
|
17 |
|
protected function initialize() { |
|
22
|
17 |
|
$this->item_type = 'users'; |
|
23
|
17 |
|
$this->field_slug = 'u_role'; |
|
24
|
17 |
|
$this->meta_box_slug = 'bd_users_by_role'; |
|
25
|
17 |
|
$this->action = 'delete_users_by_role'; |
|
26
|
17 |
|
$this->cron_hook = 'do-bulk-delete-users-by-role'; |
|
27
|
17 |
|
$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-users-by-role/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ur'; |
|
28
|
17 |
|
$this->messages = array( |
|
29
|
17 |
|
'box_label' => __( 'By User Role', 'bulk-delete' ), |
|
30
|
17 |
|
'scheduled' => __( 'Users from the selected user role are scheduled for deletion.', 'bulk-delete' ), |
|
31
|
17 |
|
'cron_label' => __( 'Delete Users by User Role', 'bulk-delete' ), |
|
32
|
17 |
|
'validation_error' => __( 'Select at least one user role from which users should be deleted', 'bulk-delete' ), |
|
33
|
17 |
|
'confirm_deletion' => __( 'Are you sure you want to delete all the users from the selected user role?', 'bulk-delete' ), |
|
34
|
17 |
|
'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the users from the selected user role?', 'bulk-delete' ), |
|
35
|
|
|
/* translators: 1 Number of users deleted */ |
|
36
|
17 |
|
'deleted_one' => __( 'Deleted %d user from the selected roles', 'bulk-delete' ), |
|
37
|
|
|
/* translators: 1 Number of users deleted */ |
|
38
|
17 |
|
'deleted_multiple' => __( 'Deleted %d users from the selected roles', 'bulk-delete' ), |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Render delete users box. |
|
44
|
|
|
* |
|
45
|
|
|
* @since 5.5 |
|
46
|
|
|
*/ |
|
47
|
|
|
public function render() { |
|
48
|
|
|
?> |
|
49
|
|
|
<h4><?php _e( 'Select the user roles from which you want to delete users', 'bulk-delete' ); ?></h4> |
|
50
|
|
|
|
|
51
|
|
|
<fieldset class="options"> |
|
52
|
|
|
<table class="optiontable"> |
|
53
|
|
|
<?php $this->render_user_role_dropdown( true ); ?> |
|
54
|
|
|
</table> |
|
55
|
|
|
|
|
56
|
|
|
<table class="optiontable"> |
|
57
|
|
|
<?php |
|
58
|
|
|
$this->render_filtering_table_header(); |
|
59
|
|
|
$this->render_user_login_restrict_settings(); |
|
60
|
|
|
$this->render_user_with_no_posts_settings(); |
|
61
|
|
|
$this->render_limit_settings(); |
|
62
|
|
|
$this->render_post_reassign_settings(); |
|
63
|
|
|
$this->render_cron_settings(); |
|
64
|
|
|
?> |
|
65
|
|
|
</table> |
|
66
|
|
|
</fieldset> |
|
67
|
|
|
<?php |
|
68
|
|
|
$this->render_submit_button(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
|
72
|
|
|
$selected_roles = bd_array_get( $request, 'smbd_' . $this->field_slug . '_roles', array() ); |
|
73
|
|
|
|
|
74
|
|
|
$key = array_search( 'none', $selected_roles, true ); |
|
75
|
|
|
if ( false !== $key ) { |
|
76
|
|
|
unset( $selected_roles[ $key ] ); |
|
77
|
|
|
$options['delete_users_with_no_role'] = true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$options['selected_roles'] = $selected_roles; |
|
81
|
|
|
|
|
82
|
|
|
return $options; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Handle both users with roles and without roles. |
|
87
|
|
|
* |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $options Array of Delete options. |
|
91
|
|
|
* |
|
92
|
|
|
* @return int Number of items that were deleted. |
|
93
|
|
|
*/ |
|
94
|
17 |
|
protected function do_delete( $options ) { |
|
95
|
17 |
|
$users_with_roles_deleted = parent::do_delete( $options ); |
|
96
|
|
|
|
|
97
|
17 |
|
if ( ! isset( $options['delete_users_with_no_role'] ) ) { |
|
98
|
15 |
|
return $users_with_roles_deleted; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
return $users_with_roles_deleted + $this->delete_users_with_no_roles( $options ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Delete users with no roles. |
|
106
|
|
|
* |
|
107
|
|
|
* @since 6.0.0 |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $options User options. |
|
110
|
|
|
* |
|
111
|
|
|
* @return int Number of users that were deleted. |
|
112
|
|
|
*/ |
|
113
|
2 |
|
protected function delete_users_with_no_roles( $options ) { |
|
114
|
2 |
|
$query = $this->build_query_for_deleting_users_with_no_roles( $options ); |
|
115
|
|
|
|
|
116
|
2 |
|
if ( empty( $query ) ) { |
|
117
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
|
118
|
|
|
return 0; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
$query = $this->exclude_users_from_deletion( $query ); |
|
122
|
2 |
|
$query = $this->exclude_current_user( $query ); |
|
123
|
|
|
|
|
124
|
2 |
|
return $this->delete_users_from_query( $query, $options ); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Build query params for WP_User_Query by using delete options for deleting users with no roles. |
|
129
|
|
|
* |
|
130
|
|
|
* Return an empty query array to short-circuit deletion. |
|
131
|
|
|
* |
|
132
|
|
|
* @since 6.0.0 |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $options Delete options. |
|
135
|
|
|
* |
|
136
|
|
|
* @return array Query. |
|
137
|
|
|
*/ |
|
138
|
2 |
|
protected function build_query_for_deleting_users_with_no_roles( $options ) { |
|
139
|
|
|
// Users with no role is not selected. |
|
140
|
2 |
|
if ( ! isset( $options['delete_users_with_no_role'] ) || ! $options['delete_users_with_no_role'] ) { |
|
141
|
|
|
return array(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
$roles = get_editable_roles(); |
|
145
|
2 |
|
$role_names = array_keys( $roles ); |
|
146
|
|
|
|
|
147
|
|
|
$query = array( |
|
148
|
2 |
|
'role__not_in' => $role_names, |
|
149
|
2 |
|
'number' => $options['limit_to'], |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
2 |
|
$date_query = $this->get_date_query( $options ); |
|
153
|
|
|
|
|
154
|
2 |
|
if ( ! empty( $date_query ) ) { |
|
155
|
|
|
$query['date_query'] = $date_query; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
return $query; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Build query params for WP_User_Query by using delete options. |
|
163
|
|
|
* |
|
164
|
|
|
* Return an empty query array to short-circuit deletion. |
|
165
|
|
|
* |
|
166
|
|
|
* @since 6.0.0 |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $options Delete options. |
|
169
|
|
|
* |
|
170
|
|
|
* @return array Query. |
|
171
|
|
|
*/ |
|
172
|
17 |
|
protected function build_query( $options ) { |
|
173
|
|
|
// No role is selected. |
|
174
|
17 |
|
if ( empty( $options['selected_roles'] ) ) { |
|
175
|
2 |
|
return array(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$query = array( |
|
179
|
15 |
|
'role__in' => $options['selected_roles'], |
|
180
|
15 |
|
'number' => $options['limit_to'], |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
15 |
|
$date_query = $this->get_date_query( $options ); |
|
184
|
|
|
|
|
185
|
15 |
|
if ( ! empty( $date_query ) ) { |
|
186
|
5 |
|
$query['date_query'] = $date_query; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
15 |
|
return $query; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
protected function append_to_js_array( $js_array ) { |
|
193
|
|
|
$js_array['validators'][ $this->action ] = 'validateUserRole'; |
|
194
|
|
|
|
|
195
|
|
|
return $js_array; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
|
199
|
|
|
protected function prepare_cli_input( $input ) { |
|
200
|
|
|
// Handle multiple post types for no posts filter. |
|
201
|
|
|
$input['no_post_post_types'] = explode( ',', $input['no_post_post_types'] ); |
|
202
|
|
|
// Handle multiple user roles selection. |
|
203
|
|
|
$input['roles'] = explode( ',', $input['roles'] ); |
|
204
|
|
|
|
|
205
|
|
|
return parent::prepare_cli_input( $input ); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|