function sgPopupMediaButtonThickboxs()
{
    global $pagenow, $typenow;
    $pages = array('post.php', 'page.php', 'post-new.php', 'post-edit.php');
    $checkPage = in_array($pagenow, $pages);
    if ($checkPage && $typenow != 'download') {
        ?>
		<script type="text/javascript">
			jQuery(document).ready(function ($) {
				$('#sg-ptp-popup-insert').on('click', function () {
					var id = $('#sg-insert-popup-id').val();
					if ('' === id) {
						alert('Select your popup');
						return;
					}
					selectionText = '';
					if (typeof(tinyMCE.editors.content) != "undefined") {
						selectionText = (tinyMCE.activeEditor.selection.getContent()) ? tinyMCE.activeEditor.selection.getContent() : '';
					}
					window.send_to_editor('[sg_popup id="' + id + '"]'+selectionText+"[/sg_popup]");
				});
			});
		</script>

		<div id="sg-popup-thickbox" style="display: none;">
			<div class="wrap">
				<p>Insert the shortcode for showing a Popup.</p>
				<div>
					<select id="sg-insert-popup-id">
						<option value="">Please select...</option>
						<?php 
        global $wpdb;
        $proposedTypes = array();
        $orderBy = 'id DESC';
        $allPopups = SGPopup::findAll($orderBy);
        foreach ($allPopups as $allPopup) {
            ?>
								<option value="<?php 
            echo $allPopup->getId();
            ?>
"><?php 
            echo $allPopup->getTitle();
            echo " - " . $allPopup->getType();
            ?>
</option>;
							<?php 
        }
        ?>
					</select>
				</div>
				<p class="submit">
					<input type="button" id="sg-ptp-popup-insert" class="button-primary dashicons-welcome-widgets-menus" value="Insert"/>
					<a id="sg_popup_cancel" class="button-secondary" onclick="tb_remove();" title="Cancel">Cancel</a>
				</p>
			</div>
		</div>
	<?php 
    }
}
function sgPopupDelete()
{
    $id = (int) @$_POST['popup_id'];
    if (!$id) {
        return;
    }
    require_once SG_APP_POPUP_CLASSES . '/SGPopup.php';
    SGPopup::delete($id);
}
function sgPopupCallback($post)
{
    wp_nonce_field(basename(__FILE__), 'prfx_nonce');
    $prfx_stored_meta = get_post_meta($post->ID);
    ?>
	<p class="preview-paragaraph">
<?php 
    global $wpdb;
    $proposedTypes = array();
    $orderBy = 'id DESC';
    $proposedTypes = SGPopup::findAll($orderBy);
    function sgCreateSelect($options, $name, $selecteOption)
    {
        $selected = '';
        $str = "";
        $str .= "<select class=\"choose-popup-type\" name=\"{$name}\">";
        $str .= "<option value=''>Not selected</potion>";
        foreach ($options as $option) {
            if ($option) {
                $title = $option->getTitle();
                $type = $option->getType();
                $id = $option->getId();
                if ($selecteOption == $id) {
                    $selected = "selected";
                } else {
                    $selected = '';
                }
                $str .= "<option value='" . $id . "' disable='" . $id . "' " . $selected . " >{$title} - {$type}</potion>";
            }
        }
        $str .= "</select>";
        return $str;
    }
    global $post;
    $page = (int) $post->ID;
    $popup = "sg_promotional_popup";
    $popupId = SGPopup::getPagePopupId($page, $popup);
    echo sgCreateSelect($proposedTypes, 'sg_promotional_popup', $popupId);
    $SG_APP_POPUP_URL = SG_APP_POPUP_URL;
    ?>
	</p>
	<input type="hidden" value="<?php 
    echo $SG_APP_POPUP_URL;
    ?>
" id="SG_APP_POPUP_URL">
<?php 
}
function sgOnloadPopup()
{
    $page = get_queried_object_id();
    $popup = "sg_promotional_popup";
    $popupId = SGPopup::getPagePopupId($page, $popup);
    if (!$popupId) {
        return;
    }
    sgRenderPopupOpen($popupId);
}
function sgOnloadPopup()
{
    $sgOnloadPopupId = get_option('SG_POPUP_ONLOAD_ID');
    if ($sgOnloadPopupId) {
        sgRenderPopupOpen($sgOnloadPopupId);
    }
    $page = get_queried_object_id();
    $popup = "sg_promotional_popup";
    $popupId = SGPopup::getPagePopupId($page, $popup);
    if (!$popupId) {
        return;
    }
    sgRenderPopupOpen($popupId);
}
<?php

$pagenum = isset($_GET['pn']) ? (int) $_GET['pn'] : 1;
$limit = SG_APP_POPUP_TABLE_LIMIT;
$offset = ($pagenum - 1) * $limit;
$total = SGPopup::getTotalRowCount();
$num_of_pages = ceil(esc_html($total) / $limit);
if ($pagenum > $num_of_pages || $pagenum < 1) {
    $offset = 0;
    $pagenum = 1;
}
$orderBy = 'id DESC';
$entries = SGPopup::findAll($orderBy, $limit, $offset);
?>
<div class="wrap">
	<div class="headers-wrapper">
	<h2>Popups <a href="<?php 
echo admin_url();
?>
admin.php?page=create-popup" class="add-new-h2">Add New</a></h2>
		<?php 
if (!SG_POPUP_PRO) {
    ?>
				<input type="button" class="main-update-to-pro" value="Upgrade to PRO version" onclick="window.open('<?php 
    echo SG_POPUP_PRO_URL;
    ?>
')">
		<?php 
}
?>
	</div>
 public function render()
 {
     return parent::render();
 }
function sgPopupSave()
{
    global $wpdb;
    $socialButtons = array();
    $socialOptions = array();
    $countdownOptions = array();
    $fblikeOptions = array();
    $options = array();
    $showAllPages = sgSanitize('allPages');
    $socialOptions = array('sgSocialTheme' => sgSanitize('sgSocialTheme'), 'sgSocialButtonsSize' => sgSanitize('sgSocialButtonsSize'), 'sgSocialLabel' => sgSanitize('sgSocialLabel'), 'sgSocialShareCount' => sgSanitize('sgSocialShareCount'), 'sgRoundButton' => sgSanitize('sgRoundButton'), 'fbShareLabel' => sgSanitize('fbShareLabel'), 'lindkinLabel' => sgSanitize('lindkinLabel'), 'sgShareUrl' => sgSanitize('sgShareUrl'), 'shareUrlType' => sgSanitize('shareUrlType'), 'googLelabel' => sgSanitize('googLelabel'), 'twitterLabel' => sgSanitize('twitterLabel'), 'pinterestLabel' => sgSanitize('pinterestLabel'), 'sgMailSubject' => sgSanitize('sgMailSubject'), 'sgMailLable' => sgSanitize('sgMailLable'));
    $socialButtons = array('sgTwitterStatus' => sgSanitize('sgTwitterStatus'), 'sgFbStatus' => sgSanitize('sgFbStatus'), 'sgEmailStatus' => sgSanitize('sgEmailStatus'), 'sgLinkedinStatus' => sgSanitize('sgLinkedinStatus'), 'sgGoogleStatus' => sgSanitize('sgGoogleStatus'), 'sgPinterestStatus' => sgSanitize('sgPinterestStatus'), 'pushToBottom' => sgSanitize('pushToBottom'));
    $countdownOptions = array('pushToBottom' => sgSanitize('pushToBottom'), 'countdownNumbersBgColor' => sgSanitize('countdownNumbersBgColor'), 'countdownNumbersTextColor' => sgSanitize('countdownNumbersTextColor'), 'sg-due-date' => sgSanitize('sg-due-date'), 'countdown-position' => sgSanitize('countdown-position'), 'counts-language' => sgSanitize('counts-language'), 'sg-time-zone' => sgSanitize('sg-time-zone'), 'sg-countdown-type' => sgSanitize('sg-countdown-type'));
    $videoOptions = array('video-autoplay' => sgSanitize('video-autoplay'));
    $exitIntentOptions = array("exit-intent-type" => sgSanitize('exit-intent-type'), "exit-intent-expire-time" => sgSanitize('exit-intent-expire-time'), "exit-intent-alert" => sgSanitize('exit-intent-alert'));
    $fblikeOptions = array("fblike-like-url" => sgSanitize("fblike-like-url"), "fblike-layout" => sgSanitize("fblike-layout"));
    $options = array('width' => sgSanitize('width'), 'height' => sgSanitize('height'), 'delay' => (int) sgSanitize('delay'), 'duration' => (int) sgSanitize('duration'), 'effect' => sgSanitize('effect'), 'escKey' => sgSanitize('escKey'), 'scrolling' => sgSanitize('scrolling'), 'reposition' => sgSanitize('reposition'), 'overlayClose' => sgSanitize('overlayClose'), 'contentClick' => sgSanitize('contentClick'), 'opacity' => sgSanitize('opacity'), 'sgOverlayColor' => sgSanitize('sgOverlayColor'), 'popupFixed' => sgSanitize('popupFixed'), 'fixedPostion' => sgSanitize('fixedPostion'), 'maxWidth' => sgSanitize('maxWidth'), 'maxHeight' => sgSanitize('maxHeight'), 'initialWidth' => sgSanitize('initialWidth'), 'initialHeight' => sgSanitize('initialHeight'), 'closeButton' => sgSanitize('closeButton'), 'theme' => sgSanitize('theme'), 'onScrolling' => sgSanitize('onScrolling'), 'beforeScrolingPrsent' => (int) sgSanitize('beforeScrolingPrsent'), 'forMobile' => sgSanitize('forMobile'), 'openMobile' => sgSanitize('openMobile'), 'repeatPopup' => sgSanitize('repeatPopup'), 'autoClosePopup' => sgSanitize('autoClosePopup'), 'disablePopup' => sgSanitize('disablePopup'), 'popupClosingTimer' => sgSanitize('popupClosingTimer'), 'yesButtonLabel' => sgSanitize('yesButtonLabel'), 'noButtonLabel' => sgSanitize('noButtonLabel'), 'restrictionUrl' => sgSanitize('restrictionUrl'), 'yesButtonBackgroundColor' => sgSanitize('yesButtonBackgroundColor'), 'noButtonBackgroundColor' => sgSanitize('noButtonBackgroundColor'), 'yesButtonTextColor' => sgSanitize('yesButtonTextColor'), 'noButtonTextColor' => sgSanitize('noButtonTextColor'), 'yesButtonRadius' => (int) sgSanitize('yesButtonRadius'), 'noButtonRadius' => (int) sgSanitize('noButtonRadius'), 'pushToBottom' => sgSanitize('pushToBottom'), 'socialButtons' => json_encode($socialButtons), 'socialOptions' => json_encode($socialOptions), 'countdownOptions' => json_encode($countdownOptions), 'exitIntentOptions' => json_encode($exitIntentOptions), 'videoOptions' => json_encode($videoOptions), 'fblikeOptions' => json_encode($fblikeOptions));
    $html = stripslashes(sgSanitize("sg_popup_html"));
    $fblike = stripslashes(sgSanitize("sg_popup_fblike"));
    $ageRestriction = stripslashes(sgSanitize('sg_ageRestriction'));
    $social = stripslashes(sgSanitize('sg_social'));
    $image = sgSanitize('ad_image');
    $countdown = stripslashes(sgSanitize('sg_countdown'));
    $iframe = sgSanitize('iframe');
    $video = sgSanitize('video');
    $shortCode = stripslashes(sgSanitize('shortcode'));
    $exitIntent = stripslashes(sgSanitize('sg-exit-intent'));
    $type = sgSanitize('type');
    $title = sgSanitize('title');
    $id = sgSanitize('hidden_popup_number');
    $jsonDataArray = json_encode($options);
    $data = array('id' => $id, 'title' => $title, 'type' => $type, 'image' => $image, 'html' => $html, 'fblike' => $fblike, 'iframe' => $iframe, 'video' => $video, 'shortcode' => $shortCode, 'ageRestriction' => $ageRestriction, 'countdown' => $countdown, 'exitIntent' => $exitIntent, 'social' => $social, 'options' => $jsonDataArray);
    function setPopupForAllPages($id, $allPages)
    {
        if ($allPages) {
            update_option('SG_POPUP_ONLOAD_ID', $id);
        } else {
            delete_option('SG_POPUP_ONLOAD_ID');
        }
    }
    function setOptionPopupType($id, $type)
    {
        update_option("SG_POPUP_" . strtoupper($type) . "_" . $id, $id);
    }
    if (empty($title)) {
        wp_redirect(SG_APP_POPUP_ADMIN_URL . "admin.php?page=edit-popup&type={$type}&titleError=1");
        exit;
    }
    $popupName = "SG" . ucfirst(strtolower($_POST['type']));
    $popupClassName = $popupName . "Popup";
    require_once SG_APP_POPUP_PATH . "/classes/" . $popupClassName . ".php";
    if ($id == "") {
        global $wpdb;
        call_user_func(array($popupClassName, 'create'), $data);
        $lastId = $wpdb->get_var("SELECT LAST_INSERT_ID() FROM " . $wpdb->prefix . "sg_popup");
        setPopupForAllPages($lastId, $showAllPages);
        setOptionPopupType($lastId, $type);
        wp_redirect(SG_APP_POPUP_ADMIN_URL . "admin.php?page=edit-popup&id=" . $lastId . "&type={$type}&saved=1");
        exit;
    } else {
        $popup = SGPopup::findById($id);
        $popup->setTitle($title);
        $popup->setId($id);
        $popup->setType($type);
        $popup->setOptions($jsonDataArray);
        switch ($popupName) {
            case 'SGImage':
                $popup->setUrl($image);
                break;
            case 'SGIframe':
                $popup->setUrl($iframe);
                break;
            case 'SGVideo':
                $popup->setUrl($video);
                $popup->setRealUrl($video);
                $popup->setVideoOptions(json_encode($videoOptions));
                break;
            case 'SGHtml':
                $popup->setContent($html);
                break;
            case 'SGFblike':
                $popup->setContent($fblike);
                $popup->setFblikeOptions(json_encode($fblikeOptions));
                break;
            case 'SGShortcode':
                $popup->setShortcode($shortCode);
                break;
            case 'SGAgerestriction':
                $popup->setContent($ageRestriction);
                $popup->setYesButton($options['yesButtonLabel']);
                $popup->setNoButton($options['noButtonLabel']);
                $popup->setRestrictionUrl($options['restrictionUrl']);
                break;
            case 'SGCountdown':
                $popup->setCountdownContent($countdown);
                $popup->setCountdownOptions(json_encode($countdownOptions));
                break;
            case 'SGSocial':
                $popup->setSocialContent($social);
                $popup->setButtons(json_encode($socialButtons));
                $popup->setSocialOptions(json_encode($socialOptions));
                break;
            case 'SGExitintent':
                $popup->setContent($exitIntent);
                $popup->setExitIntentOptions(json_encode($exitIntentOptions));
                break;
        }
        setPopupForAllPages($id, $showAllPages);
        setOptionPopupType($id, $type);
        $popup->save();
        wp_redirect(SG_APP_POPUP_ADMIN_URL . "admin.php?page=edit-popup&id={$id}&type={$type}&saved=1");
        exit;
    }
}