Skip to content

Commit 036a64b

Browse files
committed
feat(ipbase): added support for ipbase.com
1 parent 9de63d2 commit 036a64b

File tree

6 files changed

+202
-6
lines changed

6 files changed

+202
-6
lines changed

core/class-utilities.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,10 @@ public static function geolocation( $ip ) {
849849
}
850850
}
851851
}
852+
// 2. Query ipbase
853+
$location_details = apply_filters( 'zero_spam_ip_address_details', $ip, $location_details );
854+
echo 1;
855+
die();
852856

853857
// 3. Query the IPinfo API.
854858
$ipinfo_location = ZeroSpam\Modules\IPinfoModule::get_geolocation( $ip );

includes/class-plugin.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@ private function init_modules() {
9696
// Project Honeypot module.
9797
new \ZeroSpam\Modules\ProjectHoneypot();
9898

99-
// ipstack module.
100-
new \ZeroSpam\Modules\ipstack();
99+
// ipbase module
100+
new \ZeroSpam\Modules\ipbase\ipbase();
101101

102102
// IPinfo module.
103103
new \ZeroSpam\Modules\IPinfoModule();
104104

105+
// ipstack module.
106+
new \ZeroSpam\Modules\ipstack();
107+
105108
if ( is_admin() ) {
106109
// Google API module.
107110
new \ZeroSpam\Modules\Google();

modules/ipbase/class-ipbase.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
/**
3+
* Adds integration for ipbase.com IP lookup service
4+
*
5+
* @package ZeroSpam
6+
*/
7+
8+
namespace ZeroSpam\Modules\ipbase;
9+
10+
// Security Note: Blocks direct access to the plugin PHP files.
11+
defined( 'ABSPATH' ) || die();
12+
13+
/**
14+
* ipbase
15+
*/
16+
class ipbase {
17+
/**
18+
* Constructor
19+
*/
20+
public function __construct() {
21+
add_action( 'init', array( $this, 'init' ) );
22+
}
23+
24+
/**
25+
* Fires after WordPress has finished loading but before any headers are sent
26+
*/
27+
public function init() {
28+
add_filter( 'zerospam_setting_sections', array( $this, 'sections' ) );
29+
add_filter( 'zerospam_settings', array( $this, 'settings' ), 10, 1 );
30+
add_filter( 'zero_spam_ip_address_details', array( $this, 'ip_address_details' ), 10, 2 );
31+
}
32+
33+
/**
34+
* Admin setting sections
35+
*
36+
* @param array $sections Array of admin setting sections.
37+
*/
38+
public function sections( $sections ) {
39+
$sections['ipbase'] = array(
40+
'title' => __( 'ipbase (geolocation)', 'zero-spam' ),
41+
'icon' => 'modules/ipbase/icon-ipbase.svg',
42+
);
43+
44+
return $sections;
45+
}
46+
47+
/**
48+
* Admin settings
49+
*
50+
* @param array $settings Array of available settings.
51+
*/
52+
public function settings( $settings ) {
53+
$options = get_option( 'zero-spam-ipbase' );
54+
55+
$settings['ipbase_api_key'] = array(
56+
'title' => __( 'API Key', 'zero-spam' ),
57+
'desc' => sprintf(
58+
wp_kses(
59+
/* translators: %1$s: Replaced with the ipbase URL */
60+
__( 'Enter your ipbase API key. Don\'t have an API key? <a href="%1$s" target="_blank" rel="noopener noreferrer"><strong>Get one for free!</strong></a>', 'zero-spam' ),
61+
array(
62+
'strong' => array(),
63+
'a' => array(
64+
'target' => array(),
65+
'href' => array(),
66+
'rel' => array(),
67+
),
68+
)
69+
),
70+
esc_url( 'https://app.ipbase.com/register' )
71+
),
72+
'module' => 'ipbase',
73+
'type' => 'text',
74+
'field_class' => 'regular-text',
75+
'placeholder' => __( 'Enter your ipbase API key.', 'zero-spam' ),
76+
'value' => ! empty( $options['ipbase_api_key'] ) ? $options['ipbase_api_key'] : false,
77+
);
78+
79+
$settings['ipbase_api_timeout'] = array(
80+
'title' => __( 'API Timeout', 'zero-spam' ),
81+
'module' => 'ipbase',
82+
'type' => 'number',
83+
'field_class' => 'small-text',
84+
'suffix' => __( 'seconds', 'zero-spam' ),
85+
'placeholder' => __( '5', 'zero-spam' ),
86+
'desc' => __( 'Controls how long to wait for the api to return a response, 5 seconds is recommended. Too high could result in degraded performance, too low & it won\'t have time to respond.', 'zero-spam' ),
87+
'value' => ! empty( $options['ipbase_api_timeout'] ) ? $options['ipbase_api_timeout'] : 5,
88+
'recommended' => 5,
89+
);
90+
91+
$settings['ipbase_cache'] = array(
92+
'title' => __( 'Cache Expiration', 'zero-spam' ),
93+
'module' => 'ipbase',
94+
'type' => 'number',
95+
'field_class' => 'small-text',
96+
'suffix' => __( 'day(s)', 'zero-spam' ),
97+
'placeholder' => __( 'Number of days', 'zero-spam' ),
98+
'desc' => __( 'Controls how long IP data is cached, 30 days is recommended. Too low could result in degraded performance.', 'zero-spam' ),
99+
'value' => ! empty( $options['ipbase_cache'] ) ? $options['ipbase_cache'] : 30,
100+
'recommended' => 30,
101+
);
102+
103+
return $settings;
104+
}
105+
106+
/**
107+
* Returns an IP addresses' details
108+
*
109+
* @param string $ip_address_details IP address details.
110+
*/
111+
public function ip_address_details( $ip_address, $ip_address_details ) {
112+
$response_mapping = array(
113+
'country_code' => 'country_code',
114+
'country_name' => 'country_name',
115+
'region_code' => 'region_code',
116+
'region_name' => 'region_name',
117+
'city' => 'city',
118+
'zip_code' => 'zip',
119+
'time_zone' => 'timezone',
120+
'latitude' => 'latitude',
121+
'longitude' => 'longitude',
122+
);
123+
124+
$api_response = self::query_ip_address( $ip_address );
125+
if ( $api_response ) {
126+
foreach ( $response_mapping as $api_key => $details_key ) {
127+
if ( ! empty( $api_response[ $api_key ] ) ) {
128+
$ip_address_details[ $details_key ] = sanitize_text_field( $api_response[ $api_key ] );
129+
}
130+
}
131+
}
132+
133+
return $ip_address_details;
134+
}
135+
136+
/**
137+
* Query an IP address
138+
*
139+
* @param string $ip_address IP address to query.
140+
*/
141+
public static function query_ip_address( $ip_address ) {
142+
$plugin_settings = \ZeroSpam\Core\Settings::get_settings();
143+
144+
if ( empty( $plugin_settings['ipbase_api_key']['value'] ) || ! rest_is_ip_address( $ip_address ) ) {
145+
return false;
146+
}
147+
148+
$queried_cache_key = \ZeroSpam\Core\Utilities::cache_key(
149+
array(
150+
'ipinfo',
151+
$ip,
152+
)
153+
);
154+
155+
$result = wp_cache_get( $queried_cache_key );
156+
if ( false === $result ) {
157+
$endpoint = esc_url( "https://api.ipbase.com/json/$ip_address?apikey=" . $plugin_settings['ipbase_api_key']['value'] );
158+
159+
$response_timeout = 5;
160+
if ( ! empty( $settings['ipbase_api_timeout'] ) ) {
161+
$timeout = intval( $settings['ipbase_api_timeout']['value'] );
162+
}
163+
164+
$response = \ZeroSpam\Core\Utilities::remote_get( $endpoint, array( 'timeout' => $timeout ) );
165+
if ( $response ) {
166+
$result = json_decode( $response, true );
167+
168+
if ( empty( $result ) || ! empty( $result['message'] ) ) {
169+
\ZeroSpam\Core\Utilities::log( 'ipbase_api: ' . $result['message'] );
170+
return false;
171+
}
172+
173+
$cache_expiration = 14 * DAY_IN_SECONDS;
174+
if ( ! empty( $settings['ipbase_cache']['value'] ) ) {
175+
$expiration = $settings['ipbase_cache']['value'] * DAY_IN_SECONDS;
176+
}
177+
178+
wp_cache_set( $cache_key, $result, 'zerospam', $expiration );
179+
}
180+
}
181+
182+
return $result;
183+
}
184+
}

modules/ipbase/icon-ipbase.svg

Lines changed: 1 addition & 0 deletions
Loading

readme.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ Donate link: https://www.zerospam.org/subscribe/
55
Requires at least: 5.2
66
Tested up to: 6.0.2
77
Requires PHP: 7.3
8-
Stable tag: 5.4.1
8+
Stable tag: 5.4.2
99
License: GNU GPLv3
1010
License URI: https://choosealicense.com/licenses/gpl-3.0/
1111

1212
Protection against spam & malicious users using cutting-edge AI. Firewall, real-time monitoring & more.
1313

1414
== Description ==
1515

16-
Protect against spam & malicious users using an advanced, highly-configurable firewall, real-time monitoring, integration with popular blacklists & plugins like [WooCommerce](https://wordpress.org/plugins/woocommerce/), [GiveWP](https://givewp.com/ref/1118/), [Gravity Forms](https://www.gravityforms.com/) & more.
16+
Protect against spam & malicious users using an advanced, highly-configurable firewall, real-time monitoring, integration with popular blacklists, plugins like [WooCommerce](https://wordpress.org/plugins/woocommerce/), [GiveWP](https://givewp.com/ref/1118/), [Gravity Forms](https://www.gravityforms.com/) & more.
1717

1818
**Install, activate, configure, then put your mind at ease!**
1919

@@ -105,6 +105,10 @@ If hosting with Pantheon, see their [known issues page](https://pantheon.io/docs
105105

106106
== Changelog ==
107107

108+
= v5.4.2 =
109+
110+
* feat(ipbase): added support for ipbase.com
111+
108112
= v5.4.1 =
109113

110114
* feat(dashboard): dashboard ui enhancements

wordpress-zero-spam.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Plugin Name: Zero Spam for WordPress
1414
* Plugin URI: https://www.highfivery.com/projects/zero-spam/
1515
* Description: Tired of all the ineffective WordPress anti-spam & security plugins? Zero Spam for WordPress makes blocking spam &amp; malicious activity a cinch. <strong>Just activate, configure, and say goodbye to spam.</strong>
16-
* Version: 5.4.1
16+
* Version: 5.4.2
1717
* Requires at least: 5.2
1818
* Requires PHP: 7.3
1919
* Author: Highfivery LLC
@@ -31,7 +31,7 @@
3131
define( 'ZEROSPAM', __FILE__ );
3232
define( 'ZEROSPAM_PATH', plugin_dir_path( ZEROSPAM ) );
3333
define( 'ZEROSPAM_PLUGIN_BASE', plugin_basename( ZEROSPAM ) );
34-
define( 'ZEROSPAM_VERSION', '5.4.1' );
34+
define( 'ZEROSPAM_VERSION', '5.4.2' );
3535

3636
if ( defined( 'ZEROSPAM_DEVELOPMENT_URL' ) ) {
3737
define( 'ZEROSPAM_URL', ZEROSPAM_DEVELOPMENT_URL );

0 commit comments

Comments
 (0)