<?php
/**
 * Popup builder (Ultimate Bundle).
 *
 * @package EasySite_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Render popup markup in footer.
 */
function easysite_render_popup() {
	if ( ! easysite_get_mod( 'easysite_popup_enabled', false ) && ! easysite_ve_active() ) {
		return;
	}

	if ( is_admin() ) {
		return;
	}

	$type    = easysite_get_mod( 'easysite_popup_type', 'newsletter' );
	$trigger = easysite_get_mod( 'easysite_popup_trigger', 'delay' );
	$title   = easysite_get_mod( 'easysite_popup_title', __( 'Stay in the loop', 'easysite-pro' ) );
	$text    = easysite_get_mod( 'easysite_popup_text', __( 'Subscribe for updates and special offers.', 'easysite-pro' ) );
	$btn     = easysite_get_mod( 'easysite_popup_button_text', __( 'Subscribe', 'easysite-pro' ) );
	$link    = easysite_get_mod( 'easysite_popup_button_link', '#' );
	$ve      = easysite_ve_active();
	?>
	<div id="easysite-popup" class="easysite-popup easysite-popup-<?php echo esc_attr( $type ); ?><?php echo $ve ? ' esb-ve-region' : ''; ?>" data-trigger="<?php echo esc_attr( $trigger ); ?>" <?php echo $ve ? '' : 'hidden'; ?>>
		<div class="easysite-popup-overlay" data-close></div>
		<div class="easysite-popup-content" role="dialog" aria-modal="true" aria-labelledby="easysite-popup-title">
			<button type="button" class="easysite-popup-close" data-close aria-label="<?php esc_attr_e( 'Close', 'easysite-pro' ); ?>">&times;</button>
			<h2 id="easysite-popup-title"<?php echo $ve ? ' contenteditable="true" data-esb-region-field="popup_title" data-esb-type="text"' : ''; ?>><?php echo esc_html( $title ); ?></h2>
			<p<?php echo $ve ? ' contenteditable="true" data-esb-region-field="popup_text" data-esb-type="text"' : ''; ?>><?php echo esc_html( $text ); ?></p>
			<?php if ( 'newsletter' === $type ) : ?>
				<form class="easysite-popup-form" action="#" method="post">
					<input type="email" placeholder="<?php esc_attr_e( 'Your email', 'easysite-pro' ); ?>" required />
					<button type="submit" class="easysite-btn<?php echo $ve ? ' esb-editable-button' : ''; ?>"<?php echo $ve ? ' contenteditable="true" data-esb-region-field="popup_button" data-esb-type="text"' : ''; ?>><?php echo esc_html( $btn ); ?></button>
				</form>
			<?php else : ?>
				<a href="<?php echo esc_url( $link ); ?>" class="easysite-btn<?php echo $ve ? ' esb-editable-button' : ''; ?>"<?php echo $ve ? ' contenteditable="true" data-esb-region-field="popup_button" data-esb-type="text"' : ''; ?>><?php echo esc_html( $btn ); ?></a>
			<?php endif; ?>
		</div>
	</div>
	<?php
}
add_action( 'wp_footer', 'easysite_render_popup', 50 );
