/
home
/
altere25
/
.trash
/
wp-to-sanity-migration.6
/
/home/altere25/.trash/wp-to-sanity-migration.6
mkdir
upload
Name
Size
Mode
Actions
acf-json\.gitkeep
0
0644
edit
dl
rm
assets\css\admin.css
12113
0644
edit
dl
rm
assets\js\admin.js
52894
0644
edit
dl
rm
includes\class-ad-admin.php
10358
0644
edit
dl
rm
includes\class-ad-ai-client.php
10724
0644
edit
dl
rm
includes\class-ad-auth.php
9189
0644
edit
dl
rm
includes\class-ad-cli.php
8671
0644
edit
dl
rm
includes\class-ad-extractor.php
17237
0644
edit
dl
rm
includes\class-ad-mcp.php
6429
0644
edit
dl
rm
includes\class-ad-plugin-core.php
10252
0644
edit
dl
rm
includes\class-ad-progress.php
8022
0644
edit
dl
rm
includes\class-ad-rest.php
43443
0644
edit
dl
rm
includes\class-ad-uninstall.php
1966
0644
edit
dl
rm
includes\class-ad-webhook.php
24172
0644
edit
dl
rm
languages\.gitkeep
0
0644
edit
dl
rm
templates\admin\wizard.php
29414
0644
edit
dl
rm
wp-to-sanity-migration\acf-json\.gitkeep
0
0644
edit
dl
rm
wp-to-sanity-migration\assets\
0
0644
edit
dl
rm
wp-to-sanity-migration\assets\css\admin.css
12113
0644
edit
dl
rm
wp-to-sanity-migration\assets\js\admin.js
52894
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-admin.php
10358
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-ai-client.php
10724
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-auth.php
9189
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-cli.php
8671
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-extractor.php
17237
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-mcp.php
6429
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-plugin-core.php
10252
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-progress.php
8022
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-rest.php
43443
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-uninstall.php
1966
0644
edit
dl
rm
wp-to-sanity-migration\includes\class-ad-webhook.php
24172
0644
edit
dl
rm
wp-to-sanity-migration\languages\.gitkeep
0
0644
edit
dl
rm
wp-to-sanity-migration\package.json
220
0644
edit
dl
rm
wp-to-sanity-migration\readme.txt
8174
0644
edit
dl
rm
wp-to-sanity-migration\templates\
0
0644
edit
dl
rm
wp-to-sanity-migration\templates\admin\wizard.php
29414
0644
edit
dl
rm
wp-to-sanity-migration\uninstall.php
365
0644
edit
dl
rm
wp-to-sanity-migration\wp-to-sanity-migration.php
4418
0644
edit
dl
rm
Edit:
/home/altere25/.trash/wp-to-sanity-migration.6/includes\class-ad-ai-client.php
(10724B)
<?php /** * Altered Digital Migration — AI Service HTTP Client * * Thin wp_remote_* wrapper around the Altered Digital AI service * (apps/ai-service). The plugin authenticates with the Altered Digital * account JWT (AD_Auth) on user-facing routes, and with an internal token * on Worker-invoked routes. * * Phase B: stubbed — methods return empty/`not_configured` payloads. The * Phase C work wires these to the live AI service endpoints. * * @package AlteredDigitalMigration */ if (!defined('ABSPATH')) { exit; } class AD_AI_Client { /** @var AD_Plugin_Core */ private $core; public function __construct(AD_Plugin_Core $core) { $this->core = $core; } /** * Base AI service URL. Defaults to the product-wide Altered Digital AI * service; self-host deployments override via settings. */ private function service_url(): string { $url = $this->core->setting('ai_service_url', ''); if (empty($url)) { // Fall back to the product default (handles installs that saved // settings before the default was populated). $defaults = $this->core->default_settings(); $url = $defaults['ai_service_url'] ?? ''; } return $url; } /** * Internal token used for Worker→AI-service calls (distinct from the * user's AD account JWT). */ private function internal_token(): string { return $this->core->setting('ai_service_internal_token', ''); } /** * Trigger a full-site analysis. POSTs the plugin's aggregated analysis * payload (builder distribution, post counts, ACF inventory, media count) * to the AI service, which classifies complexity + estimates effort. * * @param string $wp_site_url * @param string $account_jwt Altered Digital account JWT (AD_Auth). * @param array $plugin_analysis_payload From AD_Extractor::build_analysis_payload(). * @return array{success:bool,data:array<string,mixed>} */ public function analyze(string $wp_site_url, string $account_jwt, array $plugin_analysis_payload = []): array { $url = $this->service_url(); if (empty($url)) { return [ 'success' => false, 'data' => ['error' => 'not_configured', 'message' => 'AI service URL not set.'], ]; } $endpoint = rtrim($url, '/') . '/analyze'; $response = wp_remote_post($endpoint, [ 'timeout' => 60, 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $account_jwt, ], 'body' => wp_json_encode([ 'wp_site_url' => $wp_site_url, 'plugin_analysis_payload' => $plugin_analysis_payload, ]), ]); return $this->parse_response($response); } /** * Fetch analysis status by id. * * @param string $analysis_id * @param string $account_jwt * @return array{success:bool,data:array<string,mixed>} */ public function analysis_status(string $analysis_id, string $account_jwt): array { $url = $this->service_url(); if (empty($url)) { return ['success' => false, 'data' => ['error' => 'not_configured']]; } $endpoint = rtrim($url, '/') . '/analyze/' . rawurlencode($analysis_id) . '/status'; $response = wp_remote_get($endpoint, [ 'timeout' => 15, 'headers' => ['Authorization' => 'Bearer ' . $account_jwt], ]); return $this->parse_response($response); } /** * Request a quote for an analysis. * * @param string $analysis_id * @param array<int,string> $add_ons * @param string $account_jwt * @param mixed|null $analysis_json Inline analysis data (avoids in-memory * cache lookup on serverless — Vercel instances don't share memory). * @return array{success:bool,data:array<string,mixed>} */ public function quote(string $analysis_id, array $add_ons, string $account_jwt, $analysis_json = null): array { $url = $this->service_url(); if (empty($url)) { return ['success' => false, 'data' => ['error' => 'not_configured']]; } $endpoint = rtrim($url, '/') . '/quote'; $body = [ 'analysis_id' => $analysis_id, 'add_ons' => $add_ons, ]; if (!empty($analysis_json)) { $body['analysis_json'] = $analysis_json; } $response = wp_remote_post($endpoint, [ 'timeout' => 30, 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $account_jwt, ], 'body' => wp_json_encode($body), ]); return $this->parse_response($response); } /** * Begin Stripe Checkout for a quote (50% upfront). * * @param string $quote_id * @param string $account_jwt * @return array{success:bool,data:array<string,mixed>} */ public function start_checkout(string $quote_id, string $account_jwt): array { $url = $this->service_url(); if (empty($url)) { return ['success' => false, 'data' => ['error' => 'not_configured']]; } $endpoint = rtrim($url, '/') . '/stripe/checkout'; $response = wp_remote_post($endpoint, [ 'timeout' => 30, 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $account_jwt, ], 'body' => wp_json_encode(['quote_id' => $quote_id]), ]); return $this->parse_response($response); } /** * Approve a migration (runs QA + captures final 50%). * * @param string $quote_id * @param string $account_jwt * @return array{success:bool,data:array<string,mixed>} */ public function approve(string $quote_id, string $account_jwt): array { $url = $this->service_url(); if (empty($url)) { return ['success' => false, 'data' => ['error' => 'not_configured']]; } $endpoint = rtrim($url, '/') . '/approve'; // Pass plugin-side return URLs so the customer lands back on the // wizard after paying. The AI service forwards these to Stripe so // the success_url is on the WP admin domain (not the AI service // domain, which 404s once the Stripe-hosted page is done). $success_url = admin_url('admin.php?page=wp-to-sanity&payment=final_success"e_id=' . rawurlencode($quote_id)); $cancel_url = admin_url('admin.php?page=wp-to-sanity&payment=final_cancelled"e_id=' . rawurlencode($quote_id)); $response = wp_remote_post($endpoint, [ 'timeout' => 45, 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $account_jwt, ], 'body' => wp_json_encode([ 'quote_id' => $quote_id, 'success_url' => $success_url, 'cancel_url' => $cancel_url, ]), ]); return $this->parse_response($response); } /** * Check final payment status for a quote. Read-only Supabase lookup * on the AI service. Used by the wizard's payment-return flow to * verify Stripe webhook has landed before unlocking Step 10. * * @param string $quote_id * @param string $account_jwt * @return array{success:bool,data:array<string,mixed>} */ public function payment_status(string $quote_id, string $account_jwt): array { $url = $this->service_url(); if (empty($url)) { return ['success' => false, 'data' => ['error' => 'not_configured']]; } $endpoint = rtrim($url, '/') . '/payment-status?quote_id=' . rawurlencode($quote_id); $response = wp_remote_get($endpoint, [ 'timeout' => 15, 'headers' => ['Authorization' => 'Bearer ' . $account_jwt], ]); return $this->parse_response($response); } /** * Generate a Sanity Studio schema from the site analysis. * * @param array $analysis_payload From AD_Extractor::build_analysis_payload(). * @param string $account_jwt * @return array{success:bool,data:array<string,mixed>} */ public function schema_generate(array $analysis_payload, string $account_jwt): array { $url = $this->service_url(); if (empty($url)) { return ['success' => false, 'data' => ['error' => 'not_configured']]; } $endpoint = rtrim($url, '/') . '/schema-generate'; $response = wp_remote_post($endpoint, [ 'timeout' => 90, 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $account_jwt, ], 'body' => wp_json_encode(['analysis_json' => $analysis_payload]), ]); return $this->parse_response($response); } // ----------------------------------------------------------------- // Internal helpers // ----------------------------------------------------------------- /** * Normalize a wp_remote_* response into the {success,data} envelope. */ private function parse_response($response): array { if (is_wp_error($response)) { return [ 'success' => false, 'data' => ['error' => $response->get_error_message()], ]; } $code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); $decoded = json_decode($body, true); if ($code < 200 || $code >= 300 || !is_array($decoded)) { return [ 'success' => false, 'data' => [ 'error' => is_array($decoded) && !empty($decoded['error']) ? $decoded['error'] : 'http_' . intval($code), 'status' => intval($code), 'body' => $body, ], ]; } // AI service already returns {success, data} envelopes. Re-wrapping // would nest data twice (resp.data.data.checkout_url) which breaks // the JS admin. Pass through when the envelope is already shaped. if (is_array($decoded) && isset($decoded['success'])) { return $decoded; } return [ 'success' => true, 'data' => $decoded, ]; } }
Save
cmd:
run