<?php
/**
 * NOTE:
 * The WordPress.org hosted core plugin does NOT handle extension updates.
 * Each premium extension must implement its own updater class.
 * This helper only provides access to stored license data.
 */

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

/**
 * Helper for reading extension license keys and statuses.
 */
class IPB_License_Helper {

	/**
	 * Mapping of product unique IDs to license option keys.
	 *
	 * @var array<string, array<string, string>>
	 */
	private static $option_map = [
		'CFIPB7'    => [
			'license_key'    => '_contact_from_license_key',
			'license_active' => 'contact_from_license_active',
		],
		'Ctpb22'    => [
			'license_key'    => '_countdown_timer_license_key',
			'license_active' => 'countdown_timer_license_active',
		],
		'Sdpb21'    => [
			'license_key'    => '_subscribe_to_download_license_key',
			'license_active' => 'subscribe_to_download_license_active',
		],
		'ACipb9'    => [
			'license_key'    => '_advanced_closing_license_key',
			'license_active' => 'advanced_closing_license_active',
		],
		'IPB-102'   => [
			'license_key'    => '_subscription_license_key',
			'license_active' => 'subscription_license_active',
		],
		'WCIPB-005' => [
			'license_key'    => '_woocommerce_license_key',
			'license_active' => 'woocommerce_license_active',
		],
		'IPB-004'   => [
			'license_key'    => '_scroll_license_key',
			'license_active' => 'scroll_license_active',
		],
		'iTipb9'    => [
			'license_key'    => '_inactivity_license_key',
			'license_active' => 'inactivity_license_active',
		],
		'IPB-101'   => [
			'license_key'    => '_exit_intent_license_key',
			'license_active' => 'exit_intent_license_active',
		],
		'aTipb9'    => [
			'license_key'    => '_advanced_triggers_license_key',
			'license_active' => 'advanced_triggers_license_active',
		],
		'adIPB1'    => [
			'license_key'    => '_adblock_license_key',
			'license_active' => 'adblock_license_active',
		],
		'Sp-PB3'    => [
			'license_key'    => '_smart_license_key',
			'license_active' => 'smart_license_active',
		],
		'ACIPB-019' => [
			'license_key'    => '_analytics_license_key',
			'license_active' => 'analytics_license_active',
		],
		'PS_IBP1'   => [
			'license_key'    => '_scheduling_license_key',
			'license_active' => 'scheduling_license_active',
		],
		'gtPB1'     => [
			'license_key'    => '_geo_license_key',
			'license_active' => 'geo_license_active',
		],
		'Ctipb1'    => [
			'license_key'    => '_custom_targeting_license_key',
			'license_active' => 'custom_targeting_license_active',
		],
		'CTIPB1'    => [
			'license_key'    => '_custom_targeting_license_key',
			'license_active' => 'custom_targeting_license_active',
		],
		'RoPB1'     => [
			'license_key'    => '_recent_orders_license_key',
			'license_active' => 'recent_orders_license_active',
		],
		'IPB-001'   => [
			'license_key'    => '_video_license_key',
			'license_active' => 'video_license_active',
		],
		'PDFPB1'    => [
			'license_key'    => '_pdf_license_key',
			'license_active' => 'pdf_license_active',
		],
		'IgPB1'     => [
			'license_key'    => '_image_gallery_license_key',
			'license_active' => 'image_gallery_license_active',
		],
		'AvPB7'     => [
			'license_key'    => '_age_verification_license_key',
			'license_active' => 'age_verification_license_active',
		],
		'Ipb-ssp'   => [
			'license_key'    => '_social_share_popup_license_key',
			'license_active' => 'social_share_popup_license_active',
		],
		'LiSu-ipb'  => [
			'license_key'    => '_login_signup_popup_license_key',
			'license_active' => 'login_signup_popup_license_active',
		],
		'StW-ipb'   => [
			'license_key'    => '_spin_to_win_popup_license_key',
			'license_active' => 'spin_to_win_popup_license_active',
		],
		'AbIpbs'    => [
			'license_key'    => '_announcement_bar_popup_license_key',
			'license_active' => 'announcement_bar_popup_license_active',
		],
		'Lant-ipb'  => [
			'license_key'    => '_language_targeting_license_key',
			'license_active' => 'language_targeting_license_active',
		],
	];

	/**
	 * Get stored license key by extension unique ID.
	 *
	 * @param string $unique_id Product unique ID.
	 * @return string
	 */
	public static function get_license_key( $unique_id ) {
		if ( ! isset( self::$option_map[ $unique_id ]['license_key'] ) ) {
			return '';
		}

		return (string) get_option( self::$option_map[ $unique_id ]['license_key'], '' );
	}

	/**
	 * Check whether the extension license is active.
	 *
	 * @param string $unique_id Product unique ID.
	 * @return bool
	 */
	public static function is_license_active( $unique_id ) {
		if ( ! isset( self::$option_map[ $unique_id ]['license_active'] ) ) {
			return false;
		}

		return 'yes' === get_option( self::$option_map[ $unique_id ]['license_active'], 'no' );
	}
}
