public
Created

WordPress Plugin Comment IP List

  • Download Gist
comment-ip-column.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
<?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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.