<?php
/**
 * Paywall gate template (included by BSPP_Paywall_Overlay::render()).
 *
 * @var int         $post_id
 * @var string      $preview_text
 * @var string      $heading_id
 * @var string      $status_id
 * @var int         $enable_subscription
 * @var int         $enable_tip_unlock
 * @var int         $enable_article_unlock
 * @var mixed       $subscription_price
 * @var mixed       $annual_subscription_price
 * @var mixed       $unlock_price
 * @var mixed       $default_tip_amount
 * @var mixed       $minimum_tip_amount
 * @var bool        $show_free_indicator
 * @var string      $free_indicator_text
 * @var bool        $show_free_unlock_action
 * @var bool        $show_anonymous_prompt
 * @var string      $anonymous_prompt_text
 * @var string      $magic_login_url
 * @var bool        $is_logged_in
 * @var bool        $bspp_premium
 */

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

$bspp_subscribe_monthly_url = wp_nonce_url(
    admin_url('admin-post.php?action=bspp_start_subscription&post_id=' . $post_id . '&subscription_plan=monthly'),
    'bspp_start_subscription',
    'bspp_subscribe_nonce'
);
$bspp_subscribe_annual_url = wp_nonce_url(
    admin_url('admin-post.php?action=bspp_start_subscription&post_id=' . $post_id . '&subscription_plan=annual'),
    'bspp_start_subscription',
    'bspp_subscribe_nonce'
);
$bspp_unlock_url = wp_nonce_url(
    admin_url('admin-post.php?action=bspp_unlock_article&post_id=' . $post_id),
    'bspp_unlock_article',
    'bspp_unlock_nonce'
);

$bspp_has_secondary_actions = ($enable_subscription || ($bspp_premium && $enable_article_unlock));
?>
<div class="sp-paywall">
	<div class="sp-paywall-preview" aria-hidden="true">
		<div class="sp-paywall-preview__inner"><?php echo esc_html($preview_text); ?></div>
	</div>

	<section class="sp-paywall-cta" role="region" aria-labelledby="<?php echo esc_attr($heading_id); ?>">
		<h2 class="sp-paywall-cta__title" id="<?php echo esc_attr($heading_id); ?>"><?php esc_html_e('This content is locked', 'bobasoftware-publisher-paywall'); ?></h2>
		<p class="sp-paywall-cta__lede"><?php esc_html_e('Subscribe or unlock to continue reading.', 'bobasoftware-publisher-paywall'); ?></p>

		<?php if ($show_free_indicator) : ?>
			<p class="sp-paywall-free-indicator"><?php echo esc_html($free_indicator_text); ?></p>
		<?php endif; ?>

		<?php if ($show_anonymous_prompt) : ?>
			<?php
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, magic login URL state.
			$bspp_magic_state = isset($_GET['bspp_magic']) ? sanitize_key(wp_unslash($_GET['bspp_magic'])) : '';
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, magic login URL state.
			$bspp_sent_email = isset($_GET['bspp_magic_email']) ? sanitize_email(wp_unslash($_GET['bspp_magic_email'])) : '';
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, magic login URL state.
			$bspp_magic_resend = isset($_GET['bspp_magic_resend']) ? sanitize_key(wp_unslash($_GET['bspp_magic_resend'])) : '';
			?>

			<hr class="sp-paywall-divider" />

			<div class="sp-paywall-anon-block">
			<?php if ($bspp_magic_state === 'sent') : ?>
				<p class="sp-paywall-anon-helper"><?php esc_html_e('Check your email for the login link. It expires in 10 minutes.', 'bobasoftware-publisher-paywall'); ?></p>
				<p class="sp-paywall-anon-helper"><?php esc_html_e("Didn't get the email?", 'bobasoftware-publisher-paywall'); ?></p>
				<?php if ($bspp_magic_resend === '1') : ?>
					<p class="sp-paywall-anon-helper"><?php esc_html_e('We sent you a new login link.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php endif; ?>

				<form class="sp-paywall-anon-login" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
					<input type="hidden" name="action" value="bspp_magic_login_request" />
					<input type="hidden" name="redirect_to" value="<?php echo esc_attr((string) add_query_arg('bspp_magic_resend', '1', get_permalink($post_id))); ?>" />
					<?php wp_nonce_field('bspp_magic_login_request', 'bspp_magic_login_nonce'); ?>

					<?php if (!empty($bspp_sent_email) && is_email($bspp_sent_email)) : ?>
						<input type="hidden" name="email" value="<?php echo esc_attr($bspp_sent_email); ?>" />
					<?php else : ?>
						<label class="sp-paywall-anon-login__label" for="bspp_magic_login_email_retry"><?php esc_html_e('Email', 'bobasoftware-publisher-paywall'); ?></label>
						<input id="bspp_magic_login_email_retry" type="email" name="email" placeholder="<?php echo esc_attr__('Email', 'bobasoftware-publisher-paywall'); ?>" required />
					<?php endif; ?>

					<button type="submit" class="button sp-paywall-btn sp-paywall-btn--primary"><?php esc_html_e('Send another link', 'bobasoftware-publisher-paywall'); ?></button>
				</form>

				<?php if (!empty($bspp_sent_email) && is_email($bspp_sent_email)) : ?>
					<?php
					$bspp_change_email_url = remove_query_arg(['bspp_magic', 'bspp_magic_email', 'bspp_magic_resend'], get_permalink($post_id));
					?>
					<p class="sp-paywall-anon-helper">
						<a href="<?php echo esc_url($bspp_change_email_url); ?>"><?php esc_html_e('Change email', 'bobasoftware-publisher-paywall'); ?></a>
					</p>
				<?php endif; ?>
			<?php else : ?>
				<p class="sp-paywall-anon-prompt">
					<?php
					printf(
						/* translators: %d: free post limit */
						esc_html__('Create a free account to get %d free posts', 'bobasoftware-publisher-paywall'),
						(int) $free_article_limit
					);
					?>
				</p>
				<p class="sp-paywall-anon-helper">
					<?php esc_html_e('We will email you a secure login link. It expires in 10 minutes.', 'bobasoftware-publisher-paywall'); ?>
				</p>
				<form class="sp-paywall-anon-login" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
					<input type="hidden" name="action" value="bspp_magic_login_request" />
					<input type="hidden" name="redirect_to" value="<?php echo esc_attr((string) get_permalink($post_id)); ?>" />
					<?php wp_nonce_field('bspp_magic_login_request', 'bspp_magic_login_nonce'); ?>
					<label class="sp-paywall-anon-login__label" for="bspp_magic_login_email"><?php esc_html_e('Email', 'bobasoftware-publisher-paywall'); ?></label>
					<input id="bspp_magic_login_email" type="email" name="email" placeholder="<?php echo esc_attr__('Email', 'bobasoftware-publisher-paywall'); ?>" required />
					<button type="submit" class="button sp-paywall-btn sp-paywall-btn--primary"><?php esc_html_e('Send login link', 'bobasoftware-publisher-paywall'); ?></button>
				</form>
			<?php endif; ?>
			</div>
		<?php endif; ?>

		<div id="<?php echo esc_attr($status_id); ?>" class="sp-paywall-status" role="status" aria-live="polite" aria-atomic="true">
			<?php
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			if (isset($_GET['bspp_subscribe']) && sanitize_key(wp_unslash($_GET['bspp_subscribe'])) === 'pending') :
				?>
				<p class="sp-paywall-status--info"><?php esc_html_e('Subscription checkout is not connected yet. Configure Stripe in Bobasoftware Publisher Paywall settings.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_stripe']) && sanitize_key(wp_unslash($_GET['bspp_stripe'])) === 'success') :
				?>
				<p class="sp-paywall-status--success"><?php esc_html_e('Subscription payment completed. If the post does not unlock, refresh once.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_stripe']) && sanitize_key(wp_unslash($_GET['bspp_stripe'])) === 'cancel') :
				?>
				<p class="sp-paywall-status--warn"><?php esc_html_e('Subscription checkout was canceled.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_unlock']) && sanitize_key(wp_unslash($_GET['bspp_unlock'])) === 'pending') :
				?>
				<p class="sp-paywall-status--info"><?php esc_html_e('Per-article purchase requires Stripe. Configure API keys in Bobasoftware Publisher Paywall settings.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_unlock']) && sanitize_key(wp_unslash($_GET['bspp_unlock'])) === 'success') :
				?>
				<p class="sp-paywall-status--success"><?php esc_html_e('Unlock payment completed. If the post does not unlock, refresh once.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_unlock']) && sanitize_key(wp_unslash($_GET['bspp_unlock'])) === 'cancel') :
				?>
				<p class="sp-paywall-status--warn"><?php esc_html_e('Per-article purchase checkout was canceled.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_tip_unlock']) && sanitize_key(wp_unslash($_GET['bspp_tip_unlock'])) === 'pending') :
				?>
				<p class="sp-paywall-status--info"><?php esc_html_e('Tip payments require Stripe. Configure API keys in Bobasoftware Publisher Paywall settings.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_tip_unlock']) && sanitize_key(wp_unslash($_GET['bspp_tip_unlock'])) === 'success') :
				?>
				<p class="sp-paywall-status--success"><?php esc_html_e('Tip payment completed. If the post does not unlock, refresh once.', 'bobasoftware-publisher-paywall'); ?></p>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not a form submission, Stripe return URL state.
			elseif (isset($_GET['bspp_tip_unlock']) && sanitize_key(wp_unslash($_GET['bspp_tip_unlock'])) === 'cancel') :
				?>
				<p class="sp-paywall-status--warn"><?php esc_html_e('Tip checkout was canceled.', 'bobasoftware-publisher-paywall'); ?></p>
			<?php endif; ?>
		</div>

		<?php if ($show_free_unlock_action || $bspp_has_secondary_actions || ($bspp_premium && $enable_tip_unlock)) : ?>
			<?php if ($show_anonymous_prompt || $show_free_indicator) : ?>
				<hr class="sp-paywall-divider" />
			<?php endif; ?>

			<div class="sp-paywall-actions">
				<?php if ($show_free_unlock_action) : ?>
					<div class="sp-paywall-actions-primary">
						<form class="sp-paywall-free-unlock-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
							<input type="hidden" name="action" value="bspp_use_free_post" />
							<input type="hidden" name="post_id" value="<?php echo esc_attr((string) $post_id); ?>" />
							<?php wp_nonce_field('bspp_use_free_post', 'bspp_use_free_post_nonce'); ?>
							<button type="submit" class="button sp-paywall-btn sp-paywall-btn--primary"><?php esc_html_e('Use 1 free post to unlock', 'bobasoftware-publisher-paywall'); ?></button>
						</form>
					</div>
				<?php endif; ?>

				<?php if ($bspp_has_secondary_actions) : ?>
					<div class="sp-paywall-actions-row sp-paywall-actions--secondary">
						<?php if ($enable_subscription) : ?>
							<?php
							$bspp_sub_mo_price = is_numeric($subscription_price)
								? number_format((float) $subscription_price, 2, '.', '')
								: (string) $subscription_price;
							$bspp_sub_yr_price = is_numeric($annual_subscription_price)
								? number_format((float) $annual_subscription_price, 2, '.', '')
								: (string) $annual_subscription_price;
							?>
							<a class="sp-paywall-btn sp-paywall-btn--secondary" href="<?php echo esc_url($bspp_subscribe_monthly_url); ?>">
								<span class="sp-paywall-btn__lines"><?php esc_html_e('Subscribe', 'bobasoftware-publisher-paywall'); ?><br />(<?php echo esc_html('$' . $bspp_sub_mo_price); ?>/mo)</span>
							</a>
							<a class="sp-paywall-btn sp-paywall-btn--secondary" href="<?php echo esc_url($bspp_subscribe_annual_url); ?>">
								<span class="sp-paywall-btn__lines"><?php esc_html_e('Subscribe', 'bobasoftware-publisher-paywall'); ?><br />(<?php echo esc_html('$' . $bspp_sub_yr_price); ?>/yr)</span>
							</a>
						<?php endif; ?>

						<?php if ($bspp_premium && $enable_article_unlock) : ?>
							<?php
							$bspp_unlock_price_display = is_numeric($unlock_price)
								? number_format((float) $unlock_price, 2, '.', '')
								: (string) $unlock_price;
							?>
							<a class="sp-paywall-btn sp-paywall-btn--secondary" href="<?php echo esc_url($bspp_unlock_url); ?>">
								<span class="sp-paywall-btn__lines"><?php esc_html_e('Unlock for', 'bobasoftware-publisher-paywall'); ?><br /><?php echo esc_html('$' . $bspp_unlock_price_display); ?></span>
							</a>
						<?php endif; ?>
					</div>
				<?php endif; ?>

				<?php if ($bspp_premium && $enable_tip_unlock) : ?>
					<?php
					$bspp_tip_amount_display = number_format(max(0, (float) $default_tip_amount), 2, '.', '');
					?>
					<form class="sp-paywall-tip-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
						<input type="hidden" name="action" value="bspp_start_tip_unlock" />
						<input type="hidden" name="post_id" value="<?php echo esc_attr((string) $post_id); ?>" />
						<?php wp_nonce_field('bspp_start_tip_unlock', 'bspp_tip_unlock_nonce'); ?>
						<label for="bspp_tip_amount_<?php echo esc_attr((string) $post_id); ?>"><?php esc_html_e('Tip $', 'bobasoftware-publisher-paywall'); ?></label>
						<input
							id="bspp_tip_amount_<?php echo esc_attr((string) $post_id); ?>"
							type="number"
							name="tip_amount"
							min="<?php echo esc_attr(number_format(max(0, (float) $minimum_tip_amount), 2, '.', '')); ?>"
							step="0.01"
							value="<?php echo esc_attr($bspp_tip_amount_display); ?>"
							required
						/>
						<button type="submit" class="button sp-paywall-btn sp-paywall-btn--secondary"><?php esc_html_e('Buy the creator a coffee ☕', 'bobasoftware-publisher-paywall'); ?></button>
					</form>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<?php if ( ! $is_logged_in ) : ?>
			<p class="sp-paywall-returning-login">
				<?php esc_html_e( 'Already a member?', 'bobasoftware-publisher-paywall' ); ?>
				<a href="<?php echo esc_url( $magic_login_url ); ?>"><?php esc_html_e( 'Log in', 'bobasoftware-publisher-paywall' ); ?></a>
			</p>
			<?php if ( ! $show_anonymous_prompt ) : ?>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only magic login URL state.
				$bspp_member_magic_state = isset( $_GET['bspp_magic'] ) ? sanitize_key( wp_unslash( $_GET['bspp_magic'] ) ) : '';
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only magic login URL state.
				$bspp_member_sent_email = isset( $_GET['bspp_magic_email'] ) ? sanitize_email( wp_unslash( $_GET['bspp_magic_email'] ) ) : '';
				$bspp_show_member_form = isset( $_GET['bspp_magic_login'] ) || $bspp_member_magic_state !== '';
				?>
				<?php if ( $bspp_show_member_form ) : ?>
				<div class="sp-paywall-returning-login-form-wrap" id="sp-paywall-returning-login">
					<?php if ( $bspp_member_magic_state === 'sent' ) : ?>
						<p class="sp-paywall-anon-helper"><?php esc_html_e( 'Check your email for the login link. It expires in 10 minutes.', 'bobasoftware-publisher-paywall' ); ?></p>
					<?php elseif ( $bspp_member_magic_state === 'invalid' ) : ?>
						<p class="sp-paywall-anon-helper"><?php esc_html_e( 'That login link is invalid or expired.', 'bobasoftware-publisher-paywall' ); ?></p>
					<?php elseif ( $bspp_member_magic_state === 'rate_limited' ) : ?>
						<p class="sp-paywall-anon-helper"><?php esc_html_e( 'Please wait a minute before requesting another login link.', 'bobasoftware-publisher-paywall' ); ?></p>
					<?php elseif ( $bspp_member_magic_state === 'mail_failed' ) : ?>
						<p class="sp-paywall-anon-helper"><?php esc_html_e( 'We could not send email from this environment. Please verify local mail setup.', 'bobasoftware-publisher-paywall' ); ?></p>
					<?php endif; ?>
					<form class="sp-paywall-anon-login sp-paywall-returning-login-form" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
						<input type="hidden" name="action" value="bspp_magic_login_request" />
						<input type="hidden" name="redirect_to" value="<?php echo esc_attr( (string) $magic_login_url ); ?>" />
						<?php wp_nonce_field( 'bspp_magic_login_request', 'bspp_magic_login_nonce' ); ?>
						<?php if ( $bspp_member_magic_state === 'sent' && ! empty( $bspp_member_sent_email ) && is_email( $bspp_member_sent_email ) ) : ?>
							<input type="hidden" name="email" value="<?php echo esc_attr( $bspp_member_sent_email ); ?>" />
							<button type="submit" class="button sp-paywall-btn sp-paywall-btn--secondary"><?php esc_html_e( 'Send another link', 'bobasoftware-publisher-paywall' ); ?></button>
						<?php else : ?>
							<label class="sp-paywall-anon-login__label" for="bspp_magic_login_email_member"><?php esc_html_e( 'Email', 'bobasoftware-publisher-paywall' ); ?></label>
							<input id="bspp_magic_login_email_member" type="email" name="email" placeholder="<?php echo esc_attr__( 'Email', 'bobasoftware-publisher-paywall' ); ?>" required />
							<button type="submit" class="button sp-paywall-btn sp-paywall-btn--secondary"><?php esc_html_e( 'Send login link', 'bobasoftware-publisher-paywall' ); ?></button>
						<?php endif; ?>
					</form>
				</div>
				<?php endif; ?>
			<?php endif; ?>
		<?php endif; ?>
	</section>
</div>
