Last active
February 11, 2020 14:51
-
-
Save wpmudev-sls/3ceb92f75a10e3ed902a8da46fbe785c to your computer and use it in GitHub Desktop.
[Hustle Pro] - Limit Countries On Client Side
This file contains 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 Pro] - Limit countries On Client Side | |
* Description: [Hustle Pro] - Limit countries On Client Side - 1147366844511292 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_hustle_limit_country_on_client_side_func', 100 ); | |
function wpmudev_hustle_limit_country_on_client_side_func() { | |
if( defined('HUSTLE_SUI_VERSION') && class_exists( 'Hustle_Module_Collection' ) ){ | |
/** | |
* For mapped site, you can check the IP info here:https://check-host.net/ | |
* If you disable the country of the mapped site | |
* you need to set your mapped site's IP for this variable: $mapped_site_ip | |
* to allow the user from mapped site can access the form | |
*/ | |
add_filter( 'wpoi-get-user-country', 'wpmudev_hustle_pass_user_from_mapped_site', 10, 2 ); | |
function wpmudev_hustle_pass_user_from_mapped_site( $country, $ip ){ | |
//enter your mapped site IP | |
//$mapped_site_ip = '54.217.222.242'; | |
//if( $mapped_site_ip && $ip === $mapped_site_ip ){ | |
// $country = 'XX'; | |
//} | |
$country = 'XX';//Warning: use this on the case we can't define spefic ip of mapped site(s), and we will always render the hustle form, we only disable via css and js | |
return $country; | |
} | |
add_action( 'wp_footer', 'wpmudev_hustle_visible_country_on_client_side', 21 ); | |
function wpmudev_hustle_visible_country_on_client_side(){ | |
$modules = apply_filters( 'hustle_sort_modules', Hustle_Module_Collection::instance()->get_all( true ) ); | |
$visible_countries = wp_cache_get( 'visible_countries', 'wpmudev_hustle' ); | |
if( false === $visible_countries ){ | |
$visible_countries = []; | |
foreach( $modules as $module ){ | |
if ( ! $module instanceof Hustle_Module_Model ) { | |
continue; | |
} | |
$all_conditions = json_decode( $module->get_meta( Hustle_Module_Model::KEY_VISIBILITY, '{}', true ), true); | |
if( isset( $all_conditions['conditions'] ) ){ | |
$all_conditions = $all_conditions['conditions']; | |
foreach( $all_conditions as $condition ){ | |
if( isset( $condition['visitor_country'] ) ){ | |
if( 'only' === $condition['visitor_country']['filter_type'] ){ | |
$visible_countries[] = [ | |
'form_id' => $module->id, | |
'form_type' => $module->module_type, | |
'allow' => $condition['visitor_country']['countries'] | |
]; | |
}else{ | |
$visible_countries[] = [ | |
'form_id' => $module->id, | |
'form_type' => $module->module_type, | |
'deny' => $condition['visitor_country']['countries'] | |
]; | |
} | |
} | |
} | |
} | |
} | |
wp_cache_add( 'visible_countries', $visible_countries, 'wpmudev_hustle' ); | |
} | |
if( ! empty( $visible_countries ) ){ | |
$custom_scripts = "<div id='wpmudev-hustle-visibility' style='display:none' data-visible='". json_encode( $visible_countries ) ."'></div>"; | |
$custom_scripts .= " | |
<style> | |
body.wpmudev-hustle-loading .hustle-ui{visibility:hidden;display:none} | |
html.hustle-allow-scroll.hustle-no-scroll{ | |
overflow:visible!important; | |
} | |
</style> | |
<script> | |
(function($){ | |
$(function(){ | |
var _visible_el = $('#wpmudev-hustle-visibility'); | |
if( ! _visible_el.length ){ | |
return; | |
} | |
$('body').addClass('wpmudev-hustle-loading'); | |
var _visible_countries = _visible_el.data('visible'); | |
_visible_el.remove(); | |
var apis = [ | |
{ | |
'url': 'https://ipinfo.io/json', | |
'key': 'country' | |
}, | |
{ | |
'url': 'https://ipfind.co/me?auth=50e887ce-e3bb-4f00-a9b9-667597db5539', | |
'key': 'country_code' | |
}, | |
]; | |
test_country(); | |
function test_country(){ | |
var api = apis.shift(); | |
if( api ){ | |
$.when( | |
$.getJSON( api.url ).done(function(data) { | |
if( 'object' === typeof( data ) && api.key in data ){ | |
$.each(_visible_countries, function( id, visible_countries ){ | |
var allow_this_user = true; | |
if( 'allow' in visible_countries ){ | |
if( Array.prototype.includes ){ | |
allow_this_user = visible_countries.allow.includes( data[api.key] ); | |
}else{ | |
allow_this_user = visible_countries.allow.indexOf( data[api.key] ) > -1; | |
} | |
}else if( 'deny' in visible_countries ){ | |
if( Array.prototype.includes ){ | |
allow_this_user = ! visible_countries.deny.includes( data[api.key] ); | |
}else{ | |
allow_this_user = ! ( visible_countries.deny.indexOf( data[api.key] ) > -1 ); | |
} | |
} | |
if( ! allow_this_user ){ | |
$('.hustle_module_id_'+ visible_countries.form_id).remove(); | |
if( 'popup' === visible_countries.form_type ){ | |
$('html').addClass('hustle-allow-scroll'); | |
} | |
// $('body').removeClass('wpmudev-hustle-loading'); | |
} | |
} ); | |
apis = []; | |
return false; | |
}else{ | |
test_country(); | |
} | |
}).fail(function() { | |
test_country(); | |
}), | |
$.Deferred(function( deferred ){ | |
$( deferred.resolve ); | |
}) | |
).done(function(){ | |
$('body').removeClass('wpmudev-hustle-loading'); | |
}); | |
} | |
} | |
}); | |
})(window.jQuery) | |
</script>"; | |
echo $custom_scripts; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment