Passed
Push — dev/6.0.0 ( f2d62a...b411c2 )
by Sudar
02:48
created

UsersModule::exclude_users_from_deletion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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