1
|
|
|
<?php |
2
|
|
|
namespace BulkWP\BulkDelete\Core\Metas\Modules; |
3
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Metas\MetasModule; |
5
|
|
|
|
6
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Delete User Meta. |
10
|
|
|
* |
11
|
|
|
* @since 6.0.0 |
12
|
|
|
*/ |
13
|
|
|
class DeleteUserMetaModule extends MetasModule { |
14
|
5 |
|
protected function initialize() { |
15
|
5 |
|
$this->field_slug = 'um'; // Ideally it should be `meta_user`. But we are keeping it as pm for backward compatibility. |
16
|
5 |
|
$this->meta_box_slug = 'bd-user-meta'; |
17
|
5 |
|
$this->action = 'delete_user_meta'; |
18
|
5 |
|
$this->cron_hook = 'do-bulk-delete-user-meta'; |
19
|
5 |
|
$this->messages = array( |
20
|
5 |
|
'box_label' => __( 'Bulk Delete User Meta', 'bulk-delete' ), |
21
|
5 |
|
'scheduled' => __( 'User meta fields from the users with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
22
|
5 |
|
'cron_label' => __( 'Delete User Meta`', 'bulk-delete' ), |
23
|
|
|
); |
24
|
5 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Render the Modules. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function render() { |
32
|
|
|
?> |
33
|
|
|
<!-- User Meta box start--> |
34
|
|
|
<fieldset class="options"> |
35
|
|
|
<h4><?php _e( 'Select the user role whose user meta fields you want to delete', 'bulk-delete' ); ?></h4> |
36
|
|
|
<table class="optiontable"> |
37
|
|
|
<?php |
38
|
|
|
$users_count = count_users(); |
39
|
|
|
foreach ( $users_count['avail_roles'] as $role => $count ) { |
40
|
|
|
?> |
41
|
|
|
<tr> |
42
|
|
|
<td> |
43
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_role" value = "<?php echo $role; ?>" type = "radio" <?php checked( $role, 'administrator' ); ?>> |
44
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_role"><?php echo $role; ?> (<?php echo $count . ' '; _e( 'Users', 'bulk-delete' ); ?>)</label> |
45
|
|
|
</td> |
46
|
|
|
</tr> |
47
|
|
|
<?php |
48
|
|
|
} |
49
|
|
|
?> |
50
|
|
|
</table> |
51
|
|
|
|
52
|
|
|
<h4><?php _e( 'Choose your user meta field settings', 'bulk-delete' ); ?></h4> |
53
|
|
|
<table class="optiontable"> |
54
|
|
|
<tr> |
55
|
|
|
<td> |
56
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked> |
57
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on user meta key name only', 'bulk-delete' ); ?></label> |
58
|
|
|
</td> |
59
|
|
|
</tr> |
60
|
|
|
|
61
|
|
|
<tr> |
62
|
|
|
<td> |
63
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="true" type="radio" disabled> |
64
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on user meta key name and value', 'bulk-delete' ); ?></label> |
65
|
|
|
<span class="bd-um-pro" style="color:red; vertical-align: middle;"> |
66
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-user-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-u" target="_blank">Buy now</a> |
67
|
|
|
</span> |
68
|
|
|
</td> |
69
|
|
|
</tr> |
70
|
|
|
|
71
|
|
|
<tr> |
72
|
|
|
<td> |
73
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php _e( 'User Meta Key ', 'bulk-delete' ); ?></label> |
74
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>"> |
75
|
|
|
</td> |
76
|
|
|
</tr> |
77
|
|
|
</table> |
78
|
|
|
<?php |
79
|
|
|
/** |
80
|
|
|
* Add more fields to the delete user meta field form. |
81
|
|
|
* This hook can be used to add more fields to the delete user meta field form. |
82
|
|
|
* |
83
|
|
|
* @since 5.4 |
84
|
|
|
*/ |
85
|
|
|
do_action( 'bd_delete_user_meta_form' ); |
86
|
|
|
?> |
87
|
|
|
<table class="optiontable"> |
88
|
|
|
<tr> |
89
|
|
|
<td> |
90
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
91
|
|
|
</td> |
92
|
|
|
</tr> |
93
|
|
|
|
94
|
|
|
<tr> |
95
|
|
|
<td> |
96
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_limit" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_limit" value = "true" type = "checkbox"> |
97
|
|
|
<?php _e( 'Only delete user meta field from first ', 'bulk-delete' );?> |
98
|
|
|
<input type ="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_limit_to" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_limit_to" disabled value ="0" maxlength="4" size="4"><?php _e( 'users.', 'bulk-delete' );?> |
99
|
|
|
<?php _e( 'Use this option if there are more than 1000 users and the script times out.', 'bulk-delete' ) ?> |
100
|
|
|
</td> |
101
|
|
|
</tr> |
102
|
|
|
|
103
|
|
|
<tr> |
104
|
|
|
<td> |
105
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value = "false" type = "radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
106
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value = "true" type = "radio" id = "smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" disabled > <?php _e( 'Schedule', 'bulk-delete' ); ?> |
107
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" id = "smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" value = "now" type = "text" disabled><?php _e( 'repeat ', 'bulk-delete' );?> |
108
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" id = "smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" disabled> |
109
|
|
|
<option value = "-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
110
|
|
|
<?php |
111
|
|
|
$schedules = wp_get_schedules(); |
112
|
|
|
foreach ( $schedules as $key => $value ) { |
113
|
|
|
?> |
114
|
|
|
<option value = "<?php echo $key; ?>"><?php echo $value['display']; ?></option> |
115
|
|
|
<?php |
116
|
|
|
} |
117
|
|
|
?> |
118
|
|
|
</select> |
119
|
|
|
<span class="bd-um-pro" style="color:red"> |
120
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-user-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-u">Buy now</a> |
121
|
|
|
</span> |
122
|
|
|
</td> |
123
|
|
|
</tr> |
124
|
|
|
|
125
|
|
|
<tr> |
126
|
|
|
<td> |
127
|
|
|
<?php _e( 'Enter time in Y-m-d H:i:s format or enter now to use current time', 'bulk-delete' );?> |
128
|
|
|
</td> |
129
|
|
|
</tr> |
130
|
|
|
|
131
|
|
|
</table> |
132
|
|
|
</fieldset> |
133
|
|
|
|
134
|
|
|
<p> |
135
|
|
|
<button type="submit" name="bd_action" value="delete_meta_user" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ) ?>»</button> |
136
|
|
|
</p> |
137
|
|
|
<!-- User Meta box end--> |
138
|
|
|
<?php |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
142
|
|
|
$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) ); |
143
|
|
|
|
144
|
|
|
$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ); |
145
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) ); |
146
|
|
|
|
147
|
|
|
return $options; |
148
|
|
|
} |
149
|
|
|
|
150
|
5 |
|
protected function do_delete( $options ) { |
151
|
5 |
|
$count = 0; |
152
|
5 |
|
$user_role = $options['user_role']; |
153
|
5 |
|
$meta_key = $options['meta_key']; |
154
|
5 |
|
$use_value = $options['use_value']; |
155
|
5 |
|
$limit_to = $options['limit_to']; |
156
|
|
|
|
157
|
|
|
$options = array( |
158
|
5 |
|
'role' => $user_role, |
159
|
|
|
); |
160
|
|
|
|
161
|
5 |
|
if ( $limit_to > 0 ) { |
162
|
1 |
|
$options['number'] = $limit_to; |
163
|
|
|
} |
164
|
|
|
|
165
|
5 |
|
if ( $use_value ) { |
166
|
|
|
$options['meta_query'] = apply_filters( 'bd_delete_user_meta_query', array(), $options ); |
167
|
|
|
} else { |
168
|
5 |
|
$options['meta_key'] = $meta_key; |
169
|
|
|
} |
170
|
|
|
|
171
|
5 |
|
$users = get_users( $options ); |
172
|
|
|
|
173
|
5 |
|
foreach ( $users as $user ) { |
174
|
5 |
|
if ( delete_user_meta( $user->ID, $meta_key ) ) { |
175
|
5 |
|
$count++; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
5 |
|
return $count; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function filter_js_array( $js_array ) { |
183
|
|
|
$js_array['dt_iterators'][] = '_um'; |
184
|
|
|
$js_array['validators']['delete_meta_user'] = 'noValidation'; |
185
|
|
|
|
186
|
|
|
$js_array['pre_action_msg']['delete_meta_user'] = 'deleteUMWarning'; |
187
|
|
|
$js_array['msg']['deleteUMWarning'] = __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ); |
188
|
|
|
|
189
|
|
|
return $js_array; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function get_success_message( $items_deleted ) { |
193
|
|
|
/* translators: 1 Number of posts deleted */ |
194
|
|
|
return _n( 'Deleted user meta field from %d user', 'Deleted user meta field from %d users', $items_deleted, 'bulk-delete' ); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|