bool, * 'withinQuota' => bool, * 'plan' => string, * 'includedTryOns'=> int, * 'remaining' => int, * 'resetsAt' => string (ISO8601), * 'trial' => array|null, * 'error' => string|null, // populated on transport/parse failure * ) * * @param bool $force Bypass the cache and re-fetch. * @return array */ public function get( $force = false ) { if ( ! Plugin::is_connected() ) { return $this->empty_status( __( 'Not connected', 'aisthetix-virtual-try-on' ) ); } if ( ! $force ) { $cached = get_transient( self::TRANSIENT ); if ( is_array( $cached ) ) { return $cached; } } $fresh = $this->fetch_from_brain(); // Cache successful responses for the full TTL; cache failures briefly so // a flaky brain does not hammer us, but recovers reasonably fast. $ttl = empty( $fresh['error'] ) ? self::CACHE_TTL : MINUTE_IN_SECONDS; set_transient( self::TRANSIENT, $fresh, $ttl ); return $fresh; } /** * Whether the try-on entry point should be shown on the storefront. * * Per DESIGN.md decision #14/#15: render only when active && withinQuota. * * @return bool */ public function should_render() { $status = $this->get(); return ! empty( $status['active'] ) && ! empty( $status['withinQuota'] ); } /** * Force-refresh the cache (used after Connect or via an admin action). * * @return array Fresh status. */ public function refresh() { delete_transient( self::TRANSIENT ); return $this->get( true ); } /** * Perform the server-to-server status call to the brain. * * @return array Normalised status (see {@see get()}). */ private function fetch_from_brain() { $url = Plugin::brain_url() . '/api/storefront/status'; $response = wp_remote_get( $url, array( 'timeout' => 10, 'headers' => array( 'X-Aisthetix-Secret' => Plugin::secret_key(), 'Accept' => 'application/json', ), ) ); if ( is_wp_error( $response ) ) { return $this->empty_status( $response->get_error_message() ); } $code = (int) wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( 200 !== $code || ! is_array( $data ) ) { /* translators: %d: HTTP status code. */ return $this->empty_status( sprintf( __( 'Brain returned HTTP %d', 'aisthetix-virtual-try-on' ), $code ) ); } $trial = null; if ( isset( $data['trial'] ) && is_array( $data['trial'] ) ) { $trial = array( 'cap' => isset( $data['trial']['cap'] ) ? (int) $data['trial']['cap'] : 50, 'used' => isset( $data['trial']['used'] ) ? (int) $data['trial']['used'] : 0, 'remaining' => isset( $data['trial']['remaining'] ) ? (int) $data['trial']['remaining'] : 0, 'endsAt' => isset( $data['trial']['endsAt'] ) ? sanitize_text_field( $data['trial']['endsAt'] ) : '', ); } return array( 'active' => ! empty( $data['active'] ), 'withinQuota' => ! empty( $data['withinQuota'] ), 'plan' => isset( $data['plan'] ) ? sanitize_text_field( $data['plan'] ) : '', 'includedTryOns' => isset( $data['includedTryOns'] ) ? (int) $data['includedTryOns'] : 0, 'remaining' => isset( $data['remaining'] ) ? (int) $data['remaining'] : 0, 'resetsAt' => isset( $data['resetsAt'] ) ? sanitize_text_field( $data['resetsAt'] ) : '', 'trial' => $trial, 'replay' => self::normalise_replay( isset( $data['replay'] ) ? $data['replay'] : null ), 'error' => null, ); } /** * Reduce Core's replay block to the four values the widget needs. * * Defaults to OFF for anything it does not recognise. An older Core that * sends no block at all, a malformed response, a failed request — none of * them are permission to record a shopper. * * @param mixed $raw The `replay` value from Core, if any. * @return array */ private static function normalise_replay( $raw ) { if ( ! is_array( $raw ) ) { return array( 'enabled' => false, 'key' => '', 'host' => '', 'environment' => '', ); } return array( 'enabled' => ! empty( $raw['enabled'] ), 'key' => isset( $raw['key'] ) ? sanitize_text_field( $raw['key'] ) : '', 'host' => isset( $raw['host'] ) ? esc_url_raw( $raw['host'] ) : '', 'environment' => isset( $raw['environment'] ) ? sanitize_text_field( $raw['environment'] ) : '', ); } /** * Build a "not available" status payload. * * @param string $error Human-readable reason. * @return array */ private function empty_status( $error ) { return array( 'active' => false, 'withinQuota' => false, 'plan' => '', 'includedTryOns' => 0, 'remaining' => 0, 'resetsAt' => '', 'trial' => null, // No answer from Core is not permission to record. This is the state // during an outage, during a bad deploy, and on a site whose keys have // been revoked — every one of them a moment to be recording less, not // more. 'replay' => self::normalise_replay( null ), 'error' => $error, ); } }