<?php // Google Analytics - Opt-Out Stuff

if (!defined('ABSPATH')) die();

function ga_google_analytics_pro_optout() {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$optout = (isset($options_optout['optout_enable']) && !empty($options_optout['optout_enable'])) ? 1 : 0;
	
	return $optout;
	
}

function ga_google_analytics_pro_optout_footer() {
	
	if (ga_google_analytics_pro_optout()) {
		
		echo ga_google_analytics_pro_optout_output();
		
	}
	
}

// [ga_pro_optout]

function ga_google_analytics_pro_optout_output($attr = null) {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$location     = isset($options_optout['css_location']) ? $options_optout['css_location'] : 0;
	$disable_hide = isset($options_optout['disable_hide']) ? $options_optout['disable_hide'] : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	$close  = isset($_COOKIE['gapro-close'])  ? $_COOKIE['gapro-close']  : 0;
	
	if (!$disable_hide && $close && ($optout || $optin)) return '';
	
	$message = ga_google_analytics_pro_optout_message();
	
	$title_info = __('Click for more information', 'ga-google-analytics-pro');
	
	$title_hide = __('Click to close this notice', 'ga-google-analytics-pro');
	
	$title_info = apply_filters('ga_google_analytics_pro_optout_title_info', $title_info);
	
	$title_hide = apply_filters('ga_google_analytics_pro_optout_title_hide', $title_hide);
	
	if ($location === 0 || $location === 1) {
		
		$output = '<div id="ga-pro" class="ga-google-analytics-pro-optout">';
		
		$output .= '<div class="ga-google-analytics-pro-optout-content">';
		
		$output .= '<a class="ga-google-analytics-pro-optout-icon ga-google-analytics-pro-optout-info" href="#ga-optout-info" title="'. esc_attr($title_info) .'">?</a>';
		
		$output .= '<div class="ga-google-analytics-pro-optout-message">'. $message .'</div>';
		
		$output .= '<a class="ga-google-analytics-pro-optout-icon ga-google-analytics-pro-optout-close" href="#ga-optout-hide" title="'. esc_attr($title_hide) .'">&#10005;</a>';
		
		$output .= '</div>';
		
		$output .= '</div>';
		
	} else {
		
		$output = '<div id="ga-pro" class="ga-google-analytics-pro-optout">';
		
		$output .= '<div class="ga-google-analytics-pro-optout-content">';
		
		$output .= '<span class="ga-google-analytics-pro-optout-message">'. $message .'</span> ';
		
		$output .= '<a class="ga-google-analytics-pro-optout-icon ga-google-analytics-pro-optout-info ga-google-analytics-pro-optout-center" href="#ga-optout-info" title="'. esc_attr($title_info) .'">?</a> ';
		
		$output .= '<a class="ga-google-analytics-pro-optout-icon ga-google-analytics-pro-optout-close" href="#ga-optout-hide" title="'. esc_attr($title_hide) .'">&#10005;</a>';
		
		$output .= '</div>';
		
		$output .= '</div>';
		
	}
	
	return apply_filters('ga_google_analytics_pro_optout_output', $output);
	
}

function ga_google_analytics_pro_optout_message() {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$message = isset($options_optout['optout_message']) ? $options_optout['optout_message'] : ''; 
	
	$message = ga_google_analytics_pro_optout_message_filter($message);
	
	return apply_filters('ga_google_analytics_pro_optout_message', $message);
	
}

function ga_google_analytics_pro_using_shortcode() {
	
	global $posts;
	
	if (empty($posts)) return false;
	
	$using = false;
	
	foreach ($posts as $post) {
		
		if (stripos($post->post_content, '[ga_pro_optout]') !== false) {
			
			$using = true;
			
			break;
			
		}
		
	}
	
	return $using;
	
}

function ga_google_analytics_pro_script_loader_tag($tag, $handle) {
	
	if ($handle === 'ga-google-analytics-pro' && false === stripos($tag, 'defer')) {
		
		return str_replace('<script ', '<script defer ', $tag);
		
	}
	
	return $tag;
	
}

function ga_google_analytics_pro_wp_enqueue_scripts() {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$disable_hide   = isset($options_optout['disable_hide'])   ? $options_optout['disable_hide']   : 0;
	$disable_styles = isset($options_optout['disable_styles']) ? $options_optout['disable_styles'] : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	$close  = isset($_COOKIE['gapro-close'])  ? $_COOKIE['gapro-close']  : 0;
	
	wp_register_script('ga-pro-optout-common', GAPRO_URL .'js/optout-common.js', array('jquery'), GAPRO_VERSION, true);
	
	wp_register_script('ga-pro-optout-box', GAPRO_URL .'js/optout-box.js', array('jquery', 'ga-pro-optout-common'), GAPRO_VERSION, true);
	
	wp_register_script('ga-pro-optout-plain', GAPRO_URL .'js/optout-plain.js', array('jquery', 'ga-pro-optout-common'), GAPRO_VERSION, true);
	
	//
	
	if (!$disable_hide && $close && ($optout || $optin)) return;
	
	if (ga_google_analytics_pro_optout() || ga_google_analytics_pro_using_shortcode()) {
		
		wp_enqueue_script('ga-pro-optout-common');
		
		wp_enqueue_script('ga-pro-optout-box');
		
		ga_google_analytics_pro_wp_localize_script();
		
		if (empty($disable_styles)) {
			
			$custom      = isset($options_optout['css_custom'])      ? $options_optout['css_custom']           : '';
			$location    = isset($options_optout['css_location'])    ? intval($options_optout['css_location']) : 0;
			$color_text  = isset($options_optout['css_color_text'])  ? $options_optout['css_color_text']       : '#dce8ee';
			$color_bg    = isset($options_optout['css_color_bg'])    ? $options_optout['css_color_bg']         : '#5599bb';
			$color_link  = isset($options_optout['css_color_link'])  ? $options_optout['css_color_link']       : '#ffffff';
			$color_hover = isset($options_optout['css_color_hover']) ? $options_optout['css_color_hover']      : '#dce8ee';
			
			if ($location === 0 || $location === 1) {
				
				wp_register_style('ga-pro-optout', GAPRO_URL .'css/optout.css', array(), GAPRO_VERSION);
				
				wp_enqueue_style('ga-pro-optout', GAPRO_URL .'css/optout.css', array(), GAPRO_VERSION);
				
			} else {
				
				wp_register_style('ga-pro-optout', GAPRO_URL .'css/optout-center.css', array(), GAPRO_VERSION);
				
				wp_enqueue_style('ga-pro-optout', GAPRO_URL .'css/optout-center.css', array(), GAPRO_VERSION);
				
			}
			
			ga_google_analytics_pro_add_inline_style($custom, $location, $color_text, $color_bg, $color_link, $color_hover);
			
		}
		
	}
	
}

function ga_google_analytics_pro_wp_localize_script() {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$css_location   = isset($options_optout['css_location'])   ? $options_optout['css_location']   : 0;
	$disable_hide   = isset($options_optout['disable_hide'])   ? $options_optout['disable_hide']   : 0;
	$after_optin    = isset($options_optout['after_optin'])    ? $options_optout['after_optin']    : 0;
	$success_optout = isset($options_optout['success_optout']) ? $options_optout['success_optout'] : '';
	$success_optin  = isset($options_optout['success_optin'])  ? $options_optout['success_optin']  : '';
	$info_text      = isset($options_optout['info_text'])      ? $options_optout['info_text']      : '';
	
	$success_optout = ga_google_analytics_pro_optout_message_filter($success_optout);
	$success_optin  = ga_google_analytics_pro_optout_message_filter($success_optin);
	
	$cookie_optout  = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	$cookie_optin   = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	$cookie_close   = isset($_COOKIE['gapro-close'])  ? $_COOKIE['gapro-close']  : 0;
	
	$array = array(
		
		'cookie_optout'  => $cookie_optout,
		'cookie_optin'   => $cookie_optin,
		'cookie_close'   => $cookie_close,
		'css_location'   => $css_location,
		'disable_hide'   => $disable_hide,
		'after_optin'    => $after_optin,
		'success_optout' => $success_optout,
		'success_optin'  => $success_optin,
		'info_text'      => $info_text,
		
	);
	
	wp_localize_script('ga-pro-optout-box', 'gapro', $array);
	
}

function ga_google_analytics_pro_add_inline_style($custom, $location, $color_text, $color_bg, $color_link, $color_hover) {
	
	$style = '';
	
	if ($location === 0 || $location === 1) {
		
		$position = $location ? 'top' : 'bottom';
		
		$style .= "#ga-pro.ga-google-analytics-pro-optout { $position: 0; color: ". sanitize_hex_color($color_text) ."; background-color: ". sanitize_hex_color($color_bg) ."; }";
		
	} else { // center
		
		$style .= "#ga-pro.ga-google-analytics-pro-optout .ga-google-analytics-pro-optout-content { color: ". sanitize_hex_color($color_text) ."; background-color: ". sanitize_hex_color($color_bg) ."; }";
		
	}
	
	$style .= "#ga-pro.ga-google-analytics-pro-optout a:link, #ga-pro.ga-google-analytics-pro-optout a:visited { color: ". sanitize_hex_color($color_link) ."; }";
	
	$style .= "#ga-pro.ga-google-analytics-pro-optout a:hover, #ga-pro.ga-google-analytics-pro-optout a:active { color: ". sanitize_hex_color($color_hover) ."; text-decoration: none; }";
	
	$style .= $custom;
	
	wp_add_inline_style('ga-pro-optout', $style);
	
}

function ga_google_analytics_pro_optout_message_filter($message) {
	
	$patterns = array();
	$patterns[0] = "/\[ga_pro_optout_link\]/";
	$patterns[1] = "/\[ga_pro_optin_link\]/";
	
	$replacements = array();
	$replacements[0] = ga_google_analytics_pro_optout_link();
	$replacements[1] = ga_google_analytics_pro_optin_link();
	
	$message = preg_replace($patterns, $replacements, $message);
	
	return apply_filters('ga_google_analytics_pro_filter_message', $message);
	
}

// [ga_pro_optout_link text="Link Text" title="Link Title Attribute"]

function ga_google_analytics_pro_optout_link($attr = null) {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$text  = isset($options_optout['optout_text']) ? $options_optout['optout_text'] : __('Click here to opt out', 'ga-google-analytics-pro');
	
	$title = apply_filters('ga_google_analytics_pro_optout_title', __('Click here to opt out of Google Analytics', 'ga-google-analytics-pro'));
	
	if ($attr) {
		
		extract(shortcode_atts(array(
			
			'text'  => $text,
			'title' => $title,
			
		), $attr));
		
	}
	
	$link = '<a class="ga-google-analytics-pro-optout-link" href="#ga-optout-link" title="'. esc_attr($title) .'">'. esc_html($text) .'</a>';
	
	return apply_filters('ga_google_analytics_pro_optout_link', $link);
	
}

// [ga_pro_optin_link text="Link Text" title="Link Title Attribute"]

function ga_google_analytics_pro_optin_link($attr = null) {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$text  = isset($options_optout['optin_text']) ? $options_optout['optin_text'] : __('I understand and agree', 'ga-google-analytics-pro');
	
	$title = apply_filters('ga_google_analytics_pro_optin_title', __('Continue using Google Analytics', 'ga-google-analytics-pro'));
	
	if ($attr) {
		
		extract(shortcode_atts(array(
			
			'text'  => $text,
			'title' => $title,
			
		), $attr));
		
	}
	
	$link = '<a class="ga-google-analytics-pro-optin-link" href="#ga-optin-link" title="'. esc_attr($title) .'">'. esc_html($text) .'</a>';
	
	return apply_filters('ga_google_analytics_pro_optin_link', $link);
	
}

// [gapro_optin_plain text="Link Text" title="Link Title Attribute" alert="Popup Alert Message"]

function ga_google_analytics_pro_optin_plain($attr = null) {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$text  = isset($options_optout['optin_text']) ? $options_optout['optin_text'] : __('I understand and agree', 'ga-google-analytics-pro');
	
	$title = apply_filters('ga_google_analytics_pro_optin_plain_title', __('Continue using Google Analytics', 'ga-google-analytics-pro'));
	
	$alert = isset($options_optout['success_optin']) ? $options_optout['success_optin'] : __('Thanks. We will continue using Google Analytics to help improve your experience on our site.', 'ga-google-analytics-pro');
	
	$toggle = '';
	
	if (is_array($attr)) {
		
		extract(shortcode_atts(array(
			
			'text'  => $text,
			'title' => $title,
			'alert' => $alert,
			
		), $attr));
		
	} else {
		
		$class = $attr ? 'gapro-toggle gapro-toggle-optin' : 'gapro-optin-plain';
		
	}
	
	$link = '<a class="'. esc_attr($class) .'" href="#gapro-optin" data-alert="'. esc_attr($alert) .'" title="'. esc_attr($title) .'">'. esc_html($text) .'</a>';
	
	wp_enqueue_script('ga-pro-optout-common');
	wp_enqueue_script('ga-pro-optout-plain');
	
	return apply_filters('ga_google_analytics_pro_optin_plain', $link);
	
}

// [gapro_optout_plain text="Link Text" title="Link Title Attribute" alert="Popup Alert Message"]

function ga_google_analytics_pro_optout_plain($attr = null) {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$text  = isset($options_optout['optout_text']) ? $options_optout['optout_text'] : __('Click here to opt out', 'ga-google-analytics-pro');
	
	$title = apply_filters('ga_google_analytics_pro_optout_plain_title', __('Click here to opt out', 'ga-google-analytics-pro'));
	
	$alert = isset($options_optout['success_optout']) ? $options_optout['success_optout'] : __('Thanks. We set a cookie in your browser, so Google Analytics will not track your future visits.', 'ga-google-analytics-pro');
	
	$toggle = '';
	
	if (is_array($attr)) {
		
		extract(shortcode_atts(array(
			
			'text'  => $text,
			'title' => $title,
			'alert' => $alert,
			
		), $attr));
		
	} else {
		
		$class = $attr ? 'gapro-toggle gapro-toggle-optout' : 'gapro-optout-plain';
		
	}
	
	$link = '<a class="'. esc_attr($class) .'" href="#gapro-optout" data-alert="'. esc_attr($alert) .'" title="'. esc_attr($title) .'">'. esc_html($text) .'</a>';
	
	wp_enqueue_script('ga-pro-optout-common');
	wp_enqueue_script('ga-pro-optout-plain');
	
	return apply_filters('ga_google_analytics_pro_optout_plain', $link);
	
}

// [gapro_optout_toggle] = toggles default links
// [gapro_optout_toggle text1="Opt Out" text2="Opt In" title1="Opt Out" title2="Opt In" alert1="You Opted Out" alert2="You Opted In"] = custom toggle

function ga_google_analytics_pro_optout_toggle($attr = null) {
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$text1 = $text2 = $title1 = $title2 = $alert1 = $alert2 = '';
	
	if ($attr) {
		
		extract(shortcode_atts(array(
			
			'text1'  => $text1,
			'text2'  => $text2,
			'title1' => $title1,
			'title2' => $title2,
			'alert1' => $alert1,
			'alert2' => $alert2,
			
		), $attr));
		
	}
	
	$link  = ga_google_analytics_pro_optout_plain(true);
	$link .= ga_google_analytics_pro_optin_plain(true);
	
	$link_custom  = '<a class="gapro-toggle gapro-toggle-optout" href="#gapro-optout" data-alert="'. esc_attr($alert1) .'" title="'. esc_attr($title1) .'">'. esc_html($text1) .'</a>';
	$link_custom .= '<a class="gapro-toggle gapro-toggle-optin"  href="#gapro-optin"  data-alert="'. esc_attr($alert2) .'" title="'. esc_attr($title2) .'">'. esc_html($text2) .'</a>';
	
	if ($optout) {
		
		$link = (empty($text1) && empty($text2)) ? $link : $link_custom;
		
	} else {
		
		$link = (empty($text1) && empty($text2)) ? $link : $link_custom;
		
	}
	
	wp_enqueue_script('ga-pro-optout-common');
	wp_enqueue_script('ga-pro-optout-plain');
	
	return apply_filters('ga_google_analytics_pro_optout_plain', $link);
	
}
