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 Post Meta. |
10
|
|
|
* |
11
|
|
|
* @since 6.0.0 |
12
|
|
|
*/ |
13
|
|
|
class DeletePostMetaModule extends MetasModule { |
14
|
|
|
protected function initialize() { |
15
|
|
|
$this->field_slug = 'pm'; // Ideally it should be `meta_post`. But we are keeping it as pm for backward compatibility. |
16
|
|
|
$this->meta_box_slug = 'bd-post-meta'; |
17
|
|
|
$this->action = 'delete_post_meta'; |
18
|
|
|
$this->cron_hook = 'do-bulk-delete-post-meta'; |
19
|
|
|
$this->scheduler_url = 'https://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p'; |
20
|
|
|
$this->messages = array( |
21
|
|
|
'box_label' => __( 'Bulk Delete Post Meta', 'bulk-delete' ), |
22
|
|
|
'scheduled' => __( 'Post meta fields from the posts with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
23
|
|
|
'cron_label' => __( 'Delete Post Meta', 'bulk-delete' ), |
24
|
|
|
'confirm_deletion' => __( 'Are you sure you want to delete all the post meta fields that match the selected filters?', 'bulk-delete' ), |
25
|
|
|
'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the post meta fields that match the selected filters?', 'bulk-delete' ), |
26
|
|
|
'validation_error' => __( 'Please enter meta key', 'bulk-delete' ), |
27
|
|
|
/* translators: 1 Number of posts deleted */ |
28
|
|
|
'deleted_one' => __( 'Deleted post meta field from %d post', 'bulk-delete' ), |
29
|
|
|
/* translators: 1 Number of posts deleted */ |
30
|
|
|
'deleted_multiple' => __( 'Deleted post meta field from %d posts', 'bulk-delete' ), |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Render the Modules. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function render() { |
40
|
|
|
?> |
41
|
|
|
<!-- Post Meta box start--> |
42
|
|
|
<fieldset class="options"> |
43
|
|
|
<h4><?php _e( 'Select the post type whose post meta fields you want to delete', 'bulk-delete' ); ?></h4> |
44
|
|
|
|
45
|
|
|
<table class="optiontable"> |
46
|
|
|
<?php $this->render_post_type_with_status( false ); ?> |
47
|
|
|
</table> |
48
|
|
|
|
49
|
|
|
<h4><?php _e( 'Choose your post meta field settings', 'bulk-delete' ); ?></h4> |
50
|
|
|
<table class="optiontable"> |
51
|
|
|
<tr> |
52
|
|
|
<td> |
53
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="use_key" type="radio" checked> |
54
|
|
|
<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> |
55
|
|
|
</td> |
56
|
|
|
</tr> |
57
|
|
|
|
58
|
|
|
<tr> |
59
|
|
|
<td> |
60
|
|
|
<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> |
61
|
|
|
<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> |
62
|
|
|
<span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
63
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "https://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> |
64
|
|
|
</span> |
65
|
|
|
</td> |
66
|
|
|
</tr> |
67
|
|
|
|
68
|
|
|
<tr> |
69
|
|
|
<td> |
70
|
|
|
<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> |
71
|
|
|
<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> |
72
|
|
|
<span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
73
|
|
|
<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "https://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
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php _e( 'Post Meta Key ', 'bulk-delete' ); ?></label> |
81
|
|
|
<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;"> |
82
|
|
|
<option value="starts_with">starts with</option> |
83
|
|
|
<option value="contains">contains</option> |
84
|
|
|
<option value="ends_with">ends with</option> |
85
|
|
|
</select> |
86
|
|
|
<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' ); ?>" class="validate"> |
87
|
|
|
</td> |
88
|
|
|
</tr> |
89
|
|
|
</table> |
90
|
|
|
|
91
|
|
|
<?php |
92
|
|
|
/** |
93
|
|
|
* Add more fields to the delete post meta field form. |
94
|
|
|
* This hook can be used to add more fields to the delete post meta field form. |
95
|
|
|
* |
96
|
|
|
* @since 5.4 |
97
|
|
|
*/ |
98
|
|
|
do_action( 'bd_delete_post_meta_form' ); |
99
|
|
|
?> |
100
|
|
|
|
101
|
|
|
<table class="optiontable"> |
102
|
|
|
<tr> |
103
|
|
|
<td colspan="2"> |
104
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
105
|
|
|
</td> |
106
|
|
|
</tr> |
107
|
|
|
|
108
|
|
|
<?php $this->render_restrict_settings(); ?> |
109
|
|
|
<?php $this->render_limit_settings(); ?> |
110
|
|
|
<?php $this->render_cron_settings(); ?> |
111
|
|
|
|
112
|
|
|
</table> |
113
|
|
|
</fieldset> |
114
|
|
|
|
115
|
|
|
<?php $this->render_submit_button(); ?> |
116
|
|
|
|
117
|
|
|
<!-- Post Meta box end--> |
118
|
|
|
<?php |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
122
|
|
|
$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug ) ); |
123
|
|
|
|
124
|
|
|
$options['use_value'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_use_value', 'use_key' ); |
125
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) ); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Delete post-meta delete options filter. |
129
|
|
|
* |
130
|
|
|
* This filter is for processing filtering options for deleting post meta. |
131
|
|
|
* |
132
|
|
|
* @since 5.4 |
133
|
|
|
*/ |
134
|
|
|
return apply_filters( 'bd_delete_post_meta_options', $options, $request ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function do_delete( $options ) { |
138
|
|
|
$args = $this->get_post_type_and_status_args( $options['post_type'] ); |
139
|
|
|
|
140
|
|
|
if ( $options['limit_to'] > 0 ) { |
141
|
|
|
$args['number'] = $options['limit_to']; |
142
|
|
|
} else { |
143
|
|
|
$args['nopaging'] = 'true'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ( $options['restrict'] ) { |
147
|
|
|
$args['date_query'] = array( |
148
|
|
|
array( |
149
|
|
|
'column' => 'post_date', |
150
|
|
|
$options['date_op'] => "{$options['days']} day ago", |
151
|
|
|
), |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ( 'use_key' === $options['use_value'] ) { |
156
|
|
|
$options['meta_key'] = $options['meta_key']; |
157
|
|
|
} else { |
158
|
|
|
$options['meta_query'] = apply_filters( 'bd_delete_post_meta_query', array(), $options ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$metas_deleted = 0; |
162
|
|
|
|
163
|
|
|
$post_ids = bd_query( $args ); |
164
|
|
|
foreach ( $post_ids as $post_id ) { |
165
|
|
|
if ( isset( $options['meta_key'] ) && is_array( $options['meta_key'] ) ) { |
166
|
|
|
$is_post_id_counted = false; |
167
|
|
|
foreach ( $options['meta_key'] as $meta_key ) { |
168
|
|
|
if ( delete_post_meta( $post_id, $meta_key ) ) { |
169
|
|
|
if ( $is_post_id_counted ) { |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
$metas_deleted++; |
173
|
|
|
$is_post_id_counted = true; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} else { |
177
|
|
|
if ( delete_post_meta( $post_id, $options['meta_key'] ) ) { |
178
|
|
|
$metas_deleted++; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $metas_deleted; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function append_to_js_array( $js_array ) { |
187
|
|
|
$js_array['validators'][ $this->action ] = 'validateTextbox'; |
188
|
|
|
|
189
|
|
|
return $js_array; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|