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 = 'http://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
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Render the Modules. |
29
|
|
|
*/ |
30
|
|
|
public function render() { |
31
|
|
|
?> |
32
|
|
|
<!-- User Meta box start--> |
33
|
|
|
<fieldset class="options"> |
34
|
|
|
<h4><?php _e( 'Select the user role whose user meta fields you want to delete', 'bulk-delete' ); ?></h4> |
35
|
|
|
|
36
|
|
|
<table class="optiontable"> |
37
|
|
|
<?php $this->render_user_role_dropdown(); ?> |
38
|
|
|
</table> |
39
|
|
|
|
40
|
|
|
<h4><?php _e( 'Choose your user meta field settings', 'bulk-delete' ); ?></h4> |
41
|
|
|
<table class="optiontable"> |
42
|
|
|
<tr> |
43
|
|
|
<td> |
44
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked> |
45
|
|
|
<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> |
46
|
|
|
</td> |
47
|
|
|
</tr> |
48
|
|
|
|
49
|
|
|
<tr> |
50
|
|
|
<td> |
51
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" |
52
|
|
|
value="true" type="radio" disabled> |
53
|
|
|
<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> |
54
|
|
|
<span class="bd-um-pro" style="color:red; vertical-align: middle;"> |
55
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> |
56
|
|
|
<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 colspan="2"> |
80
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
81
|
|
|
</td> |
82
|
|
|
</tr> |
83
|
|
|
|
84
|
|
|
<?php $this->render_limit_settings(); ?> |
85
|
|
|
<?php $this->render_cron_settings(); ?> |
86
|
|
|
</table> |
87
|
|
|
</fieldset> |
88
|
|
|
|
89
|
|
|
<?php $this->render_submit_button(); ?> |
90
|
|
|
<!-- User Meta box end--> |
91
|
|
|
<?php |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
95
|
|
|
$options['selected_roles'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_roles' ) ); |
96
|
|
|
$options['use_value'] = sanitize_text_field( bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ) ); |
97
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) ); |
98
|
|
|
$options['meta_value'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value', '' ) ); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Delete user-meta delete options filter. |
102
|
|
|
* |
103
|
|
|
* This filter is for processing filtering options for deleting user meta. |
104
|
|
|
* |
105
|
|
|
* @since 5.4 |
106
|
|
|
*/ |
107
|
|
|
return apply_filters( 'bd_delete_user_meta_options', $options, $request ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function do_delete( $options ) { |
111
|
|
|
$count = 0; |
112
|
|
|
$meta_key = $options['meta_key']; |
113
|
|
|
$use_value = $options['use_value']; |
114
|
|
|
$limit_to = $options['limit_to']; |
115
|
|
|
|
116
|
|
|
$args = array( |
117
|
|
|
'role__in' => $options['selected_roles'], |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
if ( $limit_to > 0 ) { |
121
|
|
|
$args['number'] = $limit_to; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ( $use_value ) { |
125
|
|
|
$meta_value = $options['meta_value']; |
126
|
|
|
$args['meta_query'] = apply_filters( 'bd_delete_user_meta_query', array(), $options ); |
127
|
|
|
} else { |
128
|
|
|
$args['meta_key'] = $meta_key; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$users = get_users( $args ); |
132
|
|
|
|
133
|
|
|
foreach ( $users as $user ) { |
134
|
|
|
if ( $use_value ) { |
135
|
|
|
if ( delete_user_meta( $user->ID, $meta_key, $meta_value ) ) { |
|
|
|
|
136
|
|
|
$count++; |
137
|
|
|
} |
138
|
|
|
} else { |
139
|
|
|
if ( delete_user_meta( $user->ID, $meta_key ) ) { |
140
|
|
|
$count++; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $count; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function append_to_js_array( $js_array ) { |
149
|
|
|
$js_array['validators']['delete_user_meta'] = 'noValidation'; |
150
|
|
|
|
151
|
|
|
$js_array['pre_action_msg']['delete_user_meta'] = 'deleteUMWarning'; |
152
|
|
|
$js_array['msg']['deleteUMWarning'] = __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ); |
153
|
|
|
|
154
|
|
|
return $js_array; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function get_success_message( $items_deleted ) { |
158
|
|
|
/* translators: 1 Number of posts deleted */ |
159
|
|
|
return _n( 'Deleted user meta field from %d user', 'Deleted user meta field from %d users', $items_deleted, 'bulk-delete' ); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|