| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abilities: Get the current user's uploaded snippets, favourite snippets, and toggle favourites. |
| 4 |
*/ |
| 5 |
|
| 6 |
declare(strict_types=1); |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit(); |
| 10 |
} |
| 11 |
|
| 12 |
wp_register_ability('wdesignkit/get-my-snippets', [ |
| 13 |
'label' => __('Get My WDesignKit Snippets', 'wdesignkit'), |
| 14 |
'description' => __( |
| 15 |
'Lists the code snippets the current user has uploaded to the WDesignKit cloud marketplace. Supports search and pagination. Requires cloud login.', |
| 16 |
'wdesignkit', |
| 17 |
), |
| 18 |
'category' => 'wdesignkit', |
| 19 |
'input_schema' => [ |
| 20 |
'type' => 'object', |
| 21 |
'properties' => [ |
| 22 |
'search' => [ |
| 23 |
'type' => 'string', |
| 24 |
'description' => 'Keyword to filter snippets by title.', |
| 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' => 'Items per page. Defaults to 12.', |
| 34 |
'minimum' => 1, |
| 35 |
'maximum' => 100, |
| 36 |
], |
| 37 |
], |
| 38 |
'additionalProperties' => false, |
| 39 |
], |
| 40 |
'output_schema' => [ |
| 41 |
'type' => 'object', |
| 42 |
'properties' => [ |
| 43 |
'success' => ['type' => 'boolean'], |
| 44 |
'message' => ['type' => 'string'], |
| 45 |
'response' => ['type' => ['object', 'array']], |
| 46 |
], |
| 47 |
], |
| 48 |
'execute_callback' => 'wdesignkit_mcp_get_my_snippets', |
| 49 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 50 |
'meta' => [ |
| 51 |
'public' => true, |
| 52 |
'show_in_rest' => true, |
| 53 |
'mcp' => ['public' => true], |
| 54 |
'annotations' => [ |
| 55 |
'instructions' => implode("\n", [ |
| 56 |
'Lists snippets the current user has uploaded to the WDesignKit cloud. Requires cloud login.', |
| 57 |
'Use wdesignkit/delete-snippet to remove a snippet, or wdesignkit/save-snippet with stype "existing" to update.', |
| 58 |
]), |
| 59 |
'readonly' => true, |
| 60 |
'destructive' => false, |
| 61 |
'idempotent' => true, |
| 62 |
], |
| 63 |
], |
| 64 |
]); |
| 65 |
|
| 66 |
wp_register_ability('wdesignkit/get-my-favourite-snippets', [ |
| 67 |
'label' => __('Get My WDesignKit Favourite Snippets', 'wdesignkit'), |
| 68 |
'description' => __( |
| 69 |
'Lists the code snippets the current user has marked as favourites on the WDesignKit cloud marketplace. Supports search and pagination. Requires cloud login.', |
| 70 |
'wdesignkit', |
| 71 |
), |
| 72 |
'category' => 'wdesignkit', |
| 73 |
'input_schema' => [ |
| 74 |
'type' => 'object', |
| 75 |
'properties' => [ |
| 76 |
'search' => [ |
| 77 |
'type' => 'string', |
| 78 |
'description' => 'Keyword to filter favourites by title.', |
| 79 |
], |
| 80 |
'page' => [ |
| 81 |
'type' => 'integer', |
| 82 |
'description' => 'Page number (1-based). Defaults to 1.', |
| 83 |
'minimum' => 1, |
| 84 |
], |
| 85 |
'per_page' => [ |
| 86 |
'type' => 'integer', |
| 87 |
'description' => 'Items per page. Defaults to 24.', |
| 88 |
'minimum' => 1, |
| 89 |
'maximum' => 100, |
| 90 |
], |
| 91 |
], |
| 92 |
'additionalProperties' => false, |
| 93 |
], |
| 94 |
'output_schema' => [ |
| 95 |
'type' => 'object', |
| 96 |
'properties' => [ |
| 97 |
'success' => ['type' => 'boolean'], |
| 98 |
'message' => ['type' => 'string'], |
| 99 |
'response' => ['type' => ['object', 'array']], |
| 100 |
], |
| 101 |
], |
| 102 |
'execute_callback' => 'wdesignkit_mcp_get_my_favourite_snippets', |
| 103 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 104 |
'meta' => [ |
| 105 |
'public' => true, |
| 106 |
'show_in_rest' => true, |
| 107 |
'mcp' => ['public' => true], |
| 108 |
'annotations' => [ |
| 109 |
'instructions' => implode("\n", [ |
| 110 |
'Lists the current user\'s favourited snippets from the WDesignKit cloud. Requires cloud login.', |
| 111 |
'Use wdesignkit/favourite-snippet to add or remove snippets from favourites.', |
| 112 |
]), |
| 113 |
'readonly' => true, |
| 114 |
'destructive' => false, |
| 115 |
'idempotent' => true, |
| 116 |
], |
| 117 |
], |
| 118 |
]); |
| 119 |
|
| 120 |
wp_register_ability('wdesignkit/favourite-snippet', [ |
| 121 |
'label' => __('Favourite / Unfavourite WDesignKit Snippet', 'wdesignkit'), |
| 122 |
'description' => __( |
| 123 |
'Marks or unmarks a WDesignKit marketplace snippet as a favourite for the current user. Requires cloud login.', |
| 124 |
'wdesignkit', |
| 125 |
), |
| 126 |
'category' => 'wdesignkit', |
| 127 |
'input_schema' => [ |
| 128 |
'type' => 'object', |
| 129 |
'properties' => [ |
| 130 |
'snippet_id' => [ |
| 131 |
'type' => 'string', |
| 132 |
'description' => 'Cloud snippet ID to favourite or unfavourite.', |
| 133 |
], |
| 134 |
'action' => [ |
| 135 |
'type' => 'string', |
| 136 |
'description' => '"favorite" to mark as favourite, "unfavorite" to remove from favourites.', |
| 137 |
'enum' => ['favorite', 'unfavorite'], |
| 138 |
], |
| 139 |
], |
| 140 |
'required' => ['snippet_id', 'action'], |
| 141 |
'additionalProperties' => false, |
| 142 |
], |
| 143 |
'output_schema' => [ |
| 144 |
'type' => 'object', |
| 145 |
'properties' => [ |
| 146 |
'success' => ['type' => 'boolean'], |
| 147 |
'message' => ['type' => 'string'], |
| 148 |
'response' => ['type' => ['object', 'array']], |
| 149 |
], |
| 150 |
], |
| 151 |
'execute_callback' => 'wdesignkit_mcp_favourite_snippet', |
| 152 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 153 |
'meta' => [ |
| 154 |
'public' => true, |
| 155 |
'show_in_rest' => true, |
| 156 |
'mcp' => ['public' => true], |
| 157 |
'annotations' => [ |
| 158 |
'instructions' => implode("\n", [ |
| 159 |
'Marks or removes a snippet from the current user\'s favourites. Requires cloud login.', |
| 160 |
'Get snippet_id from wdesignkit/browse-snippets or wdesignkit/get-my-snippets.', |
| 161 |
'Use wdesignkit/get-my-favourite-snippets to list current favourites.', |
| 162 |
]), |
| 163 |
'readonly' => false, |
| 164 |
'destructive' => false, |
| 165 |
'idempotent' => true, |
| 166 |
], |
| 167 |
], |
| 168 |
]); |
| 169 |
|
| 170 |
function wdesignkit_mcp_get_my_snippets(array $input): array { |
| 171 |
// Timeout guard: cloud HTTP call uses the default 60s timeout. |
| 172 |
set_time_limit(90); |
| 173 |
|
| 174 |
if (!defined('WDKIT_SERVER_API_URL')) { |
| 175 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 176 |
} |
| 177 |
|
| 178 |
$auth = wdesignkit_mcp_template_get_auth(); |
| 179 |
if (empty($auth['logged_in'])) { |
| 180 |
return ['success' => false, 'message' => $auth['message'] ?? 'Not logged in to WDesignKit cloud.']; |
| 181 |
} |
| 182 |
|
| 183 |
$search = sanitize_text_field((string) ($input['search'] ?? '')); |
| 184 |
$page = max(1, (int) ($input['page'] ?? 1)); |
| 185 |
$per_page = min(100, max(1, (int) ($input['per_page'] ?? 12))); |
| 186 |
|
| 187 |
$cloud = wdesignkit_mcp_template_cloud_call('snippet/mysnippets', [ |
| 188 |
'token' => $auth['token'], |
| 189 |
'search' => $search, |
| 190 |
'CurrentPage' => $page, |
| 191 |
'ParPage' => $per_page, |
| 192 |
], 'form'); |
| 193 |
|
| 194 |
return [ |
| 195 |
'success' => !empty($cloud['success']), |
| 196 |
'message' => $cloud['message'] ?? $cloud['massage'] ?? '', |
| 197 |
'response' => $cloud, |
| 198 |
]; |
| 199 |
} |
| 200 |
|
| 201 |
function wdesignkit_mcp_get_my_favourite_snippets(array $input): array { |
| 202 |
// Timeout guard: cloud HTTP call uses the default 60s timeout. |
| 203 |
set_time_limit(90); |
| 204 |
|
| 205 |
if (!defined('WDKIT_SERVER_API_URL')) { |
| 206 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 207 |
} |
| 208 |
|
| 209 |
$auth = wdesignkit_mcp_template_get_auth(); |
| 210 |
if (empty($auth['logged_in'])) { |
| 211 |
return ['success' => false, 'message' => $auth['message'] ?? 'Not logged in to WDesignKit cloud.']; |
| 212 |
} |
| 213 |
|
| 214 |
$search = sanitize_text_field((string) ($input['search'] ?? '')); |
| 215 |
$page = max(1, (int) ($input['page'] ?? 1)); |
| 216 |
$per_page = min(100, max(1, (int) ($input['per_page'] ?? 24))); |
| 217 |
|
| 218 |
$cloud = wdesignkit_mcp_template_cloud_call('snippet/favorite/get', [ |
| 219 |
'token' => $auth['token'], |
| 220 |
'search' => $search, |
| 221 |
'CurrentPage' => $page, |
| 222 |
'ParPage' => $per_page, |
| 223 |
], 'form'); |
| 224 |
|
| 225 |
return [ |
| 226 |
'success' => !empty($cloud['success']), |
| 227 |
'message' => $cloud['message'] ?? $cloud['massage'] ?? '', |
| 228 |
'response' => $cloud, |
| 229 |
]; |
| 230 |
} |
| 231 |
|
| 232 |
function wdesignkit_mcp_favourite_snippet(array $input): array { |
| 233 |
// Timeout guard: cloud HTTP call uses the default 60s timeout; guards both favorite and unfavorite paths. |
| 234 |
set_time_limit(90); |
| 235 |
|
| 236 |
if (!defined('WDKIT_SERVER_API_URL')) { |
| 237 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 238 |
} |
| 239 |
|
| 240 |
$auth = wdesignkit_mcp_template_get_auth(); |
| 241 |
if (empty($auth['logged_in'])) { |
| 242 |
return ['success' => false, 'message' => $auth['message'] ?? 'Not logged in to WDesignKit cloud.']; |
| 243 |
} |
| 244 |
|
| 245 |
$snippet_id = sanitize_text_field((string) ($input['snippet_id'] ?? '')); |
| 246 |
$action = sanitize_text_field((string) ($input['action'] ?? '')); |
| 247 |
|
| 248 |
if ($snippet_id === '' || $action === '') { |
| 249 |
return ['success' => false, 'message' => 'snippet_id and action are required.']; |
| 250 |
} |
| 251 |
|
| 252 |
$cloud = wdesignkit_mcp_template_cloud_call('snippet/favorite', [ |
| 253 |
'token' => $auth['token'], |
| 254 |
'snippet_id' => $snippet_id, |
| 255 |
'type' => $action, |
| 256 |
], 'form'); |
| 257 |
|
| 258 |
return [ |
| 259 |
'success' => !empty($cloud['success']), |
| 260 |
'message' => $cloud['message'] ?? $cloud['massage'] ?? ($cloud['success'] ? "Snippet {$action}d." : "Failed to {$action} snippet."), |
| 261 |
'response' => $cloud, |
| 262 |
]; |
| 263 |
} |
| 264 |
|