1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Users; |
4
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Base\BaseModule; |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Encapsulates the Bulk Delete User Meta box Module Logic. |
11
|
|
|
* All Bulk Delete User Meta box Modules should extend this class. |
12
|
|
|
* |
13
|
|
|
* @see BaseModule |
14
|
|
|
* @since 5.5.2 |
15
|
|
|
* @since 6.0.0 Renamed to UsersModule. |
16
|
|
|
*/ |
17
|
|
|
abstract class UsersModule extends BaseModule { |
18
|
|
|
/** |
19
|
|
|
* Build query params for WP_User_Query by using delete options. |
20
|
|
|
* |
21
|
|
|
* Return an empty query array to short-circuit deletion. |
22
|
|
|
* |
23
|
|
|
* @since 6.0.0 |
24
|
|
|
* |
25
|
|
|
* @param array $options Delete options. |
26
|
|
|
* |
27
|
|
|
* @return array Query. |
28
|
|
|
*/ |
29
|
|
|
abstract protected function build_query( $options ); |
30
|
|
|
|
31
|
|
|
protected function parse_common_filters( $request ) { |
32
|
|
|
$options = array(); |
33
|
|
|
|
34
|
|
|
$options['login_restrict'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_login_restrict", false ); |
35
|
|
|
$options['login_days'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_login_days", 0 ) ); |
36
|
|
|
|
37
|
|
|
$options['registered_restrict'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_registered_restrict", false ); |
38
|
|
|
$options['registered_date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' ); |
39
|
|
|
$options['registered_days'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_registered_days", 0 ) ); |
40
|
|
|
|
41
|
|
|
$options['no_posts'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_no_posts", false ); |
42
|
|
|
$options['no_posts_post_types'] = bd_array_get( $request, "smbd_{$this->field_slug}_no_post_post_types", array() ); |
43
|
|
|
|
44
|
|
|
$options['limit_to'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_limit_to", 0 ) ); |
45
|
|
|
|
46
|
|
|
return $options; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function do_delete( $options ) { |
50
|
|
|
$query = $this->build_query( $options ); |
51
|
|
|
|
52
|
|
|
if ( empty( $query ) ) { |
53
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
54
|
|
|
return 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$query = $this->exclude_users_from_deletion( $query ); |
58
|
|
|
$query = $this->exclude_current_user( $query ); |
59
|
|
|
|
60
|
|
|
return $this->delete_users_from_query( $query, $options ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Query and Delete users. |
65
|
|
|
* |
66
|
|
|
* @since 5.5.2 |
67
|
|
|
* @access protected |
68
|
|
|
* |
69
|
|
|
* @param array $query Options to query users. |
70
|
|
|
* @param array $options Delete options. |
71
|
|
|
* |
72
|
|
|
* @return int Number of users who were deleted. |
73
|
|
|
*/ |
74
|
|
|
protected function delete_users_from_query( $query, $options ) { |
75
|
|
|
$count = 0; |
76
|
|
|
$users = $this->query_users( $query ); |
77
|
|
|
|
78
|
|
|
if ( ! function_exists( 'wp_delete_user' ) ) { |
79
|
|
|
require_once ABSPATH . 'wp-admin/includes/user.php'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ( $users as $user ) { |
83
|
|
|
if ( ! $this->can_delete_by_logged_date( $options, $user ) ) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ( ! $this->can_delete_by_post_count( $options, $user ) ) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Filter the user. |
93
|
|
|
* |
94
|
|
|
* @since 6.0.0 |
95
|
|
|
* |
96
|
|
|
* @param array $options Processed options. |
97
|
|
|
* @param array $user User Object. |
98
|
|
|
*/ |
99
|
|
|
if ( apply_filters( 'bd_exclude_edd_customers', $options, $user ) ) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$deleted = wp_delete_user( $user->ID ); |
104
|
|
|
if ( $deleted ) { |
105
|
|
|
$count ++; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $count; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Query users using options. |
114
|
|
|
* |
115
|
|
|
* @param array $options Query options. |
116
|
|
|
* |
117
|
|
|
* @return \WP_User[] List of users. |
118
|
|
|
*/ |
119
|
|
|
protected function query_users( $options ) { |
120
|
|
|
$defaults = array( |
121
|
|
|
'count_total' => false, |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$options = wp_parse_args( $options, $defaults ); |
125
|
|
|
|
126
|
|
|
$wp_user_query = new \WP_User_Query( $options ); |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* This action before the query happens. |
130
|
|
|
* |
131
|
|
|
* @since 6.0.0 |
132
|
|
|
* |
133
|
|
|
* @param \WP_User_Query $wp_user_query Query object. |
134
|
|
|
*/ |
135
|
|
|
do_action( 'bd_before_query', $wp_user_query ); |
136
|
|
|
|
137
|
|
|
$users = (array) $wp_user_query->get_results(); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* This action runs after the query happens. |
141
|
|
|
* |
142
|
|
|
* @since 6.0.0 |
143
|
|
|
* |
144
|
|
|
* @param \WP_User_Query $wp_user_query Query object. |
145
|
|
|
*/ |
146
|
|
|
do_action( 'bd_after_query', $wp_user_query ); |
147
|
|
|
|
148
|
|
|
return $users; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Can the user be deleted based on the 'post count' option? |
153
|
|
|
* |
154
|
|
|
* This doesn't work well in batches. |
155
|
|
|
* |
156
|
|
|
* @link https://github.com/sudar/bulk-delete/issues/511 Github issue. |
157
|
|
|
* @since 5.5.2 |
158
|
|
|
* @access protected |
159
|
|
|
* |
160
|
|
|
* @param array $delete_options Delete Options. |
161
|
|
|
* @param \WP_User $user User object that needs to be deleted. |
162
|
|
|
* |
163
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
164
|
|
|
*/ |
165
|
|
|
protected function can_delete_by_post_count( $delete_options, $user ) { |
166
|
|
|
return ! ( |
167
|
|
|
$delete_options['no_posts'] && |
168
|
|
|
count_user_posts( $user->ID, $delete_options['no_posts_post_types'] ) > 0 |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the date query part for WP_User_Query. |
174
|
|
|
* |
175
|
|
|
* Date query corresponds to user registered date. |
176
|
|
|
* |
177
|
|
|
* @since 6.0.0 |
178
|
|
|
* |
179
|
|
|
* @param array $options Delete options. |
180
|
|
|
* |
181
|
|
|
* @return array Date Query. |
182
|
|
|
*/ |
183
|
|
|
protected function get_date_query( $options ) { |
184
|
|
|
if ( ! $options['registered_restrict'] ) { |
185
|
|
|
return array(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ( $options['registered_days'] <= 0 ) { |
189
|
|
|
return array(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ( ! isset( $options['registered_date_op'] ) ) { |
193
|
|
|
return array( |
194
|
|
|
'before' => $options['registered_days'] . ' days ago', |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ( 'before' === $options['registered_date_op'] || 'after' === $options['registered_date_op'] ) { |
199
|
|
|
return array( |
200
|
|
|
$options['registered_date_op'] => $options['registered_days'] . ' days ago', |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return array(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Can the user be deleted based on the 'logged in date' option? |
209
|
|
|
* |
210
|
|
|
* This doesn't work well in batches. |
211
|
|
|
* |
212
|
|
|
* @link https://github.com/sudar/bulk-delete/issues/511 Github issue. |
213
|
|
|
* @since 5.5.2 |
214
|
|
|
* @access protected |
215
|
|
|
* |
216
|
|
|
* @param array $delete_options Delete Options. |
217
|
|
|
* @param \WP_User $user User object that needs to be deleted. |
218
|
|
|
* |
219
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
220
|
|
|
*/ |
221
|
|
|
protected function can_delete_by_logged_date( $delete_options, $user ) { |
222
|
|
|
if ( $delete_options['login_restrict'] ) { |
223
|
|
|
$login_days = $delete_options['login_days']; |
224
|
|
|
$last_login = bd_get_last_login( $user->ID ); |
225
|
|
|
|
226
|
|
|
if ( null !== $last_login ) { |
|
|
|
|
227
|
|
|
// we have a logged-in entry for the user in simple login log plugin. |
228
|
|
|
if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) { |
229
|
|
|
return false; |
230
|
|
|
} |
231
|
|
|
} else { |
232
|
|
|
// we don't have a logged-in entry for the user in simple login log plugin. |
233
|
|
|
if ( $login_days > 0 ) { |
234
|
|
|
// non-zero value for login date. So don't delete this user. |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Render User Login restrict settings. |
245
|
|
|
* |
246
|
|
|
* @since 5.5 |
247
|
|
|
*/ |
248
|
|
|
protected function render_user_login_restrict_settings() { |
249
|
|
|
?> |
250
|
|
|
<tr> |
251
|
|
|
<td scope="row" colspan="2"> |
252
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_registered_restrict"> |
253
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_registered_restrict" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_registered_restrict" value="true" type="checkbox"> |
254
|
|
|
<?php _e( 'Restrict to users who are registered in the site ', 'bulk-delete' ); ?> |
255
|
|
|
</label> |
256
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_op" disabled> |
257
|
|
|
<option value="before"><?php _e( 'for at least', 'bulk-delete' ); ?></option> |
258
|
|
|
<option value="after"><?php _e( 'in the last', 'bulk-delete' ); ?></option> |
259
|
|
|
</select> |
260
|
|
|
<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_registered_days" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_registered_days" class="screen-per-page" disabled value="0" min="0"><?php _e( ' days.', 'bulk-delete' ); ?> |
261
|
|
|
</td> |
262
|
|
|
</tr> |
263
|
|
|
<tr> |
264
|
|
|
<td scope="row" colspan="2"> |
265
|
|
|
<label> |
266
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_login_restrict" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_login_restrict" |
267
|
|
|
value="true" type="checkbox" <?php disabled( false, bd_is_simple_login_log_present() ); ?>> |
268
|
|
|
<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' ); ?> |
269
|
|
|
</label> |
270
|
|
|
<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_login_days" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_login_days" class="screen-per-page" value="0" min="0" disabled> <?php _e( 'days', 'bulk-delete' ); ?>. |
271
|
|
|
|
272
|
|
|
<?php if ( ! bd_is_simple_login_log_present() ) : ?> |
273
|
|
|
<span style = "color:red"> |
274
|
|
|
<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "https://wordpress.org/plugins/simple-login-log/">Install now</a> |
275
|
|
|
</span> |
276
|
|
|
<?php endif; ?> |
277
|
|
|
</td> |
278
|
|
|
</tr> |
279
|
|
|
|
280
|
|
|
<?php if ( bd_is_simple_login_log_present() ) : ?> |
281
|
|
|
<tr> |
282
|
|
|
<td scope="row" colspan="2"> |
283
|
|
|
<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?> |
284
|
|
|
</tr> |
285
|
|
|
<?php endif; ?> |
286
|
|
|
<?php |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Render delete user with no posts settings. |
291
|
|
|
* |
292
|
|
|
* @since 5.5 |
293
|
|
|
*/ |
294
|
|
|
protected function render_user_with_no_posts_settings() { |
295
|
|
|
?> |
296
|
|
|
<tr> |
297
|
|
|
<td scope="row" colspan="2"> |
298
|
|
|
<input type="checkbox" value="true" |
299
|
|
|
name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" |
300
|
|
|
id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" class="user_restrict_to_no_posts_filter"> |
301
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts"> |
302
|
|
|
<?php _e( "Restrict to users who don't have any posts.", 'bulk-delete' ); ?> |
303
|
|
|
</label> |
304
|
|
|
</td> |
305
|
|
|
</tr> |
306
|
|
|
|
307
|
|
|
<tr class="user_restrict_to_no_posts_filter_items visually-hidden"> |
308
|
|
|
<td scope="row" colspan="2"> |
309
|
|
|
<table class="filter-items"> |
310
|
|
|
<tr> |
311
|
|
|
<td scope="row"> |
312
|
|
|
<?php _e( 'Select the post types. By default all post types are considered.', 'bulk-delete' ); ?> |
313
|
|
|
</td> |
314
|
|
|
</tr> |
315
|
|
|
|
316
|
|
|
<?php $this->render_post_type_checkboxes( "smbd_{$this->field_slug}_no_post_post_types" ); ?> |
317
|
|
|
</table> |
318
|
|
|
</td> |
319
|
|
|
</tr> |
320
|
|
|
|
321
|
|
|
<?php |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get unique user meta keys. |
326
|
|
|
* |
327
|
|
|
* @since 5.5 |
328
|
|
|
* |
329
|
|
|
* @return array List of unique meta keys. |
330
|
|
|
*/ |
331
|
|
|
protected function get_unique_user_meta_keys() { |
332
|
|
|
global $wpdb; |
333
|
|
|
|
334
|
|
|
return $wpdb->get_col( "SELECT DISTINCT(meta_key) FROM {$wpdb->prefix}usermeta ORDER BY meta_key" ); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Exclude current user from being deleted. |
339
|
|
|
* |
340
|
|
|
* @param array $query WP_User_Query args. |
341
|
|
|
* |
342
|
|
|
* @return array Modified query args. |
343
|
|
|
*/ |
344
|
|
|
protected function exclude_current_user( $query ) { |
345
|
|
|
$current_user_id = get_current_user_id(); |
346
|
|
|
|
347
|
|
|
if ( $current_user_id <= 0 ) { |
348
|
|
|
return $query; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
if ( isset( $query['exclude'] ) ) { |
352
|
|
|
$query['exclude'] = array_merge( $query['exclude'], array( $current_user_id ) ); |
353
|
|
|
} else { |
354
|
|
|
$query['exclude'] = array( $current_user_id ); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return $query; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Exclude users from deletion. |
362
|
|
|
* |
363
|
|
|
* @since 6.0.0 |
364
|
|
|
* |
365
|
|
|
* @param array $query Pre-built query. |
366
|
|
|
* |
367
|
|
|
* @return array Modified query. |
368
|
|
|
*/ |
369
|
|
|
protected function exclude_users_from_deletion( array $query ) { |
370
|
|
|
/** |
371
|
|
|
* Filter the list of user ids that will be excluded from deletion. |
372
|
|
|
* |
373
|
|
|
* @since 6.0.0 |
374
|
|
|
* |
375
|
|
|
* @param array $excluded_ids User IDs to be excluded during deletion. |
376
|
|
|
*/ |
377
|
|
|
$excluded_user_ids = apply_filters( 'bd_excluded_user_ids', array() ); |
378
|
|
|
|
379
|
|
|
if ( is_array( $excluded_user_ids ) && ! empty( $excluded_user_ids ) ) { |
380
|
|
|
if ( isset( $query['exclude'] ) ) { |
381
|
|
|
$query['exclude'] = array_merge( $query['exclude'], $excluded_user_ids ); |
382
|
|
|
} else { |
383
|
|
|
$query['exclude'] = $excluded_user_ids; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
return $query; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|