Completed
Pull Request — 330-fix/delete-user-meta-add-a... (#524)
by Sudar
26:21 queued 11:22
created

UsersModule::get_date_query()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 4
cts 4
cp 1
crap 3
rs 10
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
		 */
121 44
		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
		 * @since 6.0.0
129
		 *
130 44
		 * @param \WP_User_Query $wp_user_query Query object.
131
		 */
132 44
		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
	 *
140
	 * This doesn't work well in batches.
141 44
	 *
142
	 * @link https://github.com/sudar/bulk-delete/issues/511 Github issue.
143 44
	 * @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
	 */
151
	protected function can_delete_by_post_count( $delete_options, $user ) {
152
		return ! (
153
			$delete_options['no_posts'] &&
154
			count_user_posts( $user->ID, $delete_options['no_posts_post_types'] ) > 0
155
		);
156
	}
157 40
158
	/**
159 40
	 * Get the date query part for WP_User_Query.
160 40
	 *
161
	 * Date query corresponds to user registered date.
162
	 *
163
	 * @since 6.0.0
164
	 *
165
	 * @param array $options Delete options.
166
	 *
167
	 * @return array Date Query.
168
	 */
169
	protected function get_date_query( $options ) {
170
		if ( ! $options['registered_restrict'] ) {
171
			return array();
172
		}
173
174
		if ( $options['registered_days'] <= 0 ) {
175 44
			return array();
176 44
		}
177 12
178
		return array(
179 12
			'before' => $options['registered_days'] . ' days ago',
180 11
		);
181 11
	}
182 10
183
	/**
184
	 * Can the user be deleted based on the 'logged in date' option?
185
	 *
186
	 * This doesn't work well in batches.
187 40
	 *
188
	 * @link https://github.com/sudar/bulk-delete/issues/511 Github issue.
189
	 * @since  5.5.2
190
	 * @access protected
191
	 *
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
	 */
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 40
202 40
			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
					// non-zero value for login date. So don't delete this user.
211
					return false;
212
				}
213
			}
214
		}
215
216
		return true;
217
	}
218
219
	/**
220 40
	 * 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
					<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 esc_attr( $this->field_slug ); ?>_login_restrict" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_login_restrict"
240
							value="true" type="checkbox" <?php disabled( false, bd_is_simple_login_log_present() ); ?>>
241
					<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' ); ?>
242
				</label>
243
				<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' ); ?>.
244
245
				<?php if ( ! bd_is_simple_login_log_present() ) : ?>
246
					<span style = "color:red">
247
						<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "https://wordpress.org/plugins/simple-login-log/">Install now</a>
248
					</span>
249
				<?php endif; ?>
250
			</td>
251
		</tr>
252
253
		<?php if ( bd_is_simple_login_log_present() ) : ?>
254
			<tr>
255
				<td scope="row" colspan="2">
256
					<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?>
257
			</tr>
258
		<?php endif; ?>
259
<?php
260
	}
261
262
	/**
263
	 * Render delete user with no posts settings.
264
	 *
265
	 * @since 5.5
266
	 */
267
	protected function render_user_with_no_posts_settings() {
268
	?>
269
		<tr>
270
			<td scope="row" colspan="2">
271
				<input type="checkbox" value="true"
272
						name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts"
273
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" class="user_restrict_to_no_posts_filter">
274
				<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts">
275
					<?php _e( "Restrict to users who don't have any posts.", 'bulk-delete' ); ?>
276
				</label>
277
			</td>
278
		</tr>
279
280
		<tr class="user_restrict_to_no_posts_filter_items visually-hidden">
281
			<td scope="row" colspan="2">
282
				<table class="filter-items">
283
					<tr>
284
						<td scope="row">
285
							<?php _e( 'Select the post types. By default all post types are considered.', 'bulk-delete' ); ?>
286
						</td>
287
					</tr>
288
289
					<?php $this->render_post_type_checkboxes( "smbd_{$this->field_slug}_no_post_post_types" ); ?>
290
				</table>
291
			</td>
292
		</tr>
293
294
	<?php
295
	}
296
297
	/**
298
	 * Get unique user meta keys.
299
	 *
300
	 * @since 5.5
301
	 *
302
	 * @return array List of unique meta keys.
303
	 */
304
	protected function get_unique_user_meta_keys() {
305
		global $wpdb;
306
307
		return $wpdb->get_col( "SELECT DISTINCT(meta_key) FROM {$wpdb->prefix}usermeta ORDER BY meta_key" );
308
	}
309
310
	/**
311
	 * Exclude current user from being deleted.
312
	 *
313
	 * @param array $query WP_User_Query args.
314
	 *
315
	 * @return array Modified query args.
316
	 */
317
	protected function exclude_current_user( $query ) {
318
		$current_user_id = get_current_user_id();
319
320
		if ( $current_user_id <= 0 ) {
321
			return $query;
322
		}
323
324
		if ( isset( $query['exclude'] ) ) {
325
			$query['exclude'] = array_merge( $query['exclude'], array( $current_user_id ) );
326
		} else {
327
			$query['exclude'] = array( $current_user_id );
328
		}
329
330
		return $query;
331
	}
332
}
333