| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport\Ajax; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Exception; |
| 7 |
use Templately\Modules\FullSiteImport\Utils\Utils; |
| 8 |
use Templately\Modules\FullSiteImport\Utils\SessionData; |
| 9 |
use Templately\Modules\FullSiteImport\Utils\AIUtils; |
| 10 |
use Templately\Modules\FullSiteImport\Utils\PackInfoFetcher; |
| 11 |
use Templately\Utils\Helper; |
| 12 |
use Templately\Utils\Response\AjaxResponder; |
| 13 |
|
| 14 |
/** |
| 15 |
* PackInfoController — independent AJAX endpoint handler extracted from FullSiteImport. |
| 16 |
* Registered through FullSiteImport's nonce/capability wrapper (action names unchanged). |
| 17 |
* |
| 18 |
* Both handlers here are now DEPRECATED SHIMS: the primary transport moved to REST |
| 19 |
* (`REST\PackInfo` = GET /import/info, FR-018; `REST\GlobalSettings` = POST |
| 20 |
* /import/global-settings, FR-018/B8). These ajax actions are kept only so already-cached |
| 21 |
* old frontend bundles keep working. The upstream fetch is shared via `PackInfoFetcher`. |
| 22 |
*/ |
| 23 |
class PackInfoController { |
| 24 |
|
| 25 |
public function import_info() { |
| 26 |
|
| 27 |
$platform = isset($_GET['platform']) ? sanitize_text_field(wp_unslash($_GET['platform'])) : 'elementor'; |
| 28 |
$id = isset($_GET['id']) ? intval($_GET['id']) : 0; |
| 29 |
$isAi = isset($_GET['isAi']) ? sanitize_text_field(wp_unslash($_GET['isAi'])) : false; |
| 30 |
|
| 31 |
$data = ( new PackInfoFetcher() )->fetch($id, $isAi); |
| 32 |
if (is_wp_error($data)) { |
| 33 |
AjaxResponder::error($data); |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$business_niches = get_option('templately_ai_business_niches', []); |
| 38 |
$data['data']['business_niches'] = $business_niches; |
| 39 |
|
| 40 |
if (isset($data['data']['manifest'])) { |
| 41 |
$data['data']['manifest'] = json_decode($data['data']['manifest'], true); |
| 42 |
} |
| 43 |
if (isset($data['data']['settings'])) { |
| 44 |
$data['data']['settings'] = json_decode($data['data']['settings'], true); |
| 45 |
} |
| 46 |
|
| 47 |
if ($isAi) { |
| 48 |
// Get the latest AI process for the current API key |
| 49 |
$last_ai_process = AIUtils::get_latest_ai_process_by_api_key($id); |
| 50 |
if ($last_ai_process) { |
| 51 |
// The cloud API key is stored on the process for server-side matching |
| 52 |
// only. It must never reach the browser — under a global login it is |
| 53 |
// the admin's credential, not the requesting user's. |
| 54 |
unset($last_ai_process['api_key']); |
| 55 |
|
| 56 |
$data['data']['ai_process'] = $last_ai_process; |
| 57 |
} |
| 58 |
|
| 59 |
if($last_ai_process && $id == ($last_ai_process['pack_id'] ?? null)){ |
| 60 |
// Read AI preview content directly from files using the common function |
| 61 |
$session_id = $last_ai_process['session_id'] ?? null; |
| 62 |
$ai_page_ids = $last_ai_process['ai_page_ids'] ?? []; |
| 63 |
$dir_path = null; |
| 64 |
|
| 65 |
// Get session data to retrieve dir_path |
| 66 |
if ($session_id) { |
| 67 |
$session_data = SessionData::get_data($session_id); |
| 68 |
$dir_path = $session_data['dir_path'] ?? null; |
| 69 |
} |
| 70 |
|
| 71 |
// Use the common function to read AI template data if we have the required data |
| 72 |
if ($session_id && $ai_page_ids && $dir_path) { |
| 73 |
$data['data']['ai_preview_content'] = AIUtils::read_ai_template_data($session_id, $ai_page_ids, $dir_path); |
| 74 |
} else { |
| 75 |
$data['data']['ai_preview_content'] = []; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Return the response body |
| 81 |
AjaxResponder::success($data); |
| 82 |
} |
| 83 |
public function import_global_settings() { |
| 84 |
$id = isset($_GET['id']) ? intval($_GET['id']) : 0; |
| 85 |
$isAi = isset($_GET['isAi']) ? sanitize_text_field(wp_unslash($_GET['isAi'])) : false; |
| 86 |
|
| 87 |
$data = ( new PackInfoFetcher() )->fetch($id, $isAi); |
| 88 |
if (is_wp_error($data)) { |
| 89 |
AjaxResponder::error($data); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$settings = []; |
| 94 |
if (isset($data['data']['settings'])) { |
| 95 |
$settings = json_decode($data['data']['settings'], true); |
| 96 |
} |
| 97 |
|
| 98 |
AjaxResponder::success(['settings' => $settings]); |
| 99 |
} |
| 100 |
} |
| 101 |
|