Completed
Push — 552-feature/reassign-contnet-o... ( ff18e1 )
by
unknown
05:02
created

UsersModule::get_date_query()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

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