Passed
Pull Request — dev/6.0.0 (#513)
by Sudar
694:23 queued 683:25
created

UsersModule::delete_users_from_query()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.1666

Importance

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