Skip to content

Commit

Permalink
v.93 add google as a reverse geolocator
Browse files Browse the repository at this point in the history
  • Loading branch information
tamw-wnet committed Aug 22, 2023
1 parent 6e61744 commit 382a811
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion classes/class-pbs-check-dma-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function register_settings() {
add_settings_field( 'state_counties_array', 'Allowed Counties by State', array($this, 'settings_field'), $this->token, 'generalsettings', array('setting' => $this->token, 'field' => 'state_counties_array', 'type' => 'array', 'options' => array('count_source' => 'state_count', 'state' => array('label' => 'State', 'class' => 'small-text'), 'counties' => array('label' => 'Counties', 'class' => 'regular-text')), 'label' => 'IGNORED IF "Use PBS Location API" above is set to TRUE. Configure below which the list of counties, by state, that match our DMA. Counties should be a comma-separated list of county names (eg Kings, Bronx, Queens), states should be two-char abrevs eg NY', 'class' => 'medium-text') );
add_settings_field( 'state_count', 'State Count', array($this, 'settings_field'), $this->token, 'generalsettings', array('setting' => $this->token, 'field' => 'state_count', 'type' => 'text', 'default' => 4, 'label' => 'How many states appear above', 'class' => 'small-text') );

add_settings_field( 'reverse_geocoding_provider', 'Reverse GeoCoding Provider', array($this, 'settings_field'), $this->token, 'generalsettings', array('setting' => $this->token, 'field' => 'reverse_geocoding_provider', 'type' => 'select', 'options' => array('no_provider' => array('label' => 'none'), 'here.com' => array('label' => 'here.com: 250k/mo free tier'), 'fcc.gov' => array('label' => 'FCC Area and Census Block API: only returns US counties' ) ), 'label' => 'Service that will handle translating browser locations (lat/long) into state/county info. Every provider has potential API costs if many requests are made.', 'class' => 'medium-text', 'default' => 'here.com') );
add_settings_field( 'reverse_geocoding_provider', 'Reverse GeoCoding Provider', array($this, 'settings_field'), $this->token, 'generalsettings', array('setting' => $this->token, 'field' => 'reverse_geocoding_provider', 'type' => 'select', 'options' => array('no_provider' => array('label' => 'none'), 'here.com' => array('label' => 'here.com: 250k/mo free tier'), 'google' => array('label' => 'Google Geocoding API: requires billing but first 200 dollars free'), 'fcc.gov' => array('label' => 'FCC Area and Census Block API: only returns US counties' ) ), 'label' => 'Service that will handle translating browser locations (lat/long) into state/county info. Every provider has potential API costs if many requests are made.', 'class' => 'medium-text', 'default' => 'here.com') );

add_settings_field( 'reverse_geocoding_authentication', 'Reverse GeoCoding Authentication', array( $this, 'settings_field'), $this->token, 'generalsettings', array('setting' => $this->token, 'field' => 'reverse_geocoding_authentication', 'class' => 'regular-text', 'label' => 'access token, with variable names, required when making a request to the provider. If authentication is two arguments include both separated with an &, like so: app_id=somestring&app_code=someotherstring') );

Expand Down
41 changes: 37 additions & 4 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.92';
$this->version = '0.93';

// Load public-facing style sheet and JavaScript.
//add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
Expand Down Expand Up @@ -141,10 +141,15 @@ public function get_location_by_reverse_geocode($latitude, $longitude, $provider
$requesturl .= "&lat=$latitude&lon=$longitude";
$requesturl .= "&format=json";
break;
case "google" :
$requesturl = "https://maps.googleapis.com/maps/api/geocode/json";
$requesturl .= "?key=" . $authentication;
$requesturl .= "&latlng=$latitude,$longitude";
break;
}
// other providers TK
if (empty($requesturl)) {
return array("errors" => "no reverse geolocation url can be constructed with provideer $provider");
return array("errors" => "no reverse geolocation url can be constructed with provider $provider");
}

$response = wp_remote_get($requesturl);
Expand Down Expand Up @@ -175,8 +180,9 @@ public function get_location_by_reverse_geocode($latitude, $longitude, $provider
// zip+4 doesn't work with PBS
$zipcode = substr($zipcode, 0, 5);
}

$return = array("zipcode" => $zipcode, "state" => $address["stateCode"], "county" => $address["county"], "country" => $address["countryCode"]);
error_log(json_encode($address));
$county = isset($address["county"]) ? $address["county"] : $address["district"];
$return = array("zipcode" => $zipcode, "state" => $address["stateCode"], "county" => $county, "country" => $address["countryCode"]);
break;
case "fcc.gov" :
if (empty($parsed["results"][0]["county_name"])) {
Expand All @@ -186,8 +192,35 @@ public function get_location_by_reverse_geocode($latitude, $longitude, $provider
$result = $parsed["results"][0];
$return = array("state" => $result["state_code"], "county" => $result["county_name"], "country" => "USA");
break;
case "google" :
if (empty($parsed["results"][0]["address_components"])) {
return array('errors' => "No address", 'response' => $parsed);
}
// loop through the components to assign the pieces
$return = array();
foreach ($parsed["results"][0]["address_components"] as $address_component) {
$component_type = $address_component["types"][0]; // the second element is usually 'political'
switch ($component_type) {
case "postal_code" :
$return["zipcode"] = substr($address_component["short_name"], 0, 5);
break;
case "country" :
$return["country"] = $address_component["short_name"];
$return["country_code"] = $address_component["short_name"];
$return["country_name"] = $address_component["long_name"];
break;
case "administrative_area_level_1" :
$return["state"] = $address_component["short_name"];
break;
case "administrative_area_level_2" :
$return["county"] = $address_component["short_name"];
break;
}
}
break;
}
return $return;

}

public function compare_county_to_allowed_list($location) {
Expand Down
2 changes: 1 addition & 1 deletion pbs-check-dma.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
* Plugin Name: PBS Check DMA
* Version: 0.92 - enabled switch to video.js
* Version: 0.93 - adding Google as a reverse geolocator
* Plugin URI: http://ieg.wnet.org/
* Description: Use geolocation to restrict content based on a PBS stations' DMA
* Author: William Tam
Expand Down

0 comments on commit 382a811

Please sign in to comment.