Created
May 29, 2024 11:32
-
-
Save adczk/4b50965650662d7ddaa0cf84a4dc5c90 to your computer and use it in GitHub Desktop.
Hustle - geolocation country detction caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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