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