1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base class for a Bulk Delete User Meta Box Module. |
4
|
|
|
* |
5
|
|
|
* @since 5.5.2 |
6
|
|
|
* |
7
|
|
|
* @author Sudar |
8
|
|
|
* |
9
|
|
|
* @package BulkDelete\Base\Users |
10
|
|
|
*/ |
11
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Encapsulates the Bulk Delete User Meta box Module Logic. |
15
|
|
|
* All Bulk Delete User Meta box Modules should extend this class. |
16
|
|
|
* |
17
|
|
|
* @see BD_Meta_Box_Module |
18
|
|
|
* @since 5.5.2 |
19
|
|
|
* @abstract |
20
|
|
|
*/ |
21
|
|
|
abstract class BD_User_Meta_Box_Module extends BD_Meta_Box_Module { |
22
|
|
|
/** |
23
|
|
|
* Query and Delete users. |
24
|
|
|
* |
25
|
|
|
* @since 5.5.2 |
26
|
|
|
* @access protected |
27
|
|
|
* |
28
|
|
|
* @param array $options Options to query users. |
29
|
|
|
* @param array $delete_options Delete options. |
30
|
|
|
* |
31
|
|
|
* @return int Number of users who were deleted. |
32
|
|
|
*/ |
33
|
|
|
protected function delete_users( $options, $delete_options ) { |
34
|
|
|
$count = 0; |
35
|
|
|
$users = get_users( $options ); |
36
|
|
|
|
37
|
|
|
foreach ( $users as $user ) { |
38
|
|
|
if ( ! $this->can_delete_by_registered_date( $delete_options, $user ) ) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ( ! $this->can_delete_by_logged_date( $delete_options, $user ) ) { |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( ! $this->can_delete_by_post_count( $delete_options, $user ) ) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$deleted = wp_delete_user( $user->ID ); |
51
|
|
|
if ( $deleted ) { |
52
|
|
|
$count ++; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $count; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Can the user be deleted based on the 'post count' option? |
61
|
|
|
* |
62
|
|
|
* @since 5.5.2 |
63
|
|
|
* @access protected |
64
|
|
|
* |
65
|
|
|
* @param array $delete_options Delete Options. |
66
|
|
|
* @param object $user User object that needs to be deleted. |
67
|
|
|
* |
68
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
69
|
|
|
*/ |
70
|
|
|
protected function can_delete_by_post_count( $delete_options, $user ) { |
71
|
|
|
return ! ( |
72
|
|
|
$delete_options['no_posts'] && |
73
|
|
|
count_user_posts( $user->ID, $delete_options['no_posts_post_types'] ) > 0 |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Can the user be deleted based on the 'registered date' option? |
79
|
|
|
* |
80
|
|
|
* @since 5.5.3 |
81
|
|
|
* @access protected |
82
|
|
|
* |
83
|
|
|
* @param array $delete_options Delete Options. |
84
|
|
|
* @param object $user User object that needs to be deleted. |
85
|
|
|
* |
86
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
87
|
|
|
*/ |
88
|
|
|
protected function can_delete_by_registered_date( $delete_options, $user ) { |
89
|
|
|
if ( $delete_options['registered_restrict'] ) { |
90
|
|
|
$registered_days = $delete_options['registered_days']; |
91
|
|
|
|
92
|
|
|
if ( $registered_days > 0 ) { |
93
|
|
|
$user_meta = get_userdata( $user->ID ); |
94
|
|
|
if ( strtotime( $user_meta->user_registered ) > strtotime( '-' . $registered_days . 'days' ) ) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Can the user be deleted based on the 'logged in date' option? |
105
|
|
|
* |
106
|
|
|
* @since 5.5.2 |
107
|
|
|
* @access protected |
108
|
|
|
* |
109
|
|
|
* @param array $delete_options Delete Options. |
110
|
|
|
* @param object $user User object that needs to be deleted. |
111
|
|
|
* |
112
|
|
|
* @return bool True if the user can be deleted, false otherwise. |
113
|
|
|
*/ |
114
|
|
|
protected function can_delete_by_logged_date( $delete_options, $user ) { |
115
|
|
|
if ( $delete_options['login_restrict'] ) { |
116
|
|
|
$login_days = $delete_options['login_days']; |
117
|
|
|
$last_login = bd_get_last_login( $user->ID ); |
118
|
|
|
|
119
|
|
|
if ( null !== $last_login ) { |
120
|
|
|
// we have a logged-in entry for the user in simple login log plugin. |
121
|
|
|
if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
} else { |
125
|
|
|
// we don't have a logged-in entry for the user in simple login log plugin. |
126
|
|
|
if ( $login_days > 0 ) { |
127
|
|
|
// non-zero value for login date. So don't delete this user. |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Process user delete form. |
138
|
|
|
* |
139
|
|
|
* Helper function to handle common delete user fields. |
140
|
|
|
* Nonce verification is done in the hook that calls this function. |
141
|
|
|
* phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification |
142
|
|
|
* |
143
|
|
|
* @since 5.5.3 |
144
|
|
|
* @access protected |
145
|
|
|
* |
146
|
|
|
* @param array $delete_options Delete Options. |
147
|
|
|
*/ |
148
|
|
|
protected function process_user_delete( $delete_options ) { |
149
|
|
|
$delete_options['login_restrict'] = array_get_bool( $_POST, "smbd_{$this->field_slug}_login_restrict", false ); |
150
|
|
|
$delete_options['login_days'] = absint( array_get( $_POST, "smbd_{$this->field_slug}_login_days", 0 ) ); |
151
|
|
|
|
152
|
|
|
$delete_options['registered_restrict'] = array_get_bool( $_POST, "smbd_{$this->field_slug}_registered_restrict", false ); |
153
|
|
|
$delete_options['registered_days'] = absint( array_get( $_POST, "smbd_{$this->field_slug}_registered_days", 0 ) ); |
154
|
|
|
|
155
|
|
|
$delete_options['no_posts'] = array_get_bool( $_POST, "smbd_{$this->field_slug}_no_posts", false ); |
156
|
|
|
$delete_options['no_posts_post_types'] = bd_array_get( $_POST, "smbd_{$this->field_slug}_no_post_post_types", array() ); |
157
|
|
|
|
158
|
|
|
$delete_options['limit_to'] = absint( array_get( $_POST, "smbd_{$this->field_slug}_limit_to", 0 ) ); |
159
|
|
|
|
160
|
|
|
$this->process_delete( $delete_options ); |
161
|
|
|
} // phpcs:disable |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Render User Login restrict settings. |
165
|
|
|
* |
166
|
|
|
* @since 5.5 |
167
|
|
|
*/ |
168
|
|
|
protected function render_user_login_restrict_settings() { |
169
|
|
|
?> |
170
|
|
|
<tr> |
171
|
|
|
<td scope="row" colspan="2"> |
172
|
|
|
<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"> |
173
|
|
|
<?php _e( 'Restrict to users who are registered in the site for at least ', 'bulk-delete' );?> |
174
|
|
|
<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' );?> |
175
|
|
|
</td> |
176
|
|
|
</tr> |
177
|
|
|
|
178
|
|
|
<?php |
179
|
|
|
if ( bd_is_simple_login_log_present() ) { |
180
|
|
|
$disabled = ''; |
181
|
|
|
} else { |
182
|
|
|
$disabled = 'disabled'; |
183
|
|
|
} |
184
|
|
|
?> |
185
|
|
|
<tr> |
186
|
|
|
<td scope="row" colspan="2"> |
187
|
|
|
<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; ?>> |
188
|
|
|
<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' );?> |
189
|
|
|
<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' );?>. |
190
|
|
|
<?php if ( 'disabled' == $disabled ) { ?> |
191
|
|
|
<span style = "color:red"> |
192
|
|
|
<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "http://wordpress.org/plugins/simple-login-log/">Install now</a> |
193
|
|
|
</span> |
194
|
|
|
<?php } ?> |
195
|
|
|
</td> |
196
|
|
|
</tr> |
197
|
|
|
|
198
|
|
|
<?php if ( bd_is_simple_login_log_present() ) : ?> |
199
|
|
|
<tr> |
200
|
|
|
<td scope="row" colspan="2"> |
201
|
|
|
<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?> |
202
|
|
|
</tr> |
203
|
|
|
<?php endif; ?> |
204
|
|
|
<?php |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Render delete user with no posts settings. |
209
|
|
|
* |
210
|
|
|
* @since 5.5 |
211
|
|
|
*/ |
212
|
|
|
protected function render_user_with_no_posts_settings() { |
213
|
|
|
?> |
214
|
|
|
<tr> |
215
|
|
|
<td scope="row" colspan="2"> |
216
|
|
|
<input type="checkbox" value="true" |
217
|
|
|
name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" |
218
|
|
|
id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" class="user_restrict_to_no_posts_filter"> |
219
|
|
|
|
220
|
|
|
<?php _e( "Restrict to users who don't have any posts.", 'bulk-delete' ); ?> |
221
|
|
|
</td> |
222
|
|
|
</tr> |
223
|
|
|
|
224
|
|
|
<tr class="user_restrict_to_no_posts_filter_items visually-hidden"> |
225
|
|
|
<td scope="row" colspan="2"> |
226
|
|
|
<table class="filter-items"> |
227
|
|
|
<tr> |
228
|
|
|
<td scope="row"> |
229
|
|
|
<?php _e( 'Select the post types. By default all post types are considered.', 'bulk-delete' ); ?> |
230
|
|
|
</td> |
231
|
|
|
</tr> |
232
|
|
|
|
233
|
|
|
<?php $this->render_post_type_checkboxes( "smbd_{$this->field_slug}_no_post_post_types" ); ?> |
234
|
|
|
</table> |
235
|
|
|
</td> |
236
|
|
|
</tr> |
237
|
|
|
|
238
|
|
|
<?php |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Render Post Types as checkboxes. |
243
|
|
|
* |
244
|
|
|
* @since 5.6.0 |
245
|
|
|
* |
246
|
|
|
* @param $name Name of post type checkboxes. |
247
|
|
|
*/ |
248
|
|
|
protected function render_post_type_checkboxes( $name ) { |
249
|
|
|
$post_types = bd_get_post_type_objects(); |
250
|
|
|
?> |
251
|
|
|
|
252
|
|
|
<?php foreach( $post_types as $post_type ) : ?> |
253
|
|
|
|
254
|
|
|
<tr> |
255
|
|
|
<td scope="row"> |
256
|
|
|
<input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $post_type->name ); ?>" |
257
|
|
|
id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>" checked> |
258
|
|
|
|
259
|
|
|
<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>"> |
260
|
|
|
<?php echo esc_html( $post_type->label ); ?> |
261
|
|
|
</label> |
262
|
|
|
</td> |
263
|
|
|
</tr> |
264
|
|
|
|
265
|
|
|
<?php endforeach; ?> |
266
|
|
|
<?php |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|