Completed
Push — dev/5.6 ( 138387...b7c820 )
by Sudar
05:39 queued 04:12
created

base/users/class-bd-user-meta-box-module.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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['post_type']           = array_get( $_POST, "smbd_{$this->field_slug}_post_type", array() );
0 ignored issues
show
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
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
		$dropdown_args = array(
218
			'is_multi_value_select' => true,
219
			'is_select_all_options' => true,
220
			'style_attribute'       => 'width: 100px;',
221
		);
222
		bd_render_post_type_dropdown( $this->field_slug, $dropdown_args );
223
	}
224
}
225