| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: Browse / search the WDesignKit public marketplace Website Kit collection. |
| 4 |
* |
| 5 |
* Complements the per-kit browsing: use this to FIND a kit (by keyword/category/builder), |
| 6 |
* then wdesignkit/browse-kit-pages to list that kit's importable pages, then |
| 7 |
* wdesignkit/import-template to import a page. |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit(); |
| 14 |
} |
| 15 |
|
| 16 |
wp_register_ability('wdesignkit/browse-kits', [ |
| 17 |
'label' => __('Browse WDesignKit Marketplace Kits', 'wdesignkit'), |
| 18 |
'description' => __( |
| 19 |
'Searches and lists full-site Website Kits available in the WDesignKit public marketplace (the "AI Kits" collection). Supports keyword search plus category, builder, free/pro and pagination filters, and returns each kit\'s id, title and thumbnail. Feed a returned kit id into wdesignkit/browse-kit-pages to list that kit\'s importable pages.', |
| 20 |
'wdesignkit', |
| 21 |
), |
| 22 |
'category' => 'wdesignkit', |
| 23 |
'input_schema' => [ |
| 24 |
'type' => 'object', |
| 25 |
'properties' => [ |
| 26 |
'search' => [ |
| 27 |
'type' => 'string', |
| 28 |
'description' => 'Keyword search (matches kit title / keywords). Pass empty string to clear the search filter.', |
| 29 |
], |
| 30 |
'category' => [ |
| 31 |
'type' => 'string', |
| 32 |
'description' => 'Category / term ID to filter by. Leave empty to include all.', |
| 33 |
], |
| 34 |
'builder' => [ |
| 35 |
'type' => 'string', |
| 36 |
'description' => 'Optional exact-match filter on the kit\'s stored builder value. Leave empty to include all builders (recommended — search by keyword instead).', |
| 37 |
], |
| 38 |
'free_pro' => [ |
| 39 |
'type' => 'string', |
| 40 |
'description' => 'Limit to free or pro kits.', |
| 41 |
'enum' => ['', 'free', 'pro'], |
| 42 |
], |
| 43 |
'page' => [ |
| 44 |
'type' => 'integer', |
| 45 |
'description' => 'Page number (1-based). Defaults to 1.', |
| 46 |
'minimum' => 1, |
| 47 |
], |
| 48 |
'per_page' => [ |
| 49 |
'type' => 'integer', |
| 50 |
'description' => 'Items per page. Defaults to 12.', |
| 51 |
'minimum' => 1, |
| 52 |
'maximum' => 100, |
| 53 |
], |
| 54 |
], |
| 55 |
'additionalProperties' => false, |
| 56 |
], |
| 57 |
'output_schema' => [ |
| 58 |
'type' => 'object', |
| 59 |
'properties' => [ |
| 60 |
'success' => ['type' => 'boolean'], |
| 61 |
'message' => ['type' => 'string'], |
| 62 |
'filters' => ['type' => 'object'], |
| 63 |
'response' => ['type' => ['object', 'array']], |
| 64 |
], |
| 65 |
], |
| 66 |
'execute_callback' => 'wdesignkit_mcp_browse_kits', |
| 67 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 68 |
'meta' => [ |
| 69 |
'public' => true, |
| 70 |
'show_in_rest' => true, |
| 71 |
'mcp' => ['public' => true], |
| 72 |
'annotations' => [ |
| 73 |
'instructions' => implode("\n", [ |
| 74 |
'Searches the WDesignKit marketplace Website Kit (full-site) collection. No cloud login required.', |
| 75 |
'Typical flow: wdesignkit/browse-kits (find a kit by keyword) -> wdesignkit/browse-kit-pages (list that kit\'s pages by its id) -> wdesignkit/import-template (import a page by its id).', |
| 76 |
'All filter operations map to argument combinations: pass search / category / free_pro to filter; call with no arguments to list everything.', |
| 77 |
'Each result includes the kit id (use with wdesignkit/browse-kit-pages), title and thumbnail.', |
| 78 |
]), |
| 79 |
'readonly' => true, |
| 80 |
'destructive' => false, |
| 81 |
'idempotent' => true, |
| 82 |
], |
| 83 |
], |
| 84 |
]); |
| 85 |
|
| 86 |
function wdesignkit_mcp_browse_kits(array $input): array { |
| 87 |
// Timeout guard: marketplace HTTP call uses a 30s timeout; guard PHP execution with a buffer. |
| 88 |
set_time_limit(60); |
| 89 |
|
| 90 |
if (!defined('WDKIT_SERVER_API_URL')) { |
| 91 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 92 |
} |
| 93 |
|
| 94 |
$search = sanitize_text_field((string) ($input['search'] ?? '')); |
| 95 |
$category = sanitize_text_field((string) ($input['category'] ?? '')); |
| 96 |
$builder = sanitize_text_field((string) ($input['builder'] ?? '')); |
| 97 |
$free_pro = sanitize_text_field((string) ($input['free_pro'] ?? '')); |
| 98 |
$page = max(1, (int) ($input['page'] ?? 1)); |
| 99 |
$per_page = min(100, max(1, (int) ($input['per_page'] ?? 12))); |
| 100 |
|
| 101 |
// Mirrors the BrowseKits cloud handler's expected params (post /wp/browse_kit). |
| 102 |
$args = [ |
| 103 |
'CurrentPage' => $page, |
| 104 |
'ParPage' => $per_page, |
| 105 |
'search' => $search, |
| 106 |
'category' => $category, |
| 107 |
'builder' => $builder, |
| 108 |
'free_pro' => $free_pro, |
| 109 |
]; |
| 110 |
|
| 111 |
$response = wp_remote_post( |
| 112 |
WDKIT_SERVER_API_URL . 'api/wp/browse_kit', |
| 113 |
[ |
| 114 |
'method' => 'POST', |
| 115 |
'body' => $args, |
| 116 |
'timeout' => 30, |
| 117 |
] |
| 118 |
); |
| 119 |
|
| 120 |
if (is_wp_error($response)) { |
| 121 |
return ['success' => false, 'message' => $response->get_error_message()]; |
| 122 |
} |
| 123 |
|
| 124 |
$status = wp_remote_retrieve_response_code($response); |
| 125 |
$body = wp_remote_retrieve_body($response); |
| 126 |
$data = json_decode($body, true); |
| 127 |
|
| 128 |
if (200 !== (int) $status) { |
| 129 |
return ['success' => false, 'message' => "WDesignKit marketplace returned status {$status}."]; |
| 130 |
} |
| 131 |
|
| 132 |
// Unwrap the nested data envelope so callers get the kit list directly. |
| 133 |
$payload = $data; |
| 134 |
if (is_array($data) && !empty($data['success']) && isset($data['data'])) { |
| 135 |
$payload = is_array($data['data']) ? $data['data'] : $data; |
| 136 |
} |
| 137 |
|
| 138 |
return [ |
| 139 |
'success' => is_array($data) ? !empty($data['success']) : false, |
| 140 |
'message' => (is_array($data) ? ($data['message'] ?? $data['massage'] ?? '') : ''), |
| 141 |
'filters' => [ |
| 142 |
'search' => $search, |
| 143 |
'category' => $category, |
| 144 |
'builder' => $builder, |
| 145 |
'free_pro' => $free_pro, |
| 146 |
'page' => $page, |
| 147 |
'per_page' => $per_page, |
| 148 |
], |
| 149 |
'response' => $payload, |
| 150 |
]; |
| 151 |
} |
| 152 |
|