Completed
Push — 496-fix/add-javascript-validat... ( 498e5a...c7d4d8 )
by Sudar
13:26 queued 13:23
created

UsersModule   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Test Coverage

Coverage 53.75%

Importance

Changes 0
Metric Value
eloc 120
dl 0
loc 284
ccs 43
cts 80
cp 0.5375
rs 10
c 0
b 0
f 0
wmc 28

9 Methods

Rating   Name   Duplication   Size   Complexity  
A parse_common_filters() 0 15 1
B delete_users_from_query() 0 28 7
A do_delete() 0 9 2
A render_user_login_restrict_settings() 0 35 4
A can_delete_by_post_count() 0 4 2
A can_delete_by_registered_date() 0 13 4
A render_user_with_no_posts_settings() 0 23 1
A can_delete_by_logged_date() 0 20 5
A query_users() 0 36 2
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
	/**
32
	 * Handle common filters.
33
	 *
34
	 * @param array $request Request array.
35
	 *
36
	 * @return array User options.
37
	 */
38
	protected function parse_common_filters( $request ) {
39
		$options = array();
40
41
		$options['login_restrict'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_login_restrict", false );
42
		$options['login_days']     = absint( bd_array_get( $request, "smbd_{$this->field_slug}_login_days", 0 ) );
43
44
		$options['registered_restrict'] = bd_array_get_bool( $request, "smbd_{$this->field_slug}_registered_restrict", false );
45
		$options['registered_days']     = absint( bd_array_get( $request, "smbd_{$this->field_slug}_registered_days", 0 ) );
46
47
		$options['no_posts']            = bd_array_get_bool( $request, "smbd_{$this->field_slug}_no_posts", false );
48
		$options['no_posts_post_types'] = bd_array_get( $request, "smbd_{$this->field_slug}_no_post_post_types", array() );
49
50
		$options['limit_to'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_limit_to", 0 ) );
51
52
		return $options;
53
	}
54
55 44
	protected function do_delete( $options ) {
56 44
		$query = $this->build_query( $options );
57
58 44
		if ( empty( $query ) ) {
59
			// Short circuit deletion, if nothing needs to be deleted.
60
			return 0;
61
		}
62
63 44
		return $this->delete_users_from_query( $query, $options );
64
	}
65
66
	/**
67
	 * Query and Delete users.
68
	 *
69
	 * @since  5.5.2
70
	 * @access protected
71
	 *
72
	 * @param array $query   Options to query users.
73
	 * @param array $options Delete options.
74
	 *
75
	 * @return int Number of users who were deleted.
76
	 */
77 44
	protected function delete_users_from_query( $query, $options ) {
78 44
		$count = 0;
79 44
		$users = $this->query_users( $query );
80
81 44
		if ( ! function_exists( 'wp_delete_user' ) ) {
82
			require_once ABSPATH . 'wp-admin/includes/user.php';
83
		}
84
85 44
		foreach ( $users as $user ) {
86 44
			if ( ! $this->can_delete_by_registered_date( $options, $user ) ) {
87 10
				continue;
88
			}
89
90 40
			if ( ! $this->can_delete_by_logged_date( $options, $user ) ) {
91
				continue;
92
			}
93
94 40
			if ( ! $this->can_delete_by_post_count( $options, $user ) ) {
95 9
				continue;
96
			}
97
98 39
			$deleted = wp_delete_user( $user->ID );
99 39
			if ( $deleted ) {
100 39
				$count ++;
101
			}
102
		}
103
104 44
		return $count;
105
	}
106
107
	/**
108
	 * Query users using options.
109
	 *
110
	 * @param array $options Query options.
111
	 *
112
	 * @return \WP_User[] List of users.
113
	 */
114 44
	protected function query_users( $options ) {
115
		$defaults = array(
116 44
			'count_total' => false,
117
		);
118
119
		// Exclude current user.
120 44
		$current_user = wp_get_current_user();
121 44
		if ( $current_user instanceof \WP_User ) {
0 ignored issues
show
introduced by
$current_user is always a sub-type of WP_User.
Loading history...
122 44
			$options['exclude'] = array( $current_user->ID );
123
		}
124
125 44
		$options = wp_parse_args( $options, $defaults );
126
127 44
		$wp_user_query = new \WP_User_Query( $options );
128
129
		/**
130
		 * This action before the query happens.
131
		 *
132
		 * @since 6.0.0
133
		 *
134
		 * @param \WP_User_Query $wp_user_query Query object.
135
		 */
136 44
		do_action( 'bd_before_query', $wp_user_query );
137
138 44
		$users = (array) $wp_user_query->get_results();
139
140
		/**
141
		 * This action runs after the query happens.
142
		 *
143
		 * @since 6.0.0
144
		 *
145
		 * @param \WP_User_Query $wp_user_query Query object.
146
		 */
147 44
		do_action( 'bd_after_query', $wp_user_query );
148
149 44
		return $users;
150
	}
151
152
	/**
153
	 * Can the user be deleted based on the 'post count' option?
154
	 *
155
	 * @since  5.5.2
156
	 * @access protected
157
	 *
158
	 * @param array    $delete_options Delete Options.
159
	 * @param \WP_User $user           User object that needs to be deleted.
160
	 *
161
	 * @return bool True if the user can be deleted, false otherwise.
162
	 */
163 40
	protected function can_delete_by_post_count( $delete_options, $user ) {
164
		return ! (
165 40
			$delete_options['no_posts'] &&
166 40
			count_user_posts( $user->ID, $delete_options['no_posts_post_types'] ) > 0
167
		);
168
	}
169
170
	/**
171
	 * Can the user be deleted based on the 'registered date' option?
172
	 *
173
	 * @since  5.5.3
174
	 * @access protected
175
	 *
176
	 * @param array    $delete_options Delete Options.
177
	 * @param \WP_User $user           User object that needs to be deleted.
178
	 *
179
	 * @return bool True if the user can be deleted, false otherwise.
180
	 */
181 44
	protected function can_delete_by_registered_date( $delete_options, $user ) {
182 44
		if ( $delete_options['registered_restrict'] ) {
183 12
			$registered_days = $delete_options['registered_days'];
184
185 12
			if ( $registered_days > 0 ) {
186 11
				$user_meta = get_userdata( $user->ID );
187 11
				if ( strtotime( $user_meta->user_registered ) > strtotime( '-' . $registered_days . 'days' ) ) {
188 10
					return false;
189
				}
190
			}
191
		}
192
193 40
		return true;
194
	}
195
196
	/**
197
	 * Can the user be deleted based on the 'logged in date' option?
198
	 *
199
	 * @since  5.5.2
200
	 * @access protected
201
	 *
202
	 * @param array    $delete_options Delete Options.
203
	 * @param \WP_User $user           User object that needs to be deleted.
204
	 *
205
	 * @return bool True if the user can be deleted, false otherwise.
206
	 */
207 40
	protected function can_delete_by_logged_date( $delete_options, $user ) {
208 40
		if ( $delete_options['login_restrict'] ) {
209
			$login_days = $delete_options['login_days'];
210
			$last_login = bd_get_last_login( $user->ID );
211
212
			if ( null !== $last_login ) {
0 ignored issues
show
introduced by
The condition null !== $last_login is always true.
Loading history...
213
				// we have a logged-in entry for the user in simple login log plugin.
214
				if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) {
215
					return false;
216
				}
217
			} else {
218
				// we don't have a logged-in entry for the user in simple login log plugin.
219
				if ( $login_days > 0 ) {
220
					// non-zero value for login date. So don't delete this user.
221
					return false;
222
				}
223
			}
224
		}
225
226 40
		return true;
227
	}
228
229
	/**
230
	 * Render User Login restrict settings.
231
	 *
232
	 * @since 5.5
233
	 */
234
	protected function render_user_login_restrict_settings() {
235
?>
236
		<tr>
237
			<td scope="row" colspan="2">
238
			<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">
239
				<?php _e( 'Restrict to users who are registered in the site for at least ', 'bulk-delete' );?>
240
				<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' );?>
241
			</td>
242
		</tr>
243
244
		<?php
245
		if ( bd_is_simple_login_log_present() ) {
246
			$disabled = '';
247
		} else {
248
			$disabled = 'disabled';
249
		}
250
?>
251
		<tr>
252
			<td scope="row" colspan="2">
253
			<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; ?>>
254
				<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' );?>
255
				<input type="number" name="smbd_<?php echo $this->field_slug; ?>_login_days" id="smbd_<?php echo $this->field_slug; ?>_login_days" class="screen-per-page" value="0" min="0" disabled> <?php _e( 'days', 'bulk-delete' );?>.
256
		<?php if ( 'disabled' == $disabled ) { ?>
257
				<span style = "color:red">
258
					<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "http://wordpress.org/plugins/simple-login-log/">Install now</a>
259
				</span>
260
		<?php } ?>
261
			</td>
262
		</tr>
263
264
		<?php if ( bd_is_simple_login_log_present() ) : ?>
265
			<tr>
266
				<td scope="row" colspan="2">
267
					<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?>
268
			</tr>
269
		<?php endif; ?>
270
<?php
271
	}
272
273
	/**
274
	 * Render delete user with no posts settings.
275
	 *
276
	 * @since 5.5
277
	 */
278
	protected function render_user_with_no_posts_settings() {
279
	?>
280
		<tr>
281
			<td scope="row" colspan="2">
282
				<input type="checkbox" value="true"
283
				       name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts"
284
				       id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" class="user_restrict_to_no_posts_filter">
285
286
				<?php _e( "Restrict to users who don't have any posts.", 'bulk-delete' ); ?>
287
			</td>
288
		</tr>
289
290
		<tr class="user_restrict_to_no_posts_filter_items visually-hidden">
291
			<td scope="row" colspan="2">
292
				<table class="filter-items">
293
					<tr>
294
						<td scope="row">
295
							<?php _e( 'Select the post types. By default all post types are considered.', 'bulk-delete' ); ?>
296
						</td>
297
					</tr>
298
299
					<?php $this->render_post_type_checkboxes( "smbd_{$this->field_slug}_no_post_post_types" ); ?>
300
				</table>
301
			</td>
302
		</tr>
303
304
	<?php
305
	}
306
}
307