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 Meta. |
11
|
|
|
* |
12
|
|
|
* @since 5.5 |
13
|
|
|
* @since 6.0.0 Renamed to DeleteUsersByUserMetaModule. |
14
|
|
|
*/ |
15
|
|
|
class DeleteUsersByUserMetaModule extends UsersModule { |
16
|
|
|
/** |
17
|
|
|
* Initialize and setup variables. |
18
|
|
|
* |
19
|
|
|
* @since 5.5 |
20
|
|
|
*/ |
21
|
52 |
|
protected function initialize() { |
22
|
52 |
|
$this->item_type = 'users'; |
23
|
52 |
|
$this->field_slug = 'u_meta'; |
24
|
52 |
|
$this->meta_box_slug = 'bd_users_by_meta'; |
25
|
52 |
|
$this->action = 'delete_users_by_meta'; |
26
|
52 |
|
$this->cron_hook = 'do-bulk-delete-users-by-meta'; |
27
|
52 |
|
$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-users-by-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ma'; |
28
|
52 |
|
$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
|
52 |
|
protected function get_messages() { |
37
|
52 |
|
if ( is_multisite() ) { |
38
|
|
|
return array( |
39
|
|
|
'box_label' => __( 'By User Meta', 'bulk-delete' ), |
40
|
|
|
'scheduled' => __( 'Users from with the selected user meta are scheduled for removal.', 'bulk-delete' ), |
41
|
|
|
'cron_label' => __( 'Remove Users by User Meta', 'bulk-delete' ), |
42
|
|
|
'confirm_deletion' => __( 'Are you sure you want to remove all the users from the selected user meta?', 'bulk-delete' ), |
43
|
|
|
'confirm_scheduled' => __( 'Are you sure you want to schedule removal for all the users from the selected user meta?', 'bulk-delete' ), |
44
|
|
|
/* translators: 1 Number of users removed */ |
45
|
|
|
'deleted_one' => __( 'Removed %d user with the selected user meta', 'bulk-delete' ), |
46
|
|
|
/* translators: 1 Number of users removed */ |
47
|
|
|
'deleted_multiple' => __( 'Removed %d users with the selected user meta', 'bulk-delete' ), |
48
|
|
|
); |
49
|
|
|
} else { |
50
|
|
|
return array( |
51
|
52 |
|
'box_label' => __( 'By User Meta', 'bulk-delete' ), |
52
|
52 |
|
'scheduled' => __( 'Users from with the selected user meta are scheduled for deletion.', 'bulk-delete' ), |
53
|
52 |
|
'cron_label' => __( 'Delete Users by User Meta', 'bulk-delete' ), |
54
|
52 |
|
'confirm_deletion' => __( 'Are you sure you want to delete all the users from the selected user meta?', 'bulk-delete' ), |
55
|
52 |
|
'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the users from the selected user meta?', 'bulk-delete' ), |
56
|
|
|
/* translators: 1 Number of users deleted */ |
57
|
52 |
|
'deleted_one' => __( 'Deleted %d user with the selected user meta', 'bulk-delete' ), |
58
|
|
|
/* translators: 1 Number of users deleted */ |
59
|
52 |
|
'deleted_multiple' => __( 'Deleted %d users with the selected user meta', 'bulk-delete' ), |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Render delete users box. |
66
|
|
|
* |
67
|
|
|
* @since 5.5 |
68
|
|
|
*/ |
69
|
|
|
public function render() { |
70
|
|
|
?> |
71
|
|
|
<!-- Users Start--> |
72
|
|
|
<?php $action = is_multisite() ? 'remove' : 'delete'; ?> |
73
|
|
|
<h4><?php _e( 'Select the user meta from which you want to ' . esc_attr( $action ) . ' users', 'bulk-delete' ); ?></h4> |
74
|
|
|
|
75
|
|
|
<fieldset class="options"> |
76
|
|
|
<table class="optiontable"> |
77
|
|
|
<select name="smbd_u_meta_key" class="enhanced-dropdown"> |
78
|
|
|
<?php |
79
|
|
|
$meta_keys = $this->get_unique_user_meta_keys(); |
80
|
|
|
foreach ( $meta_keys as $meta_key ) { |
81
|
|
|
printf( '<option value="%s">%s</option>', esc_attr( $meta_key ), esc_html( $meta_key ) ); |
82
|
|
|
} |
83
|
|
|
?> |
84
|
|
|
</select> |
85
|
|
|
|
86
|
|
|
<select name="smbd_u_meta_compare"> |
87
|
|
|
<option value="=">Equals to</option> |
88
|
|
|
<option value="!=">Not Equals to</option> |
89
|
|
|
<option value=">">Greater than</option> |
90
|
|
|
<option value=">=">Greater than or equals to</option> |
91
|
|
|
<option value="<">Less than</option> |
92
|
|
|
<option value="<=">Less than or equals to</option> |
93
|
|
|
<option value="LIKE">Contains</option> |
94
|
|
|
<option value="NOT LIKE">Not Contains</option> |
95
|
|
|
<option value="STARTS WITH">Starts with</option> |
96
|
|
|
<option value="ENDS WITH">Ends with</option> |
97
|
|
|
</select> |
98
|
|
|
<input type="text" name="smbd_u_meta_value" id="smbd_u_meta_value" placeholder="<?php _e( 'Meta Value', 'bulk-delete' ); ?>"> |
99
|
|
|
|
100
|
|
|
</table> |
101
|
|
|
|
102
|
|
|
<p> |
103
|
|
|
<?php _e( 'If you want to check for null values, then leave the value column blank', 'bulk-delete' ); ?> |
104
|
|
|
</p> |
105
|
|
|
|
106
|
|
|
<table class="optiontable"> |
107
|
|
|
<?php |
108
|
|
|
$this->render_filtering_table_header(); |
109
|
|
|
$this->render_user_login_restrict_settings(); |
110
|
|
|
$this->render_user_with_no_posts_settings(); |
111
|
|
|
$this->render_limit_settings(); |
112
|
|
|
$this->render_post_reassign_settings(); |
113
|
|
|
$this->render_cron_settings(); |
114
|
|
|
?> |
115
|
|
|
</table> |
116
|
|
|
</fieldset> |
117
|
|
|
<!-- Users end--> |
118
|
|
|
|
119
|
|
|
<?php |
120
|
|
|
is_multisite() ? $this->render_remove_button() : $this->render_submit_button(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Process user input and create metabox options. |
125
|
|
|
* |
126
|
|
|
* @param array $request Request array. |
127
|
|
|
* @param array $options User options. |
128
|
|
|
* |
129
|
|
|
* @return array User options. |
130
|
|
|
*/ |
131
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
132
|
|
|
$options['meta_key'] = bd_array_get( $request, 'smbd_u_meta_key' ); |
133
|
|
|
$options['meta_compare'] = bd_array_get( $request, 'smbd_u_meta_compare', '=' ); |
134
|
|
|
$options['meta_value'] = bd_array_get( $request, 'smbd_u_meta_value' ); |
135
|
|
|
|
136
|
|
|
switch ( strtolower( trim( $options['meta_compare'] ) ) ) { |
137
|
|
|
case 'starts with': |
138
|
|
|
$options['meta_compare'] = 'REGEXP'; |
139
|
|
|
$options['meta_value'] = '^' . $options['meta_value']; |
140
|
|
|
break; |
141
|
|
|
case 'ends with': |
142
|
|
|
$options['meta_compare'] = 'REGEXP'; |
143
|
|
|
$options['meta_value'] = $options['meta_value'] . '$'; |
144
|
|
|
break; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $options; |
148
|
|
|
} |
149
|
|
|
|
150
|
52 |
|
protected function build_query( $options ) { |
151
|
|
|
$query = array( |
152
|
|
|
'meta_query' => array( |
153
|
|
|
array( |
154
|
52 |
|
'key' => $options['meta_key'], |
155
|
52 |
|
'value' => $options['meta_value'], |
156
|
52 |
|
'compare' => $options['meta_compare'], |
157
|
|
|
), |
158
|
|
|
), |
159
|
|
|
); |
160
|
|
|
|
161
|
52 |
|
if ( $options['limit_to'] > 0 ) { |
162
|
16 |
|
$query['number'] = $options['limit_to']; |
163
|
|
|
} |
164
|
|
|
|
165
|
52 |
|
$date_query = $this->get_date_query( $options ); |
166
|
|
|
|
167
|
52 |
|
if ( ! empty( $date_query ) ) { |
168
|
21 |
|
$query['date_query'] = $date_query; |
169
|
|
|
} |
170
|
|
|
|
171
|
52 |
|
return $query; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function append_to_js_array( $js_array ) { |
175
|
|
|
$js_array['validators'][ $this->action ] = 'noValidation'; |
176
|
|
|
|
177
|
|
|
return $js_array; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|