Skip to content

Instantly share code, notes, and snippets.

@tokkonopapa
Last active October 26, 2018 14:26
Show Gist options
  • Save tokkonopapa/494949213f3d086cf4a28613f759314c to your computer and use it in GitHub Desktop.
Save tokkonopapa/494949213f3d086cf4a28613f759314c to your computer and use it in GitHub Desktop.
Drop-in for IP Geo Block custom filters for admin
<?php
/**
* Drop-in for IP Geo Block custom filters for admin
*
* This file should be named as `drop-in-admin.php`.
*
* @package IP_Geo_Block
* @author tokkonopapa <[email protected]>
* @license GPL-3.0
* @link http://www.ipgeoblock.com/
* @see http://www.ipgeoblock.com/codex/#filter-hooks
* @example Use `IP_Geo_Block::add_filter()` instead of `add_filter()`
*/
class_exists( 'IP_Geo_Block', FALSE ) or die;
/**
* Analyze entries in "Validation logs"
*
* Each entry has an array:
* Array (
* [0 DB row number] => 154
* [1 Target ] => comment
* [2 Time ] => 1534580897
* [3 IP address ] => 102.177.147.***
* [4 Country code ] => ZA
* [5 Result ] => blocked
* [6 AS number ] => AS328239
* [7 Request ] => POST[80]:/wp-comments-post.php
* [8 User agent ] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) ...
* [9 HTTP headers ] => HTTP_ORIGIN=http://localhost,HTTP_X_FORWARDED_FOR=102.177.147.***
* [10 $_POST data ] => comment=Hello.,author,email,url,comment_post_ID,comment_parent
* )
*
* And put a mark at "Target"
* ¹¹: Passed in Whitelist
* ¹²: Passed in Blacklist
* ¹³: Passed not in list
* ²¹: Blocked in Whitelist
* ²²: Blocked in Blacklist
* ²³: Blocked not in list
*/
function ip_geo_block_logs( $logs = array() ) {
// Get settings of IP Geo Block
$settings = IP_Geo_Block::get_option();
// White/Black list for back-end
$white_backend = $settings['white_list'];
$black_backend = $settings['black_list'];
// White/Black list for front-end
if ( $settings['public']['matching_rule'] < 0 ) {
// Follow "Validation rule settings"
$white_frontend = $white_backend;
$black_frontend = $black_backend;
} else {
// Whitelist or Blacklist for "Public facing pages"
$white_frontend = $settings['public']['white_list'];
$black_frontend = $settings['public']['black_list'];
}
foreach ( $logs as $key => $log ) {
// Passed or Blocked
$mark = IP_Geo_Block::is_passed( $log[5] ) ? '&sup1;' : '&sup2;';
// Whitelisted, Blacklisted or N/A
if ( 'public' === $log[1] ) {
$mark .= IP_Geo_Block::is_listed( $log[4], $white_frontend ) ? '&sup1;' : (
IP_Geo_Block::is_listed( $log[4], $black_frontend ) ? '&sup2;' : '&sup3;' );
} else {
$mark .= IP_Geo_Block::is_listed( $log[4], $white_backend ) ? '&sup1;' : (
IP_Geo_Block::is_listed( $log[4], $black_backend ) ? '&sup2;' : '&sup3;' );
}
// Put a mark at "Target"
$logs[ $key ][1] .= $mark;
}
return $logs;
}
IP_Geo_Block::add_filter( 'ip-geo-block-logs', 'ip_geo_block_logs' );
/**
* Register UI "Preset filters" at "Search in logs"
*
* @param array An empty array by default.
* @return array The pare of 'title' and 'value'.
*/
function ip_geo_block_logs_preset( $filters = array() ) {
return $filters + array(
array( 'title' => 'Passed in Whitelist', 'value' => '&sup1;&sup1;' ),
array( 'title' => 'Passed in Blacklist', 'value' => '&sup1;&sup2;' ),
array( 'title' => 'Passed not in list', 'value' => '&sup1;&sup3;' ),
array( 'title' => 'Blocked in Whitelist', 'value' => '&sup2;&sup1;' ),
array( 'title' => 'Blocked in Blacklist', 'value' => '&sup2;&sup2;' ),
array( 'title' => 'Blocked not in list', 'value' => '&sup2;&sup3;' ),
);
}
IP_Geo_Block::add_filter( 'ip-geo-block-logs-preset', 'ip_geo_block_logs_preset' );
@tokkonopapa
Copy link
Author

tokkonopapa commented Aug 19, 2018

Forum topic in WordPress.org: Request: Log filtering

Codex: ip-geo-block-logs[-preset]

An example of Preset filters:Preset filters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment