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 / mcp-abilities / Abilities / ImportTemplateIntoPostAbility.php

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

128 lines 4.8 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-into-post` — insert a single template's
4 * content into an existing post, headlessly (FR-006, FR-008, FR-009, FR-014).
5 *
6 * The new capability this feature introduces: no server-side equivalent
7 * existed before (research.md §3). Fetches the template's processed content
8 * via the existing `/insert` route (the same one the live editor
9 * would otherwise merge client-side) and delegates the actual merge to
10 * `Editor\{ElementorHeadlessInsert,GutenbergHeadlessInsert}`, which own the
11 * conflict (FR-014) and platform-compatibility (FR-009) checks.
12 *
13 * @package Templately\Modules\McpAbilities\Abilities
14 */
15
16 namespace Templately\Modules\McpAbilities\Abilities;
17
18 use Templately\Modules\McpAbilities\Editor\ElementorHeadlessInsert;
19 use Templately\Modules\McpAbilities\Editor\GutenbergHeadlessInsert;
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 ImportTemplateIntoPostAbility {
29
30 const ID = 'templately/import-template-into-post';
31
32 public static function descriptor(): array {
33 return [
34 'id' => self::ID,
35 'label' => __( 'Insert Templately Template into Existing Post', 'templately' ),
36 'description' => __( 'Merge a single Templately template\'s content into an existing post, without needing a live editor session.', 'templately' ),
37 'input_schema' => [
38 'type' => 'object',
39 'properties' => [
40 'id' => [ 'type' => 'integer', 'description' => __( 'Template ID to insert.', 'templately' ) ],
41 'post_id' => [ 'type' => 'integer', 'description' => __( 'Existing post to modify.', 'templately' ) ],
42 'base_hash' => [ 'type' => 'string', 'description' => __( 'md5 of the post\'s last-read relevant content (see templately/get-template-detail\'s sibling read for the target post).', 'templately' ) ],
43 'platform' => [ 'type' => 'string', 'enum' => [ 'elementor', 'gutenberg' ], 'default' => 'elementor' ],
44 ],
45 'required' => [ 'id', 'post_id', 'base_hash' ],
46 'additionalProperties' => false,
47 ],
48 'output_schema' => [
49 'type' => 'object',
50 'properties' => [
51 'target' => [ 'type' => 'string' ],
52 'status' => [ 'type' => 'string', 'enum' => [ 'success', 'partial' ] ],
53 'post_id' => [ 'type' => 'integer' ],
54 'failed_parts' => [ 'type' => 'array' ],
55 ],
56 ],
57 'execute_callback' => [ self::class, 'execute' ],
58 'permission_callback' => [ self::class, 'check_permission' ],
59 'access_level' => ToolDescriptor::ACCESS_FULL,
60 'annotations' => [ 'readonly' => false, 'destructive' => true, 'idempotent' => false ],
61 ];
62 }
63
64 /**
65 * FR-007 (manage_options) + FR-008 (target post must exist and be editable).
66 *
67 * @param array $input
68 * @return bool
69 */
70 public static function check_permission( array $input ): bool {
71 if ( ! Permissions::can_use_abilities() ) {
72 return false;
73 }
74
75 $post_id = (int) ( $input['post_id'] ?? 0 );
76
77 return $post_id > 0 && Permissions::can_edit_post( $post_id );
78 }
79
80 /**
81 * @param array $input
82 * @return array|WP_Error
83 */
84 public static function execute( array $input ) {
85 $id = (int) ( $input['id'] ?? 0 );
86 $post_id = (int) ( $input['post_id'] ?? 0 );
87 $base_hash = (string) ( $input['base_hash'] ?? '' );
88 $platform = $input['platform'] ?? 'elementor';
89
90 if ( empty( $id ) || empty( $post_id ) || $base_hash === '' ) {
91 return new WP_Error( 'not_found', __( 'A template id, target post id, and base_hash are all required.', 'templately' ) );
92 }
93
94 $rate_limit_error = ImportRateLimiter::check();
95 if ( $rate_limit_error ) {
96 return $rate_limit_error;
97 }
98
99 $dependency_check = DependencyGateResolver::check( $id, $platform );
100 if ( is_wp_error( $dependency_check ) ) {
101 return $dependency_check;
102 }
103
104 $content_response = RestDispatcher::dispatch( 'POST', '/templately/v1/insert', [
105 'platform' => $platform,
106 'origin' => 'remote',
107 'id' => $id,
108 'postId' => $post_id,
109 ] );
110
111 if ( is_wp_error( $content_response ) ) {
112 return ProGateResolver::to_purchase_error( $content_response, $id );
113 }
114
115 $processed = $content_response->get_data();
116
117 if ( empty( $processed['content'] ) ) {
118 return new WP_Error( 'import_failed', __( 'Template content could not be prepared for insertion.', 'templately' ) );
119 }
120
121 if ( $platform === 'gutenberg' ) {
122 return GutenbergHeadlessInsert::insert( $post_id, (string) $processed['content'], $base_hash );
123 }
124
125 return ElementorHeadlessInsert::insert( $post_id, (array) $processed['content'], $base_hash );
126 }
127 }
128