PluginProbe
WDesignKit – AI Templates, Widget Builder & MCP Workflow / 2.7.0
WDesignKit – AI Templates, Widget Builder & MCP Workflow v2.7.0
2.7.1 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 All 130 releases
wdesignkit / includes / abilities / code-snippets / wdesignkit-snippet-kit.php

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

151 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Ability: Get the snippets contained in a WDesignKit snippet kit (bundle).
4 */
5
6 declare(strict_types=1);
7
8 if (!defined('ABSPATH')) {
9 exit();
10 }
11
12 wp_register_ability('wdesignkit/get-snippet-kit', [
13 'label' => __('Get WDesignKit Snippet Kit', 'wdesignkit'),
14 'description' => __(
15 'Fetches individual code snippets inside a WDesignKit snippet kit (bundle). A kit is a collection of related snippets. Returns snippet summaries (id, name, description, type, free_pro) without the raw code body to stay within the 1 MB output limit. Use page/per_page to paginate large kits. No cloud login required.',
16 'wdesignkit',
17 ),
18 'category' => 'wdesignkit',
19 'input_schema' => [
20 'type' => 'object',
21 'properties' => [
22 'kit_id' => [
23 'type' => 'string',
24 'description' => 'The kit / bundle ID to expand (from wdesignkit/browse-snippets with snippet_type "websitekit").',
25 ],
26 'page' => [
27 'type' => 'integer',
28 'description' => 'Page number (1-based). Defaults to 1.',
29 'minimum' => 1,
30 ],
31 'per_page' => [
32 'type' => 'integer',
33 'description' => 'Snippets per page. Defaults to 20. Use smaller values for kits with many snippets to avoid the 1 MB output cap.',
34 'minimum' => 1,
35 'maximum' => 50,
36 ],
37 ],
38 'required' => ['kit_id'],
39 'additionalProperties' => false,
40 ],
41 'output_schema' => [
42 'type' => 'object',
43 'properties' => [
44 'success' => ['type' => 'boolean'],
45 'message' => ['type' => 'string'],
46 'response' => ['type' => ['object', 'array']],
47 ],
48 ],
49 'execute_callback' => 'wdesignkit_mcp_get_snippet_kit',
50 'permission_callback' => 'wdesignkit_mcp_permission_callback',
51 'meta' => [
52 'public' => true,
53 'show_in_rest' => true,
54 'mcp' => ['public' => true],
55 'annotations' => [
56 'instructions' => implode("\n", [
57 'Fetches snippets inside a kit/bundle. No cloud login required.',
58 'Find kit_id by calling wdesignkit/browse-snippets with snippet_type "websitekit".',
59 'Returns summary fields only (no raw code) to avoid the 1 MB output cap; use page/per_page for large kits.',
60 'Each snippet in the kit has its own id — use wdesignkit/download-snippet to install individual snippets.',
61 ]),
62 'readonly' => true,
63 'destructive' => false,
64 'idempotent' => true,
65 ],
66 ],
67 ]);
68
69 function wdesignkit_mcp_get_snippet_kit(array $input): array {
70 // Timeout guard: cloud kit-bundle HTTP call uses the default 60s timeout.
71 set_time_limit(90);
72
73 if (!defined('WDKIT_SERVER_API_URL')) {
74 return ['success' => false, 'message' => 'WDesignKit plugin is not active.'];
75 }
76
77 $kit_id = sanitize_text_field((string) ($input['kit_id'] ?? ''));
78 $page = max(1, (int) ($input['page'] ?? 1));
79 $per_page = min(50, max(1, (int) ($input['per_page'] ?? 20)));
80
81 if ($kit_id === '') {
82 return ['success' => false, 'message' => 'kit_id is required.'];
83 }
84
85 $cloud = wdesignkit_mcp_template_cloud_call('snippet/kit/' . $kit_id, [], 'form');
86
87 if (empty($cloud['success'])) {
88 // Strip raw body/HTML fragments from the error response — they can contain
89 // framework-internal details (e.g. Laravel stack traces) that should not
90 // be exposed to MCP consumers.
91 $error_response = array_diff_key($cloud, array_flip(['body', 'raw']));
92 return [
93 'success' => false,
94 'message' => $cloud['message'] ?? $cloud['massage'] ?? 'Failed to fetch snippet kit.',
95 'response' => $error_response,
96 ];
97 }
98
99 // Strip large code-body fields from each snippet to stay under the 1 MB MCP output cap.
100 // Callers can download individual snippets via wdesignkit/download-snippet using the returned id.
101 $heavy_keys = ['langCode', 'content', 'code_content', 'htmlHooks', 'nxt-php-code', 'nxt-css-code', 'nxt-js-code', 'nxt-html-code'];
102
103 // The cloud may return snippets under different keys depending on the API version.
104 // Also check nested data envelope (data.snippet, data.snippets).
105 $snippets_raw = $cloud['snippet']
106 ?? $cloud['snippets']
107 ?? (isset($cloud['data']) && is_array($cloud['data']) ? ($cloud['data']['snippet'] ?? $cloud['data']['snippets'] ?? null) : null)
108 ?? [];
109 $total = (int) (
110 $cloud['snippetcount']
111 ?? $cloud['total']
112 ?? (isset($cloud['data']) && is_array($cloud['data']) ? ($cloud['data']['snippetcount'] ?? $cloud['data']['total'] ?? null) : null)
113 ?? count($snippets_raw)
114 );
115
116 if (!is_array($snippets_raw)) {
117 $snippets_raw = [];
118 }
119
120 // Paginate
121 $offset = ($page - 1) * $per_page;
122 $slice = array_slice($snippets_raw, $offset, $per_page);
123 $snippets = [];
124 foreach ($slice as $s) {
125 if (!is_array($s)) {
126 $snippets[] = $s;
127 continue;
128 }
129 foreach ($heavy_keys as $k) {
130 unset($s[$k]);
131 }
132 $snippets[] = $s;
133 }
134
135 $total_pages = $per_page > 0 ? (int) ceil($total / $per_page) : 1;
136
137 // Build a clean response — omit the raw cloud envelope to keep size down.
138 $response_out = array_diff_key($cloud, array_flip(['snippet', 'snippets']));
139 $response_out['snippet'] = $snippets;
140 $response_out['snippetcount'] = $total;
141 $response_out['page'] = $page;
142 $response_out['per_page'] = $per_page;
143 $response_out['total_pages'] = $total_pages;
144
145 return [
146 'success' => true,
147 'message' => $cloud['message'] ?? $cloud['massage'] ?? '',
148 'response' => $response_out,
149 ];
150 }
151