Batch24: Gravity Forms tools (guarded, net-new)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q8t9GEyyLZbUQu26na1JQg
This commit is contained in:
2026-10-16 04:36:12 +00:00
parent a788538b3f
commit 1985793f35
+79
View File
@@ -0,0 +1,79 @@
<?php
/**
* Batch 24 — Gravity Forms tools via the public GFAPI. Net-new, GPL-2.0-or-later. See NOTICE.md.
* Inert unless Gravity Forms (class GFAPI) is active.
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch24 {
public static function register($registry): void {
if (!class_exists('GFAPI')) 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\\Batch24::' . $method, 'inputSchema' => $schema];
};
$registry->register($def('gf_detect', 'Report whether Gravity Forms is active and its version.', [], [], 'read', 'detect'));
$registry->register($def('gf_list_forms', 'List Gravity Forms (id, title, active, entry count).', [], [], 'read', 'listForms'));
$registry->register($def('gf_get_form', 'Get one Gravity Form: title plus its fields (id, label, type, required).',
['form_id' => ['type' => 'integer']], ['form_id'], 'read', 'getForm'));
$registry->register($def('gf_list_entries', 'List entries for a Gravity Form (newest first). Returns id, date, and a compact field map.',
['form_id' => ['type' => 'integer'], 'per_page' => ['type' => 'integer', 'default' => 20]], ['form_id'], 'read', 'listEntries'));
$registry->register($def('gf_get_entry', 'Get a single Gravity Forms entry by id.',
['entry_id' => ['type' => 'integer']], ['entry_id'], 'read', 'getEntry'));
}
public static function detect(array $args): array {
$v = class_exists('GFForms') && isset(\GFForms::$version) ? \GFForms::$version : (defined('GF_MIN_WP_VERSION') ? 'active' : null);
return ['active' => class_exists('GFAPI'), 'version' => $v];
}
public static function listForms(array $args): array {
$forms = \GFAPI::get_forms();
$out = [];
foreach ((array) $forms as $f) {
$count = method_exists('GFAPI', 'count_entries') ? \GFAPI::count_entries($f['id']) : null;
$out[] = ['id' => $f['id'], 'title' => $f['title'] ?? '', 'is_active' => !empty($f['is_active']), 'entries' => $count];
}
return ['count' => count($out), 'forms' => $out];
}
public static function getForm(array $args): array {
$id = (int) ($args['form_id'] ?? 0);
$f = \GFAPI::get_form($id);
if (empty($f)) throw new \RuntimeException("Gravity form not found: {$id}");
$fields = [];
foreach ((array) ($f['fields'] ?? []) as $fld) {
$fields[] = ['id' => $fld->id ?? null, 'label' => $fld->label ?? '', 'type' => $fld->type ?? '', 'required' => !empty($fld->isRequired)];
}
return ['id' => $id, 'title' => $f['title'] ?? '', 'is_active' => !empty($f['is_active']), 'fields' => $fields];
}
public static function listEntries(array $args): array {
$fid = (int) ($args['form_id'] ?? 0);
$per = max(1, (int) ($args['per_page'] ?? 20));
$entries = \GFAPI::get_entries($fid, [], ['key' => 'date_created', 'direction' => 'DESC'], ['offset' => 0, 'page_size' => $per]);
if (is_wp_error($entries)) throw new \RuntimeException($entries->get_error_message());
$out = [];
foreach ((array) $entries as $e) {
$vals = [];
foreach ($e as $k => $v) { if (is_numeric($k) && $v !== '') $vals[$k] = $v; }
$out[] = ['id' => $e['id'] ?? null, 'date_created' => $e['date_created'] ?? '', 'values' => $vals];
}
return ['form_id' => $fid, 'count' => count($out), 'entries' => $out];
}
public static function getEntry(array $args): array {
$id = (int) ($args['entry_id'] ?? 0);
$e = \GFAPI::get_entry($id);
if (is_wp_error($e)) throw new \RuntimeException($e->get_error_message());
return ['entry' => $e];
}
}