Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
WordPress Plugin Comment IP List
<?php # -*- coding: utf-8 -*-
/*
Plugin Name: Comment IP List
Description: Adds a column to the comment list with the IP address.
Version: 1.1
Required: 3.2
Author: Thomas Scholz
Author URI: http://toscho.de
License: GPL
*/
! defined( 'ABSPATH' ) and exit;
// Latest possible action to get started.
add_action(
'load-edit-comments.php'
, array ( 'Toscho_Comment_IP_Column', 'init' )
);
/**
* Parent class for custom comment columns.
* See class Toscho_Comment_IP_Column for an example.
*
* @author Thomas Scholz
*/
class Toscho_Comment_Column
{
/**
* Internal name.
*
* @var string
*/
protected $col = '';
/**
* Where to run what.
*
* @var array
*/
protected $filters = array ();
/**
* Visible name of the column header.
*
* @var string
*/
protected $header = '';
/**
* Order parameter.
*
* @var string
*/
protected $orderby = '';
/**
* Called on the action 'load-edit-comments.php'.
* Creates the object.
*
* Since static functions cannot be used in child classes in older PHP
* versions, you have to recreate it there.
*
* @return void
*/
public static function init()
{
new self;
}
/**
* Registers the filters.
*/
public function __construct()
{
foreach ( $this->filters as $filter => $function )
{
// We accept 99 args just to stay flexible.
add_filter( $filter, array ( $this, $function ), 10, 99 );
}
}
/**
* Column header name.
*
* @param array $headers
* @return array
*/
public function set_column_header_name( $headers )
{
return array_merge( $headers, array ( $this->col => $this->header ) );
}
/**
* Changes the link for the clickable column header.
*
* @param array $cols
* @return array
*/
public function set_sort_parameter( $cols )
{ // The 'orderby' parameter for the URL.
return array_merge( $cols, array ( $this->col => $this->orderby ) );
}
/**
* Prints the markup for a single cell.
*
* @param string $column_name
* @param int $id Comment id
* @return void
*/
public function show_cell( $column_name, $id )
{
$this->col == $column_name and $this->print_cell_markup( $id );
}
/**
* Creates the printed markup for a single cell.
*
* @param int $id Comment id
* @return void
*/
protected function print_cell_markup( $id )
{
print 'Extend me in a separate class!';
}
}
class Toscho_Comment_IP_Column extends Toscho_Comment_Column
{
protected $col = 'ip';
protected $filters = array (
// Called in wp-admin/includes/class-wp-list-table.php,
// WP_List_Table::__construct()
'manage_edit-comments_columns' => 'set_column_header_name'
// Called in wp-admin/includes/class-wp-list-table.php,
// WP_List_Table::get_column_info()
, 'manage_edit-comments_sortable_columns' => 'set_sort_parameter'
// Called in wp-admin/includes/class-wp-comments-list-table.php,
// WP_Comments_List_Table::column_default()
, 'manage_comments_custom_column' => 'show_cell'
// Called in wp-includes/wp-db.php, wpdb::query()
, 'query' => 'fix_ip_order'
);
protected $header = 'IP Address';
protected $orderby = 'comment_author_IP';
public static function init()
{
new self;
// doesn't work - but it should. dammit!
remove_filter( 'comment_author', 'floated_admin_avatar', 50 );
// this works.
$_GET['comment_status'] == 'spam' and add_filter( 'pre_option_show_avatars', '__return_zero' );
}
/**
* Correct the ordering of ip addresses.
* Setting this in $this->orderby doesn't work, it would be stripped away.
*
* @param string $q MySQL query
* @return string
*/
public function fix_ip_order( $q )
{
return str_replace( $this->orderby, "INET_ATON($this->orderby)", $q );
}
/**
* Links IP addresses to ip.toscho.de.
*
* @param int $id Comment ID
* @return void
*/
protected function print_cell_markup( $id )
{
$ip = get_comment_author_IP( $id );
$out = "<input size=15 value=$ip onfocus='this.select()'>
<br><a href='http://ip.toscho.de/?ip=$ip'>look up</a>";
// If you want to change the link, create a filter.
$out = apply_filters( 'toscho_comment_ip_output', $out, $ip, $id );
print $out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.