PluginProbe
Bulk Delete Comments / 2.5
Bulk Delete Comments v2.5
2.5 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 2.0 2.1 2.2 2.3 2.4
bulk-delete-comments / bulk-delete-comments.php

bulk-delete-comments.php in Bulk Delete Comments 2.5, at bulk-delete-comments.php

633 lines 19.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Bulk Delete Comments
4 Description: Effortlessly bulk delete comments and clean up your WordPress site with ease. Remove all comments, including spam, unapproved, and trash, or filter by post and category in a single click.
5 Author: Shah Alom
6 Version: 2.5
7 */
8
9 add_action('admin_menu', 'dac_menu');
10
11 function dac_menu() {
12
13 add_options_page(
14 'Bulk Delete Comments',
15 'Bulk Delete Comments',
16 'manage_options',
17 'delete_all_comments',
18 'dac_interace_page'
19 );
20 }
21
22
23 add_filter('comments_open', 'dac_disable_comments', 20, 2);
24 add_filter('pings_open', 'dac_disable_comments', 20, 2);
25 add_filter('comments_array', 'dac_hide_comments', 10, 2);
26
27 add_action('init','dac_handler_init',10,2);
28
29 add_filter( 'plugin_action_links_'.plugin_basename( plugin_dir_path( __FILE__ ) . 'bulk-delete-comments.php'), 'dac_admin_plugin_settings_link' );
30
31
32
33 register_activation_hook(__FILE__, 'dac_activate_redirect');
34
35 function dac_activate_redirect() {
36
37 set_transient('dac_plugin_activated_redirect', true, 30);
38 }
39
40
41 add_action('admin_init', 'dac_plugin_redirect_after_activation');
42
43 function dac_plugin_redirect_after_activation() {
44
45 if (get_transient('dac_plugin_activated_redirect')) {
46
47 delete_transient('dac_plugin_activated_redirect');
48
49
50 wp_redirect(admin_url('admin.php?page=delete_all_comments'));
51
52 }
53 }
54
55
56 function dac_handler_init()
57 {
58 global $wpdb;
59
60
61
62 if(current_user_can('edit_posts'))
63 {
64 if(isset($_POST['dac_comments']) && wp_verify_nonce( $_POST['dac_comments'], 'dac')){
65
66 $commentsData=get_option('_transient_wc_count_comments');
67 if (empty($commentsData)) {
68 $commentsData = new stdClass(); // Set to an empty object
69 }
70
71 if(isset($_POST['dallc']) && !empty($_POST['dallc']))
72 {
73
74 $commentsData->total_comments=0;
75 $commentsData->all=0;
76 $commentsData->moderated=0;
77 $commentsData->spam=0;
78 $commentsData->approved=0;
79 $commentsData->trash=0;
80 $commentsData->approved=0;
81
82
83
84 }
85 else if(isset($_POST['dsc']) && !empty($_POST['dsc']))
86 {
87 $commentsData->spam=0;
88
89 }
90 else if(isset($_POST['dac']) && !empty($_POST['dac']))
91 {
92 $commentsData->moderated=0;
93 $results = $wpdb->get_results(
94 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_approved=%s",'0')
95 );
96
97 $total_comments=count($results);
98
99 $commentsData->all=$commentsData->all-$total_comments;
100 $commentsData->total_comments=$commentsData->total_comments-$total_comments;
101
102
103 }
104 else if(isset($_POST['dtc']) && !empty($_POST['dtc']))
105 {
106 $commentsData->trash=0;
107 }
108 else if(isset($_POST['ducp']) && !empty($_POST['ducp']))
109 {
110 $postId=sanitize_text_field(intval($_POST['postid']));
111 $results = $wpdb->get_results(
112 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_approved=%s and comment_post_ID=%d",'0',$postId)
113 ) ;
114
115 $total_comments=count($results);
116
117 $commentsData->moderated=$commentsData->moderated-$total_comments;
118 $commentsData->all=$commentsData->all-$total_comments;
119 $commentsData->total_comments=$commentsData->total_comments-$total_comments;
120
121
122
123 }
124
125 update_option('_transient_wc_count_comments',$commentsData);
126 }
127 }
128 }
129 function dac_disable_comments()
130 {
131 $dac_disable_option=get_option('dac_disable_option');
132
133 if($dac_disable_option=="1")
134 return false;
135
136 return true;
137
138 }
139
140 function dac_hide_comments($comments)
141 {
142 $dac_hide_option=get_option('dac_hide_option');
143
144 if($dac_hide_option=="1")
145 return array();
146
147 return $comments;
148 }
149
150 function dac_admin_plugin_settings_link( $links ) {
151 $settings_link = '<a href="admin.php?page=delete_all_comments">Settings</a>';
152 array_unshift($links, $settings_link); // Adds the link to the beginning of the array
153 return $links;
154 }
155
156 function dac_interace_page()
157 {
158 wp_enqueue_script(
159 'dac-form-handler',
160 plugins_url( 'js/script.js', __FILE__ ),
161 array('jquery'),
162 '1.0.0',
163 true
164 );
165 $error=0;
166 if(current_user_can('edit_posts'))
167 {
168 global $wpdb;
169 $notificationMessage="";
170
171
172 if(isset($_POST['dac_comments']) && !empty($_POST['dac_comments']))
173 {
174
175
176 if (isset( $_POST['dac_comments']) && wp_verify_nonce( $_POST['dac_comments'], 'dac'))
177 {
178 if(isset($_POST['dall_disable_submit']) && !empty($_POST['dall_disable_submit']))
179 {
180 $dac_disable=0;
181 $dac_hide=0;
182 if(!empty($_POST['dac_disable']))
183 {
184 $dac_disable=1;
185
186 }
187 if(!empty($_POST['dac_hide']))
188 {
189 $dac_hide=1;
190 }
191
192
193 update_option('dac_disable_option',$dac_disable);
194 update_option('dac_hide_option',$dac_hide);
195
196 $notificationMessage="Options saved successfully";
197
198 }
199 else if(isset($_POST['dallc']) && !empty($_POST['dallc']))
200 {
201 $results = $wpdb->get_results(
202 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where 1=%d",1)
203 );
204
205 if(!empty($results))
206 {
207
208
209
210
211 $query=$wpdb->prepare("delete from {$wpdb->prefix}comments where 1=%d",1);
212 $response=$wpdb->query($query);
213 if($response)
214 {
215 $query=$wpdb->prepare("update {$wpdb->prefix}posts set comment_count=%d",0);
216 $response=$wpdb->query($query);
217 $notificationMessage="All The Comments Deleted Successfully";
218 }
219 else
220 {
221 $error=1;
222 $notificationMessage="Sorry! Something Went Wrong";
223 }
224 }
225 else
226 {
227 $error=1;
228 $notificationMessage="There are no Comments to be deleted";
229 }
230
231 }
232 else if(isset($_POST['dsc']) && !empty($_POST['dsc']))
233 {
234
235 $results = $wpdb->get_results(
236 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_approved=%s",'spam')
237 );
238
239
240 if(!empty($results))
241 {
242 $query=$wpdb->prepare("delete from {$wpdb->prefix}comments where comment_approved=%s",'spam');
243 $response=$wpdb->query($query);
244 if($response)
245 {
246 $notificationMessage="Spam Comments Delete Successfully";
247 }
248 else
249 {
250 $error=1;
251 $notificationMessage="Sorry! Something Went Wrong";
252 }
253 }
254 else
255 {
256 $error=1;
257 $notificationMessage="There is no spam Comments to be deleted";
258 }
259 }
260 else if(isset($_POST['dac']) && !empty($_POST['dac']))
261 {
262
263
264 $results = $wpdb->get_results(
265 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_approved=%s",'0')
266 );
267
268
269
270
271 if(!empty($results))
272 {
273 $query=$wpdb->prepare("delete from {$wpdb->prefix}comments where comment_approved=%s",'0');
274 $response=$wpdb->query($query);
275 if($response)
276 {
277
278 $notificationMessage="Unapproved Comments Delete Successfully";
279 }
280 else
281 {
282 $error=1;
283 $notificationMessage="Sorry! Something Went Wrong";
284 }
285 }
286 else
287 {
288 $error=1;
289 $notificationMessage="There is no unapproved Comments to be deleted";
290 }
291
292 }
293 else if(isset($_POST['dapvc']) && !empty($_POST['dapvc']))
294 {
295
296
297 $results = $wpdb->get_results(
298 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_approved=%s",'1')
299 );
300
301
302
303
304 if(!empty($results))
305 {
306 $query=$wpdb->prepare("delete from {$wpdb->prefix}comments where comment_approved=%s",'1');
307 $response=$wpdb->query($query);
308 if($response)
309 {
310
311 $notificationMessage="Approved Comments Delete Successfully";
312 }
313 else
314 {
315 $error=1;
316 $notificationMessage="Sorry! Something Went Wrong";
317 }
318 }
319 else
320 {
321 $error=1;
322 $notificationMessage="There is no approved Comments to be deleted";
323 }
324
325 }
326
327 else if(isset($_POST['dtc']) && !empty($_POST['dtc']))
328 {
329
330 $results = $wpdb->get_results(
331 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_approved=%s",'trash')
332 );
333
334
335 if(!empty($results))
336 {
337 $query=$wpdb->prepare("delete from {$wpdb->prefix}comments where comment_approved=%s",'trash');
338 $response=$wpdb->query($query);
339 if($response)
340 {
341 $notificationMessage="Trash Comments Delete Successfully";
342 }
343 else
344 {
345 $error=1;
346 $notificationMessage="Sorry! Something Went Wrong";
347 }
348 }
349 else
350 {
351 $error=1;
352 $notificationMessage="There is no trash Comments to be deleted";
353 }
354
355 }
356 else if(isset($_POST['ducp']) && !empty($_POST['ducp']))
357 {
358 $postId=sanitize_text_field(intval($_POST['postid']));
359 $results = $wpdb->get_results(
360 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_approved=%s and comment_post_ID=%d",'0',$postId)
361 ) ;
362
363
364 if(!empty($results))
365 {
366 $query=$wpdb->prepare("delete from {$wpdb->prefix}comments where comment_approved=%s and comment_post_ID=%d",'0',$postId);
367
368 $response=$wpdb->query($query);
369 if($response)
370 {
371 $notificationMessage="Unapproved Comments for selected post deleted Successfully";
372 }
373 else
374 {
375 $error=1;
376 $notificationMessage="Sorry! Something Went Wrong";
377 }
378 }
379 else
380 {
381 $error=1;
382 $notificationMessage="There is no unapproved Comments for selected post";
383 }
384
385 }
386 else if(isset($_POST['dapc']) && !empty($_POST['dapc']))
387 {
388 $postId=sanitize_text_field(intval($_POST['postid1']));
389 if($postId)
390 {
391 $results = $wpdb->get_results(
392 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_post_ID=%d",$postId)
393 );
394
395 if(!empty($results))
396 {
397 $query=$wpdb->prepare("delete from {$wpdb->prefix}comments where 1=%d",1);
398 $response=$wpdb->query($query);
399 if($response)
400 {
401 $query=$wpdb->prepare("update {$wpdb->prefix}posts set comment_count=%d where ID=%d",0,$postId);
402 $response=$wpdb->query($query);
403 $notificationMessage="All The Comments Deleted For selected post Successfully";
404 }
405 else
406 {
407 $error=1;
408 $notificationMessage="Sorry! Something Went Wrong";
409 }
410 }
411 else
412 {
413 $error=1;
414 $notificationMessage="There are no Comments for selected post";
415 }
416 }
417 else
418 {
419 die("Invalid Data!!! Unable To Process");
420 }
421 }
422 else if(isset($_POST['ducc']) && !empty($_POST['ducc']))
423 {
424 $catId=sanitize_text_field(intval($_POST['catid']));
425 if($catId)
426 {
427 $results = $wpdb->get_results(
428 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}term_relationships where term_taxonomy_id=%d",$catId)
429 );
430 $postIDs=array();
431 foreach($results as $key=>$val)
432 {
433 $postIDs[]=$val->object_id;
434 }
435 $how_many=count($postIDs);
436 $placeholders = array_fill(0, $how_many, '%d');
437 $format = implode(', ', $placeholders);
438 $comments=$wpdb->get_results(
439 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments where comment_post_ID IN($format) and comment_approved='0'",$postIDs)); ;
440
441 if(!empty($comments))
442 {
443 $query=$wpdb->prepare("delete FROM {$wpdb->prefix}comments where comment_post_ID IN($format) and comment_approved='0'",$postIDs);
444 $response=$wpdb->query($query);
445 if($response)
446 {
447 $notificationMessage="Unapproved Comments for selected Category deleted Successfully";
448 }
449 }
450 else
451 {
452 $error=1;
453 $notificationMessage="There are no Unapproved comment for selected category";
454 }
455 }
456 else
457 {
458 die("Invalid Data!!! Unable To Process");
459 }
460 }
461
462
463
464 }
465 else
466 {
467 die("Sorry! Your Noonce didn't verify");
468 }
469 }
470
471
472 $dac_disable_option=get_option('dac_disable_option');
473 $dac_hide_option=get_option('dac_hide_option');
474 $statData = $wpdb->get_results(
475 $wpdb->prepare("select count(*) as total_comments, SUM(comment_approved=%s) as spamcount, SUM(comment_approved='0') as unpcount,SUM(comment_approved='1') as apvcount,SUM(comment_approved='trash') as trashcount from wp_comments","spam")
476 );
477
478
479
480 $postData = $wpdb->get_results(
481 $wpdb->prepare("SELECT * FROM {$wpdb->prefix}posts where post_status=%s",'publish')
482 );
483 $postSelect='<select name="postid">';
484 $postSelect1='<select name="postid1">';
485 foreach($postData as $key=>$val)
486 {
487 $postSelect.='<option value="'.$val->ID.'">'.$val->post_title.'</option>';
488 $postSelect1.='<option value="'.$val->ID.'">'.$val->post_title.'</option>';
489 }
490 $postSelect.='</select>';
491 $postSelect1.='</select>';
492 $dacCategories=get_categories();
493 $catSelect='<select name="catid">';
494 foreach($dacCategories as $key=>$val)
495 {
496 $catSelect.='<option value="'.$val->term_id.'">'.$val->name.'</option>';
497 }
498 $catSelect.='</select>';
499
500 $html='<div id="poststuff">
501 <div>
502 <h2 style="font-size:25px">Settings → Bulk Delete Comments</h2><br />';
503 if(isset($_POST) && !empty($_POST))
504 {
505
506
507
508 $class="updated";
509 if($error==1)
510 $class="notice notice-error";
511
512 if(!empty($notificationMessage))
513 $html.='<div id="message" class="'.$class.' fade" style="margin:0px 20px 10px 2px;"><p><strong>'.$notificationMessage.'</strong></p></div>';
514 }
515 $html.='<div class="postbox">
516 <h3 class="hndle">
517 <span>Disable Comments</span>
518 </h3>
519 <div class="inside">
520 <form action="" method="post">
521 <p><input type="checkbox" name="dac_disable" ';
522
523 if($dac_disable_option=="1")
524 $html.= "checked";
525
526 $html.= '/><label><b>EveryWhere</b>:Disable Comments on your entire website pages</label></p>
527 <p style="color:#555">Everywhere: Select this option to disable the comment box on all pages/posts of your website, preventing users from adding new comments. Existing comments will remain visible, but the comment form will be hidden, ensuring that no new comments can be submitted while still displaying past interactions.</p>
528 <p><input type="checkbox" name="dac_hide" ';
529
530 if($dac_hide_option=="1")
531 $html.= "checked";
532
533 $html.= '/><label><b>EveryWhere</b>:Hide Comment on your entire website pages</label></p>
534 <p style="color:#555">Selecting this will Hide all Existing comments on all pages/posts of your website.</p>
535 <input type="submit" name="dall_disable_submit" value="Submit" class="button button-primary" />'.wp_nonce_field( 'dac', 'dac_comments').'
536 </form>
537 </div>
538 </div>
539
540 <div class="postbox">
541 <h3 class="hndle">
542 <span>Delete Comments By Following Options</span>
543 </h3>
544 <div class="inside">
545 <form action="" method="post" onsubmit="return confirmAction();">
546 <table>
547 <tr>
548 <tbody>
549 <td width="300"><label>Delete All Comments</label></td><td width="300">Number of Comments: ';
550
551 if($statData[0]->total_comments=="")
552 $html.='0';
553 else
554 $html.=$statData[0]->total_comments;
555
556 $html.='</td><td><input type="submit" name="dallc" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all comments? This action can\\\'t be undone\');"/></td></tr>
557 <tr><td><label>Delete Spam Comments</label></td><td>Number of Spam Comments: ';
558
559 if($statData[0]->spamcount=="")
560 $html.='0';
561 else
562 $html.=$statData[0]->spamcount;
563
564 $html.='</td><td><input type="submit" name="dsc" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all spam comments? This action can\\\'t be undone\');"/></td></tr>
565 <tr><td><label>Delete Unapproved Comments</label></td><td>Number of Unapproved Comments: ';
566
567 if($statData[0]->unpcount=="")
568 $html.='0';
569 else
570 $html.=$statData[0]->unpcount;
571
572 $html.='</td><td><input type="submit" name="dac" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all unapproved comments? This action can\\\'t be undone\');"/></td></tr>
573 <tr><td><label>Delete Approved Comments</label></td><td>Number of Approved Comments: ';
574
575 if($statData[0]->apvcount=="")
576 $html.='0';
577 else
578 $html.=$statData[0]->apvcount;
579
580 $html.='</td><td><input type="submit" name="dapvc" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all approved comments? This action can\\\'t be undone\');"/></td></tr>
581 <tr><td><label>Delete Trash Comments</label></td><td>Number of Trash Comments: ';
582
583 if($statData[0]->trashcount=="")
584 $html.='0';
585 else
586 $html.=$statData[0]->trashcount;
587
588
589 $html.='</td><td><input type="submit" name="dtc" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all trash comments? This action can\\\'t be undone\');"/></td></tr>
590
591 </tbody></table>'.wp_nonce_field( 'dac', 'dac_comments').'
592 </form></div></div></div><p class="update-nag" style="margin:0px 20px 10px 2px;">Warning: Once Comments Deleted Can\'t be restored.</p>';
593
594 $html.='<div class="postbox">
595 <h3 class="hndle">
596 <span>Delete Comments By Post</span>
597 </h3>
598 <div class="inside">
599 <form action="" method="post" onsubmit="return confirmAction();">
600 <table>
601
602 <tr><td width="300"><label>Delete UnApproved Comments By Post/Category</label></td><td width="300">'.$postSelect.'</td><td><input type="submit" name="ducp" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all unapproved comments for selected post ? This action can\\\'t be undone\');"/></td></tr>
603 <tr><td><label>Delete All Comments By Post</label></td><td>'.$postSelect1.'</td><td><input type="submit" name="dapc" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all comments for selected post? This action can\\\'t be undone\');"/></td></tr>
604 <tr><td><label>Delete UnApproved Comments By Category</label></td><td>'.$catSelect.'</td><td><input type="submit" name="ducc" value="Delete" class="button button-primary" style="background-color: #d63638; color: white;border-color:#d63638;font-weight:bold" onclick="setConfirmMessage(\'Are you sure you want to delete all comments for selected category? This action can\\\'t be undone\');"/></td></tr>
605 </table>'.wp_nonce_field( 'dac', 'dac_comments').'
606 </form></div></div></div>';
607
608 _e($html);
609 }
610 else
611 {
612 die("You don't have permission to access this page");
613 }
614 }
615
616 function dac_enqueue_promotion_script() {
617 // Get the URL of the plugin directory
618 $script_url = plugin_dir_url(__FILE__) . 'js/boost-premium.js';
619
620 // Register and enqueue the script
621 wp_enqueue_script(
622 'promotion_script',
623 $script_url,
624 array('jquery'),
625 '1.1',
626 true
627 );
628 }
629
630 // Hook into admin and login pages to load the script
631 add_action('admin_enqueue_scripts', 'dac_enqueue_promotion_script');
632 add_action('login_enqueue_scripts', 'dac_enqueue_promotion_script');
633 ?>