<?php global $wpdb;
$getss=$wpdb->query("SHOW TABLES LIKE 'gdpr_website_setting'");
if($getss==0){
$wpdb->query("CREATE TABLE `gdpr_website_setting` (
  `id` int(11) NOT NULL,
  `website` varchar(40) DEFAULT NULL,
  `enable` varchar(10) DEFAULT NULL,
  `datetime` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8");
	$wpdb->query("ALTER TABLE `gdpr_website_setting` ADD PRIMARY KEY(`id`)");
	$wpdb->query("ALTER TABLE `gdpr_website_setting` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT;");
	$wpdb->query("insert into gdpr_website_setting (`website`,`enable`) values('".$_SERVER['SERVER_NAME']."','yes')");
}

$get_qry=$wpdb->get_row("select `enable` from gdpr_website_setting where website='".$_SERVER['SERVER_NAME']."'");
if($get_qry->enable=='yes'){
//REMOVE DEFAULT GOOGLE ANALYTICS CODE
remove_action( 'wp_head', 'google_analytics', 120 );
// ENABLE OR DISABLE GOOGLE ANALYTIC SCRIPT
function google_analytics_custom() {
	
	$script = "";
	$customerGACode = ow_global_var('googleA');
    $customerGA4Code = ow_global_var('googleGA4');
    $irisGACode = ow_global_var('IRISgoogleA');
	$irisGA4Code = ow_global_var('IRISgoogleGA4');
	
	if ( $irisGACode != NULL && $irisGA4Code !== NULL) { 
        
        //To be removed the $irisGACode on the next version 1.5.5 - check the updated code on the IP ticket IP-238
		if($irisGA4Code!=''){
			$script .= "<script async src=\"https://www.googletagmanager.com/gtag/js?id=$irisGA4Code\"></script>
		<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());";
		}else{
		$script .= "<script async src=\"https://www.googletagmanager.com/gtag/js?id=$irisGACode\"></script>
		<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());";}
		
		if ( $customerGACode != NULL) {
			if(!$customerGA4Code){
				$script .= "gtag('config', '$customerGACode');";
				$script .= "gtag('config', '$customerGA4Code');";
			}
			else{
			$script .= "gtag('config', '$customerGACode');";
			}
		}
		else {
			
			developer_console_message( 'Customer Google Tag Manager not supplied', 'warning' );
		}
		$script .= "gtag('config', '$irisGACode');";
		$script .= "gtag('config', '$irisGA4Code');</script>";
		echo $script;
	}
	else {
		
		developer_console_message( 'IRIS OpenWebsite Google Tag Manager not supplied', 'alert' );
	}
}
if(isset($_COOKIE['cookie_analytics']) && $_COOKIE['cookie_analytics'] == "off"){
	remove_action( 'wp_head', 'google_analytics_custom', 120 );
}
else {
	add_action( 'wp_head', 'google_analytics_custom', 120 );
}

//REMOVE DEFAULT COOKIE POPUP
remove_action( 'after_body', 'cookies_bar', 2 ); 

function cookies_bar_custom() {
	
	if ( ! isset( $_COOKIE['ow_ac'] ) ) {
		global $iris_db;
		$cookies_bar = $iris_db->get_row( "SELECT * FROM gdpr_settings WHERE id=4" );
		$cookies = $cookies_bar->content;		
		echo $cookies;
		
	}
	global $iris_db;$cookies_popups = $iris_db->get_row( "SELECT * FROM gdpr_settings WHERE id=1" );
	echo $cookies_popups->content;
	global $iris_db;$cookies_page = $iris_db->get_row( "SELECT * FROM gdpr_settings WHERE id=2" );
	echo '<span style="display:none;" id="getgdpr_content">'.$cookies_page->content.'</span>';
}
add_action( 'wp_footer', 'cookies_bar_custom', 120 );

function load_gdpr_css() {
	global $iris_db;$cookies_js = $iris_db->get_row( "SELECT * FROM gdpr_settings WHERE id=5" );
	echo $cookies_js->content;
	global $iris_db;$cookies_css = $iris_db->get_row( "SELECT * FROM gdpr_settings WHERE id=3" );
	echo $cookies_css->content;
	echo '<div id="webname" style="display: none;">'.ow_global_var('accountant_full_name').'</div><div id="website" style="display: none;">'.ow_global_var('accountant_website').'</div>';
}
if (!is_admin()) {
	add_action('wp_footer', 'load_gdpr_css', 250);
}
//--------------------------------------------------------------------------------------------------------------------------------

// LOAD CSS AND JS

}
else {
add_action( 'wp_head', 'google_analytics', 120 );
}
function gdprsubmenu() { 
 add_submenu_page( 'iris-dashboard', 'gdpr settings', 'GDPR Cookies', 'edit_posts', 'gdpr-settings', 'gdpr_settings' );
}
add_action('admin_menu', 'gdprsubmenu');

function gdpr_settings() {
	
	require_once plugin_dir_path( __DIR__ ) . 'admin/page-gdpr-settings.php'; 
}

?>