Completed
Pull Request — master (#142)
by Sudar
03:06 queued 01:33
created

BD_User_Meta_Box_Module::delete_users()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Base class for a Bulk Delete User Meta Box Module.
4
 *
5
 * @since 5.5.2
6
 *
7
 * @author Sudar
8
 *
9
 * @package BulkDelete\Base\Users
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
/**
14
 * Encapsulates the Bulk Delete User Meta box Module Logic.
15
 * All Bulk Delete User Meta box Modules should extend this class.
16
 *
17
 * @see BD_Meta_Box_Module
18
 * @since 5.5.2
19
 * @abstract
20
 */
21
abstract class BD_User_Meta_Box_Module extends BD_Meta_Box_Module {
22
	/**
23
	 * Query and Delete users.
24
	 *
25
	 * @since  5.5.2
26
	 * @access protected
27
	 *
28
	 * @param array $options        Options to query users.
29
	 * @param array $delete_options Delete options.
30
	 *
31
	 * @return int Number of users who were deleted.
32
	 */
33
	protected function delete_users( $options, $delete_options ) {
34
		$count = 0;
35
		$users = get_users( $options );
36
37
		foreach ( $users as $user ) {
38
			if ( ! $this->can_delete_by_registered_date( $delete_options, $user ) ) {
39
				continue;
40
			}
41
42
			if ( ! $this->can_delete_by_logged_date( $delete_options, $user ) ) {
43
				continue;
44
			}
45
46
			if ( ! $this->can_delete_by_post_count( $delete_options, $user ) ) {
47
				continue;
48
			}
49
50
			$deleted = wp_delete_user( $user->ID );
51
			if ( $deleted ) {
52
				$count ++;
53
			}
54
		}
55
56
		return $count;
57
	}
58
59
	/**
60
	 * Can the user be deleted based on the 'post count' option?
61
	 *
62
	 * @since  5.5.2
63
	 * @access protected
64
	 *
65
	 * @param array  $delete_options Delete Options.
66
	 * @param object $user           User objet that needs to be deleted.
67
	 *
68
	 * @return bool True if the user can be deleted, false otherwise.
69
	 */
70
	protected function can_delete_by_post_count( $delete_options, $user ) {
71
		if ( $delete_options['no_posts'] &&
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($delete_options...ons['post_type']) > 0);.
Loading history...
72
			 array_key_exists( 'post_type', $delete_options ) &&
73
			 count_user_posts( $user->ID, $delete_options['post_type'] ) > 0 ) {
74
			return false;
75
		}
76
77
		return true;
78
	}
79
80
	/**
81
	 * Can the user be deleted based on the 'registered date' option?
82
	 *
83
	 * @since  5.5.3
84
	 * @access protected
85
	 *
86
	 * @param array  $delete_options Delete Options.
87
	 * @param object $user           User object that needs to be deleted.
88
	 *
89
	 * @return bool True if the user can be deleted, false otherwise.
90
	 */
91
	protected function can_delete_by_registered_date( $delete_options, $user ) {
92
		if ( $delete_options['registered_restrict'] ) {
93
			$registered_days = $delete_options['registered_days'];
94
95
			if ( $registered_days > 0 ) {
96
				$user_meta = get_userdata( $user->ID );
97
				if ( strtotime( $user_meta->user_registered ) > strtotime( '-' . $registered_days . 'days' ) ) {
98
					return false;
99
				}
100
			}
101
		}
102
103
		return true;
104
	}
105
106
	/**
107
	 * Can the user be deleted based on the 'logged in date' option?
108
	 *
109
	 * @since  5.5.2
110
	 * @access protected
111
	 *
112
	 * @param array  $delete_options Delete Options.
113
	 * @param object $user           User object that needs to be deleted.
114
	 *
115
	 * @return bool True if the user can be deleted, false otherwise.
116
	 */
117
	protected function can_delete_by_logged_date( $delete_options, $user ) {
118
		if ( $delete_options['login_restrict'] ) {
119
			$login_days = $delete_options['login_days'];
120
			$last_login = bd_get_last_login( $user->ID );
121
122
			if ( null !== $last_login ) {
123
				// we have a logged-in entry for the user in simple login log plugin.
124
				if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) {
125
					return false;
126
				}
127
			} else {
128
				// we don't have a logged-in entry for the user in simple login log plugin.
129
				if ( $login_days > 0 ) {
130
					// non-zero value for login date. So don't delete this user.
131
					return false;
132
				}
133
			}
134
		}
135
136
		return true;
137
	}
138
139
	/**
140
	 * Process user delete form.
141
	 * Helper function to handle common delete user fields.
142
	 *
143
	 * @since 5.5.3
144
	 * @access protected
145
	 *
146
	 * @param array $delete_options Delete Options.
147
	 */
148
	protected function process_user_delete( $delete_options ) {
149
		$delete_options['login_restrict']      = array_get_bool( $_POST, "smbd_{$this->field_slug}_login_restrict", false );
150
		$delete_options['login_days']          = absint( array_get( $_POST, "smbd_{$this->field_slug}_login_days", 0 ) );
151
152
		$delete_options['registered_restrict'] = array_get_bool( $_POST, "smbd_{$this->field_slug}_registered_restrict", false );
153
		$delete_options['registered_days']     = absint( array_get( $_POST, "smbd_{$this->field_slug}_registered_days", 0 ) );
154
155
		$delete_options['no_posts']            = array_get_bool( $_POST, "smbd_{$this->field_slug}_no_posts", false );
156
		$delete_options['limit_to']            = absint( array_get( $_POST, "smbd_{$this->field_slug}_limit_to", 0 ) );
157
158
		$this->process_delete( $delete_options );
159
	}
160
161
	/**
162
	 * Render User Login restrict settings.
163
	 *
164
	 * @since 5.5
165
	 */
166
	protected function render_user_login_restrict_settings() {
167
?>
168
		<tr>
169
			<td scope="row" colspan="2">
170
			<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">
171
				<?php _e( 'Restrict to users who have registered at least ', 'bulk-delete' );?>
172
				<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 ago', 'bulk-delete' );?>.
173
			</td>
174
		</tr>
175
176
		<?php
177
		if ( bd_is_simple_login_log_present() ) {
178
			$disabled = '';
179
		} else {
180
			$disabled = 'disabled';
181
		}
182
?>
183
		<tr>
184
			<td scope="row" colspan="2">
185
			<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; ?>>
186
				<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' );?>
187
				<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' );?>.
188
		<?php if ( 'disabled' == $disabled ) { ?>
189
				<span style = "color:red">
190
					<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "http://wordpress.org/plugins/simple-login-log/">Install now</a>
191
				</span>
192
		<?php } ?>
193
			</td>
194
		</tr>
195
196
		<tr>
197
			<td scope="row" colspan="2">
198
				<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?>
199
		</tr>
200
<?php
201
	}
202
203
	/**
204
	 * Render delete user with no posts settings.
205
	 *
206
	 * @since 5.5
207
	 */
208
	protected function render_user_with_no_posts_settings() {
209
?>
210
		<tr>
211
			<td scope="row" colspan="2">
212
				<input name="smbd_<?php echo $this->field_slug; ?>_no_posts" id="smbd_<?php echo $this->field_slug; ?>_no_posts" value="true" type="checkbox">
213
				<?php _e( "Only if user doesn't have any post. Posts from 'post', 'page' & all custom post types would be considered.", 'bulk-delete' ); ?>
214
			</td>
215
		</tr>
216
<?php
217
	}
218
}
219