Created
May 29, 2024 11:30
-
-
Save adczk/0a6d8033519891fb2eeb77d8d9bd89b4 to your computer and use it in GitHub Desktop.
Hustle - geolocation integration with Defender plugin
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 integration with Defender plugin | |
* Plugin URI: https://gist.github.com/adczk | |
* Description: Makes Hustle use Defender plugin country detection for country-based visibility rules | |
* Author: adczk | |
* | |
* Author URI: https://gist.github.com/adczk | |
* License: GPLv2 or later | |
* | |
* Use as MU plugin; | |
* | |
* Tested with Hustle 7.8.4 and Defender Pro 4.7.1 | |
* REQUIRES DEFENDER TO BE ACTIVE AND HAVE MAXMIND DB CONFIGURED AND DOWNLOADED | |
* | |
*/ | |
add_filter( 'hustle_get_user_country', 'hustle_get_geoip_from_defender', 11, 2); | |
function hustle_get_geoip_from_defender( $country, $ip ) { | |
// defaults to null in case detection fails | |
$country = null; | |
// if Defender active | |
if ( class_exists( 'WP_Defender\Bootstrap' ) ) { | |
// if MaxMind DB is downloaded | |
$hgip_service = wd_di()->get( \WP_Defender\Component\Blacklist_Lockout::class ); | |
if ( $hgip_service->is_geodb_downloaded() ) { | |
$hgip_model = wd_di()->get( \WP_Defender\Model\Setting\Blacklist_Lockout::class ); | |
// Additional check if MaxMind dir is deleted. | |
if ( is_null( $hgip_model->geodb_path ) || ! is_file( $hgip_model->geodb_path ) ) { | |
// if so, skip detection | |
return $country; | |
} | |
// all set so let's geolocate IP | |
$defgeo = new WP_Defender\Extra\GeoIp( $hgip_model->geodb_path ); | |
$geo = $defgeo->ip_to_country( $ip ); | |
if ( is_array($geo) ) { | |
$country = $geo['iso']; | |
} | |
} | |
} | |
return $country; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment