| 1 |
<?php |
| 2 |
/** |
| 3 |
* `templately/import-template-to-library` — save a single template into the |
| 4 |
* site's Templately Library for later reuse (FR-005). |
| 5 |
* |
| 6 |
* Wraps `Templately\API\Import::import_in_library()` via in-process REST |
| 7 |
* dispatch (research.md §2), reusing US2's shared rate-limit, dependency-gate, |
| 8 |
* and Pro-gate infrastructure (tasks.md Notes — built once, not duplicated). |
| 9 |
* |
| 10 |
* `platform` is a REQUIRED input (Auth Tools addendum) — see |
| 11 |
* ImportTemplatePageAbility's docblock for why: `import_in_library()` |
| 12 |
* silently defaults to `'elementor'` when omitted, mishandling any |
| 13 |
* Gutenberg template. |
| 14 |
* |
| 15 |
* @package Templately\Modules\McpAbilities\Abilities |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace Templately\Modules\McpAbilities\Abilities; |
| 19 |
|
| 20 |
use Templately\Modules\McpCore\Registry\ToolDescriptor; |
| 21 |
use Templately\Modules\McpCore\Support\DependencyGateResolver; |
| 22 |
use Templately\Modules\McpCore\Support\ImportRateLimiter; |
| 23 |
use Templately\Modules\McpCore\Support\Permissions; |
| 24 |
use Templately\Modules\McpCore\Support\ProGateResolver; |
| 25 |
use Templately\Modules\McpCore\Support\RestDispatcher; |
| 26 |
use WP_Error; |
| 27 |
|
| 28 |
class ImportTemplateLibraryAbility { |
| 29 |
|
| 30 |
const ID = 'templately/import-template-to-library'; |
| 31 |
|
| 32 |
public static function descriptor(): array { |
| 33 |
return [ |
| 34 |
'id' => self::ID, |
| 35 |
'label' => __( 'Save Templately Template to Library', 'templately' ), |
| 36 |
'description' => __( 'Save a single Templately template into this site\'s Templately Library for later reuse.', 'templately' ), |
| 37 |
'input_schema' => [ |
| 38 |
'type' => 'object', |
| 39 |
'properties' => [ |
| 40 |
'id' => [ 'type' => 'integer' ], |
| 41 |
'platform' => [ 'type' => 'string', 'enum' => [ 'elementor', 'gutenberg' ] ], |
| 42 |
], |
| 43 |
'required' => [ 'id', 'platform' ], |
| 44 |
'additionalProperties' => false, |
| 45 |
], |
| 46 |
'output_schema' => [ |
| 47 |
'type' => 'object', |
| 48 |
'properties' => [ |
| 49 |
'target' => [ 'type' => 'string' ], |
| 50 |
'status' => [ 'type' => 'string', 'enum' => [ 'success', 'partial' ] ], |
| 51 |
'post_id' => [ 'type' => 'integer' ], |
| 52 |
'failed_parts' => [ 'type' => 'array' ], |
| 53 |
], |
| 54 |
], |
| 55 |
'execute_callback' => [ self::class, 'execute' ], |
| 56 |
'permission_callback' => [ Permissions::class, 'can_use_abilities' ], |
| 57 |
'access_level' => ToolDescriptor::ACCESS_FULL, |
| 58 |
'annotations' => [ 'readonly' => false, 'destructive' => true, 'idempotent' => false ], |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @param array $input |
| 64 |
* @return array|WP_Error |
| 65 |
*/ |
| 66 |
public static function execute( array $input ) { |
| 67 |
$id = (int) ( $input['id'] ?? 0 ); |
| 68 |
$platform = (string) ( $input['platform'] ?? '' ); |
| 69 |
|
| 70 |
if ( empty( $id ) ) { |
| 71 |
return new WP_Error( 'not_found', __( 'A template id is required.', 'templately' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! in_array( $platform, [ 'elementor', 'gutenberg' ], true ) ) { |
| 75 |
return new WP_Error( 'invalid_platform', __( 'platform must be "elementor" or "gutenberg".', 'templately' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
$rate_limit_error = ImportRateLimiter::check(); |
| 79 |
if ( $rate_limit_error ) { |
| 80 |
return $rate_limit_error; |
| 81 |
} |
| 82 |
|
| 83 |
$dependency_check = DependencyGateResolver::check( $id ); |
| 84 |
if ( is_wp_error( $dependency_check ) ) { |
| 85 |
return $dependency_check; |
| 86 |
} |
| 87 |
|
| 88 |
$response = RestDispatcher::dispatch( 'POST', '/templately/v1/import', [ 'id' => $id, 'platform' => $platform ] ); |
| 89 |
|
| 90 |
if ( is_wp_error( $response ) ) { |
| 91 |
return ProGateResolver::to_purchase_error( $response, $id ); |
| 92 |
} |
| 93 |
|
| 94 |
$data = $response->get_data(); |
| 95 |
|
| 96 |
if ( empty( $data['post_id'] ) ) { |
| 97 |
return new WP_Error( 'import_failed', __( 'Import failed for an unknown reason.', 'templately' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
return [ |
| 101 |
'target' => 'library', |
| 102 |
'status' => 'success', |
| 103 |
'post_id' => (int) $data['post_id'], |
| 104 |
]; |
| 105 |
} |
| 106 |
} |
| 107 |
|