799331e6a1
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q8t9GEyyLZbUQu26na1JQg
78 lines
3.7 KiB
PHP
78 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* Batch 29 — Forminator tools via Forminator_API. Net-new, GPL-2.0-or-later. See NOTICE.md.
|
|
* Inert unless Forminator (class Forminator_API) is active.
|
|
*/
|
|
|
|
namespace AskTamMCP\Extensions;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class Batch29 {
|
|
|
|
public static function register($registry): void {
|
|
if (!class_exists('Forminator_API')) return;
|
|
|
|
$def = function (string $name, string $desc, array $props, array $required, string $scope, string $method) {
|
|
$schema = ['type' => 'object', 'properties' => $props ?: new \stdClass()];
|
|
if ($required) $schema['required'] = $required;
|
|
return ['name' => $name, 'description' => $desc, 'scope' => $scope,
|
|
'handler' => 'AskTamMCP\\Extensions\\Batch29::' . $method, 'inputSchema' => $schema];
|
|
};
|
|
|
|
$registry->register($def('forminator_detect', 'Report whether Forminator is active and its version.', [], [], 'read', 'detect'));
|
|
$registry->register($def('forminator_list_forms', 'List Forminator forms (id, name).',
|
|
['per_page' => ['type' => 'integer', 'default' => 100]], [], 'read', 'listForms'));
|
|
$registry->register($def('forminator_get_form', 'Get one Forminator form: name plus field count.',
|
|
['form_id' => ['type' => 'integer']], ['form_id'], 'read', 'getForm'));
|
|
$registry->register($def('forminator_list_entries', 'Count and list recent entries for a Forminator form (id, date).',
|
|
['form_id' => ['type' => 'integer'], 'per_page' => ['type' => 'integer', 'default' => 25]], ['form_id'], 'read', 'listEntries'));
|
|
}
|
|
|
|
public static function detect(array $args): array {
|
|
return ['active' => class_exists('Forminator_API'), 'version' => defined('FORMINATOR_VERSION') ? FORMINATOR_VERSION : null];
|
|
}
|
|
|
|
private static function formName($f): string {
|
|
if (is_object($f)) {
|
|
if (isset($f->settings['formName'])) return (string) $f->settings['formName'];
|
|
if (method_exists($f, 'get_name')) return (string) $f->get_name();
|
|
if (isset($f->name)) return (string) $f->name;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public static function listForms(array $args): array {
|
|
$per = max(1, (int) ($args['per_page'] ?? 100));
|
|
$forms = \Forminator_API::get_forms(null, 1, $per);
|
|
$out = [];
|
|
foreach ((array) $forms as $f) {
|
|
$id = is_object($f) ? ($f->id ?? null) : null;
|
|
$out[] = ['id' => $id, 'name' => self::formName($f)];
|
|
}
|
|
return ['count' => count($out), 'forms' => $out];
|
|
}
|
|
|
|
public static function getForm(array $args): array {
|
|
$id = (int) ($args['form_id'] ?? 0);
|
|
$f = \Forminator_API::get_form($id);
|
|
if (empty($f) || is_wp_error($f)) throw new \RuntimeException("Forminator form not found: {$id}");
|
|
$fields = is_object($f) && method_exists($f, 'get_fields') ? $f->get_fields() : [];
|
|
return ['id' => $id, 'name' => self::formName($f), 'field_count' => is_array($fields) ? count($fields) : 0];
|
|
}
|
|
|
|
public static function listEntries(array $args): array {
|
|
$fid = (int) ($args['form_id'] ?? 0);
|
|
$per = max(1, (int) ($args['per_page'] ?? 25));
|
|
$total = method_exists('Forminator_API', 'count_entries') ? (int) \Forminator_API::count_entries($fid) : null;
|
|
$out = [];
|
|
if (method_exists('Forminator_API', 'get_entries')) {
|
|
$entries = \Forminator_API::get_entries($fid);
|
|
foreach (array_slice((array) $entries, 0, $per) as $e) {
|
|
$out[] = ['id' => is_object($e) ? ($e->entry_id ?? null) : null, 'date' => is_object($e) ? ($e->date_created_sql ?? '') : ''];
|
|
}
|
|
}
|
|
return ['form_id' => $fid, 'total' => $total, 'count' => count($out), 'entries' => $out];
|
|
}
|
|
}
|