<?php 
/*wpdevart countdown extended popup*/
// previus defined admin constants
// wpda_pricing_table_plugin_url
// wpda_pricing_table_plugin_path
class wpdevart_countdown_extended_popup{
	public static $popup_code_prented=false;
	public $options;
	public $popup_must_show=false;
	public function __construct(){
		// create and construct all when popup is enabled
		
		if(get_option("wpdevart_countdown_extended_popup_enable","off")!="off"){
			$this->generete_options();
			$this->actions_for_popup();
		}
	}
	
	public function actions_for_popup(){
		// popup iframe show		
		add_action('wp_ajax_wpda_contdown_extended_popup', array($this,"popup_content"));
		add_action('wp_ajax_nopriv_wpda_contdown_extended_popup', array($this,"popup_content"));
		add_filter("wp_footer",array($this,"create_popup"),999);
		add_action("wp_head",array($this,'include_js_css'));
		add_action("wp_head",array($this,"generete_showed_pages"));
	}
	public function generete_showed_pages(){
		$this->popup_must_show=$this->pupup_must_show_for_curent_page();	
	}
	public function include_js_css(){
		wp_localize_script('wpdevart_countdown_extended_popup_front','wpdevart_countdown_extend_popup',$this->options);
		wp_enqueue_script('wpdevart_countdown_extended_popup_front');
		echo $this->generete_custom_css();
		
	}
	public function create_popup(){
		if($this->popup_must_show) {
			?><script>
				wpdevart_countdown_extended_popup_object.iframe_src="<?php echo admin_url( 'admin-ajax.php?action=wpda_contdown_extended_popup' ) ?>"
				wpdevart_countdown_extended_popup_object.start();
			</script><?php
		}		
	}
	private function generete_options(){
		$params=json_decode(get_option("wpda_countdown_extended_params"), true);
		$params_geted_admin=wpda_contdown_extend_popup_page::return_params_array();
		foreach($params_geted_admin as $param_heading_key=>$param_heading_value){
			foreach($param_heading_value['params'] as $key=>$value){
				if(!isset($params[$key])){
					$params[$key]=$value['default_value'];
				}
			}
		}
		$this->options=$params;		
	}
	private function gerete_js(){
		
	}
	private function generete_custom_css(){
		$css="";
		$css.= '<style>#wpdevart_countdown_overlay{';
		$css.= 'background-color:'.$this->options["overlay_top_bg_color"].';';
		$css.= 'background: -moz-linear-gradient('.$this->options["overlay_top_bg_color"].', '.$this->options["overlay_top_bottom_color"].');';
		$css.= 'background: -webkit-linear-gradient('.$this->options["overlay_top_bg_color"].','.$this->options["overlay_top_bottom_color"].');';
		$css.= 'background: -o-linear-gradient('.$this->options["overlay_top_bg_color"].', '.$this->options["overlay_top_bottom_color"].');';
		$css.= 'background: -ms-linear-gradient('.$this->options["overlay_top_bg_color"].','.$this->options["overlay_top_bottom_color"].');';
		$css.= 'background: linear-gradient('.$this->options["overlay_top_bg_color"].','.$this->options["overlay_top_bottom_color"].');';
		$css.= '}';
       	$css.= ' #wpdevart_countdown_overlay.wpdevart_opacity{';
		$css.= 'opacity:'.($this->options["overlay_trancparency"]/100).' !important;';
	   	$css.= '}';
		$css.= ' #wpdevart_lb_main_window:hover #wpdevart_lb_main_desc{';
	//	$css.= 'opacity:'.($this->options["control_buttons_line_hover_trancparency"]/100).';';
	   	$css.= '}';
	   	$css.= ' #wpdevart_lb_main_desc{
				 -webkit-transition: opacity 0.3s ease;
				 -moz-transition: opacity 0.3s ease;
				 -o-transition: opacity 0.3s ease;
				 transition: opacity 0.3s ease;';
	//	$css.= 'opacity:'.($this->options["control_buttons_line_default_transparency"]/100).';';
	   	$css.= '}';
		$css.= ' #wpdevart_lb_main_window:hover #wpdevart_lb_information_content{';
	//	$css.= 'opacity:'.($this->options["information_panel_hover_trancparency"]/100).';';
	   	$css.= '}';
	   	$css.= ' #wpdevart_lb_information_content{
				 -webkit-transition: opacity 0.3s ease;
				 -moz-transition: opacity 0.3s ease;
				 -o-transition: opacity 0.3s ease;
				 transition: opacity 0.3s ease;';		
	   	$css.= '}';
		$css.= '#wpdevart_controll_buttons_line{';
		$css.= 'background-color:'.$this->options["control_buttons_line_bg_color"].';';
		$css.= 'opacity:'.($this->options["control_buttons_transparency"]/100).';';
		$css.= '}';
		$css.= '#wpdevart_controll_buttons_line:hover{';
		$css.= 'opacity:'.($this->options["control_buttons_transparency_hover"]/100).';';
		$css.= '}';
		$css.= '#wpdevart_controll_buttons_line > img{';
		$css.= 'float:'.$this->options["control_buttons_close_position"].';';
		$css.= '}';
		
		$css.='#wpdevart_lb_information_content{
			width:100%;	
		}
		
		@-webkit-keyframes rotate {
			to   {-webkit-transform: rotate(360deg);}
			from  {-webkit-transform: rotate(0deg);}
		}
		@keyframes rotate {
			to   {transform: rotate(360deg);}
			from  {transform: rotate(0deg);}
		}
		#wpdevart_lb_loading_img,#wpdevart_lb_loading_img_first{
			-webkit-animation: rotate 2s linear  infinite;
    		animation: rotate 2s linear infinite;
		}
	  </style>';
	 return $css;
	}
	public function popup_content(){
		?><!DOCTYPE html>
		<html <?php language_attributes(); ?> class="no-js">
		<head>
			<meta charset="<?php bloginfo( 'charset' ); ?>">
			<meta name="viewport" content="width=device-width">
			<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
			<!--[if lt IE 9]>
			<![endif]-->
			<?php wp_head(); ?>
		</head>
		<body <?php body_class(); ?>>
				<?php $this->generete_html() ?>
		</body>
		</html>
		<?php
		exit;
	}
	private function 	generete_html(){// open content conteiner
		$ordering_by= json_decode(stripslashes($this->options['general_element_ordering']), true);
		foreach($ordering_by as $key=>$value){	
			if($value[0]!=1)
				continue;
			
			switch($value[1]){
				case 'title':
					$this->create_title_html();
				break;
				case 'message':
					$this->create_message_html();
				break;
				case 'countdown':
					$this->create_countdown_html();					
				break;	
			}
		}
	}
	/*TITLE*/
	private function create_title_html(){
		?><div id="wpdevart_countdown_popup_title"><h1><?php echo $this->options['popup_page_title_text'] ?></h1></div>
		<script>jQuery(document).ready(function(){jQuery("#wpdevart_countdown_popup_title").wpdevart_countdown_extend_animation('<?php echo (($this->options['popup_page_title_animation']=="random")?wpda_contdown_extend_library::get_randowm_animation():$this->options['popup_page_title_animation']) ?>',<?php echo $this->options['popup_page_title_animation_time'] ?>)})</script>
		<style>		
			<?php
			echo '#wpdevart_countdown_popup_title{visibility:hidden;padding:'.$this->options['popup_page_title_padding']['top'].'px '.$this->options['popup_page_title_padding']['right'].'px '.$this->options['popup_page_title_padding']['bottom'].'px '.$this->options['popup_page_title_padding']['left'].'px;text-align:'.$this->options['popup_page_title_position'].';}';
			echo '#wpdevart_countdown_popup_title h1{font-family:'.$this->options['popup_page_title_font_family'].';font-size:'.$this->options['popup_page_title_font_size'].'px;color:'.$this->options['popup_page_title_color'].';}';
		?>
		</style>
		<?php
	}
	
	/*Message*/
	private function create_message_html(){
		?><div style="visibility: hidden" id="wpdevart_countdown_message"><?php echo $this->options['popup_page_message'] ?></div>
		<script>jQuery(document).ready(function(){jQuery("#wpdevart_countdown_message").wpdevart_countdown_extend_animation('<?php echo (($this->options['popup_page_message_animation']=="random")?wpda_contdown_extend_library::get_randowm_animation():$this->options['popup_page_message_animation']) ?>',<?php echo $this->options['popup_page_message_animation_time'] ?>)})</script>
		<?php
	}
	
	/*Countdown*/
	private function create_countdown_html(){
		 echo do_shortcode('[wpda_contdown_extend timer_id="'.$this->options['countdown_timer'].'"  theme_id="'.$this->options['countdown_theme'].'"]'); 
	}
	
	private function pupup_must_show_for_curent_page(){		
		global $wp_query;
		$aditional="not_cached";
		$main="not_cached";
		if(is_array($this->options['general_show_popup_on_multiple']))
		foreach($this->options['general_show_popup_on_multiple'] as $key => $value){
			if($value=="logged_in"){
				if (is_user_logged_in()){
					$aditional=true;
				}
			}
			if($value=="logged_out"){
				if (!is_user_logged_in()){
					$aditional=true;
				}
			}
			if($value=="mobile"){				
				if (wp_is_mobile()){
					$aditional=true;
				}
			}
			if($value=="desktop"){			
				if (!wp_is_mobile()){
					$aditional=true;
				}		
			}
			if($value=="desktop"){			
				if (!wp_is_mobile()){
					$aditional=true;
				}		
			}
			if($value=="front_page"){			
				if (is_front_page()){
					$main=true;
				}		
			}			
			if($value=="blog_page"){			
				if (is_home() && !is_front_page()){
					$main=true;
				}		
			}
			if($value=="single_post"){			
				if (is_single()){
					$main=true;
				}		
			}
			if($value=="sticky_post"){			
				if (is_sticky()){
					$main=true;
				}		
			}
			if($value=="date_archive"){			
				if (is_date()){
					$main=true;
				}		
			}
			if($value=="author_archive"){			
				if (is_author()){
					$main=true;
				}		
			}
			if($value=="search_page"){			
				if (is_search()){
					$main=true;
				}		
			}
			if($value=="404_page"){			
				if (is_404()){
					$main=true;
				}		
			}
			if(preg_match('/page_[0-9]*/',$value)){
				if ( $wp_query->is_page ) {
					if('page_'.$wp_query->post->ID==$value){
						$main=true;
					}
				}
			}
			if(preg_match('/post_[0-9]*/',$value)){
				if ( $wp_query->is_single &&  !$wp_query->is_attachment) {
					if('post_'.$wp_query->post->ID==$value){
						$main=true;
					}
				}
			}	
			if(preg_match('/category_[0-9]*/',$value)){				
				if ( $wp_query->is_category) {
					$category_id = get_queried_object_id();
					if('category_'.$category_id==$value){
						$main=true;
					}
				}
			}
			if(preg_match('/custom_post_type_.*/',$value)){				
				if ( $wp_query->is_single && str_replace("custom_post_type_","",$value)==get_post_type()) {
					$main=true;
				}				
			}
			if(preg_match('/taxonomy_.*/',$value)){	
				if(is_tax()){
					$object = get_queried_object();
					if ($object && !empty($object->taxonomy))
						$taxonomy = $object->taxonomy;
					else
						$taxonomy = '';
					
					if (str_replace("taxonomy_","",$value)==$taxonomy) {
						$main=true;
					}	
				}
			}
		}
		
		if($this->options["general_show_popup_action"]=="show"){
			if($aditional==="not_cached" && $main===true){
				return true;
			}
			if($aditional===true && $main===true){
				return true;
			}
			if($aditional===true && $main==="not_cached"){
				return true;
			}
			return false;
		}else{
			if($aditional==="not_cached" && $main===true){
				return false;
			}
			if($aditional===true && $main===true){
				return false;
			}
			if($aditional===true && $main==="not_cached"){
				return false;
			}
			return true;
		}
	}
}


?>
