slug = $slug;
$this->condition = 'ip_country';
/* this adds the condition in set of rules dropdown */
add_filter("pi_".$this->slug."_condition", array($this, 'addRule'));
/* this gives value field to store condition value either select or text box */
add_action( 'wp_ajax_pi_'.$this->slug.'_value_field_'.$this->condition, array( $this, 'ajaxCall' ) );
/* This gives our field with saved value */
add_filter('pi_'.$this->slug.'_saved_values_'.$this->condition, array($this, 'savedDropdown'), 10, 3);
/* This perform condition check */
add_filter('pi_'.$this->slug.'_condition_check_'.$this->condition,array($this,'conditionCheck'),10,4);
/* This gives out logic dropdown */
add_action('pi_'.$this->slug.'_logic_'.$this->condition, array($this, 'logicDropdown'));
/* This give saved logic dropdown */
add_filter('pi_'.$this->slug.'_saved_logic_'.$this->condition, array($this, 'savedLogic'),10,3);
}
function addRule($rules){
$rules[$this->condition] = array(
'name'=>__('Customer IP address based country','advanced-free-flat-shipping-woocommerce'),
'group'=>'location_related',
'condition'=>$this->condition
);
return $rules;
}
function logicDropdown(){
$html = "";
$html .= 'var pi_logic_'.$this->condition.'= "";';
echo $html;
}
function savedLogic($html_in, $saved_logic, $count){
$html = "";
$html .= '';
return $html;
}
function ajaxCall(){
if(!current_user_can( 'manage_options' )) {
return;
die;
}
$count = filter_input(INPUT_POST,'count',FILTER_VALIDATE_INT);
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo Pi_efrs_selection_rule_main::createSelect($this->allCountries(), $count, $this->condition, "multiple",null,'static');
die;
}
function savedDropdown($html, $values, $count){
$html = Pi_efrs_selection_rule_main::createSelect($this->allCountries(), $count, $this->condition, "multiple", $values,'static');
return $html;
}
function allCountries(){
$countries_obj = new WC_Countries();
$countries = $countries_obj->get_countries();
return $countries;
}
function conditionCheck($result, $package, $logic, $values){
$or_result = false;
$user_country = $this->getIPCountry( $package );
$rule_countries = $values;
if($logic == 'equal_to'){
if(in_array($user_country, $rule_countries)){
$or_result = true;
}else{
$or_result = false;
}
}else{
if(in_array($user_country, $rule_countries)){
$or_result = false;
}else{
$or_result = true;
}
}
return $or_result;
}
function getIPCountry( $package ) {
$geo = new WC_Geolocation();
$user_ip_data = $geo->geolocate_ip();
$country = '';
if ( isset( $user_ip_data['country'] ) && ! empty( $user_ip_data['country'] ) ) {
$country = $user_ip_data['country'];
}
// Fallback to Cloudflare header if WooCommerce gives blank
if ( empty( $country ) && isset( $_SERVER['HTTP_CF_IPCOUNTRY'] ) && ! empty( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) {
$country = sanitize_text_field( $_SERVER['HTTP_CF_IPCOUNTRY'] );
}
return apply_filters( 'pi_efrs_get_ip_country', $country );
}
}
new Pi_efrs_selection_rule_ip_country(PI_EFRS_SELECTION_RULE_SLUG);