Completed
Pull Request — master (#142)
by Sudar
02:02
created

BD_User_Meta_Box_Module   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 1
dl 0
loc 196
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B delete_users() 0 25 6
A can_delete_by_post_count() 0 7 3
A can_delete_by_registered_date() 0 14 4
B can_delete_by_logged_date() 0 21 5
A process_user_delete() 0 12 1
B render_user_login_restrict_settings() 0 36 3
A render_user_with_no_posts_settings() 0 10 1
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 object 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'] && count_user_posts( $user->ID, get_post_types( array( 'public' => true ) ) ) > 0 ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($delete_options...ublic' => true))) > 0);.
Loading history...
72
			return false;
73
		}
74
75
		return true;
76
	}
77
78
	/**
79
	 * Can the user be deleted based on the 'registered date' option?
80
	 *
81
	 * @since  5.5.3
82
	 * @access protected
83
	 *
84
	 * @param array  $delete_options Delete Options.
85
	 * @param object $user           User object that needs to be deleted.
86
	 *
87
	 * @return bool True if the user can be deleted, false otherwise.
88
	 */
89
	protected function can_delete_by_registered_date( $delete_options, $user ) {
90
		if ( $delete_options['registered_restrict'] ) {
91
			$registered_days = $delete_options['registered_days'];
92
93
			if ( $registered_days > 0 ) {
94
				$user_meta = get_userdata( $user->ID );
95
				if ( strtotime( $user_meta->user_registered ) > strtotime( '-' . $registered_days . 'days' ) ) {
96
					return false;
97
				}
98
			}
99
		}
100
101
		return true;
102
	}
103
104
	/**
105
	 * Can the user be deleted based on the 'logged in date' option?
106
	 *
107
	 * @since  5.5.2
108
	 * @access protected
109
	 *
110
	 * @param array  $delete_options Delete Options.
111
	 * @param object $user           User object that needs to be deleted.
112
	 *
113
	 * @return bool True if the user can be deleted, false otherwise.
114
	 */
115
	protected function can_delete_by_logged_date( $delete_options, $user ) {
116
		if ( $delete_options['login_restrict'] ) {
117
			$login_days = $delete_options['login_days'];
118
			$last_login = bd_get_last_login( $user->ID );
119
120
			if ( null !== $last_login ) {
121
				// we have a logged-in entry for the user in simple login log plugin.
122
				if ( strtotime( $last_login ) > strtotime( '-' . $login_days . 'days' ) ) {
123
					return false;
124
				}
125
			} else {
126
				// we don't have a logged-in entry for the user in simple login log plugin.
127
				if ( $login_days > 0 ) {
128
					// non-zero value for login date. So don't delete this user.
129
					return false;
130
				}
131
			}
132
		}
133
134
		return true;
135
	}
136
137
	/**
138
	 * Process user delete form.
139
	 * Helper function to handle common delete user fields.
140
	 *
141
	 * @since 5.5.3
142
	 * @access protected
143
	 *
144
	 * @param array $delete_options Delete Options.
145
	 */
146
	protected function process_user_delete( $delete_options ) {
147
		$delete_options['login_restrict']      = array_get_bool( $_POST, "smbd_{$this->field_slug}_login_restrict", false );
148
		$delete_options['login_days']          = absint( array_get( $_POST, "smbd_{$this->field_slug}_login_days", 0 ) );
149
150
		$delete_options['registered_restrict'] = array_get_bool( $_POST, "smbd_{$this->field_slug}_registered_restrict", false );
151
		$delete_options['registered_days']     = absint( array_get( $_POST, "smbd_{$this->field_slug}_registered_days", 0 ) );
152
153
		$delete_options['no_posts']            = array_get_bool( $_POST, "smbd_{$this->field_slug}_no_posts", false );
154
		$delete_options['limit_to']            = absint( array_get( $_POST, "smbd_{$this->field_slug}_limit_to", 0 ) );
155
156
		$this->process_delete( $delete_options );
157
	}
158
159
	/**
160
	 * Render User Login restrict settings.
161
	 *
162
	 * @since 5.5
163
	 */
164
	protected function render_user_login_restrict_settings() {
165
?>
166
		<tr>
167
			<td scope="row" colspan="2">
168
			<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">
169
				<?php _e( 'Restrict to users who have registered at least ', 'bulk-delete' );?>
170
				<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' );?>.
171
			</td>
172
		</tr>
173
174
		<?php
175
		if ( bd_is_simple_login_log_present() ) {
176
			$disabled = '';
177
		} else {
178
			$disabled = 'disabled';
179
		}
180
?>
181
		<tr>
182
			<td scope="row" colspan="2">
183
			<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; ?>>
184
				<?php _e( 'Restrict to users who have not logged in the last ', 'bulk-delete' );?>
185
				<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' );?>.
186
		<?php if ( 'disabled' == $disabled ) { ?>
187
				<span style = "color:red">
188
					<?php _e( 'Need the free "Simple Login Log" Plugin', 'bulk-delete' ); ?> <a href = "http://wordpress.org/plugins/simple-login-log/">Install now</a>
189
				</span>
190
		<?php } ?>
191
			</td>
192
		</tr>
193
194
		<tr>
195
			<td scope="row" colspan="2">
196
				<?php _e( 'Enter "0 days" to delete users who have never logged in after the "Simple Login Log" plugin has been installed.', 'bulk-delete' ); ?>
197
		</tr>
198
<?php
199
	}
200
201
	/**
202
	 * Render delete user with no posts settings.
203
	 *
204
	 * @since 5.5
205
	 */
206
	protected function render_user_with_no_posts_settings() {
207
?>
208
		<tr>
209
			<td scope="row" colspan="2">
210
				<input name="smbd_<?php echo $this->field_slug; ?>_no_posts" id="smbd_<?php echo $this->field_slug; ?>_no_posts" value="true" type="checkbox">
211
				<?php _e( "Only if user doesn't have any post. Posts from 'post', 'page' & all custom post types would be considered.", 'bulk-delete' ); ?>
212
			</td>
213
		</tr>
214
<?php
215
	}
216
}
217