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
|
40 |
|
protected function initialize() { |
22
|
40 |
|
$this->item_type = 'users'; |
23
|
40 |
|
$this->field_slug = 'u_meta'; |
24
|
40 |
|
$this->meta_box_slug = 'bd_users_by_meta'; |
25
|
40 |
|
$this->action = 'delete_users_by_meta'; |
26
|
40 |
|
$this->cron_hook = 'do-bulk-delete-users-by-meta'; |
27
|
40 |
|
$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
|
40 |
|
$this->messages = array( |
29
|
40 |
|
'box_label' => __( 'By User Meta', 'bulk-delete' ), |
30
|
40 |
|
'scheduled' => __( 'Users from with the selected user meta are scheduled for deletion.', 'bulk-delete' ), |
31
|
40 |
|
'cron_label' => __( 'Delete Users by User Meta', 'bulk-delete' ), |
32
|
|
|
); |
33
|
40 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Render delete users box. |
37
|
|
|
* |
38
|
|
|
* @since 5.5 |
39
|
|
|
*/ |
40
|
|
|
public function render() { |
41
|
|
|
?> |
42
|
|
|
<!-- Users Start--> |
43
|
|
|
<h4><?php _e( 'Select the user meta from which you want to delete users', 'bulk-delete' ); ?></h4> |
44
|
|
|
|
45
|
|
|
<fieldset class="options"> |
46
|
|
|
<table class="optiontable"> |
47
|
|
|
<select name="smbd_u_meta_key" class="enhanced-dropdown"> |
48
|
|
|
<?php |
49
|
|
|
$meta_keys = $this->get_unique_meta_keys(); |
50
|
|
|
foreach ( $meta_keys as $meta_key ) { |
51
|
|
|
printf( '<option value="%s">%s</option>', $meta_key, $meta_key ); |
52
|
|
|
} |
53
|
|
|
?> |
54
|
|
|
</select> |
55
|
|
|
<select name="smbd_u_meta_compare"> |
56
|
|
|
<option value="=">Equals to</option> |
57
|
|
|
<option value="!=">Not Equals to</option> |
58
|
|
|
<option value=">">Greater than</option> |
59
|
|
|
<option value=">=">Greater than or equals to</option> |
60
|
|
|
<option value="<">Less than</option> |
61
|
|
|
<option value="<=">Less than or equals to</option> |
62
|
|
|
<option value="LIKE">Contains</option> |
63
|
|
|
<option value="NOT LIKE">Not Contains</option> |
64
|
|
|
<option value="STARTS WITH">Starts with</option> |
65
|
|
|
<option value="ENDS WITH">Ends with</option> |
66
|
|
|
</select> |
67
|
|
|
<input type="text" name="smbd_u_meta_value" id="smbd_u_meta_value" placeholder="<?php _e( 'Meta Value', 'bulk-delete' );?>"> |
68
|
|
|
|
69
|
|
|
</table> |
70
|
|
|
|
71
|
|
|
<p> |
72
|
|
|
<?php _e( 'If you want to check for null values, then leave the value column blank', 'bulk-delete' ); ?> |
73
|
|
|
</p> |
74
|
|
|
|
75
|
|
|
<table class="optiontable"> |
76
|
|
|
<?php |
77
|
|
|
$this->render_filtering_table_header(); |
78
|
|
|
$this->render_user_login_restrict_settings(); |
79
|
|
|
$this->render_user_with_no_posts_settings(); |
80
|
|
|
$this->render_limit_settings(); |
81
|
|
|
$this->render_cron_settings(); |
82
|
|
|
?> |
83
|
|
|
</table> |
84
|
|
|
</fieldset> |
85
|
|
|
<!-- Users end--> |
86
|
|
|
<?php |
87
|
|
|
$this->render_submit_button(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Process user input and create metabox options. |
92
|
|
|
* |
93
|
|
|
* @param array $request Request array. |
94
|
|
|
* @param array $options User options. |
95
|
|
|
* |
96
|
|
|
* @return array User options. |
97
|
|
|
*/ |
98
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
99
|
|
|
$options['meta_key'] = bd_array_get( $request, 'smbd_u_meta_key' ); |
100
|
|
|
$options['meta_compare'] = bd_array_get( $request, 'smbd_u_meta_compare', '=' ); |
101
|
|
|
$options['meta_value'] = bd_array_get( $request, 'smbd_u_meta_value' ); |
102
|
|
|
|
103
|
|
|
switch ( strtolower( trim( $options['meta_compare'] ) ) ) { |
104
|
|
|
case 'starts with': |
105
|
|
|
$options['meta_compare'] = 'REGEXP'; |
106
|
|
|
$options['meta_value'] = '^' . $options['meta_value']; |
107
|
|
|
break; |
108
|
|
|
case 'ends with': |
109
|
|
|
$options['meta_compare'] = 'REGEXP'; |
110
|
|
|
$options['meta_value'] = $options['meta_value'] . '$'; |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $options; |
115
|
|
|
} |
116
|
|
|
|
117
|
40 |
|
protected function build_query( $options ) { |
118
|
|
|
$query = array( |
119
|
|
|
'meta_query' => array( |
120
|
|
|
array( |
121
|
40 |
|
'key' => $options['meta_key'], |
122
|
40 |
|
'value' => $options['meta_value'], |
123
|
40 |
|
'compare' => $options['meta_compare'], |
124
|
|
|
), |
125
|
|
|
), |
126
|
|
|
); |
127
|
|
|
|
128
|
40 |
|
if ( $options['limit_to'] > 0 ) { |
129
|
10 |
|
$query['number'] = $options['limit_to']; |
130
|
|
|
} |
131
|
|
|
|
132
|
40 |
|
return $query; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Filter JS Array and add validation hooks. |
137
|
|
|
* |
138
|
|
|
* @since 5.5 |
139
|
|
|
* |
140
|
|
|
* @param array $js_array JavaScript Array. |
141
|
|
|
* |
142
|
|
|
* @return array Modified JavaScript Array |
143
|
|
|
*/ |
144
|
|
|
public function filter_js_array( $js_array ) { |
145
|
|
|
$js_array['dt_iterators'][] = '_' . $this->field_slug; |
146
|
|
|
$js_array['validators'][ $this->action ] = 'noValidation'; |
147
|
|
|
|
148
|
|
|
$js_array['pre_action_msg'][ $this->action ] = 'deleteUsersByMetaWarning'; |
149
|
|
|
$js_array['msg']['deleteUsersByMetaWarning'] = __( 'Are you sure you want to delete all the users from the selected user meta?', 'bulk-delete' ); |
150
|
|
|
|
151
|
|
|
$js_array['error_msg'][ $this->action ] = 'enterUserMetaValue'; |
152
|
|
|
$js_array['msg']['enterUserMetaValue'] = __( 'Please enter the value for the user meta field based on which you want to delete users', 'bulk-delete' ); |
153
|
|
|
|
154
|
|
|
return $js_array; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get unique user meta keys. |
159
|
|
|
* |
160
|
|
|
* @since 5.5 |
161
|
|
|
* |
162
|
|
|
* @return array List of unique meta keys. |
163
|
|
|
*/ |
164
|
|
|
private function get_unique_meta_keys() { |
165
|
|
|
global $wpdb; |
166
|
|
|
|
167
|
|
|
return $wpdb->get_col( "SELECT DISTINCT(meta_key) FROM {$wpdb->prefix}usermeta ORDER BY meta_key" ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function get_success_message( $items_deleted ) { |
171
|
|
|
/* translators: 1 Number of users deleted */ |
172
|
|
|
return _n( 'Deleted %d user with the selected user meta', 'Deleted %d users with the selected user meta', $items_deleted, 'bulk-delete' ); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|