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