| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: List the cloud plugin / builder IDs a template can be tagged with. |
| 4 |
* |
| 5 |
* wdesignkit/save-template stores plugin dependencies as a template's plugins_id, keyed by |
| 6 |
* the p_id values in the cloud's plugin list. Without this ability a caller has no way to |
| 7 |
* discover those ids and can only guess integers. |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit(); |
| 14 |
} |
| 15 |
|
| 16 |
wp_register_ability('wdesignkit/get-template-plugins', [ |
| 17 |
'label' => __('Get WDesignKit Template Plugin IDs', 'wdesignkit'), |
| 18 |
'description' => __( |
| 19 |
'Lists the plugins and page builders the WDesignKit cloud knows about, with the numeric IDs used to tag a template\'s dependencies. Feed these IDs into wdesignkit/save-template\'s "plugins" input. Requires cloud login.', |
| 20 |
'wdesignkit', |
| 21 |
), |
| 22 |
'category' => 'wdesignkit', |
| 23 |
'input_schema' => [ |
| 24 |
'type' => 'object', |
| 25 |
'properties' => [ |
| 26 |
'search' => [ |
| 27 |
'type' => 'string', |
| 28 |
'description' => 'Optional keyword to filter the returned plugins and builders by name (case-insensitive).', |
| 29 |
], |
| 30 |
], |
| 31 |
'additionalProperties' => false, |
| 32 |
], |
| 33 |
'output_schema' => [ |
| 34 |
'type' => 'object', |
| 35 |
'properties' => [ |
| 36 |
'success' => ['type' => 'boolean'], |
| 37 |
'message' => ['type' => 'string'], |
| 38 |
'plugins' => ['type' => 'array'], |
| 39 |
'builders' => ['type' => 'array'], |
| 40 |
], |
| 41 |
], |
| 42 |
'execute_callback' => 'wdesignkit_mcp_get_template_plugins', |
| 43 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 44 |
'meta' => [ |
| 45 |
'public' => true, |
| 46 |
'show_in_rest' => true, |
| 47 |
'mcp' => ['public' => true], |
| 48 |
'annotations' => [ |
| 49 |
'instructions' => implode("\n", [ |
| 50 |
'Resolves plugin names to the numeric IDs the cloud stores in a template\'s plugins_id.', |
| 51 |
'Typical use: call this, match the plugins your template needs by name, then pass their', |
| 52 |
'"id" values as wdesignkit/save-template\'s plugins array.', |
| 53 |
'Builders are listed separately — they use the same id space but describe the page builder.', |
| 54 |
]), |
| 55 |
'readonly' => true, |
| 56 |
'destructive' => false, |
| 57 |
'idempotent' => true, |
| 58 |
], |
| 59 |
], |
| 60 |
]); |
| 61 |
|
| 62 |
function wdesignkit_mcp_get_template_plugins(array $input): array { |
| 63 |
// Timeout guard: cloud HTTP call uses the default 60s timeout. |
| 64 |
set_time_limit(90); |
| 65 |
|
| 66 |
if (!defined('WDKIT_SERVER_API_URL')) { |
| 67 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 68 |
} |
| 69 |
|
| 70 |
$auth = wdesignkit_mcp_template_get_auth(); |
| 71 |
if (empty($auth['logged_in'])) { |
| 72 |
return ['success' => false, 'message' => $auth['message'] ?? 'Not logged in to WDesignKit cloud.']; |
| 73 |
} |
| 74 |
|
| 75 |
$search = strtolower(trim(sanitize_text_field((string) ($input['search'] ?? '')))); |
| 76 |
|
| 77 |
$cloud = wdesignkit_mcp_template_cloud_call('template/filter/options', [ |
| 78 |
'token' => $auth['token'], |
| 79 |
], 'form'); |
| 80 |
|
| 81 |
if (empty($cloud['success'])) { |
| 82 |
return [ |
| 83 |
'success' => false, |
| 84 |
'message' => $cloud['message'] ?? $cloud['massage'] ?? 'Failed to load the cloud plugin list.', |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
// The cloud response also carries the full user record, workspaces and favourites — |
| 89 |
// none of which belong in this answer. Keep it to the id/name pairs the caller needs. |
| 90 |
$shape = static function ($rows) use ($search): array { |
| 91 |
$out = []; |
| 92 |
|
| 93 |
foreach ((is_array($rows) ? $rows : []) as $row) { |
| 94 |
if (!is_array($row)) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
$name = (string) ($row['plugin_name'] ?? ''); |
| 99 |
|
| 100 |
if ($search !== '' && false === strpos(strtolower($name), $search)) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
$out[] = [ |
| 105 |
'id' => (int) ($row['p_id'] ?? 0), |
| 106 |
'name' => $name, |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
return $out; |
| 111 |
}; |
| 112 |
|
| 113 |
$plugins = $shape($cloud['data']['plugindb'] ?? []); |
| 114 |
$builders = $shape($cloud['data']['builderdb'] ?? []); |
| 115 |
|
| 116 |
return [ |
| 117 |
'success' => true, |
| 118 |
'message' => sprintf( |
| 119 |
'%d plugin(s) and %d builder(s) available. Pass an "id" to wdesignkit/save-template as part of its plugins array.', |
| 120 |
count($plugins), |
| 121 |
count($builders) |
| 122 |
), |
| 123 |
'plugins' => $plugins, |
| 124 |
'builders' => $builders, |
| 125 |
]; |
| 126 |
} |
| 127 |
|