NOT Pro. * - BUT the last-known-good cache is honoured until it expires, so a transient * outage does not instantly downgrade a paying customer. Only an explicit * "invalid" verdict (or cache expiry with a failed re-check) drops Pro. * * The `arwc_is_pro` filter remains the public override (e.g. for testing); it is * applied last so a site can still force Pro on regardless of the cached state. */ class ARWC_License { /** * Option storing the cached validation payload. * * @var string */ const OPTION_KEY = 'arwc_license'; /** * Option storing the raw license key the merchant pasted. * * @var string */ const OPTION_LICENSE_KEY = 'arwc_license_key'; /** * How long a validation verdict is trusted before a re-check (seconds). * * @var int */ const CACHE_TTL = 43200; // 12 hours. /** * Validation endpoint path appended to the API base. * * @var string */ const VALIDATE_PATH = '/api/license/validate'; /** * Whether the Pro tier is active. * * Resolves the cached verdict (refreshing it if stale), then lets the * `arwc_is_pro` filter override. Default for an unlicensed site is false. * * @return bool */ public function is_pro() { $active = $this->resolve_active(); /** * Filter whether Pro features are unlocked. * * @param bool $is_pro Resolved from the cached license verdict. */ return (bool) apply_filters( 'arwc_is_pro', $active ); } /** * Convenience guard for gated features. * * @return bool */ public function is_free() { return ! $this->is_pro(); } /** * The merchant's stored license key. * * @return string */ public function get_key() { return (string) get_option( self::OPTION_LICENSE_KEY, '' ); } /** * The cached validation payload, if any. * * @return array|null */ public function get_cache() { $cache = get_option( self::OPTION_KEY, null ); return is_array( $cache ) ? $cache : null; } /** * The active plan slug for display ("pro", "agency", or ''). * * @return string */ public function get_plan() { $cache = $this->get_cache(); return ( $cache && ! empty( $cache['valid'] ) ) ? (string) $cache['plan'] : ''; } /** * URL for the upgrade call-to-action shown next to locked features. * * @return string */ public function upgrade_url() { return (string) apply_filters( 'arwc_upgrade_url', 'https://www.agentictrack.ai/pricing' ); } /** * Base URL of the validation API (filterable). * * @return string */ public function api_base() { return (string) apply_filters( 'arwc_license_api_base', 'https://www.agentictrack.ai' ); } /** * Resolve whether Pro is currently active from the cache, re-validating in * the background when the cache is stale. * * @return bool */ protected function resolve_active() { $key = trim( $this->get_key() ); if ( '' === $key ) { return false; } $cache = $this->get_cache(); $now = time(); // Re-check when there is no cache or it has gone stale. $stale = ( null === $cache ) || empty( $cache['checked_at'] ) || ( ( $now - (int) $cache['checked_at'] ) > self::CACHE_TTL ); if ( $stale ) { $fresh = $this->validate_remote( $key ); if ( null !== $fresh ) { // Got a definitive verdict (valid or invalid): cache and use it. $cache = $fresh; $this->store_cache( $cache ); } elseif ( null === $cache ) { // No cache and the endpoint is unreachable: fail closed. return false; } // Otherwise (transient failure with an existing cache): keep // last-known-good and do NOT extend its freshness window, so we retry // on the next load until the endpoint answers. No write here, so // checked_at stays old and the cache is re-evaluated below. } return $this->is_cache_pro( $cache ); } /** * Whether a cached payload represents an active, unexpired Pro/Agency plan. * * @param array|null $cache Cached payload. * @return bool */ protected function is_cache_pro( $cache ) { if ( ! is_array( $cache ) || empty( $cache['valid'] ) ) { return false; } if ( isset( $cache['status'] ) && 'active' !== $cache['status'] ) { return false; } if ( ! empty( $cache['expires'] ) ) { $expires = strtotime( (string) $cache['expires'] ); if ( $expires && $expires < time() ) { return false; } } return true; } /** * Validate a key against the remote endpoint. * * @param string $key License key. * @return array|null Normalized verdict array, or null on a transient/network * error (caller should keep last-known-good). */ protected function validate_remote( $key ) { $url = trailingslashit( $this->api_base() ) . ltrim( self::VALIDATE_PATH, '/' ); $response = wp_remote_post( $url, array( 'timeout' => 15, 'headers' => array( 'Content-Type' => 'application/json' ), 'body' => wp_json_encode( array( 'key' => $key, 'site' => get_site_url(), ) ), ) ); if ( is_wp_error( $response ) ) { return null; // Transient: keep last-known-good. } $code = (int) wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { return null; // Transient/unexpected: keep last-known-good. } $data = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $data ) || ! array_key_exists( 'valid', $data ) ) { return null; // Unparseable: keep last-known-good. } // Normalize the response into our cache shape. if ( ! empty( $data['valid'] ) ) { return array( 'valid' => true, 'plan' => isset( $data['plan'] ) ? sanitize_key( $data['plan'] ) : 'pro', 'status' => isset( $data['status'] ) ? sanitize_key( $data['status'] ) : 'active', 'expires' => isset( $data['expires'] ) && null !== $data['expires'] ? sanitize_text_field( (string) $data['expires'] ) : '', 'checked_at' => time(), ); } return array( 'valid' => false, 'reason' => isset( $data['reason'] ) ? sanitize_key( $data['reason'] ) : 'not_found', 'checked_at' => time(), ); } /** * Persist the cached verdict (non-autoloaded). * * @param array $cache Verdict payload. * @return void */ protected function store_cache( array $cache ) { update_option( self::OPTION_KEY, $cache, false ); } /** * Force an immediate re-validation (e.g. right after the key is saved) and * cache the result. Used by the settings save handler. * * @return array|null The fresh verdict, or null on a transient failure. */ public function refresh() { $key = trim( $this->get_key() ); if ( '' === $key ) { delete_option( self::OPTION_KEY ); return null; } $fresh = $this->validate_remote( $key ); if ( null !== $fresh ) { $this->store_cache( $fresh ); } return $fresh; } }