| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport\Utils; |
| 4 |
|
| 5 |
use Templately\Utils\Helper; |
| 6 |
use Templately\Utils\Response\ResponseNormalizer; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
/** |
| 10 |
* Single source of the pack import-info fetch (`v2/import/info/pack/{id}`). |
| 11 |
* |
| 12 |
* De-duplicates the identical `fetch_pack_info_from_api()` that previously lived as a |
| 13 |
* private copy in BOTH `Ajax\PackInfoController` and `REST\GlobalSettings` (see the |
| 14 |
* "kept as its own copy" note the GlobalSettings B8 change left behind). All three |
| 15 |
* consumers now delegate here: |
| 16 |
* - `Ajax\PackInfoController::import_info()` / `::import_global_settings()` (deprecated shims) |
| 17 |
* - `REST\GlobalSettings::import_global_settings()` (POST /import/global-settings, FR-018/B8) |
| 18 |
* - `REST\PackInfo::import_info()` (GET /import/info, FR-018) |
| 19 |
* |
| 20 |
* The logic is byte-equivalent in effect to the two former private copies — this is a |
| 21 |
* pure consolidation, no behavior change. Error classification is delegated to the |
| 22 |
* central `ResponseNormalizer` (spec 043). |
| 23 |
*/ |
| 24 |
class PackInfoFetcher { |
| 25 |
|
| 26 |
/** |
| 27 |
* Fetch a pack's import-info payload from the Templately API. |
| 28 |
* |
| 29 |
* @param int|string $id Pack id. |
| 30 |
* @param mixed $isAi AI flag, in whatever type the caller happens to hold it |
| 31 |
* (PHP boolean, `"true"`/`"1"`/`1`, or falsy) — normalized here |
| 32 |
* to the literal string `'true'`/`'false'` via |
| 33 |
* `rest_sanitize_boolean()` before being sent as the |
| 34 |
* `x-templately-is-ai` header. This is NOT cosmetic: the |
| 35 |
* upstream API only includes `credit_balance`/`estimated_cost` |
| 36 |
* in the response when it sees the literal string `"true"`. A |
| 37 |
* raw PHP boolean `true` stringifies to `"1"` when WP's HTTP |
| 38 |
* layer serializes the header, which upstream treats as falsy |
| 39 |
* and silently omits the credit fields — that regression is |
| 40 |
* what disabled the AI sidebar's credit display/generation |
| 41 |
* entirely. Normalizing to a string here fixes the REST route |
| 42 |
* AND the deprecated ajax shims uniformly, regardless of what |
| 43 |
* each caller passes in. |
| 44 |
* |
| 45 |
* @return array|WP_Error Decoded response body, or WP_Error on transport / non-200 / API error. |
| 46 |
*/ |
| 47 |
public function fetch( $id, $isAi ) { |
| 48 |
$extra_headers = [ |
| 49 |
'x-templately-is-ai' => rest_sanitize_boolean( $isAi ) ? 'true' : 'false', |
| 50 |
]; |
| 51 |
$response = Helper::make_api_get_request( "v2/import/info/pack/$id", [], $extra_headers, 30 ); |
| 52 |
|
| 53 |
// 043 FR-003: one normalizer classifies transport failures AND non-200 |
| 54 |
// bodies. It replaces `extract_error_from_response()`, which returned |
| 55 |
// `mixed` — sometimes a string, sometimes a whole decoded body — and so |
| 56 |
// leaked upstream internals straight into a user-facing message. |
| 57 |
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { |
| 58 |
return ResponseNormalizer::normalize( $response )->error(); |
| 59 |
} |
| 60 |
// Retrieve data from the response body. |
| 61 |
$body = wp_remote_retrieve_body( $response ); |
| 62 |
$data = json_decode( $body, true ); |
| 63 |
|
| 64 |
if ( isset( $data['error'] ) ) { |
| 65 |
return new WP_Error( 'api_error', $data['error'] ); |
| 66 |
} |
| 67 |
|
| 68 |
return $data; |
| 69 |
} |
| 70 |
} |
| 71 |
|