license_key = (string) get_option('if_license_key', ''); $this->domain = $this->resolve_domain(); } public function init(): void { add_action('if_daily_license_check', [$this, 'validate_now']); if (!wp_next_scheduled('if_daily_license_check')) { wp_schedule_event(time(), 'daily', 'if_daily_license_check'); } add_action('admin_notices', [$this, 'maybe_show_notice']); // WordPress.org: auto-update disabled in free version // WordPress.org: plugin_info disabled in free version } public function activate(string $key): array { $key = sanitize_text_field($key); if ($key === '') return ['ok' => false, 'error' => 'missing_key']; $response = $this->api_request('POST', '/v1/license/activate', [ 'license_key' => $key, 'domain' => $this->domain, 'site_url' => get_site_url(), 'wp_version' => get_bloginfo('version'), 'php_version' => PHP_VERSION, 'plugin_version' => defined(self::VERSION_CONST) ? constant(self::VERSION_CONST) : '0', ]); if ($response && (!empty($response['valid']) || !empty($response['success']))) { update_option('if_license_key', $key); $this->license_key = $key; $this->save_cache($response); update_option('if_license_last_good', $response); update_option('if_license_last_check', time()); return ['ok' => true, 'plan' => $response['plan'] ?? 'free']; } return ['ok' => false, 'error' => $response['error'] ?? 'activation_failed']; } public function deactivate(): void { if ($this->license_key) { $this->api_request('POST', '/v1/license/deactivate', [ 'license_key' => $this->license_key, 'domain' => $this->domain, ]); } delete_option('if_license_key'); delete_option('if_license_last_good'); delete_option('if_license_last_check'); delete_transient(self::CACHE_KEY); delete_transient(self::CACHE_SIG_KEY); $this->license_key = ''; $this->cached_status = []; } public function validate_now(): array { if (!$this->license_key) return $this->free_mode(); $response = $this->api_request('POST', '/v1/license/validate', [], [ 'X-NIW-Key' => $this->license_key, 'X-NIW-Domain' => $this->domain, 'X-NIW-Version' => defined(self::VERSION_CONST) ? constant(self::VERSION_CONST) : '0', ]); if (!$response) { $last_good = get_option('if_license_last_good'); $last_check = (int) get_option('if_license_last_check', 0); if ($last_good && is_array($last_good) && (time() - $last_check) < 86400) { $this->cached_status = $last_good; return $last_good; } return $this->free_mode(); } if (!empty($response['valid'])) { $response['domain'] = $this->domain; $this->save_cache($response); update_option('if_license_last_good', $response); update_option('if_license_last_check', time()); return $this->cached_status; } return $this->free_mode(); } public function get_status(): array { if (!empty($this->cached_status)) return $this->cached_status; $cached = get_transient(self::CACHE_KEY); if ($cached && is_array($cached)) { $sig = (string) get_transient(self::CACHE_SIG_KEY); if ($this->verify_cache($cached, $sig)) { $this->cached_status = $cached; return $cached; } delete_transient(self::CACHE_KEY); delete_transient(self::CACHE_SIG_KEY); } if (wp_doing_ajax() || (defined('REST_REQUEST') && REST_REQUEST)) { return $this->free_mode(); } return $this->validate_now(); } public function get_plan(): string { return $this->get_status()['plan'] ?? 'free'; } public function is_pro(): bool { return in_array($this->get_plan(), ['intent_flow_pro','pro','premium','agency'], true); } public function can(string $feature): bool { return in_array($feature, (array)($this->get_status()['features'] ?? []), true); } public function get_key(): string { return $this->license_key; } public function maybe_show_notice(): void { if (!current_user_can('manage_options')) return; $screen = get_current_screen(); if (!$screen || strpos($screen->id, 'mmi') === false) return; if (!$this->license_key) { echo '
'; printf('Intent Flow Pro: Introduce tu clave de licencia para activar WooCommerce, Google Sheets y más. Activar ahora →', esc_url(admin_url('admin.php?page=mmi-license'))); echo '