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