<?php
global $wpdb, $blog_id;
$table_threats = $wpdb->prefix."spam_master_threats";
$table_keys = $wpdb->prefix."spam_master_keys";
$current_time = current_time( 'mysql' );
$minus_3months_time = date('Y-m-d h:i:s', strtotime("-3 months", strtotime($current_time)));

//Clean Up Buffer
$wpdb->query( 
			$wpdb->prepare( 
							"DELETE FROM $table_threats
							WHERE time <= %s",
							$minus_3months_time
							)
			);
$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of Spam Buffer cron successfully run.' ));

//Clean Up Comments & Clean-up Logs
if( is_multisite() ){
$spam_master_comments_clean = get_blog_option($blog_id, 'spam_master_comments_clean');
$spam_master_logs_firewall_clean = get_blog_option($blog_id, 'spam_master_logs_firewall_clean');
$spam_master_logs_registration_clean = get_blog_option($blog_id, 'spam_master_logs_registration_clean');
$spam_master_logs_comment_clean = get_blog_option($blog_id, 'spam_master_logs_comment_clean');
$spam_master_logs_contact_form_7_clean = get_blog_option($blog_id, 'spam_master_logs_contact_form_7_clean');
$spam_master_logs_woocommerce_clean = get_blog_option($blog_id, 'spam_master_logs_woocommerce_clean');
$spam_master_logs_system_clean = get_blog_option($blog_id, 'spam_master_logs_system_clean');
	if($spam_master_comments_clean == 'true'){
		$blog_prefix = $wpdb->get_blog_prefix();
		$result_comments_status = $wpdb->get_results("SELECT comment_ID,comment_author_IP,comment_author_email,comment_approved FROM {$blog_prefix}comments WHERE comment_approved = '0' OR comment_approved = '1' OR comment_approved = 'spam' OR comment_approved = 'trash'");
		foreach($result_comments_status as $status){
			$status_id = $status->comment_ID;
			$status_ip = $status->comment_author_IP;
			$status_email = $status->comment_author_email;
			$status_status = $status->comment_approved;

			$is_buffer_threat = $wpdb->get_results(
								$wpdb->prepare( 
												"SELECT threat 
												FROM $table_threats
												WHERE threat = %s OR threat = %s", 
												$status_ip, $status_email
								));
			if(!empty($is_buffer_threat)){
				wp_delete_comment( $status_id, false );
			}
		}
		//Clean old trashed comments
		$wpdb->query( 
					$wpdb->prepare( 
									"DELETE FROM {$blog_prefix}comments
									WHERE 
									(comment_approved = '0' OR comment_approved = '1' OR comment_approved = 'spam' OR comment_approved = 'trash')
									AND
									comment_date <= %s",
									$minus_3months_time
									)
					);		
		$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old spam and trashed comments cron successfully run.' ));
	}
}
else{
$spam_master_comments_clean = get_option('spam_master_comments_clean');
$spam_master_logs_firewall_clean = get_option('spam_master_logs_firewall_clean');
$spam_master_logs_registration_clean = get_option('spam_master_logs_registration_clean');
$spam_master_logs_comment_clean = get_option('spam_master_logs_comment_clean');
$spam_master_logs_contact_form_7_clean = get_option('spam_master_logs_contact_form_7_clean');
$spam_master_logs_woocommerce_clean = get_option('spam_master_logs_woocommerce_clean');
$spam_master_logs_system_clean = get_option('spam_master_logs_system_clean');
	if($spam_master_comments_clean == 'true'){
	$table_prefix = $wpdb->base_prefix;
	$result_comments_status = $wpdb->get_results("SELECT comment_ID,comment_author_IP,comment_author_email,comment_approved FROM {$table_prefix}comments WHERE comment_approved = '0' OR comment_approved = '1' OR comment_approved = 'spam' OR comment_approved = 'trash'");
		foreach($result_comments_status as $status){
			$status_id = $status->comment_ID;
			$status_ip = $status->comment_author_IP;
			$status_email = $status->comment_author_email;
			$status_status = $status->comment_approved;

			$is_buffer_threat = $wpdb->get_results(
								$wpdb->prepare( 
												"SELECT threat 
												FROM $table_threats
												WHERE threat = %s OR threat = %s", 
												$status_ip, $status_email
								));
			if(!empty($is_buffer_threat)){
				wp_delete_comment( $status_id, false );
			}
		}
		//Clean old trashed comments
		$wpdb->query( 
					$wpdb->prepare( 
									"DELETE FROM {$table_prefix}comments
									WHERE 
									(comment_approved = '0' OR comment_approved = '1' OR comment_approved = 'spam' OR comment_approved = 'trash')
									AND
									comment_date <= %s",
									$minus_3months_time
									)
					);	
		$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old spam and trashed comments cron successfully run.' ));
	}
}

//Clean Up Firewall Logs
if($spam_master_logs_firewall_clean == '0'){
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old firewall logs cron did not run because it\'s set to infinite.' ));
}
else{
	$minus_fire_months_time = date('Y-m-d h:i:s', strtotime("-".$spam_master_logs_firewall_clean." months", strtotime($current_time)));

	$wpdb->query( 
				$wpdb->prepare( 
								"DELETE FROM $table_keys
								WHERE spamkey = 'Firewall' AND time <= %s",
								$minus_fire_months_time
								)
				);
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old firewall logs cron successfully run.' ));
}

//Clean Up Registration Logs
if($spam_master_logs_registration_clean == '0'){
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old registration logs cron did not run because it\'s set to infinite.' ));
}
else{
	$minus_reg_months_time = date('Y-m-d h:i:s', strtotime("-".$spam_master_logs_registration_clean." months", strtotime($current_time)));
	
	$wpdb->query( 
				$wpdb->prepare( 
								"DELETE FROM $table_keys
								WHERE spamkey = 'Registration' AND time <= %s",
								$minus_reg_months_time
								)
				);
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old registration logs cron successfully run.' ));
}
//Clean Up Comment Logs
if($spam_master_logs_comment_clean == '0'){
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old comment logs cron did not run because it\'s set to infinite.' ));
}
else{
	$minus_com_months_time = date('Y-m-d h:i:s', strtotime("-".$spam_master_logs_comment_clean." months", strtotime($current_time)));
	$wpdb->query( 
				$wpdb->prepare( 
								"DELETE FROM $table_keys
								WHERE spamkey = 'Comment' AND time <= %s",
								$minus_com_months_time
								)
				);
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old comment logs cron successfully run.' ));
}
//Clean Up Contact Form Logs
if($spam_master_logs_contact_form_7_clean == '0'){
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old contact form logs cron did not run because it\'s set to infinite.' ));
}
else{
	$minus_con_months_time = date('Y-m-d h:i:s', strtotime("-".$spam_master_logs_contact_form_7_clean." months", strtotime($current_time)));
	$wpdb->query( 
				$wpdb->prepare( 
								"DELETE FROM $table_keys
								WHERE spamkey = 'Contact-Form' AND time <= %s",
								$minus_con_months_time
								)
				);
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old contact form logs cron successfully run.' ));
}
//Clean Up Woocommerce Logs
if($spam_master_logs_woocommerce_clean == '0'){
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old woocommerce logs cron did not run because it\'s set to infinite.' ));
}
else{
	$minus_woo_months_time = date('Y-m-d h:i:s', strtotime("-".$spam_master_logs_woocommerce_clean." months", strtotime($current_time)));
	$wpdb->query( 
				$wpdb->prepare( 
								"DELETE FROM $table_keys
								WHERE spamkey = 'Woocommerce' AND time <= %s",
								$minus_woo_months_time
								)
				);
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old woocommerce logs cron successfully run.' ));
}
//Clean Up System Logs
if($spam_master_logs_system_clean == '0'){
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old system logs cron did not run because it\'s set to infinite.' ));
}
else{
	$minus_sys_months_time = date('Y-m-d h:i:s', strtotime("-".$spam_master_logs_system_clean." months", strtotime($current_time)));
	$wpdb->query( 
				$wpdb->prepare( 
								"DELETE FROM $table_keys
								WHERE spamkey = 'System' AND time <= %s",
								$minus_sys_months_time
								)
				);
	$wpdb->insert( $table_keys, array( 'time' => $current_time, 'spamkey' => 'System', 'spamvalue' => 'Clean-Up of old system logs cron successfully run.' ));
}
?>
