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