| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: List the widgets the current user has uploaded to their WDesignKit cloud account. |
| 4 |
*/ |
| 5 |
|
| 6 |
declare(strict_types=1); |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit(); |
| 10 |
} |
| 11 |
|
| 12 |
wp_register_ability('wdesignkit/get-my-cloud-widgets', [ |
| 13 |
'label' => __('Get My WDesignKit Cloud Widgets', 'wdesignkit'), |
| 14 |
'description' => __( |
| 15 |
'Lists the widgets the current user has pushed to their WDesignKit cloud account, including private ones that were never published to the public marketplace. Returns each widget\'s cloud id for use with wdesignkit/download-widget and wdesignkit/manage-workspace-widget. Supports search, builder filter, 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 widgets by title.', |
| 25 |
], |
| 26 |
'builder' => [ |
| 27 |
'type' => 'string', |
| 28 |
'description' => 'Filter to a single builder: elementor, gutenberg, gutenberg_core, or bricks.', |
| 29 |
'enum' => ['elementor', 'gutenberg', 'gutenberg_core', 'bricks'], |
| 30 |
], |
| 31 |
'private_only' => [ |
| 32 |
'type' => 'boolean', |
| 33 |
'description' => 'true returns only unpublished (private) widgets. Default false returns both public and private.', |
| 34 |
], |
| 35 |
'page' => [ |
| 36 |
'type' => 'integer', |
| 37 |
'description' => 'Page number (1-based). Defaults to 1.', |
| 38 |
'minimum' => 1, |
| 39 |
], |
| 40 |
'per_page' => [ |
| 41 |
'type' => 'integer', |
| 42 |
'description' => 'Items per page. Defaults to 12.', |
| 43 |
'minimum' => 1, |
| 44 |
'maximum' => 100, |
| 45 |
], |
| 46 |
], |
| 47 |
'additionalProperties' => false, |
| 48 |
], |
| 49 |
'output_schema' => [ |
| 50 |
'type' => 'object', |
| 51 |
'properties' => [ |
| 52 |
'success' => ['type' => 'boolean'], |
| 53 |
'message' => ['type' => 'string'], |
| 54 |
'count' => ['type' => 'integer'], |
| 55 |
'widgets' => ['type' => 'array'], |
| 56 |
'response' => ['type' => ['object', 'array']], |
| 57 |
], |
| 58 |
], |
| 59 |
'execute_callback' => 'wdesignkit_mcp_get_my_cloud_widgets', |
| 60 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 61 |
'meta' => [ |
| 62 |
'public' => true, |
| 63 |
'show_in_rest' => true, |
| 64 |
'mcp' => ['public' => true], |
| 65 |
'annotations' => [ |
| 66 |
'instructions' => implode("\n", [ |
| 67 |
'Lists widgets the current user has pushed to the WDesignKit cloud. Requires cloud login.', |
| 68 |
'Unlike wdesignkit/browse-widgets (public marketplace only), this includes the user\'s own private widgets.', |
| 69 |
'Use the returned "id" with wdesignkit/download-widget to redownload a widget onto this site,', |
| 70 |
'or with wdesignkit/manage-workspace-widget to add it to a workspace.', |
| 71 |
'Widgets appear here after a successful wdesignkit/push-widget.', |
| 72 |
]), |
| 73 |
'readonly' => true, |
| 74 |
'destructive' => false, |
| 75 |
'idempotent' => true, |
| 76 |
], |
| 77 |
], |
| 78 |
]); |
| 79 |
|
| 80 |
function wdesignkit_mcp_get_my_cloud_widgets(array $input): array { |
| 81 |
// Timeout guard: cloud HTTP call uses the default 60s timeout. |
| 82 |
set_time_limit(90); |
| 83 |
|
| 84 |
if (!defined('WDKIT_SERVER_API_URL')) { |
| 85 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 86 |
} |
| 87 |
|
| 88 |
$auth = wdesignkit_mcp_template_get_auth(); |
| 89 |
if (empty($auth['logged_in'])) { |
| 90 |
return ['success' => false, 'message' => $auth['message'] ?? 'Not logged in to WDesignKit cloud.']; |
| 91 |
} |
| 92 |
|
| 93 |
$search = sanitize_text_field((string) ($input['search'] ?? '')); |
| 94 |
$builder = sanitize_text_field((string) ($input['builder'] ?? '')); |
| 95 |
$private_only = !empty($input['private_only']); |
| 96 |
$page = max(1, (int) ($input['page'] ?? 1)); |
| 97 |
$per_page = min(100, max(1, (int) ($input['per_page'] ?? 12))); |
| 98 |
|
| 99 |
$args = [ |
| 100 |
'token' => $auth['token'], |
| 101 |
'searchBar' => $search, |
| 102 |
'CurrentPage' => $page, |
| 103 |
'ParPage' => $per_page, |
| 104 |
]; |
| 105 |
|
| 106 |
if ($builder !== '') { |
| 107 |
$args['builder'] = $builder; |
| 108 |
} |
| 109 |
|
| 110 |
// The cloud reads any non-empty 'visibility' as "private rows only". |
| 111 |
if ($private_only) { |
| 112 |
$args['visibility'] = 'private'; |
| 113 |
} |
| 114 |
|
| 115 |
$cloud = wdesignkit_mcp_template_cloud_call('widget/mywidgets', $args, 'form'); |
| 116 |
|
| 117 |
if (empty($cloud['success'])) { |
| 118 |
return [ |
| 119 |
'success' => false, |
| 120 |
'message' => $cloud['message'] ?? $cloud['massage'] ?? 'Failed to load your cloud widgets.', |
| 121 |
'response' => $cloud, |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
// Trim the cloud rows to the fields a caller acts on — the raw payload carries the |
| 126 |
// full widget record (serialised terms, licence blobs, responsive image sets) and |
| 127 |
// would bloat the MCP response for a list of dozens of widgets. |
| 128 |
$rows = $cloud['data']['widgets'] ?? []; |
| 129 |
$widgets = []; |
| 130 |
|
| 131 |
foreach ((is_array($rows) ? $rows : []) as $row) { |
| 132 |
if (!is_array($row)) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
$widgets[] = [ |
| 137 |
'id' => (string) ($row['id'] ?? ''), |
| 138 |
'title' => (string) ($row['title'] ?? ''), |
| 139 |
'builder' => (string) ($row['builderName']['plugin_name'] ?? $row['builder'] ?? ''), |
| 140 |
'status' => (string) ($row['status'] ?? ''), |
| 141 |
'free_pro' => (string) ($row['free_pro'] ?? ''), |
| 142 |
'image' => (string) ($row['post_imageurl'] ?? ''), |
| 143 |
'created_at' => (string) ($row['created_at'] ?? ''), |
| 144 |
]; |
| 145 |
} |
| 146 |
|
| 147 |
$count = (int) ($cloud['data']['widgetscount'] ?? count($widgets)); |
| 148 |
|
| 149 |
return [ |
| 150 |
'success' => true, |
| 151 |
'message' => $count > 0 |
| 152 |
? "Found {$count} widget(s) in your WDesignKit cloud account." |
| 153 |
: 'No widgets found in your WDesignKit cloud account. Push one with wdesignkit/push-widget first.', |
| 154 |
'count' => $count, |
| 155 |
'widgets' => $widgets, |
| 156 |
'response' => $cloud, |
| 157 |
]; |
| 158 |
} |
| 159 |
|