1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Users; |
4
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Base\BaseModule; |
6
|
|
|
|
7
|
1 |
|
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
|
|
|
/** |
32
|
|
|
* Handle common filters. |
33
|
|
|
* |
34
|
|
|
* @param array $request Request array. |
35
|
|
|
* |
36
|
|
|
* @return array User options. |
37
|
|
|
*/ |
38
|
|
|
protected function parse_common_filters( $request ) { |
39
|
|
|
$options = array(); |
40
|
|
|
|
41
|
|
|
$options['login_restrict'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_login_restrict", false ); |
42
|
|
|
$options['login_days'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_login_days", 0 ) ); |
43
|
|
|
|
44
|
|
|
$options['registered_restrict'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_registered_restrict", false ); |
45
|
|
|
$options['registered_days'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_registered_days", 0 ) ); |
46
|
|
|
|
47
|
|
|
$options['no_posts'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_no_posts", false ); |
48
|
|
|
$options['no_posts_post_types'] = bd_array_get( $request, "smbd_{$this->field_slug}_no_post_post_types", array() ); |
49
|
|
|
|
50
|
|
|
$options['limit_to'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_limit_to", 0 ) ); |
51
|
|
|
|
52
|
|
|
return $options; |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
protected function do_delete( $options ) { |
56
|
8 |
|
$query = $this->build_query( $options ); |
57
|
|
|
|
58
|
8 |
|
if ( empty( $query ) ) { |
59
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
60
|
|
|
return 0; |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
return $this->delete_users_from_query( $query, $options ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Query and Delete users. |
68
|
|
|
* |
69
|
|
|
* @since 5.5.2 |
70
|
|
|
* @access protected |
71
|
|
|
* |
72
|
|
|
* @param array $query Options to query users. |
73
|
|
|
* @param array $options Delete options. |
74
|
|
|
* |
75
|
|
|
* @return int Number of users who were deleted. |
76
|
|
|
*/ |
77
|
8 |
|
protected function delete_users_from_query( $query, $options ) { |
78
|
8 |
|
$count = 0; |
79
|
8 |
|
$users = $this->query_users( $query ); |
80
|
|
|
|
81
|
8 |
|
if ( ! function_exists( 'wp_delete_user' ) ) { |
82
|
|
|
require_once ABSPATH . 'wp-admin/includes/user.php'; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
foreach ( $users as $user ) { |
86
|
8 |
|
if ( ! $this->can_delete_by_registered_date( $options, $user ) ) { |
87
|
2 |
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
if ( ! $this->can_delete_by_logged_date( $options, $user ) ) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
if ( ! $this->can_delete_by_post_count( $options, $user ) ) { |
95
|
2 |
|
continue; |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
$deleted = wp_delete_user( $user->ID ); |
99
|
6 |
|
if ( $deleted ) { |
100
|
6 |
|
$count ++; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
8 |
|
return $count; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Query users using options. |
109
|
|
|
* |
110
|
|
|
* @param array $options Query options. |
111
|
|
|
* |
112
|
|
|
* @return \WP_User[] List of users. |
113
|
|
|
*/ |
114
|
8 |
|
protected function query_users( $options ) { |
115
|
|
|
$defaults = array( |
116
|
8 |
|
'count_total' => false, |
117
|
|
|
); |
118
|
|
|
|
119
|
8 |
|
$options = wp_parse_args( $options, $defaults ); |
120
|
|
|
|
121
|
8 |
|
$wp_user_query = new \WP_User_Query( $options ); |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* This action before the query happens. |
125
|
|
|
* |
126
|
|
|
* @since 6.0.0 |
127
|
|
|
* |
128
|
|
|
* @param \WP_User_Query $wp_user_query Query object. |
129
|
|
|
*/ |
130
|
8 |
|
do_action( 'bd_before_query', $wp_user_query ); |
131
|
|
|
|
132
|
8 |
|
$users = (array) $wp_user_query->get_results(); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* This action runs after the query happens. |
136
|
|
|
* |
137
|
|
|
* @since 6.0.0 |
138
|
|
|
* |
139
|
|
|
* @param \WP_User_Query $wp_user_query Query object. |
140
|
|
|
*/ |
141
|
8 |
|
do_action( 'bd_after_query', $wp_user_query ); |
142
|
|
|
|
143
|
8 |
|
return $users; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Can the user be deleted based on the 'post count' option? |
148
|
|
|
* |
149
|
|
|
* @since 5.5.2 |
150
|
|
|
* @access protected |
151
|
|
|
* |
152
|
|
|
* @param array $delete_options Delete Options. |
153
|
|
|
* @param \WP_User $user User object that needs to be deleted. |
154
|
|
|
* |
155
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
156
|
|
|
*/ |
157
|
6 |
|
protected function can_delete_by_post_count( $delete_options, $user ) { |
158
|
|
|
return ! ( |
159
|
6 |
|
$delete_options['no_posts'] && |
160
|
6 |
|
count_user_posts( $user->ID, $delete_options['no_posts_post_types'] ) > 0 |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Can the user be deleted based on the 'registered date' option? |
166
|
|
|
* |
167
|
|
|
* @since 5.5.3 |
168
|
|
|
* @access protected |
169
|
|
|
* |
170
|
|
|
* @param array $delete_options Delete Options. |
171
|
|
|
* @param \WP_User $user User object that needs to be deleted. |
172
|
|
|
* |
173
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
174
|
|
|
*/ |
175
|
8 |
|
protected function can_delete_by_registered_date( $delete_options, $user ) { |
176
|
8 |
|
if ( $delete_options['registered_restrict'] ) { |
177
|
4 |
|
$registered_days = $delete_options['registered_days']; |
178
|
|
|
|
179
|
4 |
|
if ( $registered_days > 0 ) { |
180
|
4 |
|
$user_meta = get_userdata( $user->ID ); |
181
|
4 |
|
if ( strtotime( $user_meta->user_registered ) > strtotime( '-' . $registered_days . 'days' ) ) { |
182
|
2 |
|
return false; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
6 |
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Can the user be deleted based on the 'logged in date' option? |
192
|
|
|
* |
193
|
|
|
* @since 5.5.2 |
194
|
|
|
* @access protected |
195
|
|
|
* |
196
|
|
|
* @param array $delete_options Delete Options. |
197
|
|
|
* @param \WP_User $user User object that needs to be deleted. |
198
|
|
|
* |
199
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
200
|
|
|
*/ |
201
|
6 |
|
protected function can_delete_by_logged_date( $delete_options, $user ) { |
202
|
6 |
|
if ( $delete_options['login_restrict'] ) { |
203
|
|
|
$login_days = $delete_options['login_days']; |
204
|
|
|
$last_login = bd_get_last_login( $user->ID ); |
205
|
|
|
|
206
|
|
|
if ( null !== $last_login ) { |
|
|
|
|
207
|
|
|
// we have a logged-in entry for the user in simple login log plugin. |
208
|
|
|
if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) { |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
} else { |
212
|
|
|
// we don't have a logged-in entry for the user in simple login log plugin. |
213
|
|
|
if ( $login_days > 0 ) { |
214
|
|
|
// non-zero value for login date. So don't delete this user. |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
6 |
|
return true; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Render User Login restrict settings. |
225
|
|
|
* |
226
|
|
|
* @since 5.5 |
227
|
|
|
*/ |
228
|
|
|
protected function render_user_login_restrict_settings() { |
229
|
|
|
?> |
230
|
|
|
<tr> |
231
|
|
|
<td scope="row" colspan="2"> |
232
|
|
|
<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"> |
233
|
|
|
<?php _e( 'Restrict to users who are registered in the site for at least ', 'bulk-delete' );?> |
234
|
|
|
<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" value="0" min="0" disabled> <?php _e( 'days.', 'bulk-delete' );?> |
235
|
|
|
</td> |
236
|
|
|
</tr> |
237
|
|
|
|
238
|
|
|
<?php |
239
|
|
|
if ( bd_is_simple_login_log_present() ) { |
240
|
|
|
$disabled = ''; |
241
|
|
|
} else { |
242
|
|
|
$disabled = 'disabled'; |
243
|
|
|
} |
244
|
|
|
?> |
245
|
|
|
<tr> |
246
|
|
|
<td scope="row" colspan="2"> |
247
|
|
|
<input name="smbd_<?php echo $this->field_slug; ?>_login_restrict" id="smbd_<?php echo $this->field_slug; ?>_login_restrict" value="true" type="checkbox" <?php echo $disabled; ?>> |
248
|
|
|
<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' );?> |
249
|
|
|
<input type="number" name="smbd_<?php echo $this->field_slug; ?>_login_days" id="smbd_<?php echo $this->field_slug; ?>_login_days" class="screen-per-page" value="0" min="0" disabled> <?php _e( 'days', 'bulk-delete' );?>. |
250
|
|
|
<?php if ( 'disabled' == $disabled ) { ?> |
251
|
|
|
<span style = "color:red"> |
252
|
|
|
<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "http://wordpress.org/plugins/simple-login-log/">Install now</a> |
253
|
|
|
</span> |
254
|
|
|
<?php } ?> |
255
|
|
|
</td> |
256
|
|
|
</tr> |
257
|
|
|
|
258
|
|
|
<?php if ( bd_is_simple_login_log_present() ) : ?> |
259
|
|
|
<tr> |
260
|
|
|
<td scope="row" colspan="2"> |
261
|
|
|
<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?> |
262
|
|
|
</tr> |
263
|
|
|
<?php endif; ?> |
264
|
|
|
<?php |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Render delete user with no posts settings. |
269
|
|
|
* |
270
|
|
|
* @since 5.5 |
271
|
|
|
*/ |
272
|
|
|
protected function render_user_with_no_posts_settings() { |
273
|
|
|
?> |
274
|
|
|
<tr> |
275
|
|
|
<td scope="row" colspan="2"> |
276
|
|
|
<input type="checkbox" value="true" |
277
|
|
|
name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" |
278
|
|
|
id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" class="user_restrict_to_no_posts_filter"> |
279
|
|
|
|
280
|
|
|
<?php _e( "Restrict to users who don't have any posts.", 'bulk-delete' ); ?> |
281
|
|
|
</td> |
282
|
|
|
</tr> |
283
|
|
|
|
284
|
|
|
<tr class="user_restrict_to_no_posts_filter_items visually-hidden"> |
285
|
|
|
<td scope="row" colspan="2"> |
286
|
|
|
<table class="filter-items"> |
287
|
|
|
<tr> |
288
|
|
|
<td scope="row"> |
289
|
|
|
<?php _e( 'Select the post types. By default all post types are considered.', 'bulk-delete' ); ?> |
290
|
|
|
</td> |
291
|
|
|
</tr> |
292
|
|
|
|
293
|
|
|
<?php $this->render_post_type_checkboxes( "smbd_{$this->field_slug}_no_post_post_types" ); ?> |
294
|
|
|
</table> |
295
|
|
|
</td> |
296
|
|
|
</tr> |
297
|
|
|
|
298
|
|
|
<?php |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|