adsforwp_check_ads_in_group($post->ID); } if(empty($in_group)){ add_meta_box( 'adsforwp_visitor_condition_metabox', esc_html__( 'User Targeting', 'ads-for-wp' ), array( $this, 'adsforwp_visitor_condition_callback' ), array('adsforwp', 'adsforwp-groups'), 'normal', 'low' ); } } public function adsforwp_visitor_condition_callback( $post) { $visitor_condition_enable = get_post_meta($post->ID, $key='adsforwp_v_condition_enable', true); $visitor_conditions_array = esc_sql ( get_post_meta($post->ID, 'visitor_conditions_array', true) ); $visitor_conditions_array = is_array($visitor_conditions_array)? array_values($visitor_conditions_array): array(); if ( empty( $visitor_conditions_array ) ) { $visitor_conditions_array[0] =array( 'visitor_conditions' => array( array( 'key_1' => 'device', 'key_2' => 'equal', 'key_3' => 'desktop', ) ) ); } //security check wp_nonce_field( 'adsforwp_visitor_condition_action_nonce', 'adsforwp_visitor_condition_name_nonce' );?> array( 'device' => esc_html__("Device Type",'ads-for-wp'), 'browser_language' => esc_html__("Language Of The Browser",'ads-for-wp'), 'logged_in_visitor' => esc_html__("Logged In",'ads-for-wp'), 'user_agent' => esc_html__("User Agent",'ads-for-wp'), 'user_type' => esc_html__("User Permission",'ads-for-wp'), 'referrer_url' => esc_html__("Referring URL",'ads-for-wp'), 'url_parameter' => esc_html__("URL Parameter",'ads-for-wp'), 'geo_location' => esc_html__("Country",'ads-for-wp'), 'cookie' => esc_html__("Cookie",'ads-for-wp'), ) ); $comparison = array( 'equal' => esc_html__( 'Equal to', 'ads-for-wp'), 'not_equal' => esc_html__( 'Not Equal to', 'ads-for-wp'), ); $total_group_fields = count( $visitor_conditions_array ); ?>
'; }else{ echo '
'.esc_html__( 'Enable Visitor Condition', 'ads-for-wp').''; echo '

'.esc_html__( 'Visitor conditions limit the number of users who can see your ad.', 'ads-for-wp').'

'; echo '
'; } ?>
0){ echo 'Or'; } ?>
adsforwp_visitor_condition_type_values($selected_val_key_1, $selected_val_key_3,$selected_val_key_4,$selected_val_key_5, $i,$j); ?>
adsforwp_get_client_ip(); $saved_ip_list = array(); if(isset($_COOKIE['adsforwp-user-info'])){ $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 == $data ) { $result = true; } } if ( $comparison == 'not_equal') { if ( $country_code != $data ) { $result = true; } } break; case 'url_parameter': $url = esc_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'){ if(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; default: $result = false; break; } return $result; } public function adsforwp_detect_user_agent( ){ $user_agent_name ='others'; if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') || strpos($user_agent, 'OPR/')) $user_agent_name = 'opera'; elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Edge')) $user_agent_name = 'edge'; elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')) $user_agent_name ='firefox'; elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strpos($user_agent, 'Trident/7')) $user_agent_name = 'internet_explorer'; elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'iPod')) $user_agent_name = 'ipod'; elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'iPhone')) $user_agent_name = 'iphone'; elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'iPad')) $user_agent_name = 'ipad'; elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'Android')) $user_agent_name = 'android'; elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'webOS')) $user_agent_name = 'webos'; elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome')) $user_agent_name = 'chrome'; elseif (strpos($_SERVER['HTTP_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'])) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_X_FORWARDED'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_FORWARDED'])) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if(isset($_SERVER['REMOTE_ADDR'])) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; } } if (class_exists('adsforwp_view_visitor_condition')) { new adsforwp_view_visitor_condition; };