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
|
|
|
$this->render_user_role_dropdown(); |
39
|
|
|
?> |
40
|
|
|
</table> |
41
|
|
|
|
42
|
|
|
<h4><?php _e( 'Choose your user meta field settings', 'bulk-delete' ); ?></h4> |
43
|
|
|
<table class="optiontable"> |
44
|
|
|
<tr> |
45
|
|
|
<td> |
46
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked> |
47
|
|
|
<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> |
48
|
|
|
</td> |
49
|
|
|
</tr> |
50
|
|
|
|
51
|
|
|
<tr> |
52
|
|
|
<td> |
53
|
|
|
<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> |
54
|
|
|
<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> |
55
|
|
|
<span class="bd-um-pro" style="color:red; vertical-align: middle;"> |
56
|
|
|
<?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> |
57
|
|
|
</span> |
58
|
|
|
</td> |
59
|
|
|
</tr> |
60
|
|
|
|
61
|
|
|
<tr> |
62
|
|
|
<td> |
63
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php _e( 'User Meta Key ', 'bulk-delete' ); ?></label> |
64
|
|
|
<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' ); ?>"> |
65
|
|
|
</td> |
66
|
|
|
</tr> |
67
|
|
|
</table> |
68
|
|
|
<?php |
69
|
|
|
/** |
70
|
|
|
* Add more fields to the delete user meta field form. |
71
|
|
|
* This hook can be used to add more fields to the delete user meta field form. |
72
|
|
|
* |
73
|
|
|
* @since 5.4 |
74
|
|
|
*/ |
75
|
|
|
do_action( 'bd_delete_user_meta_form' ); |
76
|
|
|
?> |
77
|
|
|
<table class="optiontable"> |
78
|
|
|
<tr> |
79
|
|
|
<td> |
80
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
81
|
|
|
</td> |
82
|
|
|
</tr> |
83
|
|
|
|
84
|
|
|
<tr> |
85
|
|
|
<td> |
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
|
|
|
<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' );?> |
89
|
|
|
<?php _e( 'Use this option if there are more than 1000 users and the script times out.', 'bulk-delete' ) ?> |
90
|
|
|
</td> |
91
|
|
|
</tr> |
92
|
|
|
|
93
|
|
|
<tr> |
94
|
|
|
<td> |
95
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value = "false" type = "radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
96
|
|
|
<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' ); ?> |
97
|
|
|
<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' );?> |
98
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" id = "smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" disabled> |
99
|
|
|
<option value = "-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
100
|
|
|
<?php |
101
|
|
|
$schedules = wp_get_schedules(); |
102
|
|
|
foreach ( $schedules as $key => $value ) { |
103
|
|
|
?> |
104
|
|
|
<option value = "<?php echo $key; ?>"><?php echo $value['display']; ?></option> |
105
|
|
|
<?php |
106
|
|
|
} |
107
|
|
|
?> |
108
|
|
|
</select> |
109
|
|
|
<span class="bd-um-pro" style="color:red"> |
110
|
|
|
<?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> |
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
|
|
|
</p> |
126
|
|
|
<!-- User Meta box end--> |
127
|
|
|
<?php |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
131
|
|
|
$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) ); |
132
|
|
|
|
133
|
|
|
$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ); |
134
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) ); |
135
|
|
|
|
136
|
|
|
return $options; |
137
|
|
|
} |
138
|
|
|
|
139
|
5 |
|
protected function do_delete( $options ) { |
140
|
5 |
|
$count = 0; |
141
|
5 |
|
$user_role = $options['user_role']; |
142
|
5 |
|
$meta_key = $options['meta_key']; |
143
|
5 |
|
$use_value = $options['use_value']; |
144
|
5 |
|
$limit_to = $options['limit_to']; |
145
|
|
|
|
146
|
|
|
$options = array( |
147
|
5 |
|
'role' => $user_role, |
148
|
|
|
); |
149
|
|
|
|
150
|
5 |
|
if ( $limit_to > 0 ) { |
151
|
1 |
|
$options['number'] = $limit_to; |
152
|
|
|
} |
153
|
|
|
|
154
|
5 |
|
if ( $use_value ) { |
155
|
|
|
$options['meta_query'] = apply_filters( 'bd_delete_user_meta_query', array(), $options ); |
156
|
|
|
} else { |
157
|
5 |
|
$options['meta_key'] = $meta_key; |
158
|
|
|
} |
159
|
|
|
|
160
|
5 |
|
$users = get_users( $options ); |
161
|
|
|
|
162
|
5 |
|
foreach ( $users as $user ) { |
163
|
5 |
|
if ( delete_user_meta( $user->ID, $meta_key ) ) { |
164
|
5 |
|
$count++; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
5 |
|
return $count; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function filter_js_array( $js_array ) { |
172
|
|
|
$js_array['dt_iterators'][] = '_' . $this->field_slug; |
173
|
|
|
$js_array['validators']['delete_meta_user'] = 'noValidation'; |
174
|
|
|
|
175
|
|
|
$js_array['pre_action_msg']['delete_meta_user'] = 'deleteUMWarning'; |
176
|
|
|
$js_array['msg']['deleteUMWarning'] = __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ); |
177
|
|
|
|
178
|
|
|
return $js_array; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function get_success_message( $items_deleted ) { |
182
|
|
|
/* translators: 1 Number of posts deleted */ |
183
|
|
|
return _n( 'Deleted user meta field from %d user', 'Deleted user meta field from %d users', $items_deleted, 'bulk-delete' ); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|