Completed
Push — 312-fix/delete-post-meta-modul... ( fbbe37...602413 )
by Sudar
14:44 queued 11:16
created

UsersModule::delete_users_from_query()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0957

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 12
nop 2
dl 0
loc 28
ccs 14
cts 16
cp 0.875
crap 7.0957
rs 6.7272
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
	/**
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() );
0 ignored issues
show
Bug introduced by
array() of type array is incompatible with the type string expected by parameter $default of bd_array_get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
		$options['no_posts_post_types'] = bd_array_get( $request, "smbd_{$this->field_slug}_no_post_post_types", /** @scrutinizer ignore-type */ array() );
Loading history...
49
50
		$options['limit_to'] = absint( bd_array_get( $request, "smbd_{$this->field_slug}_limit_to", 0 ) );
51
52
		return $options;
53
	}
54
55 8
	protected function do_delete( $options ) {
56 8
		$query = $this->build_query( $options );
57
58 8
		if ( empty( $query ) ) {
59
			// Short circuit deletion, if nothing needs to be deleted.
60
			return 0;
61
		}
62
63 8
		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 8
	protected function delete_users_from_query( $query, $options ) {
78 8
		$count = 0;
79 8
		$users = get_users( $query );
80
81 8
		if ( ! function_exists( 'wp_delete_user' ) ) {
82
			require_once ABSPATH . 'wp-admin/includes/user.php';
83
		}
84
85 8
		foreach ( $users as $user ) {
86 8
			if ( ! $this->can_delete_by_registered_date( $options, $user ) ) {
87 2
				continue;
88
			}
89
90 6
			if ( ! $this->can_delete_by_logged_date( $options, $user ) ) {
91
				continue;
92
			}
93
94 6
			if ( ! $this->can_delete_by_post_count( $options, $user ) ) {
95 2
				continue;
96
			}
97
98 6
			$deleted = wp_delete_user( $user->ID );
99 6
			if ( $deleted ) {
100 6
				$count ++;
101
			}
102
		}
103
104 8
		return $count;
105
	}
106
107
	/**
108
	 * Can the user be deleted based on the 'post count' option?
109
	 *
110
	 * @since  5.5.2
111
	 * @access protected
112
	 *
113
	 * @param array  $delete_options Delete Options.
114
	 * @param object $user           User object that needs to be deleted.
115
	 *
116
	 * @return bool True if the user can be deleted, false otherwise.
117
	 */
118 6
	protected function can_delete_by_post_count( $delete_options, $user ) {
119
		return ! (
120 6
			$delete_options['no_posts'] &&
121 6
			count_user_posts( $user->ID, $delete_options['no_posts_post_types'] ) > 0
122
		);
123
	}
124
125
	/**
126
	 * Can the user be deleted based on the 'registered date' option?
127
	 *
128
	 * @since  5.5.3
129
	 * @access protected
130
	 *
131
	 * @param array  $delete_options Delete Options.
132
	 * @param object $user           User object that needs to be deleted.
133
	 *
134
	 * @return bool True if the user can be deleted, false otherwise.
135
	 */
136 8
	protected function can_delete_by_registered_date( $delete_options, $user ) {
137 8
		if ( $delete_options['registered_restrict'] ) {
138 4
			$registered_days = $delete_options['registered_days'];
139
140 4
			if ( $registered_days > 0 ) {
141 4
				$user_meta = get_userdata( $user->ID );
142 4
				if ( strtotime( $user_meta->user_registered ) > strtotime( '-' . $registered_days . 'days' ) ) {
143 2
					return false;
144
				}
145
			}
146
		}
147
148 6
		return true;
149
	}
150
151
	/**
152
	 * Can the user be deleted based on the 'logged in date' option?
153
	 *
154
	 * @since  5.5.2
155
	 * @access protected
156
	 *
157
	 * @param array  $delete_options Delete Options.
158
	 * @param object $user           User object that needs to be deleted.
159
	 *
160
	 * @return bool True if the user can be deleted, false otherwise.
161
	 */
162 6
	protected function can_delete_by_logged_date( $delete_options, $user ) {
163 6
		if ( $delete_options['login_restrict'] ) {
164
			$login_days = $delete_options['login_days'];
165
			$last_login = bd_get_last_login( $user->ID );
166
167
			if ( null !== $last_login ) {
0 ignored issues
show
introduced by
The condition null !== $last_login is always true.
Loading history...
168
				// we have a logged-in entry for the user in simple login log plugin.
169
				if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) {
170
					return false;
171
				}
172
			} else {
173
				// we don't have a logged-in entry for the user in simple login log plugin.
174
				if ( $login_days > 0 ) {
175
					// non-zero value for login date. So don't delete this user.
176
					return false;
177
				}
178
			}
179
		}
180
181 6
		return true;
182
	}
183
184
	/**
185
	 * Render User Login restrict settings.
186
	 *
187
	 * @since 5.5
188
	 */
189
	protected function render_user_login_restrict_settings() {
190
?>
191
		<tr>
192
			<td scope="row" colspan="2">
193
			<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">
194
				<?php _e( 'Restrict to users who are registered in the site for at least ', 'bulk-delete' );?>
195
				<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' );?>
196
			</td>
197
		</tr>
198
199
		<?php
200
		if ( bd_is_simple_login_log_present() ) {
201
			$disabled = '';
202
		} else {
203
			$disabled = 'disabled';
204
		}
205
?>
206
		<tr>
207
			<td scope="row" colspan="2">
208
			<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; ?>>
209
				<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' );?>
210
				<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' );?>.
211
		<?php if ( 'disabled' == $disabled ) { ?>
212
				<span style = "color:red">
213
					<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "http://wordpress.org/plugins/simple-login-log/">Install now</a>
214
				</span>
215
		<?php } ?>
216
			</td>
217
		</tr>
218
219
		<?php if ( bd_is_simple_login_log_present() ) : ?>
220
			<tr>
221
				<td scope="row" colspan="2">
222
					<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?>
223
			</tr>
224
		<?php endif; ?>
225
<?php
226
	}
227
228
	/**
229
	 * Render delete user with no posts settings.
230
	 *
231
	 * @since 5.5
232
	 */
233
	protected function render_user_with_no_posts_settings() {
234
	?>
235
		<tr>
236
			<td scope="row" colspan="2">
237
				<input type="checkbox" value="true"
238
				       name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts"
239
				       id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" class="user_restrict_to_no_posts_filter">
240
241
				<?php _e( "Restrict to users who don't have any posts.", 'bulk-delete' ); ?>
242
			</td>
243
		</tr>
244
245
		<tr class="user_restrict_to_no_posts_filter_items visually-hidden">
246
			<td scope="row" colspan="2">
247
				<table class="filter-items">
248
					<tr>
249
						<td scope="row">
250
							<?php _e( 'Select the post types. By default all post types are considered.', 'bulk-delete' ); ?>
251
						</td>
252
					</tr>
253
254
					<?php $this->render_post_type_checkboxes( "smbd_{$this->field_slug}_no_post_post_types" ); ?>
255
				</table>
256
			</td>
257
		</tr>
258
259
	<?php
260
	}
261
262
	/**
263
	 * Render Post Types as checkboxes.
264
	 *
265
	 * @since 5.6.0
266
	 *
267
	 * @param $name Name of post type checkboxes.
0 ignored issues
show
Bug introduced by
The type BulkWP\BulkDelete\Core\Users\Name was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
268
	 */
269
	protected function render_post_type_checkboxes( $name ) {
270
		$post_types = bd_get_post_types();
271
		?>
272
273
		<?php foreach( $post_types as $post_type ) : ?>
274
275
		<tr>
276
			<td scope="row">
277
				<input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $post_type->name ); ?>"
278
					id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>" checked>
279
280
				<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
281
					<?php echo esc_html( $post_type->label ); ?>
282
				</label>
283
			</td>
284
		</tr>
285
286
		<?php endforeach; ?>
287
		<?php
288
	}
289
}
290