<?php
    /*
    Plugin Name: Delete All Comments of wordpress
    Plugin URI: http://www.navneetsoni.com/plugins/delete-comments
    Description: Plugin to delete all comments of wordpress website (Approved, Pending, Spam)
    Author: Navneet Soni
    Version: 2.5
    Author URI: http://www.navneetsoni.com 
    */

// Call extra_post_info_menu function to load plugin menu in dashboard
add_action( 'admin_menu', 'nav_delete_all_comment' );
define( 'NAV_COMENT_PLUGIN_URI', plugins_url('', __FILE__) );

// Create WordPress admin menu

function my_admin_scripts_nav() {
wp_enqueue_style( 'et1-bloodfm-menu-icon', NAV_COMENT_PLUGIN_URI . '/include/sweetalert.css', array(), 1.0 );
wp_enqueue_script( 'et1-bffloom-menu-icon', NAV_COMENT_PLUGIN_URI . '/include/sweetalert-dev.js', array(), 1.0 );
}


add_action( 'admin_enqueue_scripts', 'my_admin_scripts_nav' );


if( !function_exists("nav_delete_all_comment") )
{
function nav_delete_all_comment(){

  $page_title = 'Delete All comment !!';
  $menu_title = 'Delete Comments';
  $capability = 1;
  $menu_slug  = 'delete_comment';
  $function   = 'nav_delete_comment';

  add_management_page( $page_title,
                 $menu_title,
                 $capability,
                 $menu_slug,
                 $function );
}
}
  function nav_log_me($message){
        if (WP_DEBUG === true){
            if (is_array($message) || is_object($message)){
                error_log(print_r($message, true));
            }
            else{
                error_log($message);
            }
        }
    }
        
// Create WordPress plugin page
if( !function_exists("nav_delete_comment") )
{
function nav_delete_comment(){
?>


<?php  
 global $wpdb;
 $favcolor_nav = $_POST['nav_delete_comment'];
  // if($date_from_nav =  $_POST['nav_delete_commentfrom'] == "") { $date_from_nav = '2000-01-01';  } else { $date_from_nav =   $_POST['nav_delete_commentfrom']; }
  //if($date_to_nav = $_POST['nav_delete_commentto'] == "") { $date_to_nav = date('Y-m-d');  } else { $date_to_nav =  $_POST['nav_delete_commentto']; }
   
  
switch ($favcolor_nav)
{
case "nav_delete_all":
      
	if(wp_verify_nonce($_POST['nav@final_delete'], 'nav@final_delete')) {
                if($wpdb->query("TRUNCATE $wpdb->commentmeta") != FALSE){
                    if($wpdb->query("TRUNCATE $wpdb->comments") != FALSE){
                            $wpdb->query("Update $wpdb->posts set comment_count = 0 where post_author != 0");
                            $wpdb->query("OPTIMIZE TABLE $wpdb->commentmeta");
                            $wpdb->query("OPTIMIZE TABLE $wpdb->comments"); ?>
							<script>
							swal("Whooo!", "All comments have been deleted.!", "success")
							</script>
                  <?php  }
                    else{
                            nav_log_me('Error occured when deleting wpdb comments table');
                            ?>
							<script>
							swal("Sorry!", "No comment found between these dates !", "warning")
							</script>
                    <?Php }
                }
                else{
                    nav_log_me('Error occured when deleting wpdb commentmeta table'); ?>
					<script>
							swal("Sorry!", "No comment found between these dates !", "warning")
							</script>

             <?php   }
            } // End of verify_nonce
            else{
                nav_log_me('Security failure');
                die("Security Validation Failure");
            } // End of Security   

  break;
case "nav_delete_moderation":
    
	if(wp_verify_nonce($_POST['nav@final_delete'], 'nav@final_delete')) {
               if($wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 0") != FALSE){
                            $wpdb->query("OPTIMIZE TABLE $wpdb->comments"); 
							if ( get_option( '_transient_as_comment_count' ) !== false ) {
						      // The option already exists, so we just update it.
						      update_option( '_transient_as_comment_count', "" );
                             }
							
							?>
							<script>
							swal("Whooo!", "All moderation comments have been deleted!", "success")
							</script>
	      <?php  }  // End of verify_nonce
		   else{
                            nav_log_me('Error occured when deleting wpdb comments table'); ?>
                          
							<script>
							swal("Sorry!", "No comment found between these dates !", "warning")
							</script>

		 <?php   }      }
            else {
                nav_log_me('Security failure');
                die("Security Validation Failure");
            } // End of Security 
	
  break;
case "nav_delete_approved":
   
    if(wp_verify_nonce($_POST['nav@final_delete'], 'nav@final_delete')) {
                
                    if($wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 1") != FALSE){
                            $wpdb->query("Update $wpdb->posts set comment_count = 0 where post_author != 0");
                            $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
							
							if ( get_option( '_transient_as_comment_count' ) !== false ) {
						      // The option already exists, so we just update it.
						      update_option( '_transient_as_comment_count', "" );
                             }
							
							?>
							<script>
							swal("Whooo!", "All approved comments have been deleted !", "success")
							</script>
                    <?php }
                    else{
                            nav_log_me('Error occured when deleting wpdb comments table'); ?>
							<script>
							swal("Sorry!", "No comment found between these dates !", "warning")
							</script>
                 <?php   }
               
            } // End of verify_nonce
            else{
                nav_log_me('Security failure');
                die("Security Validation Failure");
            } // End of Security   

  break;
case "nav_delete_spam":

     if(wp_verify_nonce($_POST['nav@final_delete'], 'nav@final_delete')) {
                
                    if($wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 'spam'") != FALSE){
                            $wpdb->query("Update $wpdb->posts set comment_count = 0 where post_author != 0");
                            $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
                           if ( get_option( '_transient_as_comment_count' ) !== false ) {
						      // The option already exists, so we just update it.
						      update_option( '_transient_as_comment_count', "" );
                             }

							?>
							<script>
							swal("Whooo!", "All spam comments have been deleted.!", "success")
							</script>
                  <?Php  }
                    else{
                            nav_log_me('Error occured when deleting wpdb comments table'); ?>
							<script>
							swal("Sorry!", "No comment found between these dates !", "warning")
							</script>
                    <?php }
               
            } // End of verify_nonce
            else{
                nav_log_me('Security failure');
                die("Security Validation Failure");
            } // End of Security   

  break;
case "nav_delete_trash":
    
	if(wp_verify_nonce($_POST['nav@final_delete'], 'nav@final_delete')) {
                
                    if($wpdb->query("DELETE FROM $wpdb->comments WHERE comment_approved = 'trash'") != FALSE){
                            $wpdb->query("Update $wpdb->posts set comment_count = 0 where post_author != 0");
                            $wpdb->query("OPTIMIZE TABLE $wpdb->comments"); 
							if ( get_option( '_transient_as_comment_count' ) !== false ) {
						      // The option already exists, so we just update it.
						      update_option( '_transient_as_comment_count', "" );
                             }
							
							?>
							<script>
							swal("Whooo!", "All trash comments have been deleted!", "success")
							</script>
                   <?php } 
                    else{
                            nav_log_me('Error occured when deleting wpdb comments table'); ?>
							<script>
							swal("Sorry!", "No comment found between these dates !", "warning")
							</script>
                    <?php }
               
            } // End of verify_nonce
            else{
                nav_log_me('Security failure');
                die("Security Validation Failure");
            } // End of Security   

  break;
}
$comments_count = wp_count_comments();
?>
<div id="wpbody" role="main">
   <div id="wpbody-content" aria-label="Main content" tabindex="0" style="overflow: hidden;">
      <div class="wrap columns-2 seed_wnb">
         <!-- Screen icons are no longer used as of WordPress 3.8. -->            
         <h2><b>Delete all comments with filter !!! </b></h2>
         <div id="poststuff">
            <div id="post-body" class="metabox-holder columns-2">
               <div id="post-body-content">
                  <form action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
                      <input type="hidden" name="nav@final_delete" value="<?php echo wp_create_nonce('nav@final_delete'); ?>">
					  <div class="postbox seedprod-postbox">
                        <div class="handlediv" title="Click to toggle"><br></div>
                        <h3 style="color:red" >Delete comments !</h3>
                        <div class="inside">
                           <p>Note : Deleted Comment will not be recovered !</p>
						   <table class="form-table">
                              <tbody>
                                 <tr>
                                    <th scope="row">All Comments:</th>
                                    <td><input type="radio" name="nav_delete_comment" required <?php if($comments_count->total_comments<=0) { echo 'disabled' ;  } ?>  value="nav_delete_all"> <br><small class="description">Note : Delete all comments  (A to z)</small></td>
                                 </tr>
								  <tr>
                                    <th scope="row"> Comments in moderation:</th>
                                     <td><input type="radio" name="nav_delete_comment" required <?php if($comments_count->moderated<=0) { echo 'disabled' ;  } ?> value="nav_delete_moderation"> <br><small class="description">Note : Delete all comments which mark as  moderation.</small></td>
                                 </tr>
								  <tr>
                                    <th scope="row">Comments approved:</th>
                                 <td><input type="radio" name="nav_delete_comment" required <?php if($comments_count->approved<=0) { echo 'disabled' ;  } ?>  value="nav_delete_approved"> <br><small class="description">Note : Delete all comments which mark as  approved .</small></td>
                                 </tr>
								  <tr>
                                    <th scope="row">Comments in Spam:</th>
                                   <td><input type="radio" name="nav_delete_comment" required <?php if($comments_count->spam<=0) { echo 'disabled' ;  } ?> value="nav_delete_spam"> <br><small class="description">Note : Delete all comments which mark as  Spam .</small></td>
                                 </tr>
								 <tr>
                                    <th scope="row">Comments in Trash:</th>
                                   <td><input type="radio" name="nav_delete_comment" required <?php if($comments_count->trash<=0) { echo 'disabled' ;  } ?> value="nav_delete_trash"> <br><small class="description">Note : Delete all comments which mark as  Trash .</small></td>
                                 </tr>
                              </tbody>
                           </table>
                          
                        </div>
                     </div>
					 
					 
					 
             <!--   <div class="postbox seedprod-postbox">
                        <div class="handlediv" title="Click to toggle"><br></div>
                        <h3 style="color:green;">Advance Filter :</h3>
                        <div class="inside">
                           <table class="form-table">
                              <tbody>
							    <tr>
                                    <th scope="row">Select date:</th>
                                   <td>From:<input type="date" name="nav_delete_commentfrom" value="nav_delete_spam">
								       To:<input type="date" name="nav_delete_commentto" value="nav_delete_spam"> <br><small class="description">Note : Delete all comments between these dates (not working on delete all comment and approved comment) .</small></td>
                                 </tr>
                                <tr>
                                    <th scope="row">Delete </th>
                                    <td><input type="checkbox" name="" value="1" checked="">Note : Delete only comments which have links or spamming url, For example see image below -<br></td>
                                 </tr> 
                                
                              </tbody>
                           </table>
                        </div>
                     </div>  -->
                     <p>
                        <?php submit_button('Delete Now'); ?> 
                        <!-- <input id="reset" name="reset" type="submit" value="Reset" class="button-secondary"/>   -->
                     </p>
                   
                  </form>
               </div>
               <!-- #post-body-content -->
               <div id="postbox-container-1" class="postbox-container">
                  <div id="side-sortables" class="meta-box-sortables ui-sortable">
                     
                     <div class="postbox support-postbox" style="background-color: #fcf8e3">
                        <div class="handlediv" title="Click to toggle"><br></div>
                        <h3 class="hndle ui-sortable-handle"><span style="color:green">Comments of your Website</span></h3>
                        <div class="inside">
                           <div class="support-widget">
                                   <b>Total Comments:</b> 
                                   <?php echo $comments_count->total_comments; ?> </br>
                               
                                  <b> Comments in moderation: :</b> 
                                   <?php echo $comments_count->moderated; ?> </br>
                                
                                    <b>Comments approved: </b> 
                                  <?php echo $comments_count->approved; ?> </br>
                               
                                   <b>Comments in Spam:</b> 
                                   <?php echo $comments_count->spam ; ?> </br>
                                
                                   <b> Comments in Trash: </b> 
                                   <?php echo $comments_count->trash ; ?> </br>
                           </div>
                        </div>
                     </div>
					  
                     <div class="postbox rss-postbox" style="background-color:#d9edf7; background-color:#dff0d8">
                        <div class="handlediv" title="Click to toggle"><br></div>
                      <a href="http://codecanyon.net/user/royalnavneet/portfolio?ref=royalnavneet" target="_blank"><img src="<?php echo NAV_COMENT_PLUGIN_URI; ?>/code_canyon_250x250.jpg" /></a>
                        <br><br>
                     </div>
                     
                        
                  </div>
                  
                  
               </div>
               
                  
                     <!-- #postbox-container-1 -->
                  
               
            </div>
            
               
                  <!-- #post-body -->
               
            
         </div>
         
            
               <!-- #poststuff -->
            
         
      </div>
      
        
            <div class="clear"></div>
         
      
   </div>
   <!-- wpbody-content -->
   
      
         <div class="clear"></div>
      
   
</div>
<!-- wpbody -->
<?php
}
}
?>
