Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove Comment Author IPs
<?php
/**
* Plugin Name: Remove Comment Author IPs
* Version: 0.1.0
* Description: Removes the IP addresses of comment authors on a scheduled time.
* Author: Dominik Schilling
* Author URI: http://wphelper.de/
* Plugin URI: https://gist.github.com/1836266
*
* License: GPLv2 or later
*
* Copyright (C) 2011-2012 Dominik Schilling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Don't call this file directly.
*/
if ( ! class_exists( 'WP' ) ) {
die();
}
class Remove_Comment_IPs {
/**
* Define the days how long IP addresses should be saved.
* If the time has passed, the addresses will be removed.
*
* @var integer
*/
private static $days = 3;
/**
* Init.
*
* @since 0.1.0
*
* @return void
*/
public static function init() {
// Init the action which will be fired if the scheduled time has passed.
add_action( 'remove_comment_ips_event', array(
__CLASS__,
'do_remove_comment_ips'
) );
}
/**
* Clear the IP address from the comment row.
*
* @since 0.1.0
*
* @return int Number of deleted IP addresses
*/
public static function do_remove_comment_ips() {
global $wpdb;
$query = $wpdb->prepare(
"UPDATE $wpdb->comments SET `comment_author_IP` = '' WHERE `comment_author_IP` != '' AND `comment_date_gmt` < SUBDATE( NOW(), %d )",
self::$days
);
return $wpdb->query( $query );
}
/**
* Init the cron job on plugin activation.
* Note: Will also fire the cron job.
*
* @since 0.1.0
*
* @return void
*/
public static function on_activation() {
wp_schedule_event( time(), 'daily', 'remove_comment_ips_event' );
}
/**
* Delete the cronjob entry on plugin deactivation.
*
* @since 0.1.0
*
* @return void
*/
public static function on_deactivation() {
wp_clear_scheduled_hook( 'remove_comment_ips_event' );
}
}
// Init the plugin.
add_action( 'plugins_loaded', array( 'Remove_Comment_IPs', 'init' ) );
// Register (de)activation hooks to handle schedule event actions.
register_activation_hook( __FILE__, array(
'Remove_Comment_IPs',
'on_activation'
) );
register_deactivation_hook( __FILE__, array(
'Remove_Comment_IPs',
'on_deactivation'
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment