Passed
Pull Request — dev/6.1.0 (#709)
by
unknown
38:51 queued 36:04
created

UsersModule::delete_users_from_query()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 50
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 13.7828

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 24
nc 24
nop 2
dl 0
loc 50
ccs 15
cts 18
cp 0.8333
crap 13.7828
rs 6.6166
c 3
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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['reassign_user']    = bd_array_get_bool( $request, "smbd_{$this->field_slug}_post_reassign", false );
45
		$options['reassign_user_id'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_reassign_user_id", 0 ) );
46
		$options['limit_to']         = absint( bd_array_get( $request, "smbd_{$this->field_slug}_limit_to", 0 ) );
47
48
		return $options;
49
	}
50
51 69
	protected function do_delete( $options ) {
52 69
		$query = $this->build_query( $options );
53
54 69
		if ( empty( $query ) ) {
55
			// Short circuit deletion, if nothing needs to be deleted.
56 2
			return 0;
57
		}
58
59 67
		$query = $this->exclude_users_from_deletion( $query );
60 67
		$query = $this->exclude_current_user( $query );
61
62 67
		return $this->delete_users_from_query( $query, $options );
63
	}
64
65
	/**
66
	 * Query and Delete users.
67
	 *
68
	 * @since  5.5.2
69
	 * @access protected
70
	 *
71
	 * @param array $query   Options to query users.
72
	 * @param array $options Delete options.
73
	 *
74
	 * @return int Number of users who were deleted.
75
	 */
76 73
	protected function delete_users_from_query( $query, $options ) {
77 73
		$count = 0;
78 73
		$users = $this->query_users( $query );
79
80 73
		if ( ! function_exists( 'wp_delete_user' ) ) {
81
			require_once ABSPATH . 'wp-admin/includes/user.php';
82
		}
83
84 73
		foreach ( $users as $user ) {
85 66
			if ( ! $this->can_delete_by_logged_date( $options, $user ) ) {
86
				continue;
87
			}
88
89 66
			if ( ! $this->can_delete_by_post_count( $options, $user ) ) {
90 13
				continue;
91
			}
92
93
			/**
94
			 * Can a user be deleted.
95
			 *
96
			 * @since 6.0.0
97
			 *
98
			 * @param bool                                             Can Delete the User. (Default true)
99
			 * @param \WP_User                                $user    User Object of the user who is about to be deleted.
100
			 * @param array                                   $options Delete options.
101
			 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $this    Module that is triggering deletion.
102
			 */
103 64
			if ( ! apply_filters( 'bd_can_delete_user', true, $user, $options, $this ) ) {
104
				continue;
105
			}
106
107 64
			switch ( isset( $options['network_admin'] ) && $options['network_admin'] && is_multisite() ) {
108 1
				case true:
109
					$deleted = wpmu_delete_user( $user->ID );
110 63
					break;
111
				case false:
112
					if ( isset( $options['reassign_user'] ) && $options['reassign_user'] ) {
113 64
						$deleted = wp_delete_user( $user->ID, $options['reassign_user_id'] );
114 64
					} else {
115
						$deleted = wp_delete_user( $user->ID );
116
					}
117
					break;
118 73
			}
119
120
			if ( $deleted ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $deleted does not seem to be defined for all execution paths leading up to this point.
Loading history...
121
				$count ++;
122
			}
123
		}
124
125
		return $count;
126
	}
127
128 73
	/**
129
	 * Query users using options.
130 73
	 *
131
	 * @param array $options Query options.
132
	 *
133 73
	 * @return \WP_User[] List of users.
134
	 */
135 73
	protected function query_users( $options ) {
136
		$defaults = array(
137
			'count_total' => false,
138
		);
139
140
		$options = wp_parse_args( $options, $defaults );
141
142
		$wp_user_query = new \WP_User_Query( $options );
143
144 73
		/**
145
		 * This action before the query happens.
146 73
		 *
147
		 * @since 6.0.0
148
		 *
149
		 * @param \WP_User_Query $wp_user_query Query object.
150
		 */
151
		do_action( 'bd_before_query', $wp_user_query );
152
153
		$users = (array) $wp_user_query->get_results();
154
155 73
		/**
156
		 * This action runs after the query happens.
157 73
		 *
158
		 * @since 6.0.0
159
		 *
160
		 * @param \WP_User_Query $wp_user_query Query object.
161
		 */
162
		do_action( 'bd_after_query', $wp_user_query );
163
164
		return $users;
165
	}
166
167
	/**
168
	 * Can the user be deleted based on the 'post count' option?
169
	 *
170
	 * This doesn't work well in batches.
171
	 *
172
	 * @link https://github.com/sudar/bulk-delete/issues/511 Github issue.
173
	 * @since  5.5.2
174 66
	 * @access protected
175
	 *
176 66
	 * @param array    $delete_options Delete Options.
177 66
	 * @param \WP_User $user           User object that needs to be deleted.
178
	 *
179
	 * @return bool True if the user can be deleted, false otherwise.
180
	 */
181
	protected function can_delete_by_post_count( $delete_options, $user ) {
182
		return ! (
183
			$delete_options['no_posts'] &&
184
			count_user_posts( $user->ID, $delete_options['no_posts_post_types'] ) > 0
185
		);
186
	}
187
188
	/**
189
	 * Get the date query part for WP_User_Query.
190
	 *
191
	 * Date query corresponds to user registered date.
192 70
	 *
193 70
	 * @since 6.0.0
194 41
	 *
195
	 * @param array $options Delete options.
196
	 *
197 29
	 * @return array Date Query.
198 1
	 */
199
	protected function get_date_query( $options ) {
200
		if ( ! $options['registered_restrict'] ) {
201 28
			return array();
202
		}
203 24
204
		if ( $options['registered_days'] <= 0 ) {
205
			return array();
206
		}
207 4
208
		if ( ! isset( $options['registered_date_op'] ) ) {
209 4
			return array(
210
				'before' => $options['registered_days'] . ' days ago',
211
			);
212
		}
213
214
		if ( 'before' === $options['registered_date_op'] || 'after' === $options['registered_date_op'] ) {
215
			return array(
216
				$options['registered_date_op'] => $options['registered_days'] . ' days ago',
217
			);
218
		}
219
220
		return array();
221
	}
222
223
	/**
224
	 * Can the user be deleted based on the 'logged in date' option?
225
	 *
226
	 * This doesn't work well in batches.
227
	 *
228
	 * @link https://github.com/sudar/bulk-delete/issues/511 Github issue.
229
	 * @since  5.5.2
230 66
	 * @access protected
231 66
	 *
232
	 * @param array    $delete_options Delete Options.
233
	 * @param \WP_User $user           User object that needs to be deleted.
234
	 *
235
	 * @return bool True if the user can be deleted, false otherwise.
236
	 */
237
	protected function can_delete_by_logged_date( $delete_options, $user ) {
238
		if ( $delete_options['login_restrict'] ) {
239
			$login_days = $delete_options['login_days'];
240
			$last_login = bd_get_last_login( $user->ID );
241
242
			if ( null !== $last_login ) {
0 ignored issues
show
introduced by
The condition null !== $last_login is always true.
Loading history...
243
				// we have a logged-in entry for the user in simple login log plugin.
244
				if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) {
245
					return false;
246
				}
247
			} else {
248
				// we don't have a logged-in entry for the user in simple login log plugin.
249 66
				if ( $login_days > 0 ) {
250
					// non-zero value for login date. So don't delete this user.
251
					return false;
252
				}
253
			}
254
		}
255
256
		return true;
257
	}
258
259
	/**
260
	 * Render User Login restrict settings.
261
	 *
262
	 * @since 5.5
263
	 * @since 6.2.0 Added $show_login_filter param.
264
	 *
265
	 * @param bool $show_login_filter True/False.
266
	 */
267
	protected function render_user_login_restrict_settings( $show_login_filter = true ) {
268
		?>
269
		<tr>
270
			<td scope="row" colspan="2">
271
				<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_registered_restrict">
272
					<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">
273
					<?php _e( 'Restrict to users who are registered in the site ', 'bulk-delete' ); ?>
274
				</label>
275
				<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_op" disabled>
276
					<option value="before"><?php _e( 'for at least', 'bulk-delete' ); ?></option>
277
					<option value="after"><?php _e( 'in the last', 'bulk-delete' ); ?></option>
278
				</select>
279
				<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' ); ?>
280
			</td>
281
		</tr>
282
		<?php if ( $show_login_filter ) : ?>
283
		<tr>
284
			<td scope="row" colspan="2">
285
				<label>
286
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_login_restrict" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_login_restrict"
287
							value="true" type="checkbox" <?php disabled( false, bd_is_simple_login_log_present() ); ?>>
288
					<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' ); ?>
289
				</label>
290
				<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' ); ?>.
291
292
				<?php if ( ! bd_is_simple_login_log_present() ) : ?>
293
					<span style = "color:red">
294
						<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "https://wordpress.org/plugins/simple-login-log/">Install now</a>
295
					</span>
296
				<?php endif; ?>
297
			</td>
298
		</tr>
299
300
			<?php if ( bd_is_simple_login_log_present() ) : ?>
301
			<tr>
302
				<td scope="row" colspan="2">
303
					<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?>
304
			</tr>
305
			<?php endif; ?>
306
		<?php endif; ?>
307
		<?php
308
	}
309
310
	/**
311
	 * Render delete user with no posts settings.
312
	 *
313
	 * @since 5.5
314
	 */
315
	protected function render_user_with_no_posts_settings() {
316
	?>
317
		<tr>
318
			<td scope="row" colspan="2">
319
				<input type="checkbox" value="true"
320
						name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts"
321
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" class="user_restrict_to_no_posts_filter">
322
				<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts">
323
					<?php _e( "Restrict to users who don't have any posts.", 'bulk-delete' ); ?>
324
				</label>
325
			</td>
326
		</tr>
327
328
		<tr class="user_restrict_to_no_posts_filter_items visually-hidden">
329
			<td scope="row" colspan="2">
330
				<table class="filter-items">
331
					<tr>
332
						<td scope="row">
333
							<?php _e( 'Select the post types. By default all post types are considered.', 'bulk-delete' ); ?>
334
						</td>
335
					</tr>
336
337
					<?php $this->render_post_type_checkboxes( "smbd_{$this->field_slug}_no_post_post_types" ); ?>
338
				</table>
339
			</td>
340
		</tr>
341
342
	<?php
343
	}
344
345
	/**
346
	 * Get unique user meta keys.
347
	 *
348
	 * @since 5.5
349
	 *
350
	 * @return array List of unique meta keys.
351
	 */
352
	protected function get_unique_user_meta_keys() {
353
		global $wpdb;
354
355
		return $wpdb->get_col( "SELECT DISTINCT(meta_key) FROM {$wpdb->prefix}usermeta ORDER BY meta_key" );
356
	}
357
358 68
	/**
359 68
	 * Exclude current user from being deleted.
360
	 *
361 68
	 * @param array $query WP_User_Query args.
362 66
	 *
363
	 * @return array Modified query args.
364
	 */
365 2
	protected function exclude_current_user( $query ) {
366
		$current_user_id = get_current_user_id();
367
368 2
		if ( $current_user_id <= 0 ) {
369
			return $query;
370
		}
371 2
372
		if ( isset( $query['exclude'] ) ) {
373
			$query['exclude'] = array_merge( $query['exclude'], array( $current_user_id ) );
374
		} else {
375
			$query['exclude'] = array( $current_user_id );
376 2
		}
377
378
		if ( isset( $query['include'] ) ) {
379
			$query['include'] = array_diff( $query['include'], $query['exclude'] );
380
			unset( $query['exclude'] );
381
		}
382
383
		return $query;
384
	}
385
386
	/**
387
	 * Exclude users from deletion.
388 68
	 *
389
	 * @since 6.0.0
390
	 *
391
	 * @param array $query Pre-built query.
392
	 *
393
	 * @return array Modified query.
394
	 */
395
	protected function exclude_users_from_deletion( array $query ) {
396 68
		/**
397
		 * Filter the list of user ids that will be excluded from deletion.
398 68
		 *
399
		 * @since 6.0.0
400
		 *
401
		 * @param array $excluded_ids User IDs to be excluded during deletion.
402
		 */
403
		$excluded_user_ids = apply_filters( 'bd_excluded_user_ids', array() );
404
405
		if ( is_array( $excluded_user_ids ) && ! empty( $excluded_user_ids ) ) {
406 68
			if ( isset( $query['exclude'] ) ) {
407
				$query['exclude'] = array_merge( $query['exclude'], $excluded_user_ids );
408
			} else {
409
				$query['exclude'] = $excluded_user_ids;
410
			}
411 68
		}
412
413
		if ( isset( $query['include'] ) ) {
414
			$query['include'] = array_diff( $query['include'], $query['exclude'] );
415
			unset( $query['exclude'] );
416
		}
417
418
		return $query;
419
	}
420
421
	/**
422
	 * Get Users ids from User logins.
423
	 *
424
	 * @param array $user_logins User Logins.
425
	 *
426
	 * @return array User ids.
427
	 */
428
	protected function get_user_ids_from_logins( $user_logins ) {
429
		$args = array(
430
			'fields'    => 'ID',
431
			'login__in' => $user_logins,
432
		);
433
434
		return get_users( $args );
435
	}
436
437
	/**
438
	 * Get Users ids from User emails.
439
	 *
440
	 * This is done in a batch of 500 since it involves a IN query.
441
	 *
442
	 * @param array $user_emails User emails.
443
	 *
444
	 * @return array User ids.
445
	 */
446
	protected function get_user_ids_from_emails( $user_emails ) {
447
		global $wpdb;
448
449
		$user_email_chunks = array_chunk( $user_emails, 500 );
450
451
		$user_ids = array();
452
453
		foreach ( $user_email_chunks as $user_email_chunk ) {
454
			$placeholders = array_fill( 0, count( $user_email_chunk ), '%s' );
455
			$format       = implode( ',', $placeholders );
456
457
			$query      = "SELECT ID FROM $wpdb->users WHERE user_email in ($format)";
458
			$user_ids[] = $wpdb->get_col( $wpdb->prepare( $query, $user_email_chunk ) );
459
		}
460
461
		return $user_ids;
462
	}
463
}
464