ID, 'visitor_conditions_array', true );
if ( is_array( $visitor_conditions_array ) && ! empty( $visitor_conditions_array ) ) {
foreach ( $visitor_conditions_array as $key => $value ) {
foreach ( $value as $gkey => $gvalue ) {
foreach ( $gvalue as $hkey => $hvalue ) {
if ( $hvalue['key_1'] == 'device' ) {
$user_target_type = 1;
break;
}
}
}
}
}
$message = '';
if ( function_exists( 'wpsc_init' ) ) {
global $wp_cache_mobile_enabled;
if ( ! $wp_cache_mobile_enabled ) {
$afwp_cache_mobile_support = 1;
$message = "You are using WP Super Cache plugin, if you want User Targeting for 'Device Type' to work perfectly we recommend you to enable Mobile Support of your cache plugin.";
}
} elseif ( defined( 'WP_ROCKET_FILE' ) ) {
$wp_rocket_option = get_option( 'wp_rocket_settings' );
if ( ! $wp_rocket_option['cache_mobile'] || ! $wp_rocket_option['do_caching_mobile_files'] ) {
$afwp_cache_mobile_support = 1;
$message = "You are using WP Rocket Cache plugin, if you want User Targeting for 'Device Type' to work perfectly we recommend you to enable Mobile Support of your cache plugin.";
}
} elseif ( defined( 'W3TC_FILE' ) ) {
$afwp_cache_mobile_support = 1;
$message = "You are using WP3 Total Cache plugin, if you want User Targeting for 'Device Type' to work perfectly we recommend you to enable Mobile Support of your cache plugin.";
}
$visitor_condition_enable = get_post_meta( $post->ID, $key = 'adsforwp_v_condition_enable', true );
if ( isset( $visitor_condition_enable ) && $visitor_condition_enable == 'enable' ) {
if ( $user_target_type == 1 && $afwp_cache_mobile_support == 1 ) {
?>
Warning:
adsforwp_get_client_ip();
$ipdat = wp_remote_get( 'http://www.geoplugin.net/json.gp?ip=' . $ip );
if ( ! is_wp_error( $ipdat ) && wp_remote_retrieve_response_code( $ipdat ) === 200 ) {
$body = wp_remote_retrieve_body( $ipdat );
$ipdat = json_decode( $body );
if ( $ipdat !== null ) {
$country_code = $ipdat->geoplugin_countryCode;
$country_code = $this->adsforwp_get_three_letter_country_code_using_two($country_code);
}
}
return $country_code;
}
public function adsforwp_visitor_condition_logic_checker( $input ) {
global $post;
$type = $comparison = $data = $result = '';
if ( is_array( $input ) ) {
$type = array_key_exists( 'key_1', $input ) ? $input['key_1'] : '';
$comparison = array_key_exists( 'key_2', $input ) ? $input['key_2'] : '';
$data = array_key_exists( 'key_3', $input ) ? $input['key_3'] : '';
}
// Get all the users registered
$user = wp_get_current_user();
switch ( $type ) {
case 'device':
require_once ADSFORWP_PLUGIN_DIR . '/vendor/class-adsforwp-mobile-detect.php';
$mobile_detect = $isTablet = '';
// instantiate the Mobile detect class
$mobile_detect = new Adsforwp_Mobile_Detect();
$isMobile = $mobile_detect->isMobile();
$isTablet = $mobile_detect->isTablet();
$device_name = 'desktop';
if ( $isMobile && $isTablet ) { // Only For tablet
$device_name = 'mobile';
} elseif ( $isMobile && ! $isTablet ) { // Only for mobile
$device_name = 'mobile';
}
if ( $comparison == 'equal' ) {
if ( $device_name == $data ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
if ( $device_name != $data ) {
$result = true;
}
}
break;
case 'referrer_url':
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized --Reason Using it for condition check
$referrer_url = isset( $_SERVER['HTTP_REFERER'] ) ? sanitize_url( $_SERVER['HTTP_REFERER'] ) : '';
if ( isset( $input['key_4'] ) && $input['key_3'] == 'url_custom' ) {
$data = $input['key_4'];
}
if ( $comparison == 'equal' ) {
if ( $referrer_url == $data ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
if ( $referrer_url != $data ) {
$result = true;
}
}
break;
case 'browser_width':
$result = true;
break;
case 'browser_language':
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized --Reason Using it for condition check
$browser_language = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) : '';
if ( $comparison == 'equal' ) {
if ( $browser_language == $data ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
if ( $browser_language != $data ) {
$result = true;
}
}
break;
case 'geo_location':
$country_code = '';
$saved_ip = '';
$user_ip = $this->adsforwp_get_client_ip();
$saved_ip_list = array();
if ( isset( $_COOKIE['adsforwp-user-info'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized --Reason Using it for condition check
$saved_ip_list = $_COOKIE['adsforwp-user-info'];
$saved_ip = trim( base64_decode( $saved_ip_list[0] ) );
}
if ( ! empty( $saved_ip_list ) && $saved_ip == $user_ip ) {
if ( isset( $saved_ip_list[1] ) ) {
$country_code = trim( base64_decode( $saved_ip_list[1] ) );
}
}
if ( $comparison == 'equal' ) {
if($country_code!=""){
if ( $country_code == $data ) {
$result = true;
}
}else{
$country_code = $this->adsfrowp_check_country_code($country_code);
if ( $country_code == $data ) {
$result = true;
}
}
}
if ( $comparison == 'not_equal' ) {
if($country_code!=""){
if ( $country_code != $data ) {
$result = true;
}
}else{
$country_code = $this->adsfrowp_check_country_code($country_code);
if ( $country_code != $data ) {
$result = true;
}
}
}
break;
case 'url_parameter':
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash --Reason Using it for condition check
$url = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_url( $_SERVER['REQUEST_URI'] ) : '';
if ( $comparison == 'equal' ) {
if ( strpos( $url, $data ) !== false ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
if ( strpos( $url, $data ) == false ) {
$result = true;
}
}
break;
case 'cookie':
$cookie_arr = $_COOKIE;
if ( $data == '' ) {
if ( $comparison == 'equal' ) {
if ( isset( $cookie_arr ) ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
if ( ! isset( $cookie_arr ) ) {
$result = true;
}
}
} else {
if ( $comparison == 'equal' ) {
if ( $cookie_arr ) {
foreach ( $cookie_arr as $arr ) {
if ( $arr == $data ) {
$result = true;
break;
}
}
}
}
if ( $comparison == 'not_equal' ) {
if ( isset( $cookie_arr ) ) {
foreach ( $cookie_arr as $arr ) {
if ( $arr != $data ) {
$result = true;
break;
}
}
}
}
}
break;
case 'logged_in_visitor':
if ( is_user_logged_in() ) {
$status = 'true';
} else {
$status = 'false';
}
if ( $comparison == 'equal' ) {
if ( $status == $data ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
if ( $status != $data ) {
$result = true;
}
}
break;
case 'user_agent':
$user_agent_name = $this->adsforwp_detect_user_agent();
if ( isset( $input['key_5'] ) && $input['key_3'] == 'user_agent_custom' ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Using it for condition check
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && stripos( $_SERVER['HTTP_USER_AGENT'], $input['key_5'] ) ) {
$user_agent_name = $input['key_5'];
}
$data = $input['key_5'];
}
if ( $comparison == 'equal' ) {
if ( $user_agent_name == $data ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
if ( $user_agent_name != $data ) {
$result = true;
}
}
break;
case 'user_type':
if ( $comparison == 'equal' ) {
if ( in_array( $data, (array) $user->roles ) ) {
$result = true;
}
}
if ( $comparison == 'not_equal' ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
// Get all the registered user roles
$roles = get_editable_roles();
$all_user_types = array();
foreach ( $roles as $key => $value ) {
$all_user_types[] = $key;
}
// Flip the array so we can remove the user that is selected from the dropdown
$all_user_types = array_flip( $all_user_types );
// User Removed
unset( $all_user_types[ $data ] );
// Check and make the result true that user is not found
if ( in_array( $data, (array) $all_user_types ) ) {
$result = true;
}
}
break;
case 'membership_level':
if ( $comparison == 'equal' ) {
if ( isset( $user->membership_level ) && function_exists( 'pmpro_getAllLevels' ) ) {
if ( in_array( $data, (array) $user->membership_level->id ) ) {
$result = true;
}
}
}
if ( $comparison == 'not_equal' ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
// Get all the registered user roles
if ( function_exists( 'pmpro_getAllLevels' ) ) {
$all_pmpro_levels = pmpro_getAllLevels( false, true );
$all_level_types = array();
foreach ( $all_pmpro_levels as $key => $value ) {
$all_level_types[] = $value->id;
}
// Flip the array so we can remove the user that is selected from the dropdown
$all_level_types = array_flip( $all_level_types );
// User Removed
unset( $all_level_types[ $data ] );
// Check and make the result true that user is not found
if ( in_array( $data, (array) $all_level_types ) ) {
$result = true;
}
}
}
break;
default:
$result = false;
break;
}
return $result;
}
public function adsforwp_detect_user_agent() {
$user_agent_name = 'others';
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Using it for condition check
$server_user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
if ( strpos( $server_user_agent, 'Opera' ) || strpos( $user_agent, 'OPR/' ) ) {
$user_agent_name = 'opera';
} elseif ( strpos( $server_user_agent, 'Edge' ) ) {
$user_agent_name = 'edge';
} elseif ( strpos( $server_user_agent, 'Firefox' ) ) {
$user_agent_name = 'firefox';
} elseif ( strpos( $server_user_agent, 'MSIE' ) || strpos( $user_agent, 'Trident/7' ) ) {
$user_agent_name = 'internet_explorer';
} elseif ( stripos( $server_user_agent, 'iPod' ) ) {
$user_agent_name = 'ipod';
} elseif ( stripos( $server_user_agent, 'iPhone' ) ) {
$user_agent_name = 'iphone';
} elseif ( stripos( $server_user_agent, 'iPad' ) ) {
$user_agent_name = 'ipad';
} elseif ( stripos( $server_user_agent, 'Android' ) ) {
$user_agent_name = 'android';
} elseif ( stripos( $server_user_agent, 'webOS' ) ) {
$user_agent_name = 'webos';
} elseif ( strpos( $server_user_agent, 'Chrome' ) ) {
$user_agent_name = 'chrome';
} elseif ( strpos( $server_user_agent, 'Safari' ) ) {
$user_agent_name = 'safari';
}
return $user_agent_name;
}
public function adsforwp_visitor_condition_field_data( $post_id ) {
$visitor_conditions_array = get_post_meta( $post_id, 'visitor_conditions_array', true );
$output = array();
if ( $visitor_conditions_array ) {
foreach ( $visitor_conditions_array as $gropu ) {
$output[] = array_map( array( $this, 'adsforwp_visitor_condition_logic_checker' ), $gropu['visitor_conditions'] );
}
}
return $output;
}
public function adsforwp_visitor_conditions_status( $post_id ) {
$unique_checker = '';
$visitor_condition_enable = get_post_meta( $post_id, $key = 'adsforwp_v_condition_enable', true );
if ( isset( $visitor_condition_enable ) && $visitor_condition_enable == 'enable' ) {
$resultset = $this->adsforwp_visitor_condition_field_data( $post_id );
if ( $resultset ) {
$condition_array = array();
foreach ( $resultset as $result ) {
$data = array_filter( $result );
$number_of_fields = count( $data );
$checker = 0;
if ( $number_of_fields > 0 ) {
$checker = count( array_unique( $data ) );
$array_is_false = in_array( false, $result );
if ( $array_is_false ) {
$checker = 0;
}
}
$condition_array[] = $checker;
}
$array_is_true = in_array( true, $condition_array );
if ( $array_is_true ) {
$unique_checker = 1;
}
} else {
$unique_checker = 'notset';
}
} else {
$unique_checker = 1;
}
return $unique_checker;
}
// Function to get the client IP address
public function adsforwp_get_client_ip() {
$ipaddress = '';
if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Just using it for condition check
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Just using it for condition check
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Just using it for condition check
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Just using it for condition check
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Just using it for condition check
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Just using it for condition check
$ipaddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipaddress = 'UNKNOWN';
}
return $ipaddress;
}
}
if ( class_exists( 'Adsforwp_View_Visitor_Condition' ) ) {
new Adsforwp_View_Visitor_Condition();
}