| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: Fetch the available filter option values (categories/terms, plugins, tags) |
| 4 |
* for the WDesignKit code-snippet marketplace browse screen. |
| 5 |
* |
| 6 |
* Complements wdesignkit/browse-snippets: browse-snippets APPLIES filters by id |
| 7 |
* (terms_id / plugin_id / tags), while this ability DISCOVERS the valid ids to pass. |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit(); |
| 14 |
} |
| 15 |
|
| 16 |
wp_register_ability('wdesignkit/get-snippet-filter-options', [ |
| 17 |
'label' => __('Get WDesignKit Snippet Filter Options', 'wdesignkit'), |
| 18 |
'description' => __( |
| 19 |
'Returns the dynamic filter option values used by the code-snippet marketplace browse UI: category/term ids, plugin ids, and tag ids (with their display names). Use this to discover the valid ids to pass to wdesignkit/browse-snippets\' category, plugins, and tags filters.', |
| 20 |
'wdesignkit', |
| 21 |
), |
| 22 |
'category' => 'wdesignkit', |
| 23 |
'input_schema' => [ |
| 24 |
'type' => 'object', |
| 25 |
'properties' => [ |
| 26 |
'opt_type' => [ |
| 27 |
'type' => 'string', |
| 28 |
'description' => 'Which option groups to fetch — any comma-separated combination of "terms" (categories), "plugin", and "tag". Defaults to all three.', |
| 29 |
], |
| 30 |
'search_tag' => [ |
| 31 |
'type' => 'string', |
| 32 |
'description' => 'Optional keyword to search within the tag options.', |
| 33 |
], |
| 34 |
'tags' => [ |
| 35 |
'type' => 'string', |
| 36 |
'description' => 'Optional comma-separated tag ids to resolve (maps to the cloud url_tag_id parameter).', |
| 37 |
], |
| 38 |
], |
| 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_filter_options', |
| 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 the category/term, plugin, and tag option values for the snippet marketplace.', |
| 58 |
'No cloud login required.', |
| 59 |
'opt_type accepts any comma-separated mix of "terms", "plugin", and "tag" (default: all three).', |
| 60 |
'Feed the returned ids into wdesignkit/browse-snippets (category = a term id, plugins = plugin ids, tags = tag ids).', |
| 61 |
]), |
| 62 |
'readonly' => true, |
| 63 |
'destructive' => false, |
| 64 |
'idempotent' => true, |
| 65 |
], |
| 66 |
], |
| 67 |
]); |
| 68 |
|
| 69 |
function wdesignkit_mcp_get_snippet_filter_options(array $input): array { |
| 70 |
if (!defined('WDKIT_SERVER_API_URL')) { |
| 71 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 72 |
} |
| 73 |
|
| 74 |
// Restrict opt_type to the tokens the cloud endpoint understands; default to all. |
| 75 |
$allowed_tokens = ['terms', 'plugin', 'tag']; |
| 76 |
$raw_opt_type = sanitize_text_field((string) ($input['opt_type'] ?? '')); |
| 77 |
|
| 78 |
$tokens = array_filter(array_map('trim', explode(',', $raw_opt_type)), static function (string $t) use ($allowed_tokens): bool { |
| 79 |
return in_array($t, $allowed_tokens, true); |
| 80 |
}); |
| 81 |
$opt_type = !empty($tokens) ? implode(',', $tokens) : 'terms,plugin,tag'; |
| 82 |
|
| 83 |
$search_tag = sanitize_text_field((string) ($input['search_tag'] ?? '')); |
| 84 |
$tags = sanitize_text_field((string) ($input['tags'] ?? '')); |
| 85 |
|
| 86 |
// Mirrors wdkit_code_snippet_filter_opt() in class-wdkit-code-snippet.php: |
| 87 |
// type => opt_type, search_tag => search_tag, url_tag_id => tags. |
| 88 |
$args = [ |
| 89 |
'type' => $opt_type, |
| 90 |
]; |
| 91 |
|
| 92 |
if ($search_tag !== '') { |
| 93 |
$args['search_tag'] = $search_tag; |
| 94 |
} |
| 95 |
if ($tags !== '') { |
| 96 |
$args['url_tag_id'] = $tags; |
| 97 |
} |
| 98 |
|
| 99 |
$cloud = wdesignkit_mcp_template_cloud_call('snippet/browse/filter', $args, 'form'); |
| 100 |
|
| 101 |
return [ |
| 102 |
'success' => !empty($cloud['success']), |
| 103 |
'message' => $cloud['message'] ?? $cloud['massage'] ?? '', |
| 104 |
'response' => $cloud, |
| 105 |
]; |
| 106 |
} |
| 107 |
|