1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bulk Delete Users by User Meta. |
4
|
|
|
* |
5
|
|
|
* @since 5.5 |
6
|
|
|
* |
7
|
|
|
* @author Sudar |
8
|
|
|
* |
9
|
|
|
* @package BulkDelete\Users\Modules |
10
|
|
|
*/ |
11
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Bulk Delete Users by User Meta. |
15
|
|
|
* |
16
|
|
|
* @since 5.5 |
17
|
|
|
*/ |
18
|
|
|
class Bulk_Delete_Users_By_User_Meta extends BD_User_Meta_Box_Module { |
19
|
|
|
/** |
20
|
|
|
* Make this class a "hybrid Singleton". |
21
|
|
|
* |
22
|
|
|
* @static |
23
|
|
|
* |
24
|
|
|
* @since 5.5 |
25
|
|
|
*/ |
26
|
|
|
public static function factory() { |
27
|
|
|
static $instance = false; |
28
|
|
|
|
29
|
|
|
if ( ! $instance ) { |
30
|
|
|
$instance = new self; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $instance; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialize and setup variables. |
38
|
|
|
* |
39
|
|
|
* @since 5.5 |
40
|
|
|
*/ |
41
|
|
|
protected function initialize() { |
42
|
|
|
$this->item_type = 'users'; |
43
|
|
|
$this->field_slug = 'u_meta'; |
44
|
|
|
$this->meta_box_slug = 'bd_users_by_meta'; |
45
|
|
|
$this->meta_box_hook = "bd_add_meta_box_for_{$this->item_type}"; |
46
|
|
|
$this->delete_action = 'delete_users_by_meta'; |
47
|
|
|
$this->cron_hook = 'do-bulk-delete-users-by-meta'; |
48
|
|
|
$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-users-by-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ma'; |
49
|
|
|
$this->messages = array( |
50
|
|
|
'box_label' => __( 'By User Meta', 'bulk-delete' ), |
51
|
|
|
'scheduled' => __( 'Users from with the selected user meta are scheduled for deletion.', 'bulk-delete' ), |
52
|
|
|
'deleted_single' => __( 'Deleted %d user with the selected user meta', 'bulk-delete' ), |
53
|
|
|
'deleted_plural' => __( 'Deleted %d users with the selected user meta', 'bulk-delete' ), |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Render delete users box. |
59
|
|
|
* |
60
|
|
|
* @since 5.5 |
61
|
|
|
*/ |
62
|
|
|
public function render() { |
63
|
|
|
?> |
64
|
|
|
<!-- Users Start--> |
65
|
|
|
<h4><?php _e( 'Select the user meta from which you want to delete users', 'bulk-delete' ); ?></h4> |
66
|
|
|
|
67
|
|
|
<fieldset class="options"> |
68
|
|
|
<table class="optiontable"> |
69
|
|
|
<select name="smbd_u_meta_key" class="select2"> |
70
|
|
|
<?php |
71
|
|
|
$meta_keys = $this->get_unique_meta_keys(); |
72
|
|
|
foreach ( $meta_keys as $meta_key ) { |
73
|
|
|
printf( '<option value="%s">%s</option>', $meta_key, $meta_key ); |
74
|
|
|
} |
75
|
|
|
?> |
76
|
|
|
</select> |
77
|
|
|
<select name="smbd_u_meta_compare"> |
78
|
|
|
<option value="=">Equals to</option> |
79
|
|
|
<option value="!=">Not Equals to</option> |
80
|
|
|
<option value=">">Greater than</option> |
81
|
|
|
<option value=">=">Greater than or equals to</option> |
82
|
|
|
<option value="<">Less than</option> |
83
|
|
|
<option value="<=">Less than or equals to</option> |
84
|
|
|
<option value="LIKE">Contains</option> |
85
|
|
|
<option value="NOT LIKE">Not Contains</option> |
86
|
|
|
<option value="STARTS WITH">Starts with</option> |
87
|
|
|
<option value="ENDS WITH">Ends with</option> |
88
|
|
|
</select> |
89
|
|
|
<input type="text" name="smbd_u_meta_value" id="smbd_u_meta_value" placeholder="<?php _e( 'Meta Value', 'bulk-delete' );?>"> |
90
|
|
|
|
91
|
|
|
</table> |
92
|
|
|
|
93
|
|
|
<p> |
94
|
|
|
<?php _e( 'If you want to check for null values, then leave the value column blank', 'bulk-delete' ); ?> |
95
|
|
|
</p> |
96
|
|
|
|
97
|
|
|
<table class="optiontable"> |
98
|
|
|
<?php |
99
|
|
|
$this->render_filtering_table_header(); |
100
|
|
|
$this->render_user_login_restrict_settings(); |
101
|
|
|
$this->render_user_with_no_posts_settings(); |
102
|
|
|
$this->render_limit_settings(); |
103
|
|
|
$this->render_cron_settings(); |
104
|
|
|
?> |
105
|
|
|
</table> |
106
|
|
|
</fieldset> |
107
|
|
|
<!-- Users end--> |
108
|
|
|
<?php |
109
|
|
|
$this->render_submit_button(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Process the request for deleting users by meta. |
114
|
|
|
* |
115
|
|
|
* @since 5.5 |
116
|
|
|
*/ |
117
|
|
|
public function process() { |
118
|
|
|
$delete_options = array(); |
119
|
|
|
$delete_options['meta_key'] = array_get( $_POST, 'smbd_u_meta_key' ); |
120
|
|
|
$delete_options['meta_compare'] = array_get( $_POST, 'smbd_u_meta_compare', '=' ); |
121
|
|
|
$delete_options['meta_value'] = array_get( $_POST, 'smbd_u_meta_value' ); |
122
|
|
|
|
123
|
|
|
switch ( strtolower( trim( $delete_options['meta_compare'] ) ) ) { |
124
|
|
|
case 'starts with': |
125
|
|
|
$delete_options['meta_compare'] = 'REGEXP'; |
126
|
|
|
$delete_options['meta_value'] = '^' . $delete_options['meta_value']; |
127
|
|
|
break; |
128
|
|
|
case 'ends with': |
129
|
|
|
$delete_options['meta_compare'] = 'REGEXP'; |
130
|
|
|
$delete_options['meta_value'] = $delete_options['meta_value'] . '$'; |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->process_user_delete( $delete_options ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Delete users by user meta. |
139
|
|
|
* |
140
|
|
|
* @since 5.5 |
141
|
|
|
* |
142
|
|
|
* @param array $delete_options Delete Options |
143
|
|
|
* |
144
|
|
|
* @return int Number of users deleted |
145
|
|
|
*/ |
146
|
|
|
public function delete( $delete_options ) { |
147
|
|
|
if ( ! function_exists( 'wp_delete_user' ) ) { |
148
|
|
|
require_once ABSPATH . 'wp-admin/includes/user.php'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$options = array( |
152
|
|
|
'meta_query' => array( |
153
|
|
|
array( |
154
|
|
|
'key' => $delete_options['meta_key'], |
155
|
|
|
'value' => $delete_options['meta_value'], |
156
|
|
|
'compare' => $delete_options['meta_compare'], |
157
|
|
|
), |
158
|
|
|
), |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
if ( $delete_options['limit_to'] > 0 ) { |
162
|
|
|
$options['number'] = $delete_options['limit_to']; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->delete_users( $options, $delete_options ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Filter JS Array and add validation hooks. |
170
|
|
|
* |
171
|
|
|
* @since 5.5 |
172
|
|
|
* |
173
|
|
|
* @param array $js_array JavaScript Array |
174
|
|
|
* |
175
|
|
|
* @return array Modified JavaScript Array |
176
|
|
|
*/ |
177
|
|
|
public function filter_js_array( $js_array ) { |
178
|
|
|
$js_array['dt_iterators'][] = '_' . $this->field_slug; |
179
|
|
|
$js_array['validators'][ $this->delete_action ] = 'noValidation'; |
180
|
|
|
|
181
|
|
|
$js_array['pre_action_msg'][ $this->delete_action ] = 'deleteUsersByMetaWarning'; |
182
|
|
|
$js_array['msg']['deleteUsersByMetaWarning'] = __( 'Are you sure you want to delete all the users from the selected user meta?', 'bulk-delete' ); |
183
|
|
|
|
184
|
|
|
$js_array['error_msg'][ $this->delete_action ] = 'enterUserMetaValue'; |
185
|
|
|
$js_array['msg']['enterUserMetaValue'] = __( 'Please enter the value for the user meta field based on which you want to delete users', 'bulk-delete' ); |
186
|
|
|
|
187
|
|
|
return $js_array; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get unique user meta keys. |
192
|
|
|
* |
193
|
|
|
* @since 5.5 |
194
|
|
|
* |
195
|
|
|
* @return array List of unique meta keys. |
196
|
|
|
*/ |
197
|
|
|
private function get_unique_meta_keys() { |
198
|
|
|
global $wpdb; |
199
|
|
|
|
200
|
|
|
return $wpdb->get_col( "SELECT DISTINCT(meta_key) FROM {$wpdb->prefix}usermeta ORDER BY meta_key" ); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
Bulk_Delete_Users_By_User_Meta::factory(); |
205
|
|
|
?> |
206
|
|
|
|