|
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
|
11 |
|
protected function initialize() { |
|
15
|
11 |
|
$this->field_slug = 'um'; // Ideally it should be `meta_user`. But we are keeping it as um for backward compatibility. |
|
16
|
11 |
|
$this->meta_box_slug = 'bd-user-meta'; |
|
17
|
11 |
|
$this->action = 'delete_user_meta'; |
|
18
|
11 |
|
$this->cron_hook = 'do-bulk-delete-user-meta'; |
|
19
|
11 |
|
$this->messages = array( |
|
20
|
11 |
|
'box_label' => __( 'Bulk Delete User Meta', 'bulk-delete' ), |
|
21
|
11 |
|
'scheduled' => __( 'User meta fields from the users with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
|
22
|
11 |
|
'cron_label' => __( 'Delete User Meta`', 'bulk-delete' ), |
|
23
|
|
|
); |
|
24
|
11 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Render the Modules. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function render() { |
|
30
|
|
|
?> |
|
31
|
|
|
<!-- User Meta box start--> |
|
32
|
|
|
<fieldset class="options"> |
|
33
|
|
|
<h4><?php _e( 'Select the user role whose user meta fields you want to delete', 'bulk-delete' ); ?></h4> |
|
34
|
|
|
|
|
35
|
|
|
<table class="optiontable"> |
|
36
|
|
|
<?php $this->render_user_role_dropdown(); ?> |
|
37
|
|
|
</table> |
|
38
|
|
|
|
|
39
|
|
|
<h4><?php _e( 'Choose your user meta field settings', 'bulk-delete' ); ?></h4> |
|
40
|
|
|
<table class="optiontable"> |
|
41
|
|
|
<tr> |
|
42
|
|
|
<td> |
|
43
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked> |
|
44
|
|
|
<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> |
|
45
|
|
|
</td> |
|
46
|
|
|
</tr> |
|
47
|
|
|
|
|
48
|
|
|
<tr> |
|
49
|
|
|
<td> |
|
50
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" |
|
51
|
|
|
value="true" type="radio" disabled> |
|
52
|
|
|
<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> |
|
53
|
|
|
<span class="bd-um-pro" style="color:red; vertical-align: middle;"> |
|
54
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> |
|
55
|
|
|
<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> |
|
56
|
|
|
</span> |
|
57
|
|
|
</td> |
|
58
|
|
|
</tr> |
|
59
|
|
|
|
|
60
|
|
|
<tr> |
|
61
|
|
|
<td> |
|
62
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php _e( 'User Meta Key ', 'bulk-delete' ); ?></label> |
|
63
|
|
|
<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' ); ?>"> |
|
64
|
|
|
</td> |
|
65
|
|
|
</tr> |
|
66
|
|
|
</table> |
|
67
|
|
|
<?php |
|
68
|
|
|
/** |
|
69
|
|
|
* Add more fields to the delete user meta field form. |
|
70
|
|
|
* This hook can be used to add more fields to the delete user meta field form. |
|
71
|
|
|
* |
|
72
|
|
|
* @since 5.4 |
|
73
|
|
|
*/ |
|
74
|
|
|
do_action( 'bd_delete_user_meta_form' ); |
|
75
|
|
|
?> |
|
76
|
|
|
<table class="optiontable"> |
|
77
|
|
|
<tr> |
|
78
|
|
|
<td> |
|
79
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
|
80
|
|
|
</td> |
|
81
|
|
|
</tr> |
|
82
|
|
|
|
|
83
|
|
|
<tr> |
|
84
|
|
|
<td> |
|
85
|
|
|
<label> |
|
86
|
|
|
<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"> |
|
87
|
|
|
<?php _e( 'Only delete user meta field from first ', 'bulk-delete' ); ?> |
|
88
|
|
|
</label> |
|
89
|
|
|
<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" |
|
90
|
|
|
disabled value="0" maxlength="4" size="4"> |
|
91
|
|
|
<?php _e( 'users.', 'bulk-delete' ); ?> |
|
92
|
|
|
<?php _e( 'Use this option if there are more than 1000 users and the script times out.', 'bulk-delete' ); ?> |
|
93
|
|
|
</td> |
|
94
|
|
|
</tr> |
|
95
|
|
|
|
|
96
|
|
|
<tr> |
|
97
|
|
|
<td> |
|
98
|
|
|
<label> |
|
99
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="false" type="radio" checked="checked"> |
|
100
|
|
|
<?php _e( 'Delete now', 'bulk-delete' ); ?> |
|
101
|
|
|
</label> |
|
102
|
|
|
<label> |
|
103
|
|
|
<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> |
|
104
|
|
|
<?php _e( 'Schedule', 'bulk-delete' ); ?> |
|
105
|
|
|
</label> |
|
106
|
|
|
<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> |
|
107
|
|
|
<?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 foreach ( wp_get_schedules() as $key => $value ) : ?> |
|
111
|
|
|
<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['display'] ); ?></option> |
|
112
|
|
|
<?php endforeach; ?> |
|
113
|
|
|
</select> |
|
114
|
|
|
|
|
115
|
|
|
<span class="bd-um-pro" style="color:red"> |
|
116
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> |
|
117
|
|
|
<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> |
|
118
|
|
|
</span> |
|
119
|
|
|
</td> |
|
120
|
|
|
</tr> |
|
121
|
|
|
|
|
122
|
|
|
<tr> |
|
123
|
|
|
<td scope="row" colspan="2"> |
|
124
|
|
|
<?php |
|
125
|
|
|
_e( 'Enter time in <strong>Y-m-d H:i:s</strong> format or enter <strong>now</strong> to use current time', 'bulk-delete' ); |
|
126
|
|
|
|
|
127
|
|
|
$markup = __( 'Want to add new a Cron schedule?', 'bulk-delete' ) . ' ' . |
|
128
|
|
|
'<a href="https://bulkwp.com/docs/add-a-new-cron-schedule/" target="_blank" rel="noopener">' . __( 'Find out how', 'bulk-delete' ) . '</a>'; |
|
129
|
|
|
|
|
130
|
|
|
$content = __( 'Learn how to add your desired Cron schedule.', 'bulk-delete' ); |
|
131
|
|
|
echo ' ', bd_generate_help_tooltip( $markup, $content ); |
|
132
|
|
|
?> |
|
133
|
|
|
</td> |
|
134
|
|
|
</tr> |
|
135
|
|
|
|
|
136
|
|
|
</table> |
|
137
|
|
|
</fieldset> |
|
138
|
|
|
|
|
139
|
|
|
<?php $this->render_submit_button(); ?> |
|
140
|
|
|
<!-- User Meta box end--> |
|
141
|
|
|
<?php |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
|
145
|
|
|
$options['selected_roles'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_roles' ) ); |
|
146
|
|
|
$options['use_value'] = sanitize_text_field( bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ) ); |
|
147
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) ); |
|
148
|
|
|
$options['meta_value'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value', '' ) ); |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Delete user-meta delete options filter. |
|
152
|
|
|
* |
|
153
|
|
|
* This filter is for processing filtering options for deleting user meta. |
|
154
|
|
|
* |
|
155
|
|
|
* @since 5.4 |
|
156
|
|
|
*/ |
|
157
|
|
|
return apply_filters( 'bd_delete_user_meta_options', $options, $request ); |
|
158
|
|
|
} |
|
159
|
11 |
|
|
|
160
|
11 |
|
protected function do_delete( $options ) { |
|
161
|
11 |
|
$count = 0; |
|
162
|
11 |
|
$meta_key = $options['meta_key']; |
|
163
|
11 |
|
$use_value = $options['use_value']; |
|
164
|
|
|
$limit_to = $options['limit_to']; |
|
165
|
|
|
|
|
166
|
11 |
|
$args = array( |
|
167
|
|
|
'role__in' => $options['selected_roles'], |
|
168
|
|
|
); |
|
169
|
11 |
|
|
|
170
|
1 |
|
if ( $limit_to > 0 ) { |
|
171
|
|
|
$args['number'] = $limit_to; |
|
172
|
|
|
} |
|
173
|
11 |
|
|
|
174
|
|
|
if ( $use_value ) { |
|
175
|
|
|
$meta_value = $options['meta_value']; |
|
176
|
|
|
$args['meta_query'] = apply_filters( 'bd_delete_user_meta_query', array(), $options ); |
|
177
|
11 |
|
} else { |
|
178
|
|
|
$args['meta_key'] = $meta_key; |
|
179
|
|
|
} |
|
180
|
11 |
|
|
|
181
|
|
|
$users = get_users( $args ); |
|
182
|
11 |
|
|
|
183
|
9 |
|
foreach ( $users as $user ) { |
|
184
|
|
|
if ( $use_value ) { |
|
185
|
|
|
if ( delete_user_meta( $user->ID, $meta_key, $meta_value ) ) { |
|
|
|
|
|
|
186
|
|
|
$count++; |
|
187
|
|
|
} |
|
188
|
9 |
|
} else { |
|
189
|
9 |
|
if ( delete_user_meta( $user->ID, $meta_key ) ) { |
|
190
|
|
|
$count++; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
11 |
|
|
|
195
|
|
|
return $count; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
protected function append_to_js_array( $js_array ) { |
|
199
|
|
|
$js_array['validators']['delete_user_meta'] = 'noValidation'; |
|
200
|
|
|
|
|
201
|
|
|
$js_array['pre_action_msg']['delete_user_meta'] = 'deleteUMWarning'; |
|
202
|
|
|
$js_array['msg']['deleteUMWarning'] = __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ); |
|
203
|
|
|
|
|
204
|
|
|
return $js_array; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
protected function get_success_message( $items_deleted ) { |
|
208
|
|
|
/* translators: 1 Number of posts deleted */ |
|
209
|
|
|
return _n( 'Deleted user meta field from %d user', 'Deleted user meta field from %d users', $items_deleted, 'bulk-delete' ); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|