|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Utility class for deleting Post Meta. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @author Sudar |
|
8
|
|
|
* |
|
9
|
|
|
* @package BulkDelete\Meta |
|
10
|
|
|
*/ |
|
11
|
|
|
class Bulk_Delete_Post_Meta { |
|
12
|
|
|
/** |
|
13
|
|
|
* Box slug. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 5.4 |
|
16
|
|
|
*/ |
|
17
|
|
|
const BOX_POST_META = 'bd-post-meta'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Cron Hook. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 5.4 |
|
23
|
|
|
*/ |
|
24
|
|
|
const CRON_HOOK = 'do-bulk-delete-post-meta'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Register post-meta meta box for delete meta page. |
|
28
|
|
|
* |
|
29
|
|
|
* @static |
|
30
|
|
|
* |
|
31
|
|
|
* @since 5.4 |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function add_delete_post_meta_box() { |
|
34
|
|
|
$bd = BULK_DELETE(); |
|
35
|
|
|
|
|
36
|
|
|
add_meta_box( |
|
37
|
|
|
self::BOX_POST_META, |
|
38
|
|
|
__( 'Bulk Delete Post Meta', 'bulk-delete' ), |
|
39
|
|
|
array( __CLASS__, 'render_delete_post_meta_box' ), |
|
40
|
|
|
$bd->meta_page, |
|
41
|
|
|
'advanced' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Render delete post-meta meta box for delete meta page. |
|
47
|
|
|
* |
|
48
|
|
|
* @static |
|
49
|
|
|
* |
|
50
|
|
|
* @since 5.4 |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function render_delete_post_meta_box() { |
|
53
|
|
|
if ( Bulk_Delete_Meta::is_meta_box_hidden( self::BOX_POST_META ) ) { |
|
54
|
|
|
printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete_meta::META_PAGE_SLUG ); |
|
55
|
|
|
|
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
?> |
|
59
|
|
|
<!-- Post Meta box start--> |
|
60
|
|
|
<fieldset class="options"> |
|
61
|
|
|
<?php |
|
62
|
|
|
$types = get_post_types( array( |
|
63
|
|
|
'public' => true, |
|
64
|
|
|
'_builtin' => false, |
|
65
|
|
|
), 'names' |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
array_unshift( $types, 'post' ); |
|
69
|
|
|
?> |
|
70
|
|
|
<h4><?php _e( 'Select the post type whose post meta fields you want to delete', 'bulk-delete' ); ?></h4> |
|
71
|
|
|
<table class="optiontable"> |
|
72
|
|
|
<?php |
|
73
|
|
|
foreach ( $types as $type ) { |
|
74
|
|
|
?> |
|
75
|
|
|
<tr> |
|
76
|
|
|
<td> |
|
77
|
|
|
<input name="smbd_pm_post_type" value = "<?php echo $type; ?>" type = "radio" class = "smbd_pm_post_type" <?php checked( $type, 'post' ); ?>> |
|
78
|
|
|
<label for="smbd_pm_post_type"><?php echo $type; ?> </label> |
|
79
|
|
|
</td> |
|
80
|
|
|
</tr> |
|
81
|
|
|
<?php |
|
82
|
|
|
} |
|
83
|
|
|
?> |
|
84
|
|
|
</table> |
|
85
|
|
|
|
|
86
|
|
|
<h4><?php _e( 'Choose your post meta field settings', 'bulk-delete' ); ?></h4> |
|
87
|
|
|
<table class="optiontable"> |
|
88
|
|
|
<tr> |
|
89
|
|
|
<td> |
|
90
|
|
|
<input name="smbd_pm_use_value" value="use_key" type="radio" checked> |
|
91
|
|
|
<label for="smbd_pm_use_value"><?php echo __( 'Delete based on post meta key name only', 'bulk-delete' ); ?></label> |
|
92
|
|
|
</td> |
|
93
|
|
|
</tr> |
|
94
|
|
|
|
|
95
|
|
|
<tr> |
|
96
|
|
|
<td> |
|
97
|
|
|
<input name="smbd_pm_use_value" id="smdb_pm_use_key_compare" value="use_key_compare" type="radio" disabled> |
|
98
|
|
|
<label for="smbd_pm_use_value"><?php echo __( 'Delete based on post meta key name prefix or postfix', 'bulk-delete' ); ?></label> |
|
99
|
|
|
<span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
|
100
|
|
|
<?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> |
|
101
|
|
|
</span> |
|
102
|
|
|
</td> |
|
103
|
|
|
</tr> |
|
104
|
|
|
|
|
105
|
|
|
<tr> |
|
106
|
|
|
<td> |
|
107
|
|
|
<input name="smbd_pm_use_value" id="smbd_pm_use_value" value="use_value" type="radio" disabled> |
|
108
|
|
|
<label for="smbd_pm_use_value"><?php echo __( 'Delete based on post meta key name and value', 'bulk-delete' ); ?></label> |
|
109
|
|
|
<span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
|
110
|
|
|
<?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> |
|
111
|
|
|
</span> |
|
112
|
|
|
</td> |
|
113
|
|
|
</tr> |
|
114
|
|
|
|
|
115
|
|
|
<tr> |
|
116
|
|
|
<td> |
|
117
|
|
|
<label for="smbd_pm_key"><?php _e( 'Post Meta Key ', 'bulk-delete' ); ?></label> |
|
118
|
|
|
<select name="smbd_pm_key_prefix_postfix" id="smbd_pm_key_prefix_postfix" style="display: none;"> |
|
119
|
|
|
<option value="starts_with">starts with</option> |
|
120
|
|
|
<option value="contains">contains</option> |
|
121
|
|
|
<option value="ends_with">ends with</option> |
|
122
|
|
|
</select> |
|
123
|
|
|
<input name="smbd_pm_key" id="smbd_pm_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>"> |
|
124
|
|
|
</td> |
|
125
|
|
|
</tr> |
|
126
|
|
|
</table> |
|
127
|
|
|
<?php |
|
128
|
|
|
/** |
|
129
|
|
|
* Add more fields to the delete post meta field form. |
|
130
|
|
|
* This hook can be used to add more fields to the delete post meta field form. |
|
131
|
|
|
* |
|
132
|
|
|
* @since 5.4 |
|
133
|
|
|
*/ |
|
134
|
|
|
do_action( 'bd_delete_post_meta_form' ); |
|
135
|
|
|
?> |
|
136
|
|
|
<table class="optiontable"> |
|
137
|
|
|
<tr> |
|
138
|
|
|
<td> |
|
139
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
|
140
|
|
|
</td> |
|
141
|
|
|
</tr> |
|
142
|
|
|
|
|
143
|
|
|
<tr> |
|
144
|
|
|
<td> |
|
145
|
|
|
<input name="smbd_pm_restrict" id="smbd_pm_restrict" value = "true" type = "checkbox" > |
|
146
|
|
|
<?php _e( 'Only restrict to posts which are ', 'bulk-delete' );?> |
|
147
|
|
|
<select name="smbd_pm_op" id="smbd_pm_op" disabled> |
|
148
|
|
|
<option value ="before"><?php _e( 'older than', 'bulk-delete' );?></option> |
|
149
|
|
|
<option value ="after"><?php _e( 'posted within last', 'bulk-delete' );?></option> |
|
150
|
|
|
</select> |
|
151
|
|
|
<input type ="textbox" name="smbd_pm_days" id="smbd_pm_days" disabled value ="0" maxlength="4" size="4"><?php _e( 'days', 'bulk-delete' );?> |
|
152
|
|
|
</td> |
|
153
|
|
|
</tr> |
|
154
|
|
|
|
|
155
|
|
|
<tr> |
|
156
|
|
|
<td> |
|
157
|
|
|
<input name="smbd_pm_limit" id="smbd_pm_limit" value = "true" type = "checkbox"> |
|
158
|
|
|
<?php _e( 'Only delete post meta field from first ', 'bulk-delete' );?> |
|
159
|
|
|
<input type ="textbox" name="smbd_pm_limit_to" id="smbd_pm_limit_to" disabled value ="0" maxlength="4" size="4"><?php _e( 'posts.', 'bulk-delete' );?> |
|
160
|
|
|
<?php _e( 'Use this option if there are more than 1000 posts and the script times out.', 'bulk-delete' ) ?> |
|
161
|
|
|
</td> |
|
162
|
|
|
</tr> |
|
163
|
|
|
|
|
164
|
|
|
<tr> |
|
165
|
|
|
<td> |
|
166
|
|
|
<input name="smbd_pm_cron" value = "false" type = "radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
|
167
|
|
|
<input name="smbd_pm_cron" value = "true" type = "radio" id = "smbd_pm_cron" disabled > <?php _e( 'Schedule', 'bulk-delete' ); ?> |
|
168
|
|
|
<input name="smbd_pm_cron_start" id = "smbd_pm_cron_start" value = "now" type = "text" disabled><?php _e( 'repeat ', 'bulk-delete' );?> |
|
169
|
|
|
<select name="smbd_pm_cron_freq" id = "smbd_pm_cron_freq" disabled> |
|
170
|
|
|
<option value = "-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
|
171
|
|
|
<?php |
|
172
|
|
|
$schedules = wp_get_schedules(); |
|
173
|
|
|
foreach ( $schedules as $key => $value ) { |
|
174
|
|
|
?> |
|
175
|
|
|
<option value = "<?php echo $key; ?>"><?php echo $value['display']; ?></option> |
|
176
|
|
|
<?php |
|
177
|
|
|
} |
|
178
|
|
|
?> |
|
179
|
|
|
</select> |
|
180
|
|
|
<span class="bd-pm-pro" style="color:red"> |
|
181
|
|
|
<?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> |
|
182
|
|
|
</span> |
|
183
|
|
|
</td> |
|
184
|
|
|
</tr> |
|
185
|
|
|
|
|
186
|
|
|
<tr> |
|
187
|
|
|
<td> |
|
188
|
|
|
<?php _e( 'Enter time in Y-m-d H:i:s format or enter now to use current time', 'bulk-delete' );?> |
|
189
|
|
|
</td> |
|
190
|
|
|
</tr> |
|
191
|
|
|
|
|
192
|
|
|
</table> |
|
193
|
|
|
</fieldset> |
|
194
|
|
|
|
|
195
|
|
|
<p> |
|
196
|
|
|
<button type="submit" name="bd_action" value="delete_meta_post" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ) ?>»</button> |
|
197
|
|
|
</p> |
|
198
|
|
|
<!-- Post Meta box end--> |
|
199
|
|
|
<?php |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Filter JS Array and add validation hooks. |
|
204
|
|
|
* |
|
205
|
|
|
* @since 5.4 |
|
206
|
|
|
* @static |
|
207
|
|
|
* |
|
208
|
|
|
* @param array $js_array JavaScript Array |
|
209
|
|
|
* |
|
210
|
|
|
* @return array Modified JavaScript Array |
|
211
|
|
|
*/ |
|
212
|
|
|
public static function filter_js_array( $js_array ) { |
|
213
|
|
|
$js_array['dt_iterators'][] = '_pm'; |
|
214
|
|
|
$js_array['validators']['delete_meta_post'] = 'noValidation'; |
|
215
|
|
|
|
|
216
|
|
|
$js_array['pre_action_msg']['delete_meta_post'] = 'deletePMWarning'; |
|
217
|
|
|
$js_array['msg']['deletePMWarning'] = __( 'Are you sure you want to delete all the post meta fields that match the selected filters?', 'bulk-delete' ); |
|
218
|
|
|
|
|
219
|
|
|
return $js_array; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Controller for deleting post meta fields. |
|
224
|
|
|
* |
|
225
|
|
|
* @static |
|
226
|
|
|
* |
|
227
|
|
|
* @since 5.4 |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function do_delete_post_meta() { |
|
230
|
|
|
$delete_options = array(); |
|
231
|
|
|
$delete_options['post_type'] = esc_sql( bd_array_get( $_POST, 'smbd_pm_post_type', 'post' ) ); |
|
232
|
|
|
|
|
233
|
|
|
$delete_options['use_value'] = bd_array_get( $_POST, 'smbd_pm_use_value', 'use_key' ); |
|
234
|
|
|
$delete_options['meta_key'] = esc_sql( bd_array_get( $_POST, 'smbd_pm_key', '' ) ); |
|
235
|
|
|
|
|
236
|
|
|
$delete_options['limit_to'] = absint( bd_array_get( $_POST, 'smbd_pm_limit_to', 0 ) ); |
|
237
|
|
|
|
|
238
|
|
|
$delete_options['restrict'] = bd_array_get_bool( $_POST, 'smbd_pm_restrict', false ); |
|
239
|
|
|
$delete_options['op'] = esc_sql( bd_array_get( $_POST, 'smbd_pm_op', 'before' ) ); |
|
240
|
|
|
$delete_options['days'] = absint( bd_array_get( $_POST, 'smbd_pm_days', 0 ) ); |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Delete post-meta delete options filter. |
|
244
|
|
|
* This filter is for processing filtering options for deleting post meta. |
|
245
|
|
|
* |
|
246
|
|
|
* @since 5.4 |
|
247
|
|
|
*/ |
|
248
|
|
|
$delete_options = apply_filters( 'bd_delete_post_meta_options', $delete_options, $_POST ); |
|
249
|
|
|
|
|
250
|
|
|
if ( 'true' == bd_array_get( $_POST, 'smbd_pm_cron', 'false' ) ) { |
|
251
|
|
|
$freq = $_POST['smbd_pm_cron_freq']; |
|
252
|
|
|
$time = strtotime( $_POST['smbd_pm_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
|
253
|
|
|
|
|
254
|
|
|
if ( $freq == -1 ) { |
|
255
|
|
|
wp_schedule_single_event( $time, self::CRON_HOOK, array( $delete_options ) ); |
|
256
|
|
|
} else { |
|
257
|
|
|
wp_schedule_event( $time, $freq, self::CRON_HOOK, array( $delete_options ) ); |
|
258
|
|
|
} |
|
259
|
|
|
$msg = __( 'Post meta fields from the posts with the selected criteria are scheduled for deletion.', 'bulk-delete' ) . ' ' . |
|
260
|
|
|
sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG ); |
|
261
|
|
|
} else { |
|
262
|
|
|
$deleted_count = self::delete_post_meta( $delete_options ); |
|
263
|
|
|
$msg = sprintf( _n( 'Deleted post meta field from %d post', 'Deleted post meta field from %d posts' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
add_settings_error( |
|
267
|
|
|
Bulk_Delete_Meta::META_PAGE_SLUG, |
|
268
|
|
|
'deleted-posts', |
|
269
|
|
|
$msg, |
|
270
|
|
|
'updated' |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Delete Post Meta. |
|
276
|
|
|
* |
|
277
|
|
|
* @static |
|
278
|
|
|
* |
|
279
|
|
|
* @since 5.4 |
|
280
|
|
|
* |
|
281
|
|
|
* @param array $delete_options Options for deleting |
|
282
|
|
|
* |
|
283
|
|
|
* @return int Number of posts that were deleted |
|
284
|
|
|
*/ |
|
285
|
|
|
public static function delete_post_meta( $delete_options ) { |
|
286
|
|
|
$count = 0; |
|
287
|
|
|
$post_type = $delete_options['post_type']; |
|
288
|
|
|
$limit_to = $delete_options['limit_to']; |
|
289
|
|
|
$meta_key = $delete_options['meta_key']; |
|
290
|
|
|
$use_value = $delete_options['use_value']; |
|
291
|
|
|
$restrict = $delete_options['restrict']; |
|
292
|
|
|
$days = $delete_options['days']; |
|
293
|
|
|
$op = $delete_options['op']; |
|
294
|
|
|
|
|
295
|
|
|
$options = array( |
|
296
|
|
|
'post_status' => 'publish', |
|
297
|
|
|
'post_type' => $post_type, |
|
298
|
|
|
); |
|
299
|
|
|
|
|
300
|
|
|
if ( $limit_to > 0 ) { |
|
301
|
|
|
$options['showposts'] = $limit_to; |
|
302
|
|
|
} else { |
|
303
|
|
|
$options['nopaging'] = 'true'; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
if ( $restrict ) { |
|
307
|
|
|
$options['date_query'] = array( |
|
308
|
|
|
array( |
|
309
|
|
|
'column' => 'post_date', |
|
310
|
|
|
$op => "{$days} day ago", |
|
311
|
|
|
), |
|
312
|
|
|
); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
if ( 'use_key' === $use_value ) { |
|
316
|
|
|
$options['meta_key'] = $meta_key; |
|
317
|
|
|
} else { |
|
318
|
|
|
$options['meta_query'] = apply_filters( 'bd_delete_post_meta_query', array(), $delete_options ); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$post_ids = bd_query( $options ); |
|
322
|
|
|
foreach ( $post_ids as $post_id ) { |
|
323
|
|
|
if ( isset( $delete_options['meta_keys'] ) && is_array( $delete_options['meta_keys'] ) ) { |
|
324
|
|
|
$is_post_id_counted = false; |
|
325
|
|
|
foreach ( $delete_options['meta_keys'] as $meta_key ) { |
|
326
|
|
|
if ( delete_post_meta( $post_id, $meta_key ) ) { |
|
327
|
|
|
if ( $is_post_id_counted ) { |
|
328
|
|
|
continue; |
|
329
|
|
|
} |
|
330
|
|
|
$count++; |
|
331
|
|
|
$is_post_id_counted = true; |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
} else { |
|
335
|
|
|
if ( delete_post_meta( $post_id, $meta_key ) ) { |
|
336
|
|
|
$count++; |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
return $count; |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
// hooks |
|
346
|
1 |
|
add_action( 'bd_add_meta_box_for_meta', array( 'Bulk_Delete_Post_Meta', 'add_delete_post_meta_box' ) ); |
|
347
|
1 |
|
add_action( 'bd_delete_meta_post', array( 'Bulk_Delete_Post_Meta', 'do_delete_post_meta' ) ); |
|
348
|
|
|
|
|
349
|
1 |
|
add_filter( 'bd_javascript_array', array( 'Bulk_Delete_Post_Meta', 'filter_js_array' ) ); |
|
350
|
|
|
?> |
|
351
|
|
|
|