<?php
/*
Plugin Name: Visual Composer Extensions - Ultimate Magic popup
Description: Elegant, responsive, flexible and lightweight modal plugin with jQuery.
Author: Crystalwing
Version: 1.0.0
Author URI: http://codecanyon.net/user/crystalwing?ref=crystalwing
*/
if (!class_exists( 'VC_Extensions_UMP' ) ) {

    class VC_Extensions_UMP {
        function VC_Extensions_UMP() {
            add_shortcode( 'ump_vc_modal', array( &$this, 'ultimate_magic_vc_modal_func') );

         vc_map( array(
            "name" => __("Ultimate Magic Popup", 'js_composer'),
            "base" => "ump_vc_modal",
            "class" => "ump_vc_modal_extension",
            "controls" => "full",
            "icon" => "icon-ump_vc_modal__extension_cq",
            "category" => __('Crystalwing Extensions', 'js_composer'),
            "params" => array(
              array(
                "type" => "textarea_raw_html",
                "holder" => "div",
                "class" => "", // js_composer_textarea_raw
                "heading" => __("Link label", 'js_composer'),
                "param_name" => "buttontext",
                "value" => base64_encode( 'Click to open popup' ),
                "description" => __("The link user click to open the modal, support HTML and other shortcode.", 'js_composer')
              ),
			  array(
                "type" => "dropdown",
                "holder" => "div",
                "class" => "", // js_composer_textarea_raw
                "heading" => __("Select popup type", 'js_composer'),
                "param_name" => "popup_type",
			    'value' => array(
						__( 'content', 'js_composer' ) => 'content',
						__( 'iframe', 'js_composer' ) => 'iframe',
				),
                "description" => __("Popup type iframe or content", 'js_composer')
              ),
			  array(
                "type" => "textarea_html",
                "holder" => "div",
                "class" => "",
                "heading" => __("Content", 'vc_notify_cq'),
                "param_name" => "content",
                "value" => __("<p>I am test text block. Click edit button to change this text.</p>", 'js_composer'),
                "description" => __("", 'vc_notify_cq'),
				'dependency' => array(
					'element' => 'popup_type',
					'value' => array( 'content' ),
				),
              ),
			  array(
                "type" => "textfield",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("iframe Url", 'js_composer'),
                "param_name" => "iframeurl",
                "value" => __("https://player.vimeo.com/video/22439234?autoplay=1", 'js_composer'),
                "description" => __("", 'js_composer'),
				'dependency' => array(
					'element' => 'popup_type',
					'value' => array( 'iframe' ),
				),
              ),
			  array(
                "type" => "textfield",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("Popup iframe Height", 'js_composer'),
                "param_name" => "iframeheight",
                "value" => __("", 'js_composer'),
                "description" => __("", 'js_composer'),
				'dependency' => array(
					'element' => 'popup_type',
					'value' => array( 'iframe' ),
				),
              ),
			 array(
					'type' => 'iconpicker',
					'heading' => __( 'Icon', 'js_composer' ),
					'param_name' => 'iconpicker',
					'value' => 'fa fa-info-circle',
					'settings' => array(
						'emptyIcon' => false,
						'iconsPerPage' => 4000,
					),
					'description' => __( 'Select icon from library.', 'js_composer' ),
				),
			 array(
                "type" => "colorpicker",
                "holder" => "div",
                "class" => "",
                "heading" => __("Icon Color", 'vc_extend'),
                "param_name" => "icon_color",
                "value" => '#fff',
                "description" => __("", 'vc_extend')
              ),	
              array(
                "type" => "textfield",
                "holder" => "div",
                "class" => "",
                "heading" => __("Popup Title", 'js_composer'),
                "param_name" => "popup_title",
                "value" => __("Welcome to the magic popup", 'js_composer'),
                "description" => __("", 'js_composer')
              ),
             array(
                "type" => "textfield",
                "holder" => "div",
                "class" => "",
                "heading" => __("Popup Sub Title", 'js_composer'),
                "param_name" => "popup_sub_title",
                "value" => __("One of the best popup in world", 'js_composer'),
                "description" => __("", 'js_composer')
              ),
			   array(
                "type" => "textfield",
                "holder" => "div",
                "class" => "",
                "heading" => __("Popup Extra Class", 'js_composer'),
                "param_name" => "popup_extra_class",
                "value" => __("", 'js_composer'),
                "description" => __("", 'js_composer')
              ),
              array(
                "type" => "colorpicker",
                "holder" => "div",
                "class" => "",
                "heading" => __("Popup header Color", 'js_composer'),
                "param_name" => "header_color",
                "value" => '#333',
                "description" => __("", 'js_composer')
              ),
              array(
                "type" => "colorpicker",
                "holder" => "div",
                "class" => "",
                "heading" => __("Overlay Color", 'js_composer'),
                "param_name" => "overlay_color",
                "value" => 'rgba(0, 0, 0, 0.4)',
                "description" => __("", 'js_composer')
              ),
			  
			  array(
                "type" => "colorpicker",
                "holder" => "div",
                "class" => "",
                "heading" => __("Background Color", 'js_composer'),
                "param_name" => "background_color",
                "value" => 'rgba(0, 0, 0, 0.4)',
                "description" => __("", 'js_composer')
              ),
			  
			  array(
                "type" => "colorpicker",
                "holder" => "div",
                "class" => "",
                "heading" => __("Open Reveal Color", 'js_composer'),
                "param_name" => "open_reveal_color",
                "value" => 'rgba(0, 0, 0, 0.4)',
                "description" => __("", 'js_composer')
              ),
			  
			  array(
                "type" => "dropdown",
                "holder" => "div",
                "class" => "", // js_composer_textarea_raw
                "heading" => __("Open Reveal Direction", 'js_composer'),
                "param_name" => "open_reveal_direction",
			    'value' => array(
						__( 'Top Bottom', 'js_composer' ) => 'tb',
						__( 'Bottom Top', 'js_composer' ) => 'bt',
						__( 'Left Right', 'js_composer' ) => 'lr',
						__( 'Right Left', 'js_composer' ) => 'rl',
				),
                "description" => __("Popup type iframe or content", 'js_composer')
              ),
			  
			  array(
                "type" => "colorpicker",
                "holder" => "div",
                "class" => "",
                "heading" => __("Close Reveal Color", 'js_composer'),
                "param_name" => "close_reveal_color",
                "value" => 'rgba(0, 0, 0, 0.4)',
                "description" => __("", 'js_composer')
              ),
			  
			  array(
                "type" => "dropdown",
                "holder" => "div",
                "class" => "", // js_composer_textarea_raw
                "heading" => __("Close Reveal Direction", 'js_composer'),
                "param_name" => "close_reveal_direction",
			    'value' => array(
						__( 'Bottom Top', 'js_composer' ) => 'bt',
						__( 'Top Bottom', 'js_composer' ) => 'tb',
						__( 'Left Right', 'js_composer' ) => 'lr',
						__( 'Right Left', 'js_composer' ) => 'rl',
						
				),
                "description" => __("Popup type iframe or content", 'js_composer')
              ),
			  			  
  			  array(
                "type" => "textfield",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("Reveal Animation Duration", 'js_composer'),
                "param_name" => "reveal_animation_duration",
                "value" => __("400", 'js_composer'),
                "description" => __("Value in milliseconds, (Only numberic value)", 'js_composer')
              ),
			  
              array(
                "type" => "textfield",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("Popup width", 'js_composer'),
                "param_name" => "width",
                "value" => __("640", 'js_composer'),
                "description" => __("A fixed value like 640, (Only numberic value)", 'js_composer')
              ),
              array(
                "type" => "textfield",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("Padding", 'js_composer'),
                "param_name" => "padding",
                "value" => __("20", 'js_composer'),
                "description" => __("Padding under popup (Only numberic value)", 'js_composer')
              ),
			   array(
                "type" => "dropdown",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("Attached", 'js_composer'),
                "param_name" => "attached",
                'value' => array(
						__( 'Center', 'js_composer' ) => 'center',				
						__( 'Top', 'js_composer' ) => 'top',
						__( 'Bottom', 'js_composer' ) => 'bottom',
						
				),
                "description" => __("attached under popup (Only numberic value)", 'js_composer')
              ),
			   array(
                "type" => "textfield",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("Radius", 'js_composer'),
                "param_name" => "radius",
                "value" => __("3", 'js_composer'),
                "description" => __("radius under popup (Only numberic value)", 'js_composer')
              ),
			  array(
				'type' => 'checkbox',
				'heading' => __( 'Overlay On Close', 'js_composer' ),
				'param_name' => 'overlay_close',
				'value' => array( __( 'Yes', 'js_composer' ) => 'true' ),
				"description" => __("Close popup click on overlay", 'js_composer')
			 ),
 			  array(
				'type' => 'checkbox',
				'heading' => __( 'Close On Escape', 'js_composer' ),
				'param_name' => 'close_on_escape',
				'value' => array( __( 'Yes', 'js_composer' ) => 'true' ),
				"description" => __("Close popup click on Escape", 'js_composer')
			 ),
 			  array(
				'type' => 'checkbox',
				'heading' => __( 'Focus Input', 'js_composer' ),
				'param_name' => 'focus_input',
				'value' => array( __( 'Yes', 'js_composer' ) => 'true' ),
				"description" => __("If set true, whenever you open a modal, the first visible field is active.", 'js_composer')
			 ),
			 array(
				'type' => 'checkbox',
				'heading' => __( 'Auto Open', 'js_composer' ),
				'param_name' => 'auto_open',
				"description" => __("If set true, the modal opens automatically without any user action.", 'js_composer')
			 ),
			 array(
				'type' => 'checkbox',
				'heading' => __( 'Full Screen', 'js_composer' ),
				'param_name' => 'fullscreen',
				'value' => array( __( 'Yes', 'js_composer' ) => 'true' ),
				"description" => __("Allow button in header modal to expand.", 'js_composer')
			 ),
			 array(
				'type' => 'checkbox',
				'heading' => __( 'Open Full Screen', 'js_composer' ),
				'param_name' => 'open_fullscreen',
				"description" => __("Force to open modal in fullscreen.", 'js_composer')
			 ),	
			 array(
				'type' => 'checkbox',
				'heading' => __( 'Timeout', 'js_composer' ),
				'param_name' => 'timeout',
				"description" => __("Amount in milliseconds to close the modal or false to disable.", 'js_composer')
			 ),
			 array(
                "type" => "textfield",
                "holder" => "",
                "class" => "js_composer_tiny_text",
                "heading" => __("Set Timeout Value", 'js_composer'),
                "param_name" => "timeout_value",
                "value" => __("5000", 'js_composer'),
                "description" => __("For example, 5000 stand for 5 seconds, leave it to blank if you do not want it (Only numberic value)", 'js_composer'),
				'dependency' => array(
					'element' => 'timeout',
					'value' => 'true',
				),
              ),
			  array(
				'type' => 'checkbox',
				'heading' => __( 'Show Timeout Progressbar', 'js_composer' ),
				'param_name' => 'timeout_progressbar',
				'dependency' => array(
					'element' => 'timeout',
					'value' => 'true',
				),
			 ),
			 array(
                "type" => "colorpicker",
                "holder" => "div",
                "class" => "",
                "heading" => __("Select Timeout Progressbar Color", 'js_composer'),
                "param_name" => "timeout_progressbar_color",
                "value" => '#fff',
                "description" => __("Pick color for timeout progressbar", 'js_composer'),
				'dependency' => array(
					'element' => 'timeout',
					'value' => 'true',
				),
              ),
			  
			  
			 array(
                "type" => "checkbox",
                "holder" => "",
                "class" => "vc_notify_cq",
                "heading" => __("Open When page load and After close, store it in cookie", 'vc_notify_cq'),
                "param_name" => "cookie",
                "value" => array(__("yes", "vc_notify_cq") => 'on'),
                "description" => __("", 'vc_notify_cq')
              ),
             array(
                "type" => "textfield",
                "holder" => "",
                "class" => "vc_notify_cq",
                "heading" => __("After close, do not show the popup again for days", 'vc_notify_cq'),
                "param_name" => "cookie_days",
                "value" => __("1", 'vc_notify_cq'),
                "description" => __("You have to enable the store in cookie", 'vc_notify_cq'),
				'dependency' => array(
					'element' => 'cookie',
					"value" => 'on'
				),
				
              ),
			  
			  array(
                "type" => "textfield",
                "holder" => "",
                "class" => "vc_notify_cq",
                "heading" => __("Open Popup After Following Second", 'vc_notify_cq'),
                "param_name" => "cookie_timeout",
                "value" => __("5", 'vc_notify_cq'),
                "description" => __("You have to enable the store in cookie", 'vc_notify_cq'),
				'dependency' => array(
					'element' => 'cookie',
					"value" => 'on'
				),
				
              ),
			  
			array(
				'type' => 'el_id',
				'heading' => __( 'Cookie ID', 'js_composer' ),
				'param_name' => 'el_id',
				'description' => sprintf( __( 'Enter optional ID. Make sure it is unique, and it is valid as w3c specification: %s (Must not have spaces)', 'js_composer' ), '<a target="_blank" href="http://www.w3schools.com/tags/att_global_id.asp">' . __( 'link', 'js_composer' ) . '</a>' ),
				'settings' => array(
					'auto_generate' => true,
				   ),
				),
			
			
			  
			  
			  
            )
          ) );
}

function ultimate_magic_vc_modal_func( $atts, $content=null) {
$popup_title = $popup_iframe = $popup_sub_title = $popup_sub_title  = $buttontext = $popup_extra_class=''; 
	  extract( shortcode_atts( array(
	  	'buttontext' => '',
		'popup_type' =>'content',
		'iframeheight' =>'400',
		'iconpicker' => 'fa fa-info-circle',
		'icon_color'=>' ',
		'iframeurl' =>'https://player.vimeo.com/video/22439234?autoplay=1',
		'popup_title' =>'Welcome to the magic popup',
		'popup_sub_title' =>'One of the best popup in world',
		'header_color' =>'#88A0B9',
		'overlay_color' =>'rgba(255,255,255,0.24)',
		'width' =>'600',
		'padding' =>'20',
		'attached' =>'center',
		'radius' => '3',
		'overlay_close' =>'false', //true
		'close_on_escape' =>'false', //true
		'popup_extra_class' =>'',
		'focus_input' =>'false', //true
		'auto_open' =>'false',
		'fullscreen' =>'false', // true
		'open_fullscreen' =>'false',
		'timeout' =>'false',
		'timeout_value' =>'5000',
		'timeout_progressbar' =>'false',
		'timeout_progressbar_color' =>'rgba(255,255,255,0.5)',
		'background_color' =>'#000',
		'reveal_animation_duration' =>'500',
		'open_reveal_color' =>'rgb(127, 64, 241)',
		'open_reveal_direction' =>'tb',
		'close_reveal_color' =>'rgb(127, 64, 241)',
		'close_reveal_direction' =>'bt',
		
		'cookie' => 'off',
		'cookie_days' =>'1',
		'cookie_timeout' => '5',
		'el_id' => '',
	  ), $atts ) );
	  
	  
	  if($popup_type=='content'){$popup_iframe='false';$iframeurl='null';}else{$popup_iframe='true';}
	  if($timeout=='false'){$timeout_value='false';}
	  
	  if( $popup_type == 'content' and $popup_title !='' and $popup_sub_title !='' )
	  {
		  $event_listener == 'true';
	  }
	  if($auto_open == 'true' and  $cookie == 'off'){
		$cookie = 'on' ;
		$cookie_days = 0;
	   }

	  $magic_popup_link = '';
	  $el_id = $el_id.'_'.$cookie_days;
	  $event_listener = '';
	  if ( !is_user_logged_in() ) {$magic_popup_link ='magic_popup_link'; }
	  

	  
	  wp_register_style( 'js_composer_style', plugins_url('css/iziModal.css', __FILE__) );
	  wp_enqueue_style( 'js_composer_style' );
	  wp_enqueue_style( 'font-awesome' );
	  wp_enqueue_script( 'js_composer_js', plugins_url('js/iziModal.js', __FILE__), array('jquery') );
	  
  	  wp_enqueue_script( 'anime-min', plugins_url('js/anime.min.js', __FILE__), array('jquery') );
	  wp_enqueue_script( 'anime-min-main', plugins_url('js/main.js', __FILE__), array('jquery') );
	  wp_enqueue_script( 'js_composer_cookies', plugins_url('js/cookies.min.js', __FILE__), array('jquery') );
	  wp_enqueue_script( 'js_composer_waypoints', plugins_url('js/waypoints.min.js', __FILE__), array('jquery') );
	  $tag_id = 'popup-'.rand(10000,9999999);
	  $notification_script = '';
	  
	
	  
	  
	  
	  
	  if($cookie == 'on')
	  {
		  
		  
		if($cookie_days != 0)
		{  
	  	$cookie_close_content = "
			if(cookies('notification_$el_id') == null)
			{
				 cookies({ 'notification_$el_id' : 'true' }, {
											  expires: $cookie_days * 24 * 3600,     // The time to expire in seconds
											  domain: false,                // The domain for the cookie
											  path: '/',                    // The path for the cookie
											  secure: false  // Require the use of https
											});  
			}";	  
		
		}else{
			$cookie_close_content = " 
					if(cookies('notification_$el_id') != null)
						{
							console.log('sdfsf');
							
							cookies({ 'notification_$el_id' : null });
															
						}
			";
			
		}
									
 	    $notification_script = "if(cookies('notification_$el_id') == null)
						{
							
							jQuery('#".$tag_id."').iziModal('open');
							reveal_modal_open();
															
						}";	
	}
	  
	
	  $content = wpb_js_remove_wpautop($content); // fix unclosed/unwanted paragraph tags in $content
	  return "<div id=".$tag_id." class='iziModal-content waypoint'>
					".$content."
			</div>
			<a href='#' data-type='".$popup_extra_class."' class='".$tag_id.' '.$magic_popup_link."'>".do_shortcode(urldecode(base64_decode($buttontext)))."</a>
		<style>
		#$tag_id{
			background-color:$background_color;
		}
		</style>	
		<script>
		

		
jQuery(document).ready(function($){
	
	var waypoints = $('.wrapper-site').waypoint(function(direction) {
  console.log('sfassf');
}, {
  offset: '100%'
})
			

			
			
			jQuery('body').append(jQuery('#".$tag_id."'));
			jQuery('.".$tag_id."').click(function(e){
				e.preventDefault();
				jQuery('#".$tag_id."').iziModal('open');
				reveal_modal_open();
				
			});
			
			jQuery('#".$tag_id."').iziModal({
				title: '".$popup_title."',
				subtitle: '".$popup_sub_title."',
				theme: '".$popup_extra_class."',
				headerColor: '".$header_color."',
				overlayColor: '".$overlay_color."',
				iconColor: '".$icon_color."',
				icon: '".$iconpicker."',
				attached: '$attached',
				radius: $radius,
				width: ".$width.",
				padding: ".$padding.",
				iframe: ".$popup_iframe.",
				iframeHeight: ".$iframeheight.",
				iframeURL: '".$iframeurl."',
				overlayClose: ".$overlay_close.",
				closeOnEscape: ".$close_on_escape.",
				bodyOverflow: false,
				focusInput: ".$focus_input.",
				
				fullscreen: ".$fullscreen.",
				openFullscreen: ".$open_fullscreen.",
				timeout: ".$timeout_value.",
				timeoutProgressbar: ".$timeout_progressbar.",
				timeoutProgressbarColor: '".$timeout_progressbar_color."',
				transitionIn: 'bounceInUp',
				transitionOut: 'flipInY',
				transitionInOverlay: 'fadeIn',
				transitionOutOverlay: 'fadeOut',
				onFullscreen: function(){},
				onResize: function(){},
				onOpening: function(){},
				onOpened: function(){},
				onClosing: function(){
					closeModal();
					$cookie_close_content
					},
				onClosed: function(){}
			});
			
			
			
				var modalEl = document.querySelector('#".$tag_id."'),
				revealer = new RevealFx(modalEl),
				closeCtrl = modalEl.querySelector('.iziModal-button-close');
				
			
			





		  
		  
		  function reveal_modal_open() {
				modalEl.classList.add('modal--open');
				revealer.reveal({
					bgcolor: '$open_reveal_color',
					direction: '$open_reveal_direction',
					duration: $reveal_animation_duration, 
					easing: 'easeOutCirc',
					onCover: function(contentEl, revealerEl) {
						contentEl.style.opacity = 1;
					},
					onComplete: function() {
						if('$event_listener' == 'true')
						{
							closeCtrl.addEventListener('click', closeModal);
						}
					}
				});
			}

			function closeModal(ev) {
					if('$event_listener' == 'true')
					{
						closeCtrl.removeEventListener('click', closeModal);
					}

				
				modalEl.classList.remove('modal--open');
				revealer.reveal({
					bgcolor: '$close_reveal_color',
					direction: '$close_reveal_direction',
					duration: 400, 
					easing: 'easeOutCirc',
					onCover: function(contentEl, revealerEl) {
						contentEl.style.opacity = 0;
					},
					onComplete: function() {
						modalEl.classList.remove('modal--open');
					}
				});
			}
			
			$notification_script
			
			
		});
		
		
		</script>";
	}

}

function js_composer_notice_ultimate_magic_popup(){
      $plugin_data = get_plugin_data(__FILE__);
      echo '
      <div class="updated">
        <p>'.sprintf(__('<strong>%s</strong> requires <strong><a href="http://codecanyon.net/item/visual-composer-page-builder-for-wordpress/242431?ref=crystalwing" target="_blank">Visual Composer</a></strong> plugin to be installed and activated on your site.', 'js_composer'), $plugin_data['Name']).'</p>
      </div>';
    }
    if (!defined('ABSPATH')) die('-1');
    function vc_extensions_ultimate_magic_popup_notify_init(){
        if (!defined('WPB_VC_VERSION')) {add_action('admin_notices', 'js_composer_notice_ultimate_magic_popup'); return;}
       
	    wp_register_style( 'vc_extensions_ump_notify_admin', plugins_url('css/admin_icon.css', __FILE__) );
        wp_enqueue_style( 'vc_extensions_ump_notify_admin' );

      if(class_exists('VC_Extensions_UMP')) $vc_extensions_cq = new VC_Extensions_UMP();
    }

    add_action('init', 'vc_extensions_ultimate_magic_popup_notify_init');
}

?>
