/
home
/
altere25
/
.trash
/
wp-to-sanity-migration.12
/
/home/altere25/.trash/wp-to-sanity-migration.12
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.12/includes\class-ad-progress.php
(8022B)
<?php /** * Altered Digital Migration — Progress Tracker * * Replaces the dead `pollMigrationStatus` JS path. Reads real migration * state from the Worker's multi-tenant `/v2/migrations` endpoint and * reconciles the local WP `ad_migration_log` table with D1. * * @package AlteredDigitalMigration */ if (!defined('ABSPATH')) { exit; } class AD_Progress { /** @var AD_Plugin_Core */ private $core; public function __construct(AD_Plugin_Core $core) { $this->core = $core; } /** * Fetch aggregate migration progress from the Worker and reconcile the * local log table with D1 rows. * * @return array{success:bool,data:array<string,mixed>} */ public function fetch_progress(): array { $service_url = $this->core->setting('service_url', ''); $admin_token = $this->core->setting('admin_token', ''); $tenant_id = $this->core->setting('tenant_id', ''); if (empty($service_url) || empty($admin_token)) { return [ 'success' => false, 'data' => ['error' => 'not_connected', 'message' => 'Worker service not configured.'], ]; } $endpoint = rtrim($service_url, '/') . '/v2/migrations'; if (!empty($tenant_id)) { $endpoint .= '?tenant_id=' . rawurlencode($tenant_id); } $response = wp_remote_get($endpoint, [ 'timeout' => 15, 'headers' => [ 'Authorization' => 'Bearer ' . $admin_token, 'X-AD-Tenant' => $tenant_id, ], ]); 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 || !is_array($decoded)) { return [ 'success' => false, 'data' => [ 'error' => 'http_' . intval($code), 'status' => intval($code), 'body' => $body, ], ]; } // Reconcile local log rows with the D1 payload. if (!empty($decoded['recent']) && is_array($decoded['recent'])) { $this->reconcile_local_log($decoded['recent']); } return ['success' => true, 'data' => $decoded]; } /** * Fetch per-post migration history from the Worker and reconcile the * local row for that post. * * @param int $post_id * @return array{success:bool,data:array<string,mixed>} */ public function fetch_post_history(int $post_id): array { $service_url = $this->core->setting('service_url', ''); $admin_token = $this->core->setting('admin_token', ''); $tenant_id = $this->core->setting('tenant_id', ''); if (empty($service_url) || empty($admin_token)) { return ['success' => false, 'data' => ['error' => 'not_connected']]; } $endpoint = rtrim($service_url, '/') . '/v2/migrations/' . intval($post_id); $response = wp_remote_get($endpoint, [ 'timeout' => 15, 'headers' => [ 'Authorization' => 'Bearer ' . $admin_token, 'X-AD-Tenant' => $tenant_id, ], ]); 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 || !is_array($decoded)) { return [ 'success' => false, 'data' => ['error' => 'http_' . intval($code), 'status' => intval($code)], ]; } if (!empty($decoded['entries']) && is_array($decoded['entries'])) { $this->reconcile_local_log($decoded['entries']); } return ['success' => true, 'data' => $decoded]; } // ----------------------------------------------------------------- // Local log reconciliation // ----------------------------------------------------------------- /** * Walk D1 rows and update the local `ad_migration_log` table so the * admin meta box reflects Worker-side state (status, sanity_id, * error_message). Pending rows flip to success/failed; succeeded rows * get their sanity_id backfilled. * * @param array<int,array<string,mixed>> $d1_rows */ private function reconcile_local_log(array $d1_rows): void { global $wpdb; $table = $wpdb->prefix . 'ad_migration_log'; foreach ($d1_rows as $row) { $post_id = (int) ($row['post_id'] ?? 0); $status = (string) ($row['status'] ?? ''); $sanity_id = (string) ($row['sanity_id'] ?? ''); $error = (string) ($row['error_message'] ?? ''); $action = (string) ($row['action'] ?? ''); if (empty($post_id) || empty($status)) { continue; } // Update the most recent local row for this post + action that's // still pending. If none exists, skip — the Worker is source of // truth for v2 and we don't want to fabricate local rows. $wpdb->query($wpdb->prepare( "UPDATE {$table} SET status = %s, sanity_id = %s, error_message = %s, updated_at = CURRENT_TIMESTAMP WHERE post_id = %d AND action = %s AND status = 'pending' ORDER BY id DESC LIMIT 1", $status, $sanity_id ?: null, $error ?: null, $post_id, $action )); } } /** * Insert a pending row in the local log (called from REST handlers * before firing the webhook so the meta box shows "pending" immediately). * * @param int $post_id * @param string $action */ public function log_pending(int $post_id, string $action): void { global $wpdb; $table = $wpdb->prefix . 'ad_migration_log'; $wpdb->insert( $table, [ 'post_id' => $post_id, 'action' => $action, 'status' => 'pending', ], ['%d', '%s', '%s'] ); } /** * Read the most recent local log row for a post (admin meta box). * * @param int $post_id * @return array<string,mixed>|null */ public function get_local_last_sync(int $post_id): ?array { global $wpdb; $table = $wpdb->prefix . 'ad_migration_log'; $row = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$table} WHERE post_id = %d ORDER BY created_at DESC LIMIT 1", $post_id ), ARRAY_A); return $row ?: null; } /** * Aggregate local stats for the dashboard card (offline fallback when * the Worker is unreachable). * * @return array<string,int> */ public function local_stats(): array { global $wpdb; $table = $wpdb->prefix . 'ad_migration_log'; $stats = $wpdb->get_row( "SELECT COUNT(*) as total, SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) as success, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending, SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed FROM {$table}", ARRAY_A ); return [ 'total' => (int) ($stats['total'] ?? 0), 'success' => (int) ($stats['success'] ?? 0), 'pending' => (int) ($stats['pending'] ?? 0), 'failed' => (int) ($stats['failed'] ?? 0), ]; } }
Save
cmd:
run