PluginProbe
WDesignKit – AI Templates, Widget Builder & MCP Workflow / 2.7.0
WDesignKit – AI Templates, Widget Builder & MCP Workflow v2.7.0
2.7.0 2.6.6 2.6.5 2.6.4 2.6.3 2.6.2 2.6.1 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5.0 2.4.0 2.3.3 2.3.2 2.3.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 All 129 releases
wdesignkit / includes / abilities / code-snippets / wdesignkit-get-snippet.php

wdesignkit-get-snippet.php in WDesignKit – AI Templates, Widget Builder & MCP Workflow 2.7.0, at includes/abilities/code-snippets/wdesignkit-get-snippet.php

254 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abilities: Get a local snippet's info, or fetch existing cloud snippets for the save page.
4 */
5
6 declare(strict_types=1);
7
8 if (!defined('ABSPATH')) {
9 exit();
10 }
11
12 wp_register_ability('wdesignkit/get-snippet-info', [
13 'label' => __('Get WDesignKit Snippet Info', 'wdesignkit'),
14 'description' => __(
15 'Returns the title and description of a locally stored code snippet by its WordPress post ID. Works with both post-based and file-based (Nexter Pro) snippet storage. No cloud login required.',
16 'wdesignkit',
17 ),
18 'category' => 'wdesignkit',
19 'input_schema' => [
20 'type' => 'object',
21 'properties' => [
22 'post_id' => [
23 'type' => 'integer',
24 'description' => 'WordPress post ID for post-based snippet storage. Use for legacy installations without Nexter Pro.',
25 ],
26 'file_id' => [
27 'type' => 'string',
28 'description' => 'File-based snippet key (returned by wdesignkit/download-snippet or wdesignkit/list-local-snippets). Use when Nexter Pro file-based storage is active. Example: "1-disable-emojis-for-faster".',
29 ],
30 ],
31 'additionalProperties' => false,
32 ],
33 'output_schema' => [
34 'type' => 'object',
35 'properties' => [
36 'success' => ['type' => 'boolean'],
37 'message' => ['type' => 'string'],
38 'source' => ['type' => 'string'],
39 'data' => ['type' => 'object'],
40 ],
41 ],
42 'execute_callback' => 'wdesignkit_mcp_get_snippet_info',
43 'permission_callback' => 'wdesignkit_mcp_permission_callback',
44 'meta' => [
45 'public' => true,
46 'show_in_rest' => true,
47 'mcp' => ['public' => true],
48 'annotations' => [
49 'instructions' => implode("\n", [
50 'Returns full details for a local snippet. Provide either file_id (file-based/Nexter Pro) or post_id (post-based/legacy).',
51 'source is "file" when Nexter Pro file-based storage is active, otherwise "post".',
52 'Get file_id from wdesignkit/download-snippet (file_id field) or wdesignkit/list-local-snippets.',
53 'Get post_id from wdesignkit/download-snippet (post_id field) for legacy post-based installations.',
54 'No cloud login required — reads from local storage only.',
55 ]),
56 'readonly' => true,
57 'destructive' => false,
58 'idempotent' => true,
59 ],
60 ],
61 ]);
62
63 wp_register_ability('wdesignkit/get-existing-snippet', [
64 'label' => __('Get Existing WDesignKit Cloud Snippets', 'wdesignkit'),
65 'description' => __(
66 'Fetches the current user\'s previously uploaded cloud snippets. Used when saving a snippet to check whether it already exists on the cloud and to obtain the cloud snippet ID for an update. Requires cloud login.',
67 'wdesignkit',
68 ),
69 'category' => 'wdesignkit',
70 'input_schema' => [
71 'type' => 'object',
72 'properties' => [
73 'search' => [
74 'type' => 'string',
75 'description' => 'Keyword to filter by title.',
76 ],
77 'page' => [
78 'type' => 'integer',
79 'description' => 'Page number (1-based). Defaults to 1.',
80 'minimum' => 1,
81 ],
82 'per_page' => [
83 'type' => 'integer',
84 'description' => 'Items per page. Defaults to 12.',
85 'minimum' => 1,
86 'maximum' => 100,
87 ],
88 ],
89 'additionalProperties' => false,
90 ],
91 'output_schema' => [
92 'type' => 'object',
93 'properties' => [
94 'success' => ['type' => 'boolean'],
95 'message' => ['type' => 'string'],
96 'response' => ['type' => ['object', 'array']],
97 ],
98 ],
99 'execute_callback' => 'wdesignkit_mcp_get_existing_snippet',
100 'permission_callback' => 'wdesignkit_mcp_permission_callback',
101 'meta' => [
102 'public' => true,
103 'show_in_rest' => true,
104 'mcp' => ['public' => true],
105 'annotations' => [
106 'instructions' => implode("\n", [
107 'Fetches the current user\'s uploaded cloud snippets from the WDesignKit marketplace.',
108 'Requires cloud login.',
109 'Use the returned snippet IDs as snippet_id in wdesignkit/save-snippet with stype "existing" to update.',
110 'NOTE: this endpoint (snippet/save/get_exist) is specifically for the save/update flow and may return 0 results depending on cloud state. If it returns 0, use wdesignkit/get-my-snippets instead — both cover the same data through different endpoints.',
111 ]),
112 'readonly' => true,
113 'destructive' => false,
114 'idempotent' => true,
115 ],
116 ],
117 ]);
118
119 function wdesignkit_mcp_get_snippet_info(array $input): array {
120 // Timeout guard: local DB query + optional file-based class call.
121 set_time_limit(30);
122
123 $post_id = (int) ($input['post_id'] ?? 0);
124 $file_id = sanitize_text_field((string) ($input['file_id'] ?? ''));
125
126 if ($post_id <= 0 && $file_id === '') {
127 return ['success' => false, 'message' => 'Provide either file_id (file-based/Nexter Pro) or post_id (post-based).'];
128 }
129
130 // File-based path: use explicit file_id when provided.
131 if ($file_id !== '' && class_exists('Nexter_Code_Snippets_File_Based')) {
132 $file_based = new \Nexter_Code_Snippets_File_Based();
133 $file_code_list = $file_based->getSnippetData($file_id);
134
135 if (!empty($file_code_list) && is_array($file_code_list)) {
136 $lang_code = (string) ($file_code_list['langCode'] ?? '');
137 $type = strtolower((string) ($file_code_list['type'] ?? ''));
138 if ($type === 'php' && $lang_code !== '' && !preg_match('/^\s*<\?php/i', $lang_code)) {
139 $lang_code = "<?php\n" . $lang_code;
140 }
141
142 return [
143 'success' => true,
144 'message' => '',
145 'source' => 'file',
146 'data' => [
147 'file_id' => $file_id,
148 'title' => $file_code_list['name'] ?? '',
149 'description' => $file_code_list['description'] ?? '',
150 'type' => $file_code_list['type'] ?? '',
151 'status' => $file_code_list['status'] ?? 0,
152 'tags' => $file_code_list['tags'] ?? [],
153 'codeExecute' => $file_code_list['codeExecute'] ?? '',
154 'langCode' => $lang_code,
155 ],
156 ];
157 }
158
159 return ['success' => false, 'message' => "No snippet found with file_id '{$file_id}'.", 'source' => 'file', 'data' => (object) []];
160 }
161
162 // Post-based path: use post_id (legacy / non-Nexter-Pro installs).
163 if ($post_id > 0) {
164 // Also try file-based by post_id when class is available (edge case: numeric file key).
165 if (class_exists('Nexter_Code_Snippets_File_Based')) {
166 $file_based = new \Nexter_Code_Snippets_File_Based();
167 $file_code_list = $file_based->getSnippetData((string) $post_id);
168 if (!empty($file_code_list) && is_array($file_code_list)) {
169 $lang_code = (string) ($file_code_list['langCode'] ?? '');
170 $type = strtolower((string) ($file_code_list['type'] ?? ''));
171 if ($type === 'php' && $lang_code !== '' && !preg_match('/^\s*<\?php/i', $lang_code)) {
172 $lang_code = "<?php\n" . $lang_code;
173 }
174
175 return [
176 'success' => true,
177 'message' => '',
178 'source' => 'file',
179 'data' => [
180 'file_id' => (string) $post_id,
181 'title' => $file_code_list['name'] ?? '',
182 'description' => $file_code_list['description'] ?? '',
183 'type' => $file_code_list['type'] ?? '',
184 'status' => $file_code_list['status'] ?? 0,
185 'tags' => $file_code_list['tags'] ?? [],
186 'codeExecute' => $file_code_list['codeExecute'] ?? '',
187 'langCode' => $lang_code,
188 ],
189 ];
190 }
191 }
192
193 $post = get_post($post_id);
194 if (!$post) {
195 $hint = class_exists('Nexter_Code_Snippets_File_Based')
196 ? " This site uses Nexter Pro file-based snippet storage — use file_id (from wdesignkit/list-local-snippets or wdesignkit/download-snippet) instead of post_id."
197 : '';
198 return ['success' => false, 'message' => "No snippet found with post ID {$post_id}.{$hint}", 'source' => 'post', 'data' => (object) []];
199 }
200
201 $type = (string) get_post_meta($post_id, 'nxt-code-type', true);
202 return [
203 'success' => true,
204 'message' => '',
205 'source' => 'post',
206 'data' => [
207 'post_id' => $post->ID,
208 'title' => $post->post_title,
209 'description' => (string) get_post_meta($post_id, 'nxt-code-note', true),
210 'type' => $type,
211 'post_type' => $post->post_type,
212 'status' => $post->post_status,
213 'codeExecute' => (string) get_post_meta($post_id, 'nxt-code-execute', true),
214 'langCode' => (string) get_post_meta($post_id, 'nxt-' . $type . '-code', true),
215 ],
216 ];
217 }
218
219 return ['success' => false, 'message' => 'Provide either file_id or post_id.', 'source' => '', 'data' => (object) []];
220 }
221
222 function wdesignkit_mcp_get_existing_snippet(array $input): array {
223 // Timeout guard: cloud HTTP call uses the default 60s timeout.
224 set_time_limit(90);
225
226 if (!defined('WDKIT_SERVER_API_URL')) {
227 return ['success' => false, 'message' => 'WDesignKit plugin is not active.'];
228 }
229
230 $auth = wdesignkit_mcp_template_get_auth();
231 if (empty($auth['logged_in'])) {
232 return ['success' => false, 'message' => $auth['message'] ?? 'Not logged in to WDesignKit cloud.'];
233 }
234
235 $search = sanitize_text_field((string) ($input['search'] ?? ''));
236 $page = max(1, (int) ($input['page'] ?? 1));
237 $per_page = min(100, max(1, (int) ($input['per_page'] ?? 12)));
238
239 $args = [
240 'token' => $auth['token'],
241 'search' => $search,
242 'CurrentPage' => $page,
243 'ParPage' => $per_page,
244 ];
245
246 $cloud = wdesignkit_mcp_template_cloud_call('snippet/save/get_exist', $args, 'form');
247
248 return [
249 'success' => !empty($cloud['success']),
250 'message' => $cloud['message'] ?? $cloud['massage'] ?? '',
251 'response' => $cloud,
252 ];
253 }
254