PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
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 / mcp-abilities / Abilities / ImportTemplateLibraryAbility.php

ImportTemplateLibraryAbility.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/mcp-abilities/Abilities/ImportTemplateLibraryAbility.php

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