Skip to content

Commit

Permalink
abstract listing the available callsigns
Browse files Browse the repository at this point in the history
  • Loading branch information
tamw-wnet committed Sep 25, 2023
1 parent 844567b commit ed6041d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 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.93';
$this->version = '0.94';

// Load public-facing style sheet and JavaScript.
//add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
Expand Down Expand Up @@ -257,6 +257,20 @@ public function callsign_available_in_zipcode($zipcode, $desired_callsign = '')
return false;
}
}

$available_callsigns = $this->list_available_callsigns_in_zipcode($zipcode);
if (in_array($desired_callsign, $available_callsigns)) {
return true;
}
// no luck finding the desired callsign
return false;
}

public function list_available_callsigns_in_zipcode($zipcode) {
if (empty($zipcode) || !(preg_match('/^(\d{5})?$/', $zipcode))) {
// zipcode is either empty or isn't 5 digits
return false;
}
$callsign_url = "https://services.pbs.org/callsigns/zip/";
$combined_url = $callsign_url . $zipcode . '.json';
$call_sign = false;
Expand All @@ -266,26 +280,25 @@ public function callsign_available_in_zipcode($zipcode, $desired_callsign = '')
}
$body = $response['body']; // use the content
$parsed = json_decode($body, TRUE);
$available_callsigns = [];
foreach($parsed['$items'] as $key) {
foreach($key['$links'][0]['$links'] as $link) {
if (isset( $link['$links'] )) {
foreach($link['$links'] as $i) {
if($i['$relationship'] == "flagship"){
if( $key['confidence'] == 100 ){
$call_sign = $key['$links'][0]['callsign'];
if ( $call_sign == $desired_callsign ){
return true;
}
array_push($available_callsigns, $call_sign);
}
}
}
}
}
}
}
// no luck finding the desired callsign
return false;
return $available_callsigns;
}


public function visitor_ip_is_in_dma() {
$ip = $this->get_remote_ip_address();
$location = $this->get_location_from_ip($ip);
Expand Down

0 comments on commit ed6041d

Please sign in to comment.