1
|
|
|
<?php |
2
|
|
|
namespace BulkWP\BulkDelete\Core\Metas\Modules; |
3
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Metas\MetasModule; |
5
|
|
|
|
6
|
|
|
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
|
|
|
protected function initialize() { |
15
|
|
|
$this->field_slug = 'um'; // Ideally it should be `meta_user`. But we are keeping it as um for backward compatibility. |
16
|
|
|
$this->meta_box_slug = 'bd-user-meta'; |
17
|
|
|
$this->action = 'delete_user_meta'; |
18
|
|
|
$this->cron_hook = 'do-bulk-delete-user-meta'; |
19
|
|
|
$this->scheduler_url = 'https://bulkwp.com/addons/bulk-delete-user-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-u'; |
20
|
|
|
$this->messages = array( |
21
|
|
|
'box_label' => __( 'Bulk Delete User Meta', 'bulk-delete' ), |
22
|
|
|
'scheduled' => __( 'User meta fields from the users with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
23
|
|
|
'cron_label' => __( 'Delete User Meta`', 'bulk-delete' ), |
24
|
|
|
'confirm_deletion' => __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ), |
25
|
|
|
'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the user meta fields that match the selected filters?', 'bulk-delete' ), |
26
|
|
|
'validation_error' => __( 'Please enter meta key', 'bulk-delete' ), |
27
|
|
|
/* translators: 1 Number of posts deleted */ |
28
|
|
|
'deleted_one' => __( 'Deleted user meta field from %d user', 'bulk-delete' ), |
29
|
|
|
/* translators: 1 Number of posts deleted */ |
30
|
|
|
'deleted_multiple' => __( 'Deleted user meta field from %d users', 'bulk-delete' ), |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Render the Modules. |
36
|
|
|
*/ |
37
|
|
|
public function render() { |
38
|
|
|
?> |
39
|
|
|
<!-- User Meta box start--> |
40
|
|
|
<fieldset class="options"> |
41
|
|
|
<h4><?php _e( 'Select the user role whose user meta fields you want to delete', 'bulk-delete' ); ?></h4> |
42
|
|
|
|
43
|
|
|
<table class="optiontable"> |
44
|
|
|
<?php $this->render_user_role_dropdown(); ?> |
45
|
|
|
</table> |
46
|
|
|
|
47
|
|
|
<h4><?php _e( 'Choose your user meta field settings', 'bulk-delete' ); ?></h4> |
48
|
|
|
<table class="optiontable"> |
49
|
|
|
<tr> |
50
|
|
|
<td> |
51
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked> |
52
|
|
|
<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> |
53
|
|
|
</td> |
54
|
|
|
</tr> |
55
|
|
|
|
56
|
|
|
<tr> |
57
|
|
|
<td> |
58
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" |
59
|
|
|
value="true" type="radio" disabled> |
60
|
|
|
<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> |
61
|
|
|
<span class="bd-um-pro" style="color:red; vertical-align: middle;"> |
62
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> |
63
|
|
|
<a href="https://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> |
64
|
|
|
</span> |
65
|
|
|
</td> |
66
|
|
|
</tr> |
67
|
|
|
|
68
|
|
|
<tr> |
69
|
|
|
<td> |
70
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php _e( 'User Meta Key ', 'bulk-delete' ); ?></label> |
71
|
|
|
<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' ); ?>" class="validate"> |
72
|
|
|
</td> |
73
|
|
|
</tr> |
74
|
|
|
</table> |
75
|
|
|
<?php |
76
|
|
|
/** |
77
|
|
|
* Add more fields to the delete user meta field form. |
78
|
|
|
* This hook can be used to add more fields to the delete user meta field form. |
79
|
|
|
* |
80
|
|
|
* @since 5.4 |
81
|
|
|
*/ |
82
|
|
|
do_action( 'bd_delete_user_meta_form' ); |
83
|
|
|
?> |
84
|
|
|
<table class="optiontable"> |
85
|
|
|
<tr> |
86
|
|
|
<td colspan="2"> |
87
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
88
|
|
|
</td> |
89
|
|
|
</tr> |
90
|
|
|
|
91
|
|
|
<?php $this->render_limit_settings(); ?> |
92
|
|
|
<?php $this->render_cron_settings(); ?> |
93
|
|
|
</table> |
94
|
|
|
</fieldset> |
95
|
|
|
|
96
|
|
|
<?php $this->render_submit_button(); ?> |
97
|
|
|
<!-- User Meta box end--> |
98
|
|
|
<?php |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
102
|
|
|
$options['selected_roles'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_roles' ) ); |
103
|
|
|
$options['use_value'] = sanitize_text_field( bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ) ); |
104
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) ); |
105
|
|
|
$options['meta_value'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value', '' ) ); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Delete user-meta delete options filter. |
109
|
|
|
* |
110
|
|
|
* This filter is for processing filtering options for deleting user meta. |
111
|
|
|
* |
112
|
|
|
* @since 5.4 |
113
|
|
|
*/ |
114
|
|
|
return apply_filters( 'bd_delete_user_meta_options', $options, $request ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function do_delete( $options ) { |
118
|
|
|
$count = 0; |
119
|
|
|
$meta_key = $options['meta_key']; |
120
|
|
|
$use_value = $options['use_value']; |
121
|
|
|
$limit_to = $options['limit_to']; |
122
|
|
|
|
123
|
|
|
$args = array( |
124
|
|
|
'role__in' => $options['selected_roles'], |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
if ( $limit_to > 0 ) { |
128
|
|
|
$args['number'] = $limit_to; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ( $use_value ) { |
132
|
|
|
$args['meta_query'] = apply_filters( 'bd_delete_user_meta_query', array(), $options ); |
133
|
|
|
} else { |
134
|
|
|
$args['meta_key'] = $meta_key; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$users = get_users( $args ); |
138
|
|
|
|
139
|
|
|
foreach ( $users as $user ) { |
140
|
|
|
if ( $use_value ) { |
141
|
|
|
if ( delete_user_meta( $user->ID, $meta_key, $options['meta_value'] ) ) { |
142
|
|
|
$count++; |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
if ( delete_user_meta( $user->ID, $meta_key ) ) { |
146
|
|
|
$count++; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $count; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function append_to_js_array( $js_array ) { |
155
|
|
|
$js_array['validators']['delete_user_meta'] = 'validateTextbox'; |
156
|
|
|
|
157
|
|
|
return $js_array; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|