1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Metas\Modules; |
4
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Metas\MetasModule; |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Delete Comment Meta. |
11
|
|
|
* |
12
|
|
|
* @since 6.0.0 |
13
|
|
|
*/ |
14
|
|
|
class DeleteCommentMetaModule extends MetasModule { |
15
|
|
|
protected function initialize() { |
16
|
|
|
$this->field_slug = 'comment_meta'; |
17
|
|
|
$this->meta_box_slug = 'bd-comment-meta'; |
18
|
|
|
$this->action = 'delete_comment_meta'; |
19
|
|
|
$this->cron_hook = 'do-bulk-delete-comment-meta'; |
20
|
|
|
$this->messages = array( |
21
|
|
|
'box_label' => __( 'Bulk Delete Comment Meta', 'bulk-delete' ), |
22
|
|
|
'scheduled' => __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
23
|
|
|
'cron_label' => __( 'Delete Comment Meta', 'bulk-delete' ), |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function register( $hook_suffix, $page_slug ) { |
28
|
|
|
parent::register( $hook_suffix, $page_slug ); |
29
|
|
|
|
30
|
|
|
add_action( 'bd_delete_comment_meta_form', array( $this, 'add_filtering_options' ) ); |
31
|
|
|
|
32
|
|
|
add_filter( 'bd_delete_comment_meta_options', array( $this, 'process_filtering_options' ), 10, 2 ); |
33
|
|
|
add_filter( 'bd_delete_comment_meta_query', array( $this, 'change_meta_query' ), 10, 2 ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function render() { |
37
|
|
|
?> |
38
|
|
|
<!-- Comment Meta box start--> |
39
|
|
|
<fieldset class="options"> |
40
|
|
|
<h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4> |
41
|
|
|
<table class="optiontable"> |
42
|
|
|
<?php $this->render_post_type_dropdown(); ?> |
43
|
|
|
</table> |
44
|
|
|
|
45
|
|
|
<h4><?php _e( 'Choose your comment meta field settings', 'bulk-delete' ); ?></h4> |
46
|
|
|
<table class="optiontable"> |
47
|
|
|
<tr> |
48
|
|
|
<td> |
49
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked> |
50
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on comment meta key name only', 'bulk-delete' ); ?></label> |
51
|
|
|
</td> |
52
|
|
|
</tr> |
53
|
|
|
|
54
|
|
|
<tr> |
55
|
|
|
<td> |
56
|
|
|
<input type="radio" value="true" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" |
57
|
|
|
id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"> |
58
|
|
|
|
59
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on comment meta key name and value', 'bulk-delete' ); ?></label> |
60
|
|
|
</td> |
61
|
|
|
</tr> |
62
|
|
|
|
63
|
|
|
<tr> |
64
|
|
|
<td> |
65
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label> |
66
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" |
67
|
|
|
id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" |
68
|
|
|
placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>"> |
69
|
|
|
</td> |
70
|
|
|
</tr> |
71
|
|
|
</table> |
72
|
|
|
|
73
|
|
|
<?php |
74
|
|
|
/** |
75
|
|
|
* Add more fields to the delete comment meta field form. |
76
|
|
|
* This hook can be used to add more fields to the delete comment meta field form. |
77
|
|
|
* |
78
|
|
|
* @since 5.4 |
79
|
|
|
*/ |
80
|
|
|
do_action( 'bd_delete_comment_meta_form' ); |
81
|
|
|
?> |
82
|
|
|
<table class="optiontable"> |
83
|
|
|
<tr> |
84
|
|
|
<td colspan="2"> |
85
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
86
|
|
|
</td> |
87
|
|
|
</tr> |
88
|
|
|
|
89
|
|
|
<?php $this->render_restrict_settings(); ?> |
90
|
|
|
<?php $this->render_limit_settings(); ?> |
91
|
|
|
<?php $this->render_cron_settings(); ?> |
92
|
|
|
|
93
|
|
|
</table> |
94
|
|
|
</fieldset> |
95
|
|
|
|
96
|
|
|
<?php $this->render_submit_button(); ?> |
97
|
|
|
|
98
|
|
|
<!-- Comment Meta box end--> |
99
|
|
|
<?php |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
103
|
|
|
$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) ); |
104
|
|
|
|
105
|
|
|
$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ); |
106
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) ); |
107
|
|
|
|
108
|
|
|
return $options; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function do_delete( $options ) { |
112
|
|
|
$args = array( |
113
|
|
|
'post_type' => $options['post_type'], |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
if ( $options['limit_to'] > 0 ) { |
117
|
|
|
$args['number'] = $options['limit_to']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$op = $options['date_op']; |
121
|
|
|
$days = $options['days']; |
122
|
|
|
|
123
|
|
|
if ( $options['restrict'] ) { |
124
|
|
|
$args['date_query'] = array( |
125
|
|
|
array( |
126
|
|
|
'column' => 'comment_date', |
127
|
|
|
$op => "{$days} day ago", |
128
|
|
|
), |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ( $options['use_value'] ) { |
133
|
|
|
$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options ); |
134
|
|
|
} else { |
135
|
|
|
$args['meta_key'] = $options['meta_key']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$meta_deleted = 0; |
139
|
|
|
$comments = get_comments( $args ); |
140
|
|
|
|
141
|
|
|
foreach ( $comments as $comment ) { |
142
|
|
|
if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) { |
143
|
|
|
$meta_deleted ++; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $meta_deleted; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function filter_js_array( $js_array ) { |
151
|
|
|
$js_array['dt_iterators'][] = '_' . $this->field_slug; |
152
|
|
|
$js_array['validators'][ $this->action ] = 'noValidation'; |
153
|
|
|
|
154
|
|
|
$js_array['pre_action_msg'][ $this->action ] = 'deleteCMWarning'; |
155
|
|
|
$js_array['msg']['deleteCMWarning'] = __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' ); |
156
|
|
|
|
157
|
|
|
return $js_array; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function get_success_message( $items_deleted ) { |
161
|
|
|
/* translators: 1 Number of posts deleted */ |
162
|
|
|
return _n( 'Deleted comment meta field from %d comment', 'Deleted comment meta field from %d comments', $items_deleted, 'bulk-delete' ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Append filtering options to the delete comment meta form. |
167
|
|
|
* |
168
|
|
|
* This function was originally part of the Bulk Delete Comment Meta add-on. |
169
|
|
|
* |
170
|
|
|
* @since 0.1 of Bulk Delete Comment Meta add-on |
171
|
|
|
*/ |
172
|
|
|
public function add_filtering_options() { |
173
|
|
|
?> |
174
|
|
|
<table class="optiontable" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_filters" style="display:none;"> |
175
|
|
|
<tr> |
176
|
|
|
<td> |
177
|
|
|
<?php _e( 'Comment Meta Value ', 'bulk-delete' ); ?> |
178
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op"> |
179
|
|
|
<option value="="><?php _e( 'equal to', 'bulk-delete' ); ?></option> |
180
|
|
|
<option value="!="><?php _e( 'not equal to', 'bulk-delete' ); ?></option> |
181
|
|
|
<option value="<"><?php _e( 'less than', 'bulk-delete' ); ?></option> |
182
|
|
|
<option value="<="><?php _e( 'less than or equal to', 'bulk-delete' ); ?></option> |
183
|
|
|
<option value=">"><?php _e( 'greater than', 'bulk-delete' ); ?></option> |
184
|
|
|
<option value=">="><?php _e( 'greater than or equal to', 'bulk-delete' ); ?></option> |
185
|
|
|
<option value="LIKE"><?php _e( 'like', 'bulk-delete' ); ?></option> |
186
|
|
|
<option value="NOT LIKE"><?php _e( 'not like', 'bulk-delete' ); ?></option> |
187
|
|
|
</select> |
188
|
|
|
<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type"> |
189
|
|
|
<option value="CHAR"><?php _e( 'CHAR', 'bulk-delete' ); ?></option> |
190
|
|
|
<option value="NUMERIC"><?php _e( 'NUMERIC', 'bulk-delete' ); ?></option> |
191
|
|
|
<option value="DECIMAL"><?php _e( 'DECIMAL', 'bulk-delete' ); ?></option> |
192
|
|
|
<option value="SIGNED"><?php _e( 'SIGNED', 'bulk-delete' ); ?></option> |
193
|
|
|
<option value="UNSIGNED"><?php _e( 'UNSIGNED', 'bulk-delete' ); ?></option> |
194
|
|
|
<option value="DATE"><?php _e( 'DATE', 'bulk-delete' ); ?></option> |
195
|
|
|
<option value="TIME"><?php _e( 'TIME', 'bulk-delete' ); ?></option> |
196
|
|
|
<option value="DATETIME"><?php _e( 'DATETIME', 'bulk-delete' ); ?></option> |
197
|
|
|
<option value="BINARY"><?php _e( 'BINARY', 'bulk-delete' ); ?></option> |
198
|
|
|
</select> |
199
|
|
|
|
200
|
|
|
<input type="text" placeholder="<?php _e( 'Meta Value', 'bulk-delete' ); ?>" |
201
|
|
|
name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" |
202
|
|
|
id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"> |
203
|
|
|
</td> |
204
|
|
|
</tr> |
205
|
|
|
</table> |
206
|
|
|
<?php |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Process additional delete options. |
211
|
|
|
* |
212
|
|
|
* This function was originally part of the Bulk Delete Comment Meta add-on. |
213
|
|
|
* |
214
|
|
|
* @since 0.1 of Bulk Delete Comment Meta add-on |
215
|
|
|
* |
216
|
|
|
* @param array $delete_options Delete options array. |
217
|
|
|
* @param array $post The POST array. |
218
|
|
|
* |
219
|
|
|
* @return array Processed delete options array. |
220
|
|
|
*/ |
221
|
|
|
public function process_filtering_options( $delete_options, $post ) { |
222
|
|
|
if ( 'true' == bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) { |
223
|
|
|
$delete_options['meta_op'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_meta_op', '=' ); |
224
|
|
|
$delete_options['meta_type'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' ); |
225
|
|
|
$delete_options['meta_value'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $delete_options; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Change the meta query. |
233
|
|
|
* |
234
|
|
|
* This function was originally part of the Bulk Delete Comment Meta add-on. |
235
|
|
|
* |
236
|
|
|
* @since 0.1 of Bulk Delete Comment Meta add-on |
237
|
|
|
* |
238
|
|
|
* @param array $meta_query Meta query. |
239
|
|
|
* @param array $delete_options List of options chosen by the user. |
240
|
|
|
* |
241
|
|
|
* @return array Modified meta query. |
242
|
|
|
*/ |
243
|
|
|
public function change_meta_query( $meta_query, $delete_options ) { |
244
|
|
|
$meta_query = array( |
245
|
|
|
array( |
246
|
|
|
'key' => $delete_options['meta_key'], |
247
|
|
|
'value' => $delete_options['meta_value'], |
248
|
|
|
'compare' => $delete_options['meta_op'], |
249
|
|
|
'type' => $delete_options['meta_type'], |
250
|
|
|
), |
251
|
|
|
); |
252
|
|
|
|
253
|
|
|
return $meta_query; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Hook handler. |
258
|
|
|
* |
259
|
|
|
* This function was originally part of the Bulk Delete Comment Meta add-on. |
260
|
|
|
* |
261
|
|
|
* @since 0.1 of Bulk Delete Comment Meta add-on |
262
|
|
|
* |
263
|
|
|
* @param array $delete_options Delete options array. |
264
|
|
|
*/ |
265
|
|
|
public function do_delete_comment_meta( $delete_options ) { |
266
|
|
|
do_action( 'bd_before_scheduler', $this->messages['cron_label'] ); |
267
|
|
|
$count = $this->delete( $delete_options ); |
268
|
|
|
do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count ); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|