Skip to content

Instantly share code, notes, and snippets.

@timersys
Last active August 23, 2024 19:06
Show Gist options
  • Save timersys/351329fccec1fcd71617db6f9c74f36c to your computer and use it in GitHub Desktop.
Save timersys/351329fccec1fcd71617db6f9c74f36c to your computer and use it in GitHub Desktop.
Set custom data based of GeotargetingWP debugging urls
<?php
class GeotWP_Url_Location {
public $data_url;
public function __construct() {
add_filter( 'geot/cancel_query', [ $this, 'set_custom_data' ],11 );
add_action('init', [$this , 'rocket_cookies'],21);
}
/**
* Check if url is set and modify data
*/
function set_custom_data() {
// debugging url not present or cookie continue with normal usage
// geot_debug=United+States&geot_debug_iso=US&geot_state=Florida&geot_state_code=FL&geot_city=Miami&geot_zip=33166
if( ! isset($_COOKIE['geot_data']) && ! isset( $_GET['geot_debug_c'] ) ) {
return false;
}
add_filter('geot/cancel_maxmind_database_lookup','__return_true');
if( ! empty($_GET['geot_debug_c'])) {
$data_url = [
'country' => esc_attr($_GET['geot_debug_c']),
'iso' => esc_attr($_GET['geot_debug_iso'] ?? ''),
'state' => esc_attr($_GET['geot_state']?? ''),
'state_iso' => esc_attr($_GET['geot_state_code']?? ''),
'city' => esc_attr($_GET['geot_city']?? ''),
'zip' => esc_attr($_GET['geot_zip']?? ''),
];
$expire_time = time() + (365 * 24 * 60 * 60);
setcookie( 'geot_data', json_encode($data_url), $expire_time, '/', '', true );
} else {
$data_url = json_decode(stripslashes($_COOKIE['geot_data']),true);
}
$this->data_url = $data_url;
$data = [
'country' => $data_url['country'],
'country_iso' => $data_url['iso'],
'state' => $data_url['state'],
'state_iso' => $data_url['state_iso'],
'city' => $data_url['city'],
'zip' => $data_url['zip'],
];
// return formatted object to the plugin
return $this->formatter($data);
}
public function rocket_cookies(){
$expire_time = time() + (365 * 24 * 60 * 60);
setcookie( 'geot_rocket_country', $this->data_url['country'],$expire_time , '/', '', true );
setcookie( 'geot_rocket_city', $this->data_url['city'], $expire_time, '/', '', true );
setcookie( 'geot_rocket_state', $this->data_url['state'], $expire_time, '/', '', true );
}
private function formatter( $data ) {
$state = new \stdClass;
$state->names = [ $data['state'] ];
$state->iso_code = $data['state_iso'];
$country = new \stdClass;
$country->names = [ $data['country'] ];
$country->iso_code = $data['country_iso'];
$continent = new \stdClass;
$continent->names = '';
$city = new \stdClass;
$city->names = [ $data['city'] ];
$city->zip = $data['zip'];
$geolocation = new \stdClass();
$geolocation->accuracy_radius = '';
$geolocation->longitude = '';
$geolocation->latitude = '';
$geolocation->time_zone = '';
return (object) [
'country' => $country,
'city' => $city,
'state' => $state,
'continent' => $continent,
'geolocation' => $geolocation,
];
}
}
new GeotWP_Url_Location();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment