| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: Search the current user's existing WDesignKit cloud templates by keyword. |
| 4 |
*/ |
| 5 |
|
| 6 |
declare(strict_types=1); |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit(); |
| 10 |
} |
| 11 |
|
| 12 |
wp_register_ability('wdesignkit/find-template', [ |
| 13 |
'label' => __('Find WDesignKit Template', 'wdesignkit'), |
| 14 |
'description' => __( |
| 15 |
'Searches the user\'s existing WDesignKit cloud templates by name keyword. Scope is the user\'s OWN saved templates only — this does NOT search the public marketplace / AI Kits, so marketplace kit names correctly return zero results here. Hits the "existing_template" endpoint (the same one update-template targets) so the IDs returned here can be passed straight to update-template, replace-template, remove-template, or import-template.', |
| 16 |
'wdesignkit', |
| 17 |
), |
| 18 |
'category' => 'wdesignkit', |
| 19 |
'input_schema' => [ |
| 20 |
'type' => 'object', |
| 21 |
'properties' => [ |
| 22 |
'search' => [ |
| 23 |
'type' => 'string', |
| 24 |
'description' => 'Keyword to search for. Pass empty string to list everything matching the other filters.', |
| 25 |
], |
| 26 |
'builder' => [ |
| 27 |
'type' => 'string', |
| 28 |
'description' => 'Restrict to one builder.', |
| 29 |
'enum' => ['', 'elementor', 'gutenberg'], |
| 30 |
], |
| 31 |
'type' => [ |
| 32 |
'type' => 'string', |
| 33 |
'description' => 'Template type filter (e.g. "page", "section", "block").', |
| 34 |
], |
| 35 |
'per_page' => [ |
| 36 |
'type' => 'integer', |
| 37 |
'description' => 'Results per page. Defaults to 12.', |
| 38 |
'minimum' => 1, |
| 39 |
'maximum' => 100, |
| 40 |
], |
| 41 |
], |
| 42 |
// No required fields — all filters are optional. |
| 43 |
'additionalProperties' => false, |
| 44 |
], |
| 45 |
'output_schema' => [ |
| 46 |
'type' => 'object', |
| 47 |
'properties' => [ |
| 48 |
'success' => ['type' => 'boolean'], |
| 49 |
'message' => ['type' => 'string'], |
| 50 |
'response' => ['type' => ['object', 'array']], |
| 51 |
], |
| 52 |
], |
| 53 |
'execute_callback' => 'wdesignkit_mcp_find_template', |
| 54 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 55 |
'meta' => [ |
| 56 |
'public' => true, |
| 57 |
'show_in_rest' => true, |
| 58 |
'mcp' => ['public' => true], |
| 59 |
'annotations' => [ |
| 60 |
'instructions' => implode("\n", [ |
| 61 |
'Searches existing user templates. Useful before calling update-template or remove-template when you only know the template name.', |
| 62 |
'Scope: the user\'s OWN saved templates only — NOT the public marketplace / AI Kits. Searching a marketplace kit name here returns zero results by design.', |
| 63 |
'Requires WDesignKit cloud login.', |
| 64 |
'For browsing pages of templates without keyword search prefer wdesignkit/list-templates.', |
| 65 |
]), |
| 66 |
'readonly' => true, |
| 67 |
'destructive' => false, |
| 68 |
'idempotent' => true, |
| 69 |
], |
| 70 |
], |
| 71 |
]); |
| 72 |
|
| 73 |
function wdesignkit_mcp_find_template(array $input): array { |
| 74 |
set_time_limit(90); |
| 75 |
|
| 76 |
$auth = wdesignkit_mcp_template_get_auth(); |
| 77 |
if (empty($auth['logged_in'])) { |
| 78 |
return ['success' => false, 'message' => $auth['message'] ?? 'Not logged in.']; |
| 79 |
} |
| 80 |
|
| 81 |
$type_val = sanitize_text_field((string) ($input['type'] ?? '')); |
| 82 |
|
| 83 |
$args = [ |
| 84 |
'search' => sanitize_text_field((string) ($input['search'] ?? '')), |
| 85 |
'token' => $auth['token'], |
| 86 |
'type' => 'find_template', // server dispatches on this value, not the template type filter |
| 87 |
'filter_type' => $type_val, // optional template type filter (page / section / block) |
| 88 |
'u_id' => get_option('wdkit_unique_id', ''), |
| 89 |
'builder' => sanitize_text_field((string) ($input['builder'] ?? '')), |
| 90 |
'parpage' => min(100, max(1, (int) ($input['per_page'] ?? 12))), |
| 91 |
]; |
| 92 |
|
| 93 |
$response = wdesignkit_mcp_template_cloud_call('existing_template', $args, 'form'); |
| 94 |
|
| 95 |
// Cloud returns HTTP 200 with empty body when the filter matches no templates. |
| 96 |
// An empty body is not a success — surface it as "no results" so callers are not |
| 97 |
// misled by success:true with no usable data. |
| 98 |
if (array_key_exists('raw', $response) && ($response['raw'] === '' || $response['raw'] === null)) { |
| 99 |
return [ |
| 100 |
'success' => false, |
| 101 |
'message' => 'No templates found matching the search criteria.', |
| 102 |
'response' => $response, |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
return [ |
| 107 |
'success' => (bool) ($response['success'] ?? !empty($response['data'])), |
| 108 |
'message' => $response['message'] ?? $response['massage'] ?? '', |
| 109 |
'response' => $response, |
| 110 |
]; |
| 111 |
} |
| 112 |
|