hooks(); if (!$this->woocommerce_active() || $this->pro_active()) { return; } $transfer = new ThreeDL_CC_Free_Transfer($this); $cart = new ThreeDL_CC_Free_Cart($this, $transfer); $status = new ThreeDL_CC_Free_Status($this); $transfer->set_cart($cart); $transfer->hooks(); $cart->hooks(); $status->hooks(); } public function declare_hpos_compatibility(): void { if (class_exists('Automattic\\WooCommerce\\Utilities\\FeaturesUtil')) { Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', THREEDL_CC_FREE_FILE, true); } } public static function default_options(): array { return [ 'mode' => 'disabled', 'main_store_url' => '', 'secret_key' => '', 'trusted_sources' => '', 'transfer_ttl' => 600, 'after_add' => 'return_source', ]; } public function get_options(): array { if (is_array($this->options_cache)) { return $this->options_cache; } $saved = get_option(self::OPTION_KEY, []); if (!is_array($saved)) { $saved = []; } $this->options_cache = wp_parse_args($saved, self::default_options()); return $this->options_cache; } public function clear_options_cache(): void { $this->options_cache = null; } public function get_mode(): string { $mode = (string) ($this->get_options()['mode'] ?? 'disabled'); return in_array($mode, ['main', 'source'], true) ? $mode : 'disabled'; } public function is_main(): bool { return $this->get_mode() === 'main'; } public function is_source(): bool { return $this->get_mode() === 'source'; } public function get_secret(): string { return trim((string) ($this->get_options()['secret_key'] ?? '')); } public function main_store_url(): string { if ($this->is_main()) { return $this->normalize_url(home_url('/')); } return $this->normalize_url((string) ($this->get_options()['main_store_url'] ?? '')); } public function current_store_url(): string { return $this->normalize_url(home_url('/')); } public function trusted_sources(): array { $raw = (string) ($this->get_options()['trusted_sources'] ?? ''); $lines = preg_split('/\r\n|\r|\n/', $raw) ?: []; $out = []; foreach ($lines as $line) { $url = $this->normalize_url(trim((string) $line)); if ($url !== '') { $out[$url] = true; } } return array_keys($out); } public function transfer_ttl(): int { $ttl = absint($this->get_options()['transfer_ttl'] ?? 600); return min(1800, max(60, $ttl)); } public function after_add_behavior(): string { $behavior = sanitize_key((string) ($this->get_options()['after_add'] ?? 'return_source')); return in_array($behavior, ['return_source', 'cart'], true) ? $behavior : 'return_source'; } public function normalize_url(string $url): string { $url = esc_url_raw(trim($url)); if ($url === '') { return ''; } return trailingslashit(untrailingslashit($url)); } public function source_is_trusted(string $source_url): bool { $source_url = $this->normalize_url($source_url); if ($source_url === '') { return false; } foreach ($this->trusted_sources() as $trusted) { if (hash_equals(strtolower($trusted), strtolower($source_url))) { return true; } } return false; } public function woocommerce_active(): bool { return class_exists('WooCommerce') || defined('WC_VERSION'); } public function pro_active(): bool { return class_exists('ThreeDL_Central_Checkout'); } public function environment_ready(): bool { return is_multisite() && $this->woocommerce_active(); } }