| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport\Utils\Providers; |
| 4 |
|
| 5 |
use Templately\Modules\FullSiteImport\Utils\AIContentResolver; |
| 6 |
use Templately\Modules\FullSiteImport\Utils\AIUtils; |
| 7 |
use Templately\Modules\FullSiteImport\Utils\SessionData; |
| 8 |
|
| 9 |
/** |
| 10 |
* Built-in AI content provider for the CLASSIC generation flow (business-details |
| 11 |
* form → `ai-content/modify-content` → per-page `ai-update` callback). |
| 12 |
* |
| 13 |
* On remote sites the backend POSTs each page to the callback, which writes the |
| 14 |
* `.ai.json` directly, so nothing needs pulling here. On local sites (which the |
| 15 |
* backend cannot call back into) this provider pulls the ready pages via |
| 16 |
* {@see AIUtils::poll_for_template()} — the same behaviour that previously lived |
| 17 |
* inline in `AIUtils::handle_sse_wait_with_timeout()`, now expressed as a |
| 18 |
* registered source so both AI-content versions hook the resolver symmetrically. |
| 19 |
* |
| 20 |
* Registered on {@see AIContentResolver::STAGE_HOOK}. |
| 21 |
*/ |
| 22 |
class ClassicAIContentProvider { |
| 23 |
|
| 24 |
/** |
| 25 |
* @param bool $staged True if a previous provider already produced the file. |
| 26 |
* @param array $context Resolver context (see AIContentResolver::ensure_page_ready). |
| 27 |
* @return bool |
| 28 |
*/ |
| 29 |
public static function stage( $staged, array $context ) { |
| 30 |
if ( $staged === true ) { |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
$process_data = $context['process_data'] ?? []; |
| 35 |
|
| 36 |
// Chat-sourced imports are owned by ChatAIContentProvider — skip. |
| 37 |
if ( ! empty( $process_data['chat_id'] ) ) { |
| 38 |
return $staged; |
| 39 |
} |
| 40 |
|
| 41 |
$session_id = $context['session_id'] ?? ''; |
| 42 |
$process_id = $context['process_id'] ?? null; |
| 43 |
$ai_page_ids = $context['ai_page_ids'] ?? []; |
| 44 |
$content_id = $context['content_id'] ?? null; |
| 45 |
|
| 46 |
if ( empty( $process_id ) || empty( $session_id ) || empty( $ai_page_ids ) || $content_id === null || $content_id === '' ) { |
| 47 |
return $staged; |
| 48 |
} |
| 49 |
|
| 50 |
// Remote sites receive content through the ai-update callback; only local |
| 51 |
// sites need an active pull (the backend cannot reach them). |
| 52 |
$session_data = SessionData::get_data( $session_id ); |
| 53 |
if ( empty( $session_data['isLocalSite'] ) ) { |
| 54 |
return $staged; |
| 55 |
} |
| 56 |
|
| 57 |
AIUtils::poll_for_template( $process_id, $session_id, $ai_page_ids ); |
| 58 |
|
| 59 |
return AIContentResolver::page_staged( $context ) ? true : $staged; |
| 60 |
} |
| 61 |
} |
| 62 |
|