Skip to content

Commit

Permalink
enable zip ip override
Browse files Browse the repository at this point in the history
  • Loading branch information
tamw-wnet committed Jun 26, 2024
1 parent 72b2538 commit 0dd11ab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
7 changes: 6 additions & 1 deletion classes/class-pbs-check-dma-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. <b>Leave blank to use video.js</b>', '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 <br />[<br />{"127.0.0.1":"00001"},<br />{"127.0.0.2":"00002"}<br />]<br />NO TRAILING COMMA ON LAST PAIR, thats invalid JSON<br />Be kind and don\'t delete pairs from other people.'));

}

Expand Down Expand Up @@ -138,7 +139,11 @@ public function settings_field( $args ) {
echo '<select name="' . $settingname . '[' . $field . ']" id="' . $settingname . '[' . $field . ']" class="' . $class . '">' . $optionlist . '</select>';
echo '<label for="' . $field . '"><p class="description">' . $label . '</p></label>';
break;

case 'textarea' :
$value = (($setting[$field] && strlen(trim($setting[$field]))) ? $setting[$field] : $default);
echo '<textarea name="' . $settingname . '[' . $field . ']" id="' . $settingname . '[' . $field . ']" cols=60 rows=5 >' . $value . '</textarea><p class="description">' . $args['label'] . '</p>';
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);
Expand Down
50 changes: 41 additions & 9 deletions classes/class-pbs-check-dma.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 0dd11ab

Please sign in to comment.