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 um 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
|
|
|
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
|
|
|
<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"> |
86
|
|
|
<?php _e( 'Only delete user meta field from first ', 'bulk-delete' ); ?> |
87
|
|
|
<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" |
88
|
|
|
disabled value="0" maxlength="4" size="4"> |
89
|
|
|
<?php _e( 'users.', 'bulk-delete' ); ?> |
90
|
|
|
<?php _e( 'Use this option if there are more than 1000 users and the script times out.', 'bulk-delete' ); ?> |
91
|
|
|
</td> |
92
|
|
|
</tr> |
93
|
|
|
|
94
|
|
|
<tr> |
95
|
|
|
<td> |
96
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="false" type="radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
97
|
|
|
<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> |
98
|
|
|
<?php _e( 'Schedule', 'bulk-delete' ); ?> |
99
|
|
|
<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> |
100
|
|
|
<?php _e( 'repeat ', 'bulk-delete' ); ?> |
101
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" disabled> |
102
|
|
|
<option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
103
|
|
|
<?php foreach ( wp_get_schedules() as $key => $value ) : ?> |
104
|
|
|
<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['display'] ); ?></option> |
105
|
|
|
<?php endforeach; ?> |
106
|
|
|
</select> |
107
|
|
|
|
108
|
|
|
<span class="bd-um-pro" style="color:red"> |
109
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> |
110
|
|
|
<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> |
111
|
|
|
</span> |
112
|
|
|
</td> |
113
|
|
|
</tr> |
114
|
|
|
|
115
|
|
|
<tr> |
116
|
|
|
<td> |
117
|
|
|
<?php _e( 'Enter time in Y-m-d H:i:s format or enter now to use current time', 'bulk-delete' ); ?> |
118
|
|
|
</td> |
119
|
|
|
</tr> |
120
|
|
|
|
121
|
|
|
</table> |
122
|
|
|
</fieldset> |
123
|
|
|
|
124
|
|
|
<?php $this->render_submit_button(); ?> |
125
|
|
|
<!-- User Meta box end--> |
126
|
|
|
<?php |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
130
|
|
|
$options['selected_roles'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_roles' ) ); |
131
|
|
|
|
132
|
|
|
$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ); |
133
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) ); |
134
|
|
|
|
135
|
|
|
return $options; |
136
|
|
|
} |
137
|
|
|
|
138
|
5 |
|
protected function do_delete( $options ) { |
139
|
5 |
|
$count = 0; |
140
|
5 |
|
$meta_key = $options['meta_key']; |
141
|
5 |
|
$use_value = $options['use_value']; |
142
|
5 |
|
$limit_to = $options['limit_to']; |
143
|
|
|
|
144
|
5 |
|
$options['role__in'] = $options['selected_roles']; |
145
|
5 |
|
unset( $options['selected_roles'] ); |
146
|
|
|
|
147
|
5 |
|
if ( $limit_to > 0 ) { |
148
|
1 |
|
$options['number'] = $limit_to; |
149
|
|
|
} |
150
|
|
|
|
151
|
5 |
|
if ( $use_value ) { |
152
|
|
|
$options['meta_query'] = apply_filters( 'bd_delete_user_meta_query', array(), $options ); |
153
|
|
|
} else { |
154
|
5 |
|
$options['meta_key'] = $meta_key; |
155
|
|
|
} |
156
|
5 |
|
$users = get_users( $options ); |
157
|
|
|
|
158
|
5 |
|
foreach ( $users as $user ) { |
159
|
5 |
|
if ( delete_user_meta( $user->ID, $meta_key ) ) { |
160
|
5 |
|
$count++; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
5 |
|
return $count; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function filter_js_array( $js_array ) { |
168
|
|
|
$js_array['dt_iterators'][] = '_' . $this->field_slug; |
169
|
|
|
$js_array['validators']['delete_user_meta'] = 'noValidation'; |
170
|
|
|
|
171
|
|
|
$js_array['pre_action_msg']['delete_user_meta'] = 'deleteUMWarning'; |
172
|
|
|
$js_array['msg']['deleteUMWarning'] = __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ); |
173
|
|
|
|
174
|
|
|
return $js_array; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected function get_success_message( $items_deleted ) { |
178
|
|
|
/* translators: 1 Number of posts deleted */ |
179
|
|
|
return _n( 'Deleted user meta field from %d user', 'Deleted user meta field from %d users', $items_deleted, 'bulk-delete' ); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|