| 1 |
<?php |
| 2 |
/** |
| 3 |
* `templately/get-template-detail` — full template detail + dependency |
| 4 |
* readiness ability (FR-002, FR-003). |
| 5 |
* |
| 6 |
* Wraps `Templately\API\Items` detail lookup + `Templately\API\Dependencies:: |
| 7 |
* check_dependencies()` via in-process REST dispatch (research.md §2), |
| 8 |
* merging the two so an agent gets everything in a single call (SC-002). |
| 9 |
* |
| 10 |
* @package Templately\Modules\McpAbilities\Abilities |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Templately\Modules\McpAbilities\Abilities; |
| 14 |
|
| 15 |
use Templately\Modules\McpCore\Registry\ToolDescriptor; |
| 16 |
use Templately\Modules\McpCore\Support\Permissions; |
| 17 |
use Templately\Modules\McpCore\Support\RestDispatcher; |
| 18 |
use WP_Error; |
| 19 |
|
| 20 |
class GetTemplateDetailAbility { |
| 21 |
|
| 22 |
const ID = 'templately/get-template-detail'; |
| 23 |
|
| 24 |
public static function descriptor(): array { |
| 25 |
return [ |
| 26 |
'id' => self::ID, |
| 27 |
'label' => __( 'Get Templately Template Detail', 'templately' ), |
| 28 |
'description' => __( 'Retrieve full details for a single Templately template, including dependency readiness on this site.', 'templately' ), |
| 29 |
'input_schema' => [ |
| 30 |
'type' => 'object', |
| 31 |
'properties' => [ |
| 32 |
'id' => [ 'type' => 'integer' ], |
| 33 |
], |
| 34 |
'required' => [ 'id' ], |
| 35 |
'additionalProperties' => false, |
| 36 |
], |
| 37 |
'output_schema' => [ |
| 38 |
'type' => 'object', |
| 39 |
], |
| 40 |
'execute_callback' => [ self::class, 'execute' ], |
| 41 |
'permission_callback' => [ Permissions::class, 'can_use_abilities' ], |
| 42 |
'access_level' => ToolDescriptor::ACCESS_READ, |
| 43 |
'annotations' => [ 'readonly' => true, 'destructive' => false, 'idempotent' => true ], |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param array $input |
| 49 |
* @return array|WP_Error |
| 50 |
*/ |
| 51 |
public static function execute( array $input ) { |
| 52 |
$id = (int) ( $input['id'] ?? 0 ); |
| 53 |
|
| 54 |
if ( empty( $id ) ) { |
| 55 |
return new WP_Error( 'not_found', __( 'A template id is required.', 'templately' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
// NOTE: the route's own registered default for `type` ('block', singular) does not |
| 59 |
// match `get_query_types()`'s special-cased 'blocks'/'sections' (plural), so the |
| 60 |
// route's bare default throws `invalid_type_call` — pass 'items' explicitly (the |
| 61 |
// value 'blocks'/'sections' would themselves resolve to). |
| 62 |
$detail_response = RestDispatcher::dispatch( 'GET', '/templately/v1/items/' . $id, [ 'type' => 'items' ] ); |
| 63 |
|
| 64 |
if ( is_wp_error( $detail_response ) ) { |
| 65 |
return $detail_response; |
| 66 |
} |
| 67 |
|
| 68 |
$item = $detail_response->get_data(); |
| 69 |
|
| 70 |
if ( empty( $item ) || empty( $item['id'] ) ) { |
| 71 |
return new WP_Error( 'not_found', __( 'Template not found.', 'templately' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
$item['dependencies'] = self::resolve_dependency_status( $item['dependencies'] ?? [], $item['platform'] ?? 'elementor' ); |
| 75 |
|
| 76 |
return $item; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Merge the template's declared dependencies with this site's install/active |
| 81 |
* status via the existing Dependencies::check_dependencies() logic (FR-003). |
| 82 |
* |
| 83 |
* @param array $dependencies |
| 84 |
* @param string $platform |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
private static function resolve_dependency_status( array $dependencies, string $platform ): array { |
| 88 |
if ( empty( $dependencies ) ) { |
| 89 |
return []; |
| 90 |
} |
| 91 |
|
| 92 |
$response = RestDispatcher::dispatch( 'POST', '/templately/v1/dependencies/check', [ |
| 93 |
'dependencies' => $dependencies, |
| 94 |
'platform' => $platform, |
| 95 |
] ); |
| 96 |
|
| 97 |
if ( is_wp_error( $response ) ) { |
| 98 |
// Dependency-status resolution failing should not hide the rest of the |
| 99 |
// template detail — surface the raw (unchecked) list instead. |
| 100 |
return $dependencies; |
| 101 |
} |
| 102 |
|
| 103 |
$data = $response->get_data(); |
| 104 |
|
| 105 |
return $data['dependencies'] ?? $dependencies; |
| 106 |
} |
| 107 |
} |
| 108 |
|