Skip to content

Instantly share code, notes, and snippets.

@adczk
Created May 29, 2024 11:32
Show Gist options
  • Save adczk/4b50965650662d7ddaa0cf84a4dc5c90 to your computer and use it in GitHub Desktop.
Save adczk/4b50965650662d7ddaa0cf84a4dc5c90 to your computer and use it in GitHub Desktop.
Hustle - geolocation country detction caching
<?php
/**
* Plugin Name: Hustle - geolocation country detction caching
* Plugin URI: https://gist.github.com/adczk
* Description: Makes Hustle cache country detection in transient, greatly reducing number of geolocation API requests
* Author: adczk
*
* Author URI: https://gist.github.com/adczk
* License: GPLv2 or later
*
* Use as MU plugin;
*
* Tested with Hustle 7.8.4
*
*/
add_filter( 'hustle_get_user_country', 'hustle_cache_country_ip_detect', 10, 2);
function hustle_cache_country_ip_detect( $country, $ip ) {
$country=null;
$transient_key = "hustlegeoip_".$ip;
$transient_country = get_transient( $transient_key );
// if cached in transient, get from there
if ( false !== $transient_country ) {
$country = $transient_country;
}
// otherwise proceed with normal Hustle detection
else {
if ( class_exists( 'Opt_In_Geo' ) ) {
$hustlegeo = new Opt_in_Geo;
$country = $hustlegeo->get_country_from_ip( $ip );
if ( empty( $country ) ) {
$country = 'XX';
}
set_transient( $transient_key, $country );
}
}
return $country;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment