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 Post Meta. |
10
|
|
|
* |
11
|
|
|
* @since 6.0.0 |
12
|
|
|
*/ |
13
|
|
|
class DeletePostMetaModule extends MetasModule { |
14
|
7 |
|
protected function initialize() { |
15
|
7 |
|
$this->field_slug = 'meta_post'; |
16
|
7 |
|
$this->meta_box_slug = 'bd-meta-post'; |
17
|
7 |
|
$this->action = 'delete_meta_post'; |
18
|
7 |
|
$this->cron_hook = 'do-bulk-delete-post-meta'; |
19
|
7 |
|
$this->messages = array( |
20
|
7 |
|
'box_label' => __( 'Bulk Delete Post Meta', 'bulk-delete' ), |
21
|
7 |
|
'scheduled' => __( 'Post meta fields from the posts with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
22
|
|
|
); |
23
|
7 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Render the Modules. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function render() { |
31
|
|
|
?> |
32
|
|
|
<!-- Post Meta box start--> |
33
|
|
|
<fieldset class="options"> |
34
|
|
|
<?php |
35
|
|
|
$types = get_post_types( array( |
36
|
|
|
'public' => true, |
37
|
|
|
'_builtin' => false, |
38
|
|
|
), 'names' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
array_unshift( $types, 'post' ); |
42
|
|
|
?> |
43
|
|
|
<h4><?php _e( 'Select the post type whose post meta fields you want to delete', 'bulk-delete' ); ?></h4> |
44
|
|
|
<table class="optiontable"> |
45
|
|
|
<?php |
46
|
|
|
foreach ( $types as $type ) { |
47
|
|
|
?> |
48
|
|
|
<tr> |
49
|
|
|
<td> |
50
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_post_type" value = "<?php echo $type; ?>" type = "radio" class = "smbd_<?php echo esc_attr( $this->field_slug ); ?>_post_type" <?php checked( $type, 'post' ); ?>> |
51
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_post_type"><?php echo $type; ?> </label> |
52
|
|
|
</td> |
53
|
|
|
</tr> |
54
|
|
|
<?php |
55
|
|
|
} |
56
|
|
|
?> |
57
|
|
|
</table> |
58
|
|
|
|
59
|
|
|
<h4><?php _e( 'Choose your post meta field settings', 'bulk-delete' ); ?></h4> |
60
|
|
|
<table class="optiontable"> |
61
|
|
|
<tr> |
62
|
|
|
<td> |
63
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="use_key" type="radio" checked> |
64
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on post meta key name only', 'bulk-delete' ); ?></label> |
65
|
|
|
</td> |
66
|
|
|
</tr> |
67
|
|
|
|
68
|
|
|
<tr> |
69
|
|
|
<td> |
70
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smdb_<?php echo esc_attr( $this->field_slug ); ?>_use_key_compare" value="use_key_compare" type="radio" disabled> |
71
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on post meta key name prefix or postfix', 'bulk-delete' ); ?></label> |
72
|
|
|
<span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
73
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p" target="_blank">Buy now</a> |
74
|
|
|
</span> |
75
|
|
|
</td> |
76
|
|
|
</tr> |
77
|
|
|
|
78
|
|
|
<tr> |
79
|
|
|
<td> |
80
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="use_value" type="radio" disabled> |
81
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on post meta key name and value', 'bulk-delete' ); ?></label> |
82
|
|
|
<span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
83
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p" target="_blank">Buy now</a> |
84
|
|
|
</span> |
85
|
|
|
</td> |
86
|
|
|
</tr> |
87
|
|
|
|
88
|
|
|
<tr> |
89
|
|
|
<td> |
90
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php _e( 'Post Meta Key ', 'bulk-delete' ); ?></label> |
91
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key_prefix_postfix" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key_prefix_postfix" style="display: none;"> |
92
|
|
|
<option value="starts_with">starts with</option> |
93
|
|
|
<option value="contains">contains</option> |
94
|
|
|
<option value="ends_with">ends with</option> |
95
|
|
|
</select> |
96
|
|
|
<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' ); ?>"> |
97
|
|
|
</td> |
98
|
|
|
</tr> |
99
|
|
|
</table> |
100
|
|
|
<?php |
101
|
|
|
/** |
102
|
|
|
* Add more fields to the delete post meta field form. |
103
|
|
|
* This hook can be used to add more fields to the delete post meta field form. |
104
|
|
|
* |
105
|
|
|
* @since 5.4 |
106
|
|
|
*/ |
107
|
|
|
do_action( 'bd_delete_post_meta_form' ); |
108
|
|
|
?> |
109
|
|
|
<table class="optiontable"> |
110
|
|
|
<tr> |
111
|
|
|
<td> |
112
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
113
|
|
|
</td> |
114
|
|
|
</tr> |
115
|
|
|
|
116
|
|
|
<tr> |
117
|
|
|
<td> |
118
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_restrict" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_restrict" value = "true" type = "checkbox" > |
119
|
|
|
<?php _e( 'Only restrict to posts which are ', 'bulk-delete' );?> |
120
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_op" disabled> |
121
|
|
|
<option value ="before"><?php _e( 'older than', 'bulk-delete' );?></option> |
122
|
|
|
<option value ="after"><?php _e( 'posted within last', 'bulk-delete' );?></option> |
123
|
|
|
</select> |
124
|
|
|
<input type ="textbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_days" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_days" disabled value ="0" maxlength="4" size="4"><?php _e( 'days', 'bulk-delete' );?> |
125
|
|
|
</td> |
126
|
|
|
</tr> |
127
|
|
|
|
128
|
|
|
<tr> |
129
|
|
|
<td> |
130
|
|
|
<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"> |
131
|
|
|
<?php _e( 'Only delete post meta field from first ', 'bulk-delete' );?> |
132
|
|
|
<input type ="textbox" 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( 'posts.', 'bulk-delete' );?> |
133
|
|
|
<?php _e( 'Use this option if there are more than 1000 posts and the script times out.', 'bulk-delete' ) ?> |
134
|
|
|
</td> |
135
|
|
|
</tr> |
136
|
|
|
|
137
|
|
|
<tr> |
138
|
|
|
<td> |
139
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value = "false" type = "radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
140
|
|
|
<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' ); ?> |
141
|
|
|
<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' );?> |
142
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" id = "smbd_pm_cron_freq" disabled> |
143
|
|
|
<option value = "-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
144
|
|
|
<?php |
145
|
|
|
$schedules = wp_get_schedules(); |
146
|
|
|
foreach ( $schedules as $key => $value ) { |
147
|
|
|
?> |
148
|
|
|
<option value = "<?php echo $key; ?>"><?php echo $value['display']; ?></option> |
149
|
|
|
<?php |
150
|
|
|
} |
151
|
|
|
?> |
152
|
|
|
</select> |
153
|
|
|
<span class="bd-pm-pro" style="color:red"> |
154
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p">Buy now</a> |
155
|
|
|
</span> |
156
|
|
|
</td> |
157
|
|
|
</tr> |
158
|
|
|
|
159
|
|
|
<tr> |
160
|
|
|
<td> |
161
|
|
|
<?php _e( 'Enter time in Y-m-d H:i:s format or enter now to use current time', 'bulk-delete' );?> |
162
|
|
|
</td> |
163
|
|
|
</tr> |
164
|
|
|
|
165
|
|
|
</table> |
166
|
|
|
</fieldset> |
167
|
|
|
|
168
|
|
|
<p> |
169
|
|
|
<button type="submit" name="bd_action" value="delete_meta_post" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ) ?>»</button> |
170
|
|
|
</p> |
171
|
|
|
<!-- Post Meta box end--> |
172
|
|
|
<?php |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
176
|
|
|
$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) ); |
177
|
|
|
|
178
|
|
|
$options['use_value'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_use_value', 'use_key' ); |
179
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) ); |
180
|
|
|
|
181
|
|
|
return $options; |
182
|
|
|
} |
183
|
|
|
|
184
|
7 |
|
public function delete( $options ) { |
185
|
7 |
|
$count = 0; |
186
|
|
|
$args = array( |
187
|
7 |
|
'post_type' => $options['post_type'], |
188
|
|
|
); |
189
|
|
|
|
190
|
7 |
|
if ( $options['limit_to'] > 0 ) { |
191
|
1 |
|
$args['number'] = $options['limit_to']; |
192
|
|
|
} |
193
|
|
|
|
194
|
7 |
|
$op = $options['date_op']; |
195
|
7 |
|
$days = $options['days']; |
196
|
|
|
|
197
|
7 |
|
if ( $options['restrict'] ) { |
198
|
|
|
$args['date_query'] = array( |
199
|
|
|
array( |
200
|
|
|
'column' => 'post_date', |
201
|
|
|
$op => "{$days} day ago", |
202
|
|
|
), |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
7 |
|
if ( 'use_key' === $options['use_value'] ) { |
207
|
7 |
|
$options['meta_key'] = $options['meta_key']; |
208
|
|
|
} else { |
209
|
|
|
$options['meta_query'] = apply_filters( 'bd_delete_post_meta_query', array(), $options ); |
210
|
|
|
} |
211
|
|
|
|
212
|
7 |
|
$post_ids = bd_query( $args ); |
213
|
7 |
|
foreach ( $post_ids as $post_id ) { |
214
|
7 |
|
if ( isset( $options['meta_key'] ) && is_array( $options['meta_key'] ) ) { |
215
|
|
|
$is_post_id_counted = false; |
216
|
|
|
foreach ( $options['meta_key'] as $meta_key ) { |
217
|
|
|
if ( delete_post_meta( $post_id, $meta_key ) ) { |
218
|
|
|
if ( $is_post_id_counted ) { |
219
|
|
|
continue; |
220
|
|
|
} |
221
|
|
|
$count++; |
222
|
|
|
$is_post_id_counted = true; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} else { |
226
|
7 |
|
if ( delete_post_meta( $post_id, $options['meta_key'] ) ) { |
227
|
7 |
|
$count++; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
7 |
|
return $count; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function filter_js_array( $js_array ) { |
236
|
|
|
$js_array['dt_iterators'][] = '_pm'; |
237
|
|
|
$js_array['validators']['delete_meta_post'] = 'noValidation'; |
238
|
|
|
|
239
|
|
|
$js_array['pre_action_msg']['delete_meta_post'] = 'deletePMWarning'; |
240
|
|
|
$js_array['msg']['deletePMWarning'] = __( 'Are you sure you want to delete all the post meta fields that match the selected filters?', 'bulk-delete' ); |
241
|
|
|
|
242
|
|
|
return $js_array; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
protected function get_success_message( $items_deleted ) { |
246
|
|
|
/* translators: 1 Number of posts deleted */ |
247
|
|
|
return _n( 'Deleted post meta field from %d post', 'Deleted post meta field from %d posts', $items_deleted, 'bulk-delete' ); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|