diff --git a/classes/class-pbs-check-dma-settings.php b/classes/class-pbs-check-dma-settings.php
index aadf02e..3b199e3 100644
--- a/classes/class-pbs-check-dma-settings.php
+++ b/classes/class-pbs-check-dma-settings.php
@@ -62,6 +62,7 @@ public function register_settings() {
add_settings_field( 'jwplayer_uri', 'JW Player URI', array( $this, 'settings_field'), $this->token, 'generalsettings', array('setting' => $this->token, 'field' => 'jwplayer_uri', 'class' => 'regular-text', 'label' => 'Full URI to the JW Player javascript, unique to your JWPlayer.com account. Leave blank to use video.js', 'default' => '') );
+ add_settings_field('ip_zip_override', 'Override zipcode for IPs', array( $this, 'settings_field'), $this->token, 'generalsettings', array('setting' => $this->token, 'field' => 'ip_zip_override', 'class' => 'regular-text', 'type' => 'textarea', 'default' => '', 'label' => 'Add JSON formated pairs of IP -> zipcodes if you want a specific IP address to appear to be in a zipcode eg
[
{"127.0.0.1":"00001"},
{"127.0.0.2":"00002"}
]
NO TRAILING COMMA ON LAST PAIR, thats invalid JSON
Be kind and don\'t delete pairs from other people.'));
}
@@ -138,7 +139,11 @@ public function settings_field( $args ) {
echo '';
echo '';
break;
-
+ case 'textarea' :
+ $value = (($setting[$field] && strlen(trim($setting[$field]))) ? $setting[$field] : $default);
+ echo '
' . $args['label'] . '
'; + break; + default: // any case other than selects, radios, checkboxes, or textareas formats like a text input $value = (($setting[$field] && strlen(trim($setting[$field]))) ? $setting[$field] : $default); diff --git a/classes/class-pbs-check-dma.php b/classes/class-pbs-check-dma.php index 01d262e..d0dfb84 100644 --- a/classes/class-pbs-check-dma.php +++ b/classes/class-pbs-check-dma.php @@ -17,7 +17,7 @@ public function __construct() { $this->assets_dir = trailingslashit( $this->dir ) . 'assets'; $this->assets_url = trailingslashit(plugin_dir_url( __DIR__ ) ) . 'assets'; $this->token = 'pbs_check_dma'; - $this->version = '0.94'; + $this->version = '1.01'; // Load public-facing style sheet and JavaScript. //add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); @@ -58,6 +58,8 @@ public function use_custom_template($template) { } public function get_location_from_ip($client_ip) { + #possibly manually override the zipcode + $override_zipcode = $this->manually_override_zipcode_from_ip($client_ip); $zip_url = 'https://services.pbs.org/zipcodes/ip/'; $combined_url = $zip_url . $client_ip . '.json'; $response = wp_remote_get($combined_url, array()); @@ -70,20 +72,22 @@ public function get_location_from_ip($client_ip) { if ($body) { $parsed = json_decode($body, TRUE); $item = !empty($parsed['$items'][0]) ? $parsed['$items'][0] : false; - if (!$item) { + if (!$item && !$override_zipcode) { return array('errors' => $response); } - $zipcode = !empty($item['zipcode']) ? (string) $item['zipcode'] : ''; + $zipcode = $override_zipcode ? $override_zipcode : $zipcode; $state = ''; $county = ''; - if (empty($item['$links']) || !is_array($item['$links'])) { + if (!$override_zipcode && (empty($item['$links']) || !is_array($item['$links']))) { return array('errors' => $response); } - foreach ($item['$links'] as $link) { - if ($link['$relationship'] == "related") { - $state = !empty($link['$items'][0]['$links'][0]['state']) ? $link['$items'][0]['$links'][0]['state'] : ''; - $county = !empty($link['$items'][0]['county_name']) ? $link['$items'][0]['county_name'] : ''; - break; + if (isset($item['$links'])) { + foreach ($item['$links'] as $link) { + if ($link['$relationship'] == "related") { + $state = !empty($link['$items'][0]['$links'][0]['state']) ? $link['$items'][0]['$links'][0]['state'] : ''; + $county = !empty($link['$items'][0]['county_name']) ? $link['$items'][0]['county_name'] : ''; + break; + } } } $country = !empty($zipcode) ? 'USA' : 'Outside of the US'; // the PBS endpoint returns a 404 for non-US IP addresses @@ -105,6 +109,34 @@ public function get_remote_ip_address() { return $_SERVER['REMOTE_ADDR']; } + public function manually_override_zipcode_from_ip($client_ip) { + // returns false unless there's a manually override zipcode assigned to the ip + // in which case it returns the zipcode + $return = false; + $defaults = get_option($this->token); + if (!empty(trim($defaults['ip_zip_override']))) { + $ip_zip_override = str_replace("\n", "", $defaults['ip_zip_override']); + $ip_zip_override = str_replace("\r", "", $ip_zip_override); + if (json_decode($ip_zip_override)){ + $json_ary = json_decode($ip_zip_override, true); + foreach ($json_ary as $pair) { + foreach ($pair as $ip => $zip) { + if (!filter_var($ip, FILTER_VALIDATE_IP)){ + continue; + } + if ($client_ip == $ip) { + if (is_string($zip) && 1 === preg_match("/^[0-9]{5}$/", $zip)) { + $return = $zip; + break; + } + } + } + } + } + } + return $return; + } + public function format_counties_setting_for_use() { $defaults = get_option($this->token);