api_key = $api_key; $instance->name = Settings::get_client_name(); $instance->description = Settings::get_client_desc(); if ( '' === $api_key ) { $instance->api_key = ''; $instance->name = Settings::get_client_name(); $instance->description = Settings::get_client_desc(); $instance->credits = 0; $instance->client_id = ''; $instance->authenticated = false; return $instance; } $transient_key = self::get_transient_key( $api_key ); $cached = get_transient( $transient_key ); if ( is_array( $cached ) ) { $instance->authenticated = ! empty( $cached['authenticated'] ); $instance->api_key = $api_key; $instance->client_id = (string) ( $cached['client_id'] ?? '' ); $instance->credits = (int) ( $cached['credits'] ?? 0 ); $instance->name = Settings::get_client_name(); $instance->description = Settings::get_client_desc(); return $instance; } $instance->api_key = $api_key; $instance->name = Settings::get_client_name(); $instance->description = Settings::get_client_desc(); $client = GenericRequest::get_request_payload( Endpoints::credits(), GenericRequest::get_request_args( $api_key ) ); if ( is_array( $client ) && empty( $client['error'] ) ) { $instance->credits = (int) ( $client['effectiveCredits'] ?? $client['credits'] ?? 0 ); $instance->client_id = (string) ( $client['clientId'] ?? '' ); $instance->authenticated = '' !== $instance->client_id; set_transient( $transient_key, array( 'authenticated' => $instance->authenticated, 'client_id' => $instance->client_id, 'credits' => $instance->credits, ), self::CACHE_TTL ); } else { $instance->credits = 0; $instance->client_id = ''; $instance->authenticated = false; } return $instance; } /** * Load the client from the transient cache only — never triggers a remote API call. * * When the transient is absent or expired the returned client is unauthenticated * with 0 credits. Use this for read-only rendering paths where blocking on an * outbound HTTP request is not acceptable (e.g. product-edit metabox render). * * @return self */ public static function get_cached(): self { $instance = new self(); $api_key = Settings::get_client_api_key(); $instance->api_key = $api_key; $instance->name = Settings::get_client_name(); $instance->description = Settings::get_client_desc(); $instance->authenticated = false; $instance->credits = 0; $instance->client_id = ''; if ( '' === $api_key ) { return $instance; } $cached = get_transient( self::get_transient_key( $api_key ) ); if ( is_array( $cached ) ) { $instance->authenticated = ! empty( $cached['authenticated'] ); $instance->client_id = (string) ( $cached['client_id'] ?? '' ); $instance->credits = (int) ( $cached['credits'] ?? 0 ); } return $instance; } /** * Build transient cache key for API key. * * @param string $api_key API key. * @return string */ private static function get_transient_key( string $api_key ): string { return 'ai_content_by_parallax_client_' . md5( $api_key ); } /** * Determine if the client is authenticated with the API. * * @return bool */ public function is_user_authenticated(): bool { return $this->authenticated; } /** * Get the client ID. * * @return string */ public function get_client_id(): string { return $this->client_id; } /** * Get the API key. * * @return string */ public function get_api_key(): string { return $this->api_key; } /** * Get available credits. * * @return int */ public function get_credits(): int { return $this->credits; } /** * Get the client name. * * @return string */ public function get_name(): string { return $this->name; } /** * Get the client description. * * @return string */ public function get_description(): string { return $this->description; } /** * Build a site-based identifier. * * @return string */ public function get_client_identifier(): string { return str_replace( '.', '_', wp_parse_url( site_url(), PHP_URL_HOST ) ); } /** * Represent client data as an array suitable for display. * * @return array */ public function get_info_array(): array { return array( array( 'Key' => 'Authenticated', 'Value' => $this->is_user_authenticated() ? 'yes' : 'no', ), array( 'Key' => 'Client ID', 'Value' => (string) $this->get_client_id(), ), array( 'Key' => 'Credits', 'Value' => (string) $this->get_credits(), ), array( 'Key' => 'Name', 'Value' => (string) $this->get_name(), ), array( 'Key' => 'Description', 'Value' => (string) $this->get_description(), ), array( 'Key' => 'Identifier', 'Value' => (string) $this->get_client_identifier(), ), array( 'Key' => 'API Key', 'Value' => (string) $this->get_api_key(), ), ); } }