Passed
Push — 672-feature/user-removal-in-su... ( 1c5429...743a6f )
by
unknown
10:48
created

DeleteUsersByUserRoleModule   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 59.49%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 86
c 6
b 0
f 0
dl 0
loc 205
ccs 47
cts 79
cp 0.5949
rs 10
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 8 1
A render() 0 23 3
A delete_users_with_no_roles() 0 12 2
A do_delete() 0 8 2
A convert_user_input_to_options() 0 12 2
A append_to_js_array() 0 4 1
A build_query() 0 18 3
A get_messages() 0 26 2
A build_query_for_deleting_users_with_no_roles() 0 21 4
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Users\Modules;
4
5
use BulkWP\BulkDelete\Core\Users\UsersModule;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Bulk Delete Users by User Role Module.
11
 *
12
 * @since 5.5
13
 * @since 6.0.0 Renamed to DeleteUsersByUserRoleModule
14
 */
15
class DeleteUsersByUserRoleModule extends UsersModule {
16
	/**
17
	 * Initialize and setup variables.
18
	 *
19
	 * @since 5.5
20
	 */
21 17
	protected function initialize() {
22 17
		$this->item_type     = 'users';
23 17
		$this->field_slug    = 'u_role';
24 17
		$this->meta_box_slug = 'bd_users_by_role';
25 17
		$this->action        = 'delete_users_by_role';
26 17
		$this->cron_hook     = 'do-bulk-delete-users-by-role';
27 17
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-users-by-role/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ur';
28 17
		$this->messages      = $this->get_messages();
29
	}
30
31
	/**
32
	 * Returns array of messages for multisite or single site depending on the requirement.
33
	 *
34
	 * @return array Messages array.
35
	 */
36 17
	protected function get_messages() {
37 17
		if ( is_multisite() ) {
38
			return array(
39
				'box_label'         => __( 'By User Role', 'bulk-delete' ),
40
				'scheduled'         => __( 'Users from the selected user role are scheduled for removal.', 'bulk-delete' ),
41
				'cron_label'        => __( 'Delete Users by User Role', 'bulk-delete' ),
42
				'validation_error'  => __( 'Select at least one user role from which users should be removed', 'bulk-delete' ),
43
				'confirm_deletion'  => __( 'Are you sure you want to remove all the users from the selected user role?', 'bulk-delete' ),
44
				'confirm_scheduled' => __( 'Are you sure you want to schedule removal for all the users from the selected user role?', 'bulk-delete' ),
45
				/* translators: 1 Number of users removed */
46
				'deleted_one'       => __( 'Removed %d user from the selected roles', 'bulk-delete' ),
47
				/* translators: 1 Number of users removed */
48
				'deleted_multiple'  => __( 'Removed %d users from the selected roles', 'bulk-delete' ),
49
			);
50
		} else {
51
			array(
52 17
				'box_label'         => __( 'By User Role', 'bulk-delete' ),
53 17
				'scheduled'         => __( 'Users from the selected user role are scheduled for deletion.', 'bulk-delete' ),
54 17
				'cron_label'        => __( 'Delete Users by User Role', 'bulk-delete' ),
55 17
				'validation_error'  => __( 'Select at least one user role from which users should be deleted', 'bulk-delete' ),
56 17
				'confirm_deletion'  => __( 'Are you sure you want to delete all the users from the selected user role?', 'bulk-delete' ),
57 17
				'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the users from the selected user role?', 'bulk-delete' ),
58
				/* translators: 1 Number of users deleted */
59 17
				'deleted_one'       => __( 'Deleted %d user from the selected roles', 'bulk-delete' ),
60
				/* translators: 1 Number of users deleted */
61 17
				'deleted_multiple'  => __( 'Deleted %d users from the selected roles', 'bulk-delete' ),
62
			);
63
		}
64
	}
65
	/**
66
	 * Render delete users box.
67
	 *
68
	 * @since 5.5
69
	 */
70
	public function render() {
71
		?>
72
		<?php $action = is_multisite() ? 'remove' : 'delete'; ?>
73
		<h4><?php _e( 'Select the user roles from which you want to ' . esc_attr( $action ) . ' users', 'bulk-delete' ); ?></h4>
74
75
		<fieldset class="options">
76
			<table class="optiontable">
77
				<?php $this->render_user_role_dropdown( true ); ?>
78
			</table>
79
80
			<table class="optiontable">
81
				<?php
82
					$this->render_filtering_table_header();
83
					$this->render_user_login_restrict_settings();
84
					$this->render_user_with_no_posts_settings();
85
					$this->render_limit_settings();
86
					$this->render_post_reassign_settings();
87
					$this->render_cron_settings();
88
				?>
89
			</table>
90
		</fieldset>
91
		<?php
92
		is_multisite() ? $this->render_remove_button() : $this->render_submit_button();
93
	}
94
95
	protected function convert_user_input_to_options( $request, $options ) {
96
		$selected_roles = bd_array_get( $request, 'smbd_' . $this->field_slug . '_roles', array() );
97
98
		$key = array_search( 'none', $selected_roles, true );
99
		if ( false !== $key ) {
100
			unset( $selected_roles[ $key ] );
101
			$options['delete_users_with_no_role'] = true;
102
		}
103
104
		$options['selected_roles'] = $selected_roles;
105
106
		return $options;
107
	}
108
109
	/**
110
	 * Handle both users with roles and without roles.
111
	 *
112
	 * {@inheritdoc}
113
	 *
114
	 * @param array $options Array of Delete options.
115
	 *
116
	 * @return int Number of items that were deleted.
117
	 */
118 17
	protected function do_delete( $options ) {
119 17
		$users_with_roles_deleted = parent::do_delete( $options );
120
121 17
		if ( ! isset( $options['delete_users_with_no_role'] ) ) {
122 15
			return $users_with_roles_deleted;
123
		}
124
125 2
		return $users_with_roles_deleted + $this->delete_users_with_no_roles( $options );
126
	}
127
128
	/**
129
	 * Delete users with no roles.
130
	 *
131
	 * @since 6.0.0
132
	 *
133
	 * @param array $options User options.
134
	 *
135
	 * @return int Number of users that were deleted.
136
	 */
137 2
	protected function delete_users_with_no_roles( $options ) {
138 2
		$query = $this->build_query_for_deleting_users_with_no_roles( $options );
139
140 2
		if ( empty( $query ) ) {
141
			// Short circuit deletion, if nothing needs to be deleted.
142
			return 0;
143
		}
144
145 2
		$query = $this->exclude_users_from_deletion( $query );
146 2
		$query = $this->exclude_current_user( $query );
147
148 2
		return $this->delete_users_from_query( $query, $options );
149
	}
150
151
	/**
152
	 * Build query params for WP_User_Query by using delete options for deleting users with no roles.
153
	 *
154
	 * Return an empty query array to short-circuit deletion.
155
	 *
156
	 * @since 6.0.0
157
	 *
158
	 * @param array $options Delete options.
159
	 *
160
	 * @return array Query.
161
	 */
162 2
	protected function build_query_for_deleting_users_with_no_roles( $options ) {
163
		// Users with no role is not selected.
164 2
		if ( ! isset( $options['delete_users_with_no_role'] ) || ! $options['delete_users_with_no_role'] ) {
165
			return array();
166
		}
167
168 2
		$roles      = get_editable_roles();
169 2
		$role_names = array_keys( $roles );
170
171
		$query = array(
172 2
			'role__not_in' => $role_names,
173 2
			'number'       => $options['limit_to'],
174
		);
175
176 2
		$date_query = $this->get_date_query( $options );
177
178 2
		if ( ! empty( $date_query ) ) {
179
			$query['date_query'] = $date_query;
180
		}
181
182 2
		return $query;
183
	}
184
185
	/**
186
	 * Build query params for WP_User_Query by using delete options.
187
	 *
188
	 * Return an empty query array to short-circuit deletion.
189
	 *
190
	 * @since 6.0.0
191
	 *
192
	 * @param array $options Delete options.
193
	 *
194
	 * @return array Query.
195
	 */
196 17
	protected function build_query( $options ) {
197
		// No role is selected.
198 17
		if ( empty( $options['selected_roles'] ) ) {
199 2
			return array();
200
		}
201
202
		$query = array(
203 15
			'role__in' => $options['selected_roles'],
204 15
			'number'   => $options['limit_to'],
205
		);
206
207 15
		$date_query = $this->get_date_query( $options );
208
209 15
		if ( ! empty( $date_query ) ) {
210 5
			$query['date_query'] = $date_query;
211
		}
212
213 15
		return $query;
214
	}
215
216
	protected function append_to_js_array( $js_array ) {
217
		$js_array['validators'][ $this->action ] = 'validateUserRole';
218
219
		return $js_array;
220
	}
221
}
222