|
| 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 | +} |
0 commit comments