<?php
/**
 * Admin promotional banner for the BlockWPThemes portal and theme bundle.
 *
 * Shown on all admin screens to users who can manage the theme, and snoozed
 * for three hours per user when the close button is used.
 *
 * @package FlexiBlog
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'FlexiBlog_Promo_Banner' ) ) {

	/**
	 * Renders and handles the dismissal of the admin promotional banner.
	 */
	class FlexiBlog_Promo_Banner {

		/**
		 * User meta key holding the timestamp until which the banner stays hidden.
		 *
		 * @var string
		 */
		const SNOOZE_META = 'flexiblog_promo_banner_snoozed_until';

		/**
		 * AJAX action name, also used as the nonce action.
		 *
		 * @var string
		 */
		const ACTION = 'flexiblog_dismiss_promo_banner';

		/**
		 * How long the banner stays hidden after the close button is used.
		 *
		 * @var int
		 */
		const SNOOZE_SECONDS = 3 * HOUR_IN_SECONDS;

		/**
		 * Hook everything up.
		 */
		public function __construct() {
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
			add_action( 'admin_notices', array( $this, 'render' ) );
			add_action( 'wp_ajax_' . self::ACTION, array( $this, 'handle_dismiss' ) );
			add_action( 'switch_theme', array( $this, 'reset' ) );
			add_action( 'after_switch_theme', array( $this, 'reset' ) );
		}

		/**
		 * Whether the banner should be rendered on the current request.
		 *
		 * @return bool
		 */
		public static function is_visible() {
			if ( ! is_admin() || wp_doing_ajax() ) {
				return false;
			}

			if ( ! current_user_can( 'edit_theme_options' ) ) {
				return false;
			}

			// Keep core maintenance flows free of promotional content.
			$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

			if ( $screen instanceof WP_Screen ) {
				$excluded = array( 'update-core', 'update', 'plugin-install', 'theme-install', 'plugin-editor', 'theme-editor' );

				if ( in_array( $screen->id, $excluded, true ) || in_array( $screen->base, $excluded, true ) ) {
					return false;
				}
			}

			$snoozed_until = (int) get_user_meta( get_current_user_id(), self::SNOOZE_META, true );

			return ( time() >= $snoozed_until );
		}

		/**
		 * Enqueue the banner stylesheet and script.
		 *
		 * Both files ship with the theme, so the banner also renders offline.
		 */
		public function enqueue_assets() {
			if ( ! self::is_visible() ) {
				return;
			}

			$theme   = wp_get_theme( get_template() );
			$version = $theme->get( 'Version' );
			$uri     = get_template_directory_uri() . '/promo';

			wp_enqueue_style( 'flexiblog-promo-banner', $uri . '/promo-banner.css', array(), $version );

			wp_enqueue_script( 'flexiblog-promo-banner', $uri . '/promo-banner.js', array(), $version, true );

			wp_localize_script(
				'flexiblog-promo-banner',
				'flexiBlogPromoBanner',
				array(
					'ajaxUrl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
					'action'  => self::ACTION,
					'nonce'   => wp_create_nonce( self::ACTION ),
				)
			);
		}

		/**
		 * Output the banner markup.
		 */
		public function render() {
			if ( ! self::is_visible() ) {
				return;
			}

			$bundle_url = 'https://blockwpthemes.com/product/wordpress-theme-bundle/?utm_source=wordpress&utm_medium=theme&utm_campaign=bwp_free_theme_promotion';
			$portal_url = 'https://blockwpthemes.com/wordpress-themes/?utm_source=wordpress&utm_medium=theme&utm_campaign=bwp_free_theme_promotion';

			/* translators: Bundle price as advertised on the theme author's site. */
			$price     = __( '$89', 'flexiblog' );
			/* translators: Combined value of the themes included in the bundle. */
			$was_price = __( '$735', 'flexiblog' );

			$features = array(
				__( '50+ themes', 'flexiblog' ),
				__( '1 year of updates', 'flexiblog' ),
				__( '1 year of support', 'flexiblog' ),
				__( '100% GPL licensed', 'flexiblog' ),
				__( 'One-click Demo Import', 'flexiblog' ),
			);
			?>
			<div class="notice flexiblog-promo" id="flexiblog-promo-banner">
				<div class="flexiblog-promo__inner">

					<div class="flexiblog-promo__vis" aria-hidden="true">
						<span class="flexiblog-promo__stack">
							<span class="flexiblog-promo__card"></span>
							<span class="flexiblog-promo__card"></span>
							<span class="flexiblog-promo__card"><strong>50+</strong></span>
						</span>
						<span class="flexiblog-promo__tag">
							<span class="flexiblog-promo__pulse"></span>
							<?php esc_html_e( 'Block themes', 'flexiblog' ); ?>
						</span>
					</div>

					<div class="flexiblog-promo__content">
						<p class="flexiblog-promo__eyebrow">
							<?php esc_html_e( 'BlockWPThemes', 'flexiblog' ); ?>
						</p>

						<h2 class="flexiblog-promo__title">
							<?php
							printf(
								/* translators: %s: emphasised phrase, styled in italics. */
								esc_html__( 'Professional WordPress themes %s', 'flexiblog' ),
								'<em>' . esc_html__( 'built to last', 'flexiblog' ) . '</em>'
							);
							?>
						</h2>

						<p class="flexiblog-promo__text">
							<?php
							esc_html_e( 'Get every theme we make from one customer portal — download, update and manage your licences in a single place.', 'flexiblog' );
							?>
						</p>

						<p class="flexiblog-promo__price">
							<del><?php echo esc_html( $was_price ); ?></del>
							<?php echo esc_html( $price ); ?>
							<span><?php esc_html_e( 'All-Access Bundle', 'flexiblog' ); ?></span>
						</p>

						<ul class="flexiblog-promo__features">
							<?php foreach ( $features as $feature ) : ?>
								<li><?php echo esc_html( $feature ); ?></li>
							<?php endforeach; ?>
						</ul>

						<p class="flexiblog-promo__actions">
							<a class="flexiblog-promo__btn" href="<?php echo esc_url( $bundle_url ); ?>" target="_blank" rel="noopener noreferrer">
								<?php esc_html_e( 'Get the bundle', 'flexiblog' ); ?>
								<span class="screen-reader-text"><?php esc_html_e( '(opens in a new tab)', 'flexiblog' ); ?></span>
							</a>
							<a class="flexiblog-promo__btn flexiblog-promo__btn--ghost" href="<?php echo esc_url( $portal_url ); ?>" target="_blank" rel="noopener noreferrer">
								<?php esc_html_e( 'Browse all themes', 'flexiblog' ); ?>
								<span class="screen-reader-text"><?php esc_html_e( '(opens in a new tab)', 'flexiblog' ); ?></span>
							</a>
						</p>

					</div>

					<button type="button" class="flexiblog-promo__close" data-flexiblog-promo-dismiss="snooze">
						<span class="screen-reader-text">
							<?php esc_html_e( 'Hide this notice for 3 hours', 'flexiblog' ); ?>
						</span>
						<span class="flexiblog-promo__x" aria-hidden="true"></span>
					</button>

				</div>
			</div>
			<?php
		}

		/**
		 * Snooze the banner for the current user.
		 */
		public function handle_dismiss() {
			check_ajax_referer( self::ACTION, 'nonce' );

			if ( ! current_user_can( 'edit_theme_options' ) ) {
				wp_send_json_error( array( 'message' => __( 'You are not allowed to do that.', 'flexiblog' ) ), 403 );
			}

			$snoozed_until = time() + self::SNOOZE_SECONDS;

			update_user_meta( get_current_user_id(), self::SNOOZE_META, $snoozed_until );

			wp_send_json_success( array( 'snoozedUntil' => $snoozed_until ) );
		}

		/**
		 * Clear stored snoozes when the theme is switched.
		 */
		public function reset() {
			$users = get_users(
				array(
					'meta_key' => self::SNOOZE_META, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
					'fields'   => 'ID',
					'number'   => 200,
				)
			);

			foreach ( $users as $user_id ) {
				delete_user_meta( $user_id, self::SNOOZE_META );
			}
		}
	}
}
