1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Metas\Modules; |
4
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Metas\MetasModule; |
6
|
|
|
use BulkWP\BulkDelete\Core\Metas\QueryOverriders\DateQueryOverrider; |
7
|
|
|
|
8
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Delete Comment Meta Module. |
12
|
|
|
* |
13
|
|
|
* @since 6.0.0 |
14
|
|
|
*/ |
15
|
|
|
class DeleteCommentMetaModule extends MetasModule { |
16
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
17
|
24 |
|
protected function initialize() { |
18
|
24 |
|
$this->field_slug = 'comment_meta'; |
19
|
24 |
|
$this->meta_box_slug = 'bd-comment-meta'; |
20
|
24 |
|
$this->action = 'delete_comment_meta'; |
21
|
24 |
|
$this->cron_hook = 'do-bulk-delete-comment-meta'; |
22
|
24 |
|
$this->messages = array( |
23
|
24 |
|
'box_label' => __( 'Bulk Delete Comment Meta', 'bulk-delete' ), |
24
|
24 |
|
'scheduled' => __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
25
|
24 |
|
'cron_label' => __( 'Delete Comment Meta', 'bulk-delete' ), |
26
|
24 |
|
'confirm_deletion' => __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' ), |
27
|
24 |
|
'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the comment meta fields that match the selected filters?', 'bulk-delete' ), |
28
|
24 |
|
'validation_error' => __( 'Please enter meta key', 'bulk-delete' ), |
29
|
|
|
/* translators: 1 Number of comments deleted */ |
30
|
24 |
|
'deleted_one' => __( 'Deleted comment meta field from %d comment', 'bulk-delete' ), |
31
|
|
|
/* translators: 1 Number of comments deleted */ |
32
|
24 |
|
'deleted_multiple' => __( 'Deleted comment meta field from %d comments', 'bulk-delete' ), |
33
|
|
|
); |
34
|
|
|
|
35
|
24 |
|
$this->register_cron_hooks(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
39
|
24 |
|
public function register( $hook_suffix, $page_slug ) { |
40
|
24 |
|
parent::register( $hook_suffix, $page_slug ); |
41
|
|
|
|
42
|
24 |
|
add_filter( 'bd_delete_comment_meta_options', array( $this, 'process_filtering_options' ), 10, 2 ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Register additional module specific hooks that are needed in cron jobs. |
47
|
|
|
* |
48
|
|
|
* During a cron request, the register method is not called. So these hooks should be registered separately. |
49
|
|
|
* |
50
|
|
|
* @since 6.0.2 |
51
|
|
|
*/ |
52
|
24 |
|
protected function register_cron_hooks() { |
53
|
24 |
|
add_filter( 'bd_delete_comment_meta_query', array( $this, 'change_meta_query' ), 10, 2 ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Render the Delete Comment Meta box. |
58
|
|
|
*/ |
59
|
|
|
public function render() { |
60
|
|
|
?> |
61
|
|
|
<!-- Comment Meta box start--> |
62
|
|
|
<fieldset class="options"> |
63
|
|
|
<h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4> |
64
|
|
|
<table class="optiontable"> |
65
|
|
|
<?php $this->render_post_type_with_status( false ); ?> |
66
|
|
|
</table> |
67
|
|
|
|
68
|
|
|
<h4><?php _e( 'Choose your comment meta field settings', 'bulk-delete' ); ?></h4> |
69
|
|
|
<table class="optiontable"> |
70
|
|
|
<tr> |
71
|
|
|
<td> |
72
|
|
|
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" class="use-value" value="false" type="radio" checked> |
73
|
|
|
<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> |
74
|
|
|
</td> |
75
|
|
|
</tr> |
76
|
|
|
|
77
|
|
|
<tr> |
78
|
|
|
<td> |
79
|
|
|
<input type="radio" class="use-value" value="true" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"> |
80
|
|
|
|
81
|
|
|
<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> |
82
|
|
|
</td> |
83
|
|
|
</tr> |
84
|
|
|
|
85
|
|
|
<tr> |
86
|
|
|
<td> |
87
|
|
|
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label> |
88
|
|
|
<input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>" class="validate"> |
89
|
|
|
</td> |
90
|
|
|
</tr> |
91
|
|
|
|
92
|
|
|
<?php $this->render_meta_value_filter(); ?> |
93
|
|
|
</table> |
94
|
|
|
|
95
|
|
|
<table class="optiontable"> |
96
|
|
|
<tr> |
97
|
|
|
<td colspan="2"> |
98
|
|
|
<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
99
|
|
|
</td> |
100
|
|
|
</tr> |
101
|
|
|
|
102
|
|
|
<?php $this->render_restrict_settings( 'comments' ); ?> |
103
|
|
|
<?php $this->render_limit_settings(); ?> |
104
|
|
|
<?php $this->render_cron_settings(); ?> |
105
|
|
|
|
106
|
|
|
</table> |
107
|
|
|
</fieldset> |
108
|
|
|
|
109
|
|
|
<?php |
110
|
|
|
$this->render_submit_button( |
111
|
|
|
[ |
|
|
|
|
112
|
|
|
'class' => 'value-filters visually-hidden', |
113
|
|
|
'label' => __( 'Comment Meta Value', 'bulk-delete' ), |
114
|
|
|
] |
115
|
|
|
); |
116
|
|
|
?> |
117
|
|
|
|
118
|
|
|
<!-- Comment Meta box end--> |
119
|
|
|
<?php |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
123
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
124
|
|
|
$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug ) ); |
125
|
|
|
|
126
|
|
|
$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ); |
127
|
|
|
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) ); |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Delete comment-meta delete options filter. |
131
|
|
|
* |
132
|
|
|
* This filter is for processing filtering options for deleting comment meta. |
133
|
|
|
* |
134
|
|
|
* @since 5.4 |
135
|
|
|
*/ |
136
|
|
|
return apply_filters( 'bd_delete_comment_meta_options', $options, $request ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
140
|
24 |
|
protected function do_delete( $options ) { |
141
|
24 |
|
$args = $this->get_post_type_and_status_args( $options['post_type'] ); |
142
|
|
|
|
143
|
24 |
|
if ( $options['limit_to'] > 0 ) { |
144
|
1 |
|
$args['number'] = $options['limit_to']; |
145
|
|
|
} |
146
|
|
|
|
147
|
24 |
|
if ( $options['restrict'] ) { |
148
|
2 |
|
$args['date_query'] = array( |
149
|
|
|
array( |
150
|
2 |
|
'column' => 'comment_date', |
151
|
2 |
|
$options['date_op'] => "{$options['days']} day ago", |
152
|
|
|
), |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
24 |
|
if ( $options['use_value'] ) { |
157
|
17 |
|
$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options ); |
158
|
|
|
} else { |
159
|
7 |
|
$args['meta_key'] = $options['meta_key']; |
160
|
|
|
} |
161
|
|
|
|
162
|
24 |
|
$meta_deleted = 0; |
163
|
24 |
|
$comments = get_comments( $args ); |
164
|
24 |
|
do_action( 'bd_after_meta_query' ); |
165
|
|
|
|
166
|
24 |
|
foreach ( $comments as $comment ) { |
167
|
|
|
// Todo: Don't delete all meta rows if there are duplicate meta keys. |
168
|
|
|
// See https://github.com/sudar/bulk-delete/issues/515 for details. |
169
|
24 |
|
if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) { |
170
|
24 |
|
$meta_deleted ++; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
24 |
|
return $meta_deleted; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
178
|
|
|
protected function append_to_js_array( $js_array ) { |
179
|
|
|
$js_array['validators'][ $this->action ] = 'validateTextbox'; |
180
|
|
|
|
181
|
|
|
return $js_array; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Process additional delete options. |
186
|
|
|
* |
187
|
|
|
* This function was originally part of the Bulk Delete Comment Meta add-on. |
188
|
|
|
* |
189
|
|
|
* @since 0.1 of Bulk Delete Comment Meta add-on |
190
|
|
|
* |
191
|
|
|
* @param array $delete_options Delete options array. |
192
|
|
|
* @param array $post The POST array. |
193
|
|
|
* |
194
|
|
|
* @return array Processed delete options array. |
195
|
|
|
*/ |
196
|
|
|
public function process_filtering_options( $delete_options, $post ) { |
197
|
|
|
if ( 'true' === bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) { |
198
|
|
|
$delete_options['meta_op'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_operator', '=' ); |
199
|
|
|
$delete_options['meta_type'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' ); |
200
|
|
|
$delete_options['meta_value'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' ); |
201
|
|
|
$delete_options['relative_date'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_relative_date', '' ); |
202
|
|
|
$delete_options['date_unit'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_unit', '' ); |
203
|
|
|
$delete_options['date_type'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_type', '' ); |
204
|
|
|
$delete_options['meta_value_date_format'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_format' ); |
205
|
|
|
$delete_options['whose_meta'] = 'comment'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $delete_options; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Change the meta query. |
213
|
|
|
* |
214
|
|
|
* This function was originally part of the Bulk Delete Comment Meta add-on. |
215
|
|
|
* |
216
|
|
|
* @since 0.1 of Bulk Delete Comment Meta add-on |
217
|
|
|
* |
218
|
|
|
* @param array $meta_query Meta query. |
219
|
|
|
* @param array $delete_options List of options chosen by the user. |
220
|
|
|
* |
221
|
|
|
* @return array Modified meta query. |
222
|
|
|
*/ |
223
|
17 |
|
public function change_meta_query( $meta_query, $delete_options ) { |
224
|
|
|
$query_vars = array( |
225
|
17 |
|
'key' => $delete_options['meta_key'], |
226
|
17 |
|
'compare' => $delete_options['meta_op'], |
227
|
|
|
); |
228
|
17 |
|
if ( array_key_exists( 'meta_type', $delete_options ) ) { |
229
|
17 |
|
$query_vars['type'] = $delete_options['meta_type']; |
230
|
|
|
} |
231
|
17 |
|
if ( in_array( $delete_options['meta_op'], array( 'EXISTS', 'NOT EXISTS' ), true ) ) { |
232
|
1 |
|
$meta_query = array( $query_vars ); |
233
|
|
|
|
234
|
1 |
|
return $meta_query; |
235
|
|
|
} |
236
|
16 |
|
if ( 'DATE' === $delete_options['meta_type'] ) { |
237
|
7 |
|
$bd_date_handler = new DateQueryOverrider(); |
238
|
7 |
|
$meta_query = $bd_date_handler->get_query( $delete_options ); |
239
|
|
|
|
240
|
7 |
|
return $meta_query; |
241
|
|
|
} |
242
|
9 |
|
switch ( $delete_options['meta_op'] ) { |
243
|
|
|
case 'IN': |
244
|
2 |
|
$meta_value = explode( ',', $delete_options['meta_value'] ); |
245
|
2 |
|
break; |
246
|
|
|
case 'BETWEEN': |
247
|
1 |
|
$meta_value = explode( ',', $delete_options['meta_value'] ); |
248
|
1 |
|
break; |
249
|
|
|
default: |
250
|
6 |
|
$meta_value = $delete_options['meta_value']; |
251
|
|
|
} |
252
|
|
|
|
253
|
9 |
|
$query_vars['value'] = $meta_value; |
254
|
9 |
|
$meta_query = array( $query_vars ); |
255
|
|
|
|
256
|
9 |
|
return $meta_query; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Hook handler. |
261
|
|
|
* |
262
|
|
|
* This function was originally part of the Bulk Delete Comment Meta add-on. |
263
|
|
|
* |
264
|
|
|
* @since 0.1 of Bulk Delete Comment Meta add-on |
265
|
|
|
* |
266
|
|
|
* @param array $delete_options Delete options array. |
267
|
|
|
*/ |
268
|
|
|
public function do_delete_comment_meta( $delete_options ) { |
269
|
|
|
do_action( 'bd_before_scheduler', $this->messages['cron_label'] ); |
270
|
|
|
$count = $this->delete( $delete_options ); |
271
|
|
|
do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count ); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.