PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.2
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
← All changes | includes/API/AIContent.php +613 -4 3.6.43.7.2 View file →
@@ -11,8 +11,9 @@
11 11
12 12 use Error;
13 13 use Exception;
14 14 use Templately\Utils\Helper;
15 +use Templately\Utils\Database;
15 16 use WP_REST_Request;
16 17 use WP_Error;
17 18 use Templately\Core\Importer\Utils\Utils;
18 19 use Templately\Core\Importer\Utils\AIUtils;
@@ -22,9 +23,22 @@
22 23 class AIContent extends API {
23 24 private $endpoint = 'ai-content';
24 25 private $dev_mode = false;
25 26
27 + /**
28 + * Short-lived cache of the `v2/chatbot/generated/{chat}` bundle.
29 + *
30 + * That payload is large (every generated page's block JSON, served off GCP)
31 + * and the direct-import handoff pulls it twice within seconds — once to read
32 + * the customization, once to write the pages. Only a COMPLETE bundle is ever
33 + * reused (an incomplete one has to be re-pulled to pick up new pages), and
34 + * the TTL is deliberately short so the `can_import` / already-imported gate
35 + * cannot go meaningfully stale.
36 + */
37 + const GENERATED_CACHE_KEY = 'chatbot_generated_';
38 + const GENERATED_CACHE_TTL = 60;
26 39
40 +
27 41 /**
28 42 * AIContent constructor.
29 43 *
30 44 * @param string $file File path.
@@ -42,12 +56,15 @@
42 56 $process_id = $this->get_param('process_id');
43 57
44 58 $_route = $request->get_route();
45 59 if ('/templately/v1/ai-content/ai-update' === $_route || '/templately/v1/ai-content/ai-update-preview' === $_route) {
46 - Helper::log( [
47 - 'headers' => $request->get_headers(),
48 - 'body' => $request->get_params(),
49 - ], 'ai_update_request' );
60 + // Disabled: the headers carry X-Templately-Apikey, and Helper::log()
61 + // writes to debug.log, which is web-readable on plenty of hosts. That
62 + // key authorizes this very route.
63 + // Helper::log( [
64 + // 'headers' => $request->get_headers(),
65 + // 'body' => $request->get_params(),
66 + // ], 'ai_update_request' );
50 67
51 68 if (empty($process_id)) {
52 69 return $this->error('invalid_id', __('Invalid ID.', 'templately'), 'calculate_credit', 400);
53 70 }
@@ -98,8 +115,36 @@
98 115 return is_string($param) && strlen($param) > 0 && strlen($param) <= 128 && preg_match('/^[A-Za-z0-9\-_]+$/', $param);
99 116 },
100 117 ],
101 118 ]);
119 + $this->get($this->endpoint . '/chatbot-generated', [$this, 'get_chatbot_generated'], [
120 + 'chat' => [
121 + 'required' => true,
122 + 'sanitize_callback' => 'sanitize_text_field',
123 + 'validate_callback' => function($param, $request, $key) {
124 + return is_string($param) && strlen($param) > 0 && strlen($param) <= 128 && preg_match('/^[A-Za-z0-9\-_]+$/', $param);
125 + },
126 + ],
127 + ]);
128 + $this->post($this->endpoint . '/chatbot-detected-info', [$this, 'update_chatbot_detected_info'], [
129 + 'chat' => [
130 + 'required' => true,
131 + 'sanitize_callback' => 'sanitize_text_field',
132 + 'validate_callback' => function($param, $request, $key) {
133 + return is_string($param) && strlen($param) > 0 && strlen($param) <= 128 && preg_match('/^[A-Za-z0-9\-_]+$/', $param);
134 + },
135 + ],
136 + ]);
137 + $this->post($this->endpoint . '/chatbot-import-prepare', [$this, 'chatbot_import_prepare']);
138 + $this->post($this->endpoint . '/chatbot-mark-imported', [$this, 'mark_chatbot_imported'], [
139 + 'chat' => [
140 + 'required' => true,
141 + 'sanitize_callback' => 'sanitize_text_field',
142 + 'validate_callback' => function($param, $request, $key) {
143 + return is_string($param) && strlen($param) > 0 && strlen($param) <= 128 && preg_match('/^[A-Za-z0-9\-_]+$/', $param);
144 + },
145 + ],
146 + ]);
102 147 $this->get($this->endpoint . '/attachments', [$this, 'get_attachments'], [
103 148 'type' => [
104 149 'default' => 'pack',
105 150 'required' => false,
@@ -982,8 +1027,572 @@
982 1027 return $this->error('invalid_response', __('Invalid response.', 'templately'), 'ai-content/chatbot-conversation', 500);
983 1028 }
984 1029
985 1030 return $data;
1031 + }
1032 +
1033 + /**
1034 + * Fetch the server-side generated content for a chatbot conversation (Phase 2).
1035 + *
1036 + * In Phase 2 the AI content is generated on the backend. This proxy mirrors
1037 + * {@see get_chatbot_conversation()} and returns the already-generated page
1038 + * content, customization data and signed logo URL so the plugin can run a
1039 + * thin import without triggering generation or the customizer locally.
1040 + *
1041 + * @return array|\WP_Error Pass-through of the external response { status, data } or WP_Error.
1042 + */
1043 + public function get_chatbot_generated() {
1044 + $chat = $this->get_param('chat');
1045 +
1046 + if (empty($chat)) {
1047 + return $this->error('invalid_chat_id', __('Invalid conversation ID.', 'templately'), 'ai-content/chatbot-generated', 400);
1048 + }
1049 +
1050 + $extra_headers = [
1051 + 'Accept' => 'application/json',
1052 + ];
1053 + $response = Helper::make_api_get_request("v2/chatbot/generated/{$chat}", [], $extra_headers, 30);
1054 +
1055 + if (is_wp_error($response)) {
1056 + return $this->error('request_failed', __('Failed to fetch generated content.', 'templately'), 'ai-content/chatbot-generated', 500, ['error_detail' => $response->get_error_message()]);
1057 + }
1058 +
1059 + $response_code = wp_remote_retrieve_response_code($response);
1060 + $body = wp_remote_retrieve_body($response);
1061 + $data = json_decode($body, true);
1062 +
1063 + if ($response_code !== 200) {
1064 + $message = (is_array($data) && !empty($data['message'])) ? $data['message'] : sprintf(__('API returned HTTP %d error.', 'templately'), $response_code);
1065 + return $this->error('api_http_error', $message, 'ai-content/chatbot-generated', $response_code);
1066 + }
1067 +
1068 + if (!is_array($data) || !isset($data['status'])) {
1069 + return $this->error('invalid_response', __('Invalid response.', 'templately'), 'ai-content/chatbot-generated', 500);
1070 + }
1071 +
1072 + // The direct-import handoff reads this endpoint and then immediately calls
1073 + // chatbot-import-prepare, which pulls the very same (large) bundle off GCP
1074 + // seconds later. Park it so prepare can reuse it instead of paying for a
1075 + // second identical transfer.
1076 + Database::set_transient(self::GENERATED_CACHE_KEY . $chat, $data, self::GENERATED_CACHE_TTL);
1077 +
1078 + return $data;
1079 + }
1080 +
1081 + /**
1082 + * Persist edited detected-info back to the chatbot conversation (Phase 2).
1083 + *
1084 + * Mirrors {@see get_chatbot_conversation()} / {@see get_chatbot_generated()}
1085 + * but forwards a POST. When the user edits the detected-info card in the
1086 + * sidebar, the plugin proxies the corrected values to the backend so a later
1087 + * replay reflects them. The backend route is gated by the X-Templately-Apikey
1088 + * header, so it is supplied explicitly here.
1089 + *
1090 + * Expected JSON body: { chat, detected_info: { ...fields } }
1091 + *
1092 + * @return array|\WP_Error Pass-through of the external response { status, data } or WP_Error.
1093 + */
1094 + public function update_chatbot_detected_info() {
1095 + $chat = $this->get_param('chat');
1096 + $detected_info = $this->get_param('detected_info', [], null);
1097 +
1098 + if (empty($chat)) {
1099 + return $this->error('invalid_chat_id', __('Invalid conversation ID.', 'templately'), 'ai-content/chatbot-detected-info', 400);
1100 + }
1101 +
1102 + // detected_info may arrive as a JSON string when sent via FormData.
1103 + if (is_string($detected_info)) {
1104 + $decoded = json_decode($detected_info, true);
1105 + $detected_info = is_array($decoded) ? $decoded : [];
1106 + }
1107 + if (!is_array($detected_info)) {
1108 + $detected_info = [];
1109 + }
1110 +
1111 + $extra_headers = [
1112 + 'Accept' => 'application/json',
1113 + 'X-Templately-Apikey' => $this->api_key,
1114 + ];
1115 + $response = Helper::make_api_post_request("v2/chatbot/conversation/{$chat}/detected-info", ['detected_info' => $detected_info], $extra_headers, 30);
1116 +
1117 + if (is_wp_error($response)) {
1118 + return $this->error('request_failed', __('Failed to update detected info.', 'templately'), 'ai-content/chatbot-detected-info', 500, ['error_detail' => $response->get_error_message()]);
1119 + }
1120 +
1121 + $response_code = wp_remote_retrieve_response_code($response);
1122 + $body = wp_remote_retrieve_body($response);
1123 + $data = json_decode($body, true);
1124 +
1125 + if ($response_code !== 200) {
1126 + $message = (is_array($data) && !empty($data['message'])) ? $data['message'] : sprintf(__('API returned HTTP %d error.', 'templately'), $response_code);
1127 + return $this->error('api_http_error', $message, 'ai-content/chatbot-detected-info', $response_code);
1128 + }
1129 +
1130 + if (!is_array($data) || !isset($data['status'])) {
1131 + return $this->error('invalid_response', __('Invalid response.', 'templately'), 'ai-content/chatbot-detected-info', 500);
1132 + }
1133 +
1134 + return $data;
1135 + }
1136 +
1137 + /**
1138 + * Report a completed import back to templately.dev (Phase 2 import-once gate).
1139 + *
1140 + * Once the plugin finishes importing the generated content for a conversation,
1141 + * it calls this so the backend flips the conversation status to `imported`.
1142 + * Subsequent pulls then return `already_imported = true`, and the web/plugin
1143 + * UIs refuse a second import.
1144 + *
1145 + * @return array|\WP_Error
1146 + */
1147 + public function mark_chatbot_imported() {
1148 + $chat = $this->get_param('chat');
1149 +
1150 + if (empty($chat)) {
1151 + return $this->error('invalid_chat_id', __('Invalid conversation ID.', 'templately'), 'ai-content/chatbot-mark-imported', 400);
1152 + }
1153 +
1154 + $extra_headers = [
1155 + 'Accept' => 'application/json',
1156 + 'X-Templately-Apikey' => $this->api_key,
1157 + ];
1158 + $response = Helper::make_api_post_request("v2/chatbot/conversation/{$chat}/imported", [], $extra_headers, 30);
1159 +
1160 + if (is_wp_error($response)) {
1161 + return $this->error('request_failed', __('Failed to record import.', 'templately'), 'ai-content/chatbot-mark-imported', 500, ['error_detail' => $response->get_error_message()]);
1162 + }
1163 +
1164 + $response_code = wp_remote_retrieve_response_code($response);
1165 + $body = wp_remote_retrieve_body($response);
1166 + $data = json_decode($body, true);
1167 +
1168 + if ($response_code !== 200) {
1169 + $message = (is_array($data) && !empty($data['message'])) ? $data['message'] : sprintf(__('API returned HTTP %d error.', 'templately'), $response_code);
1170 + return $this->error('api_http_error', $message, 'ai-content/chatbot-mark-imported', $response_code);
1171 + }
1172 +
1173 + if (!is_array($data) || !isset($data['status'])) {
1174 + return $this->error('invalid_response', __('Invalid response.', 'templately'), 'ai-content/chatbot-mark-imported', 500);
1175 + }
1176 +
1177 + return $data;
1178 + }
1179 +
1180 + /**
1181 + * Thin importer prepare step (Phase 2).
1182 + *
1183 + * Given a chat uuid and a session that has already been created and had its
1184 + * pack downloaded (via the existing templately_pack_create_session_and_download
1185 + * AJAX flow), this:
1186 + * 1. Registers AI process data (including `chat_id`) so ai_get_json()/
1187 + * validation keep working AND so the Finalizer's ChatAIContentProvider
1188 + * can claim this process.
1189 + * 2. Fetches the backend-generated page content for the conversation.
1190 + * 3. Writes whatever pages are ALREADY generated to the same .ai.json
1191 + * location the legacy flow uses (via AIUtils::save_template_to_file).
1192 + * 4. Downloads the signed logo URL into the WP media library (Utils::upload_logo).
1193 + *
1194 + * This endpoint NEVER waits for generation to complete. Pages still being
1195 + * generated are reported in `missing` and are pulled on demand — and waited
1196 + * for — by the Finalizer via AIContentResolver. Previously this held the
1197 + * client in a 3s poll loop until every page was ready, which delayed the
1198 + * start of the import by minutes for no benefit.
1199 + *
1200 + * It returns the resolved customization data, logo attachment and the
1201 + * process_id so the React app can build the settings FormData and run the
1202 + * existing import. No generation or local customizer is involved.
1203 + *
1204 + * Expected JSON body: { chat, session_id, ai_page_ids: { 'content/page': [...], templates: [...] } }
1205 + *
1206 + * @return array|\WP_Error
1207 + */
1208 + /**
1209 + * Does a `v2/chatbot/generated` bundle already carry every expected page?
1210 + *
1211 + * A page counts as present when it is in `templates` (string or int key, the
1212 + * upstream is inconsistent) or listed in `skipped_pages` — a skipped page is
1213 + * never coming, so waiting on it would hang the poll until it timed out.
1214 + *
1215 + * @param array $data Decoded `{ status, data }` bundle.
1216 + * @param array $expected_ids Flattened page ids, as strings.
1217 + * @return bool
1218 + */
1219 + private function is_generated_bundle_complete($data, $expected_ids) {
1220 + $generated = isset($data['data']) && is_array($data['data']) ? $data['data'] : [];
1221 +
1222 + // Never reuse a bundle the user is no longer allowed to import.
1223 + if (isset($generated['can_import']) && ! $generated['can_import']) {
1224 + return false;
1225 + }
1226 +
1227 + $templates = isset($generated['templates']) && is_array($generated['templates']) ? $generated['templates'] : [];
1228 + $skipped = isset($generated['skipped_pages']) && is_array($generated['skipped_pages']) ? array_map('strval', $generated['skipped_pages']) : [];
1229 +
1230 + if (empty($templates) && empty($skipped)) {
1231 + return false;
1232 + }
1233 +
1234 + foreach ($expected_ids as $id) {
1235 + if (array_key_exists($id, $templates) || array_key_exists((int) $id, $templates) || in_array($id, $skipped, true)) {
1236 + continue;
1237 + }
1238 + return false;
1239 + }
1240 +
1241 + return true;
1242 + }
1243 +
1244 + /**
1245 + * Resolve the conversation answers to store on a chat-driven process.
1246 + *
1247 + * Prefers what the client sent (the sidebar holds the detected info the user
1248 + * may have just edited), then what was already stored for this process (so a
1249 + * repeat call costs nothing), and only then pulls the conversation from the
1250 + * cloud — which is the path the `?process=import` landing takes, since it
1251 + * never opens the sidebar and so has no answers to send.
1252 + *
1253 + * A failure here must never break the import: it returns an empty array and
1254 + * the process is stored exactly as before.
1255 + *
1256 + * @param string $chat Conversation uuid.
1257 + * @param mixed $detected_info Raw `detected_info` sent by the client.
1258 + * @param array $ai_process_data All stored process data.
1259 + * @param string $process_id This process id.
1260 + * @return array<string,string> Map of conversation step key => value.
1261 + */
1262 + private function resolve_chat_conversation_fields($chat, $detected_info, $ai_process_data, $process_id) {
1263 + $mapped = AIUtils::map_chat_detected_info($detected_info);
1264 + if (!empty($mapped)) {
1265 + return $mapped;
1266 + }
1267 +
1268 + // Already resolved on an earlier call for this process — reuse it.
1269 + if (isset($ai_process_data[$process_id]) && AIUtils::has_conversation_data($ai_process_data[$process_id])) {
1270 + return AIUtils::map_chat_detected_info(
1271 + array_intersect_key($ai_process_data[$process_id], array_flip(AIUtils::CONVERSATION_FIELDS))
1272 + );
1273 + }
1274 +
1275 + $response = Helper::make_api_get_request("v2/chatbot/conversation/{$chat}", [], ['Accept' => 'application/json'], 30);
1276 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1277 + Helper::log(sprintf('chatbot_import_prepare[%s] conversation fetch failed — process stored without answers', $chat), 'ai-import', 'warning');
1278 + return [];
1279 + }
1280 +
1281 + $data = json_decode(wp_remote_retrieve_body($response), true);
1282 + if (!is_array($data) || empty($data['data']['detected_info'])) {
1283 + return [];
1284 + }
1285 +
1286 + return AIUtils::map_chat_detected_info($data['data']['detected_info']);
1287 + }
1288 +
1289 + public function chatbot_import_prepare() {
1290 + add_filter('wp_redirect', '__return_false', 999);
1291 + set_time_limit(3 * MINUTE_IN_SECONDS);
1292 +
1293 + $handler_started = microtime(true);
1294 +
1295 + $chat = $this->get_param('chat');
1296 + $session_id = $this->get_param('session_id');
1297 + $ai_page_ids = $this->get_param('ai_page_ids', [], null);
1298 + // Conversation context, so reopening "Build with AI" later can resume this
1299 + // app-end session instead of starting from scratch. `detected_info` is
1300 + // sanitized field-by-field in AIUtils::map_chat_detected_info().
1301 + $pack_id = $this->get_param('pack_id', 0, 'absint');
1302 + $platform = $this->get_param('platform');
1303 + $detected_info = $this->get_param('detected_info', [], null);
1304 +
1305 + if (empty($chat)) {
1306 + return $this->error('invalid_chat_id', __('Invalid conversation ID.', 'templately'), 'ai-content/chatbot-import-prepare', 400);
1307 + }
1308 +
1309 + if (empty($session_id)) {
1310 + return $this->error('invalid_session_id', __('Invalid session ID.', 'templately'), 'ai-content/chatbot-import-prepare', 400);
1311 + }
1312 +
1313 + // Security: sanitize the session id before it is used to build file paths.
1314 + $session_id = AIUtils::sanitize_path_component($session_id, 'session_id');
1315 + if (is_wp_error($session_id)) {
1316 + return $this->error('invalid_session_id', $session_id->get_error_message(), 'ai-content/chatbot-import-prepare', 400);
1317 + }
1318 +
1319 + // ai_page_ids may arrive as a JSON string when sent via FormData, and with
1320 + // scalar / comma-separated group values — normalize to the canonical
1321 + // `type/sub_type => ['id',...]` shape before anything indexes into it.
1322 + $ai_page_ids = AIUtils::normalize_ai_page_ids($ai_page_ids);
1323 + if (empty($ai_page_ids)) {
1324 + return $this->error('invalid_ai_page_ids', __('Invalid AI page IDs.', 'templately'), 'ai-content/chatbot-import-prepare', 400);
1325 + }
1326 +
1327 + // Expected page ids (flattened) — the client may redirect to customization
1328 + // as soon as the home/header/footer are ready, so by import time some pages
1329 + // can still be generating. We re-pull until every expected page is present
1330 + // (bounded wait), then proceed; anything still missing falls back to the
1331 + // pack's default content.
1332 + $expected_ids = AIUtils::flatten_ai_page_ids($ai_page_ids);
1333 +
1334 + $extra_headers = ['Accept' => 'application/json'];
1335 + $cache_key = self::GENERATED_CACHE_KEY . $chat;
1336 +
1337 + // This endpoint NEVER waits for completeness. The import starts as soon as
1338 + // the session exists; whatever pages are already generated are written
1339 + // here as a warm start, and any page still generating is pulled on demand
1340 + // by the Finalizer (Core/Importer/Utils/AIContentResolver +
1341 + // Providers/ChatAIContentProvider).
1342 + //
1343 + // Reuse the bundle chatbot-generated just parked, but ONLY when it already
1344 + // holds every expected page — a complete bundle cannot become less
1345 + // complete, whereas an incomplete one has to be re-pulled to pick up the
1346 + // pages that have since finished. On the common handoff (generation
1347 + // finished long before the user landed here) this removes an entire
1348 + // duplicate transfer of every page's block JSON.
1349 + $pull_started = microtime(true);
1350 + $pull_duration = 0;
1351 + $data = Database::get_transient($cache_key);
1352 + $from_cache = is_array($data) && $this->is_generated_bundle_complete($data, $expected_ids);
1353 +
1354 + if (! $from_cache) {
1355 + // Single pull — no server-side sleep/retry. The user can reach import as
1356 + // soon as the key pages (home/header/footer) are ready while the rest are
1357 + // still generating; rather than hold the request open until everything is
1358 + // done (which tripped a gateway 504), we return a non-fatal `pending`
1359 + // status and let the client poll (JS-side pull).
1360 + $response = Helper::make_api_get_request("v2/chatbot/generated/{$chat}", [], $extra_headers, 2 * MINUTE_IN_SECONDS);
1361 + $pull_duration = microtime(true) - $pull_started;
1362 +
1363 + if (is_wp_error($response)) {
1364 + Helper::log(sprintf('chatbot_import_prepare[%s] pull failed after %.2fs: %s', $chat, $pull_duration, $response->get_error_message()), 'ai-import', 'error');
1365 + return $this->error('request_failed', __('Failed to fetch generated content.', 'templately'), 'ai-content/chatbot-import-prepare', 500, ['error_detail' => $response->get_error_message()]);
1366 + }
1367 +
1368 + $response_code = wp_remote_retrieve_response_code($response);
1369 + $data = json_decode(wp_remote_retrieve_body($response), true);
1370 +
1371 + if ($response_code !== 200 || !is_array($data) || !isset($data['status'])) {
1372 + $message = (is_array($data) && !empty($data['message'])) ? $data['message'] : sprintf(__('API returned HTTP %d error.', 'templately'), $response_code);
1373 + Helper::log(sprintf('chatbot_import_prepare[%s] pull HTTP %d after %.2fs', $chat, $response_code, $pull_duration), 'ai-import', 'error');
1374 + return $this->error('api_http_error', $message, 'ai-content/chatbot-import-prepare', $response_code ?: 500);
1375 + }
1376 +
1377 + // Park a complete bundle for the credits re-read on the success screen.
1378 + if ($this->is_generated_bundle_complete($data, $expected_ids)) {
1379 + Database::set_transient($cache_key, $data, self::GENERATED_CACHE_TTL);
1380 + }
1381 + } else {
1382 + Helper::log(sprintf('chatbot_import_prepare[%s] reused cached bundle (no upstream pull)', $chat), 'ai-import', 'info');
1383 + }
1384 +
1385 + $generated = isset($data['data']) && is_array($data['data']) ? $data['data'] : [];
1386 +
1387 + // Access gate: the backend blocks a free user past their 7-day window.
1388 + if (isset($generated['can_import']) && ! $generated['can_import']) {
1389 + return $this->error('access_expired', __('Your free access to this generated site has ended. Upgrade your plan or purchase this template to import it.', 'templately'), 'ai-content/chatbot-import-prepare', 403);
1390 + }
1391 +
1392 + $templates = isset($generated['templates']) && is_array($generated['templates']) ? $generated['templates'] : [];
1393 +
1394 + // Pages the backend skipped (empty source JSON) or failed to generate.
1395 + // These will never appear in `templates`, so they must not be treated
1396 + // as "still generating" — without this, one skipped page keeps the poll
1397 + // pending until it times out.
1398 + $skipped_pages = isset($generated['skipped_pages']) && is_array($generated['skipped_pages']) ? array_map('strval', $generated['skipped_pages']) : [];
1399 + $skipped_expected = array_values(array_intersect($expected_ids, $skipped_pages));
1400 +
1401 + // Which expected pages are still missing from the bundle?
1402 + $missing = array_values(array_filter($expected_ids, function ($id) use ($templates, $skipped_pages) {
1403 + return !array_key_exists($id, $templates) && !array_key_exists((int) $id, $templates) && !in_array($id, $skipped_pages, true);
1404 + }));
1405 +
1406 + $ready = array_values(array_diff($expected_ids, $missing));
1407 + Helper::log(sprintf('chatbot_import_prepare[%s] warm start: ready=%d/%d missing=%d skipped=%d pull=%.2fs', $chat, count($ready), count($expected_ids), count($missing), count($skipped_expected), $pull_duration), 'ai-import', 'info');
1408 +
1409 +
1410 + // Derive a process_id for this chat-driven import and register process data
1411 + // so the existing validation/ai_get_json paths keep functioning.
1412 + $process_id = 'chat-' . $session_id;
1413 + $user = $this->utils('options')->get('user');
1414 +
1415 + $ai_process_data = AIUtils::get_ai_process_data();
1416 + $record = [
1417 + 'process_id' => $process_id,
1418 + 'session_id' => $session_id,
1419 + 'ai_page_ids' => $ai_page_ids,
1420 + 'api_key' => $this->api_key,
1421 + 'user_id' => isset($user['id']) ? $user['id'] : null,
1422 + 'chat_id' => $chat,
1423 + ];
1424 +
1425 + if (!empty($pack_id)) {
1426 + $record['pack_id'] = $pack_id;
1427 + }
1428 + if (!empty($platform)) {
1429 + $record['platform'] = $platform;
1430 + }
1431 +
1432 + // Persist the app-end answers exactly as an in-plugin conversation stores
1433 + // them, so reopening "Build with AI" resumes this session instead of
1434 + // starting over. Without this the record holds no answers at all and the
1435 + // sidebar has nothing to restore.
1436 + $conversation = AIUtils::build_conversation_fields(
1437 + $this->resolve_chat_conversation_fields($chat, $detected_info, $ai_process_data, $process_id)
1438 + );
1439 + if (!empty($conversation)) {
1440 + $record = array_merge($record, $conversation);
1441 + }
1442 +
1443 + $ai_process_data[$process_id] = $record;
1444 + AIUtils::update_ai_process_data($ai_process_data);
1445 +
1446 + // Persist each generated page to its .ai.json location for the import runners.
1447 + //
1448 + // Every page present in THIS pull is written immediately, even when others
1449 + // are still generating. Holding the writes back until the whole set was
1450 + // ready meant a single slow page threw away the full bundle on every poll
1451 + // — dozens of multi-hundred-KB pulls (all of `templates`, straight off GCP)
1452 + // discarded to save nothing. Writing as we go also lets the Finalizer
1453 + // runner finalize the pages that ARE ready instead of blocking on all of
1454 + // them. Already-written pages are skipped, so a re-poll is cheap.
1455 + $save_started = microtime(true);
1456 + $saved_count = 0;
1457 + $errors = [];
1458 + $processed_pages = get_option('templately_ai_processed_pages', []);
1459 + $already_saved = isset($processed_pages[$process_id]['pages']) ? $processed_pages[$process_id]['pages'] : [];
1460 + foreach ($templates as $content_id => $template) {
1461 + if (empty($template)) {
1462 + continue;
1463 + }
1464 +
1465 + if (array_key_exists((string) $content_id, $already_saved)) {
1466 + $saved_count++;
1467 + continue;
1468 + }
1469 +
1470 + // The runners read JSON strings; normalize arrays/objects to a string.
1471 + $template_payload = is_string($template) ? $template : wp_json_encode($template);
1472 +
1473 + $result = AIUtils::save_template_to_file(
1474 + $process_id,
1475 + $session_id,
1476 + $content_id,
1477 + $template_payload,
1478 + $ai_page_ids,
1479 + false
1480 + );
1481 +
1482 + if (is_wp_error($result)) {
1483 + $errors[$content_id] = $result->get_error_message();
1484 + continue;
1485 + }
1486 + if (isset($result['status']) && $result['status'] === 'success') {
1487 + $saved_count++;
1488 + } else {
1489 + $errors[$content_id] = isset($result['message']) ? $result['message'] : 'unknown';
1490 + }
1491 + }
1492 +
1493 + // Write each backend-skipped page as an explicit `{"isSkipped": true}`
1494 + // marker (same shape the legacy per-page callback wrote) so the import
1495 + // runners fall back to the pack's default content instead of treating
1496 + // the page as missing.
1497 + $skipped_saved = [];
1498 + foreach ($skipped_expected as $skipped_id) {
1499 + if (array_key_exists($skipped_id, $templates) || array_key_exists((int) $skipped_id, $templates)) {
1500 + continue;
1501 + }
1502 +
1503 + if (array_key_exists((string) $skipped_id, $already_saved)) {
1504 + $skipped_saved[] = $skipped_id;
1505 + continue;
1506 + }
1507 +
1508 + $result = AIUtils::save_template_to_file(
1509 + $process_id,
1510 + $session_id,
1511 + $skipped_id,
1512 + '',
1513 + $ai_page_ids,
1514 + true
1515 + );
1516 +
1517 + if (is_wp_error($result)) {
1518 + $errors[$skipped_id] = $result->get_error_message();
1519 + continue;
1520 + }
1521 + if (isset($result['status']) && $result['status'] === 'success') {
1522 + $skipped_saved[] = $skipped_id;
1523 + } else {
1524 + $errors[$skipped_id] = isset($result['message']) ? $result['message'] : 'unknown';
1525 + }
1526 + }
1527 +
1528 + $save_duration = microtime(true) - $save_started;
1529 + Helper::log(sprintf('chatbot_import_prepare[%s] saved %d/%d pages in %.2fs (skipped=%d, errors=%d)', $chat, $saved_count, count($templates), $save_duration, count($skipped_saved), count($errors)), 'ai-import', 'info');
1530 +
1531 + // NOTE: there is deliberately NO `pending` return here any more. Pages that
1532 + // are still generating come back in `missing` and are pulled on demand —
1533 + // and waited for — by the Finalizer. Returning `pending` made the client
1534 + // poll for minutes before the import could even start.
1535 + //
1536 + // NOT an error when nothing was saved: with the wait deferred to the
1537 + // Finalizer it is legitimate for zero pages to be ready at import start.
1538 + // Only a genuine write failure (something was ready but every save
1539 + // errored) is fatal.
1540 + if ($saved_count === 0 && empty($skipped_saved) && !empty($errors)) {
1541 + return $this->error('save_failed', __('Failed to save generated content.', 'templately'), 'ai-content/chatbot-import-prepare', 500, ['errors' => $errors]);
1542 + }
1543 +
1544 + Helper::log(sprintf('chatbot_import_prepare[%s] returning: pages=%d missing=%d pull=%.2fs', $chat, count($templates), count($missing), $pull_duration), 'ai-import', 'info');
1545 +
1546 + // Import the logo into the media library and map it into the customization.
1547 + $customization = isset($generated['customization_data']) && is_array($generated['customization_data']) ? $generated['customization_data'] : [];
1548 + $logo = null;
1549 + $logo_url = !empty($generated['logo_url']) ? esc_url_raw($generated['logo_url']) : '';
1550 +
1551 + if (!empty($logo_url)) {
1552 + $logo_started = microtime(true);
1553 + $uploaded = Utils::upload_logo($logo_url, $session_id);
1554 + Helper::log(sprintf('chatbot_import_prepare[%s] logo upload in %.2fs', $chat, microtime(true) - $logo_started), 'ai-import', 'info');
1555 + if (!empty($uploaded['id'])) {
1556 + $logo = [
1557 + 'id' => (int) $uploaded['id'],
1558 + 'url' => $uploaded['url'],
1559 + ];
1560 + } elseif (!empty($uploaded['error'])) {
1561 + // Logo is non-fatal: log and continue without it.
1562 + Helper::log('chatbot_import_prepare logo upload failed: ' . $uploaded['error']);
1563 + }
1564 + }
1565 +
1566 + // Reflect the imported logo back into the customization payload so React
1567 + // can build the settings FormData from a single source.
1568 + if (!empty($logo)) {
1569 + $customization['logo'] = $logo;
1570 + }
1571 +
1572 + Helper::log(sprintf('chatbot_import_prepare[%s] done in %.2fs total', $chat, microtime(true) - $handler_started), 'ai-import', 'info');
1573 +
1574 + return [
1575 + 'status' => 'success',
1576 + 'data' => [
1577 + 'session_id' => $session_id,
1578 + 'process_id' => $process_id,
1579 + 'ai_page_ids' => $ai_page_ids,
1580 + 'saved' => $saved_count,
1581 + // Pages the backend explicitly skipped — imported with the
1582 + // pack's default content instead.
1583 + 'skipped' => $skipped_expected,
1584 + // Readiness snapshot at import start. `missing` pages are NOT a
1585 + // failure: the Finalizer pulls each on demand and waits for it.
1586 + 'expected' => $expected_ids,
1587 + 'ready' => $ready,
1588 + 'missing' => $missing,
1589 + 'platform' => isset($customization['platform']) ? $customization['platform'] : null,
1590 + 'customization_data' => $customization,
1591 + 'logo' => $logo,
1592 + 'errors' => $errors,
1593 + ],
1594 + ];
986 1595 }
987 1596
988 1597 /**
989 1598 * Validate API key against database