| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\MCP\Tools; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
use FluentForm\App\Modules\MCP\Support\FormAccess; |
| 8 |
use FluentForm\App\Modules\MCP\Support\MCPHelper; |
| 9 |
use FluentForm\App\Services\Integrations\FormIntegrationService; |
| 10 |
use FluentForm\Framework\Support\Arr; |
| 11 |
|
| 12 |
/** |
| 13 |
* Integration tools (read). |
| 14 |
* |
| 15 |
* The list-integrations tool reports the integration feeds configured on one |
| 16 |
* form (provider, name, enabled) so an agent can answer "where do this form's |
| 17 |
* entries go?". It reuses FormIntegrationService::get(), the same source the admin |
| 18 |
* integrations screen reads, and is form-scoped. |
| 19 |
*/ |
| 20 |
class IntegrationTools |
| 21 |
{ |
| 22 |
public static function definitions() |
| 23 |
{ |
| 24 |
return [ |
| 25 |
'fluentform/list-integrations' => [ |
| 26 |
'label' => __('List Integrations', 'fluentform'), |
| 27 |
'group' => __('Integrations', 'fluentform'), |
| 28 |
'description' => __('List the integration feeds configured on one form (provider, name, enabled state) — the notifications and third-party connections that fire when the form is submitted. Requires form_id.', 'fluentform'), |
| 29 |
'input_schema' => [ |
| 30 |
'type' => 'object', |
| 31 |
'properties' => [ |
| 32 |
'form_id' => ['type' => 'integer'], |
| 33 |
], |
| 34 |
'required' => ['form_id'], |
| 35 |
], |
| 36 |
'execute_callback' => [self::class, 'listIntegrations'], |
| 37 |
'capability' => ['fluentform_forms_manager', 'fluentform_settings_manager', 'fluentform_dashboard_access'], |
| 38 |
'annotations' => ['readonly' => true], |
| 39 |
], |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
public static function listIntegrations($params = []) |
| 44 |
{ |
| 45 |
$form = FormAccess::resolveForm($params); |
| 46 |
if (is_wp_error($form)) { |
| 47 |
return $form; |
| 48 |
} |
| 49 |
$formId = (int) $form->id; |
| 50 |
|
| 51 |
// FormIntegrationService::get() returns ['feeds' => [...], ...]; the |
| 52 |
// configured feeds live under the 'feeds' key, not at the top level. |
| 53 |
$result = (new FormIntegrationService())->get($formId); |
| 54 |
$feeds = (isset($result['feeds']) && is_array($result['feeds'])) ? $result['feeds'] : []; |
| 55 |
|
| 56 |
$rows = []; |
| 57 |
foreach ($feeds as $feed) { |
| 58 |
if (!is_array($feed)) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
$rows[] = [ |
| 62 |
'id' => isset($feed['id']) ? (int) $feed['id'] : null, |
| 63 |
'name' => Arr::get($feed, 'name'), |
| 64 |
'provider' => Arr::get($feed, 'provider'), |
| 65 |
'enabled' => !empty($feed['enabled']), |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
return MCPHelper::envelope( |
| 70 |
sprintf( |
| 71 |
/* translators: %d: number of integration feeds */ |
| 72 |
_n('%d integration feed configured.', '%d integration feeds configured.', count($rows), 'fluentform'), |
| 73 |
count($rows) |
| 74 |
), |
| 75 |
['form_id' => $formId, 'integrations' => $rows] |
| 76 |
); |
| 77 |
} |
| 78 |
} |
| 79 |
|