PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 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 All 112 releases
templately / modules / full-site-import / Utils / PackInfoFetcher.php

PackInfoFetcher.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/full-site-import/Utils/PackInfoFetcher.php

71 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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