__('Add your site key and secret key under Settings → Sarcio first.', 'sarcio')]; } $origin = self::adminOrigin(); $cacheKey = 'sarcio_embed_' . md5($siteKey . '|' . $origin . '|' . Options::apiUrl()); if (!$refresh) { $cached = get_transient($cacheKey); if (is_array($cached) && isset($cached['embedUrl'])) { return $cached; } } $response = wp_remote_post( sprintf('%s/api/sites/%s/embed', Options::apiUrl(), rawurlencode($siteKey)), [ 'body' => (string) wp_json_encode(['origin' => $origin]), 'headers' => [ 'Authorization' => 'Bearer ' . $secret, 'Content-Type' => 'application/json', ], 'redirection' => 0, 'timeout' => 5, ], ); if (is_wp_error($response)) { return ['error' => __('Could not reach Sarcio. Check your connection and try again.', 'sarcio')]; } $status = (int) wp_remote_retrieve_response_code($response); if ($status === 401) { return ['error' => __('Sarcio did not accept the secret key. Check it under Settings → Sarcio.', 'sarcio')]; } $body = json_decode((string) wp_remote_retrieve_body($response), true); if (($status !== 200 && $status !== 201) || !is_array($body) || !is_string($body['embedUrl'] ?? null)) { return ['error' => __('Sarcio could not open the review screens for this site right now.', 'sarcio')]; } $info = [ 'embedUrl' => esc_url_raw($body['embedUrl']), 'siteName' => sanitize_text_field((string) ($body['siteName'] ?? '')), 'workspaceName' => sanitize_text_field((string) ($body['workspaceName'] ?? '')), ]; Brand::store(is_array($body['branding'] ?? null) ? $body['branding'] : null); set_transient($cacheKey, $info, self::CACHE_SEC); return $info; } public static function workspaceUrl(): ?string { $info = self::cachedInfo(); if (!isset($info['embedUrl'])) { return null; } $parts = wp_parse_url($info['embedUrl']); if (!is_array($parts) || empty($parts['scheme']) || empty($parts['host'])) { return null; } return $parts['scheme'] . '://' . $parts['host'] . (isset($parts['port']) ? ':' . $parts['port'] : ''); } public static function adminOrigin(): string { $parts = wp_parse_url(admin_url()); if (!is_array($parts) || empty($parts['host'])) { return ''; } return ($parts['scheme'] ?? 'https') . '://' . $parts['host'] . (isset($parts['port']) ? ':' . $parts['port'] : ''); } /** @return array */ private static function cachedInfo(): array { $siteKey = (string) Options::get('site_key'); $cached = get_transient('sarcio_embed_' . md5($siteKey . '|' . self::adminOrigin() . '|' . Options::apiUrl())); return is_array($cached) ? $cached : []; } }