AlkantarClanX12

Your IP : 216.73.217.24


Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/classes/
Upload File :
Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/classes/settings.php

<?php if ( ! defined( 'ABSPATH' ) ) exit;

final class LB_API_Settings {

	/* Valid values for the lead-intake routing target (LBT-1468). */
	const LEAD_INTAKE_TARGETS = array('legacy', 'leadbox_os', 'both');
	const DEFAULT_LEAD_INTAKE_TARGET = 'legacy';

	/* Default base URL of the Leadbox Platform lead-intake endpoint (LBT-1468).
	   Site-configurable via the leadbox_os_lead_intake_url option; this constant
	   is the fallback used when that option is blank or holds an invalid URL.
	   The client appends /api/v1/leads. No trailing slash. */
	const LEADBOX_OS_LEAD_INTAKE_URL = 'https://os.leadboxhq.com';

	private $apiUrl;
	private $apiKey;
	private $apiLeadKey;
	private $apiCache;

	/* Leadbox Platform lead-intake routing (LBT-1468) */
	private $leadIntakeTarget;
	private $leadboxOsLeadIntakeUrl;
	private $leadboxOsLeadIntakeToken;

    public static function getInstance()
    {
        static $inst = null;
        if ($inst === null) {
            $inst = new LB_API_Settings();
        }
        return $inst;
    }

    private function __construct()
    {
        /* Fetch settings object */
        $settings = get_option('leadbox-settings');

		/* Leadbox API */
		$this->apiKey = $settings['inventory_api_token'];
		$this->apiUrl = $settings['leads_api_url'];

    if (!empty($this->apiUrl) && strpos($this->apiUrl, 'http') !== 0) {
        $this->apiUrl = 'https://' . ltrim($this->apiUrl, '/');
    } elseif (strpos($this->apiUrl, 'http://') === 0) {
        $this->apiUrl = str_replace('http://', 'https://', $this->apiUrl);
    }

		$this->apiLeadKey = $settings['leads_api_token'];

		/* Cache */
		$this->apiCache = true;

		/* Leadbox Platform lead-intake routing (LBT-1468) */
		$this->leadIntakeTarget = $this->resolveLeadIntakeTarget($settings);

		$this->leadboxOsLeadIntakeUrl = self::sanitizeLeadIntakeUrl(
			isset($settings['leadbox_os_lead_intake_url']) ? $settings['leadbox_os_lead_intake_url'] : '',
			self::LEADBOX_OS_LEAD_INTAKE_URL
		);

		$this->leadboxOsLeadIntakeToken = isset($settings['leadbox_os_lead_intake_token'])
			? trim($settings['leadbox_os_lead_intake_token'])
			: '';
    }

	/**
	 * Resolve the configured lead-intake base URL, falling back to $default when
	 * blank or invalid. Pure (no WordPress calls) so it can be unit-tested. (LBT-1468)
	 *
	 * - Empty/whitespace        -> $default
	 * - Missing scheme          -> https:// is prepended (mirrors apiUrl handling)
	 * - http:// (non-loopback)  -> upgraded to https://
	 * - http:// (loopback host) -> kept as-is for local dev (localhost/127.x/*.local/*.test)
	 * - Not a valid URL w/ host -> $default (option A: ignore silently, never break dispatch)
	 * - Trailing slash          -> stripped (the client appends /api/v1/leads)
	 *
	 * @param string $raw     Stored option value.
	 * @param string $default Fallback base URL (the constant).
	 * @return string A valid base URL with no trailing slash.
	 */
	public static function sanitizeLeadIntakeUrl($raw, $default)
	{
		$url = trim((string) $raw);
		if ($url === '') {
			return $default;
		}

		// Ensure a scheme is present so the host can be parsed.
		if (stripos($url, 'http://') !== 0 && stripos($url, 'https://') !== 0) {
			$url = 'https://' . ltrim($url, '/');
		}

		// Loopback carve-out (local dev): keep http:// as-is so the plugin can hit
		// a Platform on the same machine over plain HTTP. Production endpoints are
		// never loopback, so they are still upgraded to https.
		if (stripos($url, 'http://') === 0 && !self::isLoopbackHost(parse_url($url, PHP_URL_HOST))) {
			$url = 'https://' . substr($url, strlen('http://'));
		}

		$url = rtrim($url, '/');

		if (!filter_var($url, FILTER_VALIDATE_URL) || parse_url($url, PHP_URL_HOST) === null) {
			return $default;
		}

		return $url;
	}

	/**
	 * Whether $host is a loopback / local-development host. Pure (no WordPress)
	 * so it is unit-testable and reusable by the OS client to relax TLS for local
	 * endpoints. Covers localhost, the 127.0.0.0/8 range, IPv6 ::1, and the
	 * *.local / *.test dev TLDs. (LBT-1468)
	 *
	 * @param mixed $host Host portion of a URL (from parse_url).
	 * @return bool
	 */
	public static function isLoopbackHost($host)
	{
		if (!is_string($host) || $host === '') {
			return false;
		}
		$host = strtolower(trim($host, '[]'));
		if (in_array($host, array('localhost', '127.0.0.1', '::1'), true)) {
			return true;
		}
		if (preg_match('/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $host)) {
			return true;
		}
		if (preg_match('/\.(local|test)$/', $host)) {
			return true;
		}
		return false;
	}

	/**
	 * Resolve the lead-intake target. The LEADBOX_LEAD_INTAKE_TARGET PHP constant
	 * (wp-config.php) overrides the stored option so ops can cut a dealer over
	 * without DB access. An invalid constant value is ignored and we fall back to
	 * the option, then to the 'legacy' default. (LBT-1468)
	 */
	private function resolveLeadIntakeTarget($settings)
	{
		if (defined('LEADBOX_LEAD_INTAKE_TARGET')) {
			$constant = strtolower((string) LEADBOX_LEAD_INTAKE_TARGET);
			if (in_array($constant, self::LEAD_INTAKE_TARGETS, true)) {
				return $constant;
			}
		}

		$option = isset($settings['lead_intake_target']) ? strtolower(trim($settings['lead_intake_target'])) : '';
		if (in_array($option, self::LEAD_INTAKE_TARGETS, true)) {
			return $option;
		}

		return self::DEFAULT_LEAD_INTAKE_TARGET;
	}

	public function getApiUrl() {
		return $this->apiUrl;
	}

	public function getApiKey() {
		return $this->apiKey;
	}

	public function getApiLeadKey() {
		return $this->apiLeadKey;
	}

	public function getApiCache() {
		return $this->apiCache;
	}

	/* Leadbox Platform lead-intake routing (LBT-1468) */

	/**
	 * @return string One of 'legacy' | 'leadbox_os' | 'both'.
	 */
	public function getLeadIntakeTarget() {
		return $this->leadIntakeTarget;
	}

	/**
	 * @return bool Whether the target was forced via the LEADBOX_LEAD_INTAKE_TARGET constant.
	 */
	public function isLeadIntakeTargetForcedByConstant() {
		if (!defined('LEADBOX_LEAD_INTAKE_TARGET')) {
			return false;
		}
		return in_array(strtolower((string) LEADBOX_LEAD_INTAKE_TARGET), self::LEAD_INTAKE_TARGETS, true);
	}

	/**
	 * @return string Base URL of the Leadbox Platform lead-intake endpoint
	 *                (no trailing slash). The site-configured value when valid,
	 *                otherwise the LEADBOX_OS_LEAD_INTAKE_URL default.
	 */
	public function getLeadboxOsLeadIntakeUrl() {
		return $this->leadboxOsLeadIntakeUrl;
	}

	/**
	 * @return string Bearer token (wl_key_...) for the Leadbox Platform lead-intake endpoint.
	 */
	public function getLeadboxOsLeadIntakeToken() {
		return $this->leadboxOsLeadIntakeToken;
	}
}