Files
advanced-mcp-plugin/includes/Extensions/Batch26.php
T

67 lines
3.5 KiB
PHP

<?php
/**
* Batch 26 — WPForms tools. Net-new, GPL-2.0-or-later. See NOTICE.md.
* Inert unless WPForms (WPFORMS_VERSION) is active. Entries require WPForms Pro (wpforms_entries table).
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch26 {
public static function register($registry): void {
if (!defined('WPFORMS_VERSION')) 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\\Batch26::' . $method, 'inputSchema' => $schema];
};
$registry->register($def('wpforms_detect', 'Report whether WPForms is active and its version.', [], [], 'read', 'detect'));
$registry->register($def('wpforms_list_forms', 'List WPForms forms (id, title).',
['per_page' => ['type' => 'integer', 'default' => 100]], [], 'read', 'listForms'));
$registry->register($def('wpforms_get_form', 'Get one WPForms form: title plus the number of fields in its config.',
['form_id' => ['type' => 'integer']], ['form_id'], 'read', 'getForm'));
$registry->register($def('wpforms_list_entries', 'List entries for a WPForms form (newest first). Requires WPForms Pro.',
['form_id' => ['type' => 'integer'], 'per_page' => ['type' => 'integer', 'default' => 25]], ['form_id'], 'read', 'listEntries'));
}
public static function detect(array $args): array {
return ['active' => defined('WPFORMS_VERSION'), 'version' => defined('WPFORMS_VERSION') ? WPFORMS_VERSION : null];
}
public static function listForms(array $args): array {
$posts = get_posts(['post_type' => 'wpforms', 'post_status' => 'publish',
'numberposts' => (int) ($args['per_page'] ?? 100), 'orderby' => 'title', 'order' => 'ASC']);
$out = [];
foreach ($posts as $p) $out[] = ['id' => $p->ID, 'title' => $p->post_title];
return ['count' => count($out), 'forms' => $out];
}
public static function getForm(array $args): array {
$id = (int) ($args['form_id'] ?? 0);
$p = get_post($id);
if (!$p || $p->post_type !== 'wpforms') throw new \RuntimeException("WPForms form not found: {$id}");
$cfg = json_decode((string) $p->post_content, true);
$n = (is_array($cfg) && isset($cfg['fields']) && is_array($cfg['fields'])) ? count($cfg['fields']) : 0;
return ['id' => $id, 'title' => $p->post_title, 'field_count' => $n];
}
public static function listEntries(array $args): array {
global $wpdb;
$t = $wpdb->prefix . 'wpforms_entries';
if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $t)) !== $t) {
return ['supported' => false, 'note' => 'Entry storage requires WPForms Pro.', 'entries' => []];
}
$fid = (int) ($args['form_id'] ?? 0);
$per = max(1, (int) ($args['per_page'] ?? 25));
$rows = $wpdb->get_results($wpdb->prepare("SELECT entry_id, form_id, status, date FROM {$t} WHERE form_id = %d ORDER BY entry_id DESC LIMIT %d", $fid, $per), ARRAY_A);
$out = [];
foreach ((array) $rows as $r) $out[] = ['id' => (int) $r['entry_id'], 'status' => $r['status'], 'date' => $r['date']];
return ['supported' => true, 'form_id' => $fid, 'count' => count($out), 'entries' => $out];
}
}