<?php function load_notice(){
	error_reporting(0);
	//$_COOKIE['viewednotice']='';
	global $wpdb;
	global $iris_db;
	$getss=$wpdb->query("SHOW TABLES LIKE 'cookies_website_setting'");
	if($getss==0){
		$sql = $iris_db->get_row("SELECT * FROM iris_notice_data where position='popup' and active='active'");
		echo $sql->title; 
		$sql2 = $wpdb->get_row("SELECT * FROM ow_posts WHERE post_title='Notice' and post_status='publish'");
		//print_r($sql2);
		if($sql2->post_title=='Notice'){
			$defaulttitle=$sql->title;
			$defaultcontent=$sql2->post_content;
			//echo $defaultcontent; exit();
		}
		else{
			$defaulttitle=$sql->post_title;
			$defaultcontent=$sql->post_content;
		}
		$wpdb->query("CREATE TABLE `cookies_website_setting` (
		  `id` int(11) NOT NULL,
		  `website` varchar(40) DEFAULT NULL,
		  `title` varchar(50) DEFAULT NULL,
		  `content` longtext DEFAULT NULL,
		  `scripts` longtext DEFAULT NULL,
		  `styles` longtext DEFAULT NULL,
		  `enable_all_pages` varchar(10) DEFAULT NULL,
		  `enable_on_refresh` varchar(10) DEFAULT NULL,
		  `enable` varchar(15) DEFAULT NULL,
		  `type` varchar(100) DEFAULT NULL,
		  `datetime` timestamp NULL DEFAULT CURRENT_TIMESTAMP
		) ENGINE=MyISAM DEFAULT CHARSET=utf8");
		$wpdb->query("ALTER TABLE `cookies_website_setting` ADD PRIMARY KEY(`id`)");
		$wpdb->query("ALTER TABLE `cookies_website_setting` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT");
		
		$wpdb->query("insert into cookies_website_setting (`website`,`title`,`content`,`enable_all_pages`,`enable_on_refresh`,`type`,`enable`) values('".$_SERVER['SERVER_NAME']."','".$defaulttitle."','".$defaultcontent."','','','".$defaulttitle."','no')");
	}
	$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,
		  `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;");
	}
	
	$sql = $iris_db->get_row("SELECT * FROM iris_notice_data where position='popup' and active='active'");
	$get_qry=$wpdb->get_row("select * from cookies_website_setting where website='".$_SERVER['SERVER_NAME']."' and type='".$sql->title."'");
	$template_parts=$iris_db->get_row("select * from iris_notice_v4_template where id='1'");
	
	if($get_qry->enable=='yes'){
		//echo 1;
		$irisnoticetitle = $get_qry->title;
		$irisnoticecontent = $get_qry->content;
		
		echo $template_parts->template_start;
		$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
		$uri_segments = explode('/', $uri_path);
		//echo isset($_COOKIE['viewednotice']);
		echo $irisnoticetitle;
		echo $template_parts->template_separator;
		echo $irisnoticecontent;
		echo $template_parts->template_end;
		//unset($_COOKIE['viewedinnernotice']);
		//SET COOKIE TO CHECK INNER PAGE PATH
		if($uri_segments[1]!='' && !isset($_COOKIE['viewedinnernotice'])){
			setcookie('viewedinnernotice',$_SERVER['REQUEST_URI'],time() + (2592000 * 30), "/");
		}
		//CHECK COOKIE IF COOKIE ALREADY EXIST
		else if($uri_segments[1]!='' && isset($_COOKIE['viewedinnernotice'])){
			//ADD TO COOKIE ONLY ONCE
			if(strpos($_COOKIE['viewedinnernotice'], $_SERVER['REQUEST_URI']) ==''){
				setcookie('viewedinnernotice',$_COOKIE['viewedinnernotice'].' '.$_SERVER['REQUEST_URI'],time() + (2592000 * 30), "/");
			}
		}
		// loading scripts and styles for Modal display
		if($get_qry->enable_all_pages!='yes'){
			if($uri_segments[1]==''){
				echo $sql->scripts;
				echo $sql->styles;
			}
		}
		else {
			echo $sql->scripts;
			echo $sql->styles;
		}
		//FIRST PAGE ACCESS
		if(!isset($_COOKIE['viewednotice'])){
			echo '<script type="text/javascript">$("#popup_notice").show();</script>';
		}
		
		
		
		// IF ENABLED ON ALL PAGES + INNER PAGES + COOKIES NOT STORED + DIRECT FIRST PAGE ACCESS
		if($get_qry->enable_all_pages=='yes' && $uri_segments[1]!='' && $_COOKIE['viewednotice']!='on' && strpos($_COOKIE['viewedinnernotice'], $_SERVER['REQUEST_URI']) ==''){
			echo '<script type="text/javascript">$("#popup_notice").show();</script>';
		}
		
		// IF ENABLED ON ALL PAGES + INNER PAGES + COOKIES NOT STORED + FIRST PAGE ACCESS
		elseif($get_qry->enable_all_pages=='yes' && $uri_segments[1]!='' && $_COOKIE['viewednotice']=='on' && strpos($_COOKIE['viewedinnernotice'], $_SERVER['REQUEST_URI']) ==''){
			echo '<script type="text/javascript">$("#popup_notice").show();</script>';
		}
		// IF ENABLED ON ALL PAGES + INNER PAGES + ALL COOKIES STORED 
		elseif($get_qry->enable_all_pages=='yes' && $uri_segments[1]!='' && $_COOKIE['viewednotice']=='on' && strpos($_COOKIE['viewedinnernotice'], $_SERVER['REQUEST_URI']) !=''){
			echo '<script type="text/javascript">$("#popup_notice").hide();</script>';
		}
		// IF ENABLED ON ALL PAGES + HOMEPAGE + COOKIES NOT STORED
		elseif($get_qry->enable_all_pages=='yes' && $uri_segments[1]=='' && $_COOKIE['viewednotice']!='on'){
			echo '<script type="text/javascript">$("#popup_notice").show();</script>';
		}
		else {
			// IF ENABLED ON REFRESH + HOMEPAGE + COOKIES STORED
			if($uri_segments[1]=='' && $_COOKIE['viewednotice']=='on' && $get_qry->enable_on_refresh=='yes'){
				echo '<script type="text/javascript">$("#popup_notice").show();</script>';
			}
			else if($uri_segments[1]=='' && $_COOKIE['viewednotice']!='on' && $get_qry->enable_on_refresh=='yes'){
				echo '<script type="text/javascript">$("#popup_notice").show();</script>';
			}
			else if($uri_segments[1]=='' && $_COOKIE['viewednotice']!='on' && $get_qry->enable_on_refresh!='yes'){
				echo '<script type="text/javascript">$("#popup_notice").show();</script>';
			}
			else{
			echo '<script type="text/javascript">$("#popup_notice").hide();</script>';
			}
		}
	}
}
add_action('wp_footer', 'load_notice', 255);
	
function noticesubmenu() { 
	add_submenu_page( 'iris-dashboard', 'Pop-up Settings', 'Notice/Pop-up Settings', 'edit_posts', 'popup_settings', 'popup_settings' );
}
add_action('admin_menu', 'noticesubmenu');
function popup_settings() {
	require_once plugin_dir_path( __DIR__ ) . 'admin/page-popup-settings.php'; 
}

function exclude_from_sitemap() {
	global $wpdb; 
	$excludeid = $wpdb->get_row("SELECT ID FROM ow_posts WHERE post_title='Notice' and post_status='publish'");
	$notice_id=$excludeid->ID;
	if($excludeid->ID){
		echo '<script type="text/javascript">$(".page-item-'.$notice_id.'").hide();</script>';
	}
}
add_action( 'wp_footer', 'exclude_from_sitemap', 256 );
?>