Snippets

Visser Labs Force clearing Akismet spam

Created by Michael Visser
<?php

// Clear the Akismet logs hourly
function custom_akismet_schedule_hourly_cron() {

	// Use wp_next_scheduled to check if the event is already scheduled
	$timestamp = wp_next_scheduled( 'custom_akismet_hourly_cron' );

	//If $timestamp == false schedule daily backups since it hasn't been done previously
	if( $timestamp == false ){
		//Schedule the event for right now, then to repeat daily using the hook 'wi_create_daily_backup'
		wp_schedule_event( time(), 'hourly', 'custom_akismet_hourly_cron' );
	}

	add_action( 'custom_akismet_hourly_cron', 'custom_akismet_delete_comments_now' );

}
add_action( 'init', 'custom_akismet_schedule_hourly_cron' );

function custom_akismet_submit_spam_comment( $comment_ID = 0, $reason ) {

	if( $comment_ID ) {
		wp_delete_comment( $comment_ID, true );
	}

}
add_action( 'akismet_submit_spam_comment', 'custom_akismet_submit_spam_comment', 10, 2 );

function custom_akismet_delete_comment_interval( $interval = 15 ) {

	$interval = 1;
	return $interval;

}
add_filter( 'akismet_delete_comment_interval', 'custom_akismet_delete_comment_interval' );
add_filter( 'akismet_delete_commentmeta_interval', 'custom_akismet_delete_comment_interval' );

function custom_akismet_delete_comments_now() {

	global $wpdb;

	$delete_limit = apply_filters( 'akismet_delete_comment_limit', defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 );
	$delete_limit = max( 1, intval( $delete_limit ) );

	$delete_interval = apply_filters( 'akismet_delete_comment_interval', 15 );
	$delete_interval = max( 1, intval( $delete_interval ) );

	while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB(NOW(), INTERVAL %d HOUR) > comment_date_gmt AND comment_approved = 'spam' LIMIT %d", $delete_interval, $delete_limit ) ) ) {
		if ( empty( $comment_ids ) )
			return;

		$wpdb->queries = array();

		foreach ( $comment_ids as $comment_id ) {
			do_action( 'delete_comment', $comment_id );
		}

		$comma_comment_ids = implode( ', ', array_map('intval', $comment_ids) );

		$wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_id IN ( $comma_comment_ids )");
		$wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( $comma_comment_ids )");

		clean_comment_cache( $comment_ids );
	}

	if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->comments ) ) // lucky number
		$wpdb->query("OPTIMIZE TABLE {$wpdb->comments}");

}

?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.