Completed
Pull Request — dev/6.0.0 (#557)
by Sudar
05:40 queued 03:08
created

UsersModule::delete_users_from_query()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8.3844

Importance

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