Completed
Push — 552-feature/reassign-contnet-o... ( 13c2d9...9c959c )
by
unknown
13:28 queued 13:24
created

UsersModule::delete_users_from_query()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.2964

Importance

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