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

74 lines
3.8 KiB
PHP

<?php
/**
* Batch 23 — Contact Form 7 (wpcf7) tools. Net-new, GPL-2.0-or-later. See NOTICE.md.
* Inert unless CF7 (WPCF7_VERSION) is active. Submissions require Flamingo.
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch23 {
public static function register($registry): void {
if (!defined('WPCF7_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\\Batch23::' . $method, 'inputSchema' => $schema];
};
$registry->register($def('cf7_detect', 'Report whether Contact Form 7 is active and its version.', [], [], 'read', 'detect'));
$registry->register($def('cf7_list_forms', 'List Contact Form 7 forms (id, title, slug).',
['per_page' => ['type' => 'integer', 'default' => 100]], [], 'read', 'listForms'));
$registry->register($def('cf7_get_form', 'Get one Contact Form 7 form: title plus its form template markup.',
['form_id' => ['type' => 'integer']], ['form_id'], 'read', 'getForm'));
$registry->register($def('cf7_list_submissions', 'List stored CF7 submissions. Requires the Flamingo plugin (CF7 does not store submissions on its own).',
['per_page' => ['type' => 'integer', 'default' => 50]], [], 'read', 'listSubmissions'));
}
public static function detect(array $args): array {
return ['active' => defined('WPCF7_VERSION'), 'version' => defined('WPCF7_VERSION') ? WPCF7_VERSION : null];
}
public static function listForms(array $args): array {
$posts = get_posts(['post_type' => 'wpcf7_contact_form', '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, 'slug' => $p->post_name];
return ['count' => count($out), 'forms' => $out];
}
public static function getForm(array $args): array {
$id = (int) ($args['form_id'] ?? 0);
$tpl = '';
if (class_exists('WPCF7_ContactForm')) {
$cf = \WPCF7_ContactForm::get_instance($id);
if ($cf) { $tpl = (string) $cf->prop('form'); $title = $cf->title(); }
}
if (empty($title)) {
$p = get_post($id);
if (!$p || $p->post_type !== 'wpcf7_contact_form') throw new \RuntimeException("CF7 form not found: {$id}");
$title = $p->post_title;
if ($tpl === '') $tpl = (string) get_post_meta($id, '_form', true);
}
return ['id' => $id, 'title' => $title, 'template' => $tpl];
}
public static function listSubmissions(array $args): array {
if (!post_type_exists('flamingo_inbound')) {
return ['supported' => false, 'note' => 'CF7 stores no submissions by default. Install the Flamingo plugin to capture them.', 'submissions' => []];
}
$posts = get_posts(['post_type' => 'flamingo_inbound', 'post_status' => 'any',
'numberposts' => (int) ($args['per_page'] ?? 50), 'orderby' => 'date', 'order' => 'DESC']);
$out = [];
foreach ($posts as $p) {
$out[] = ['id' => $p->ID, 'subject' => $p->post_title, 'from' => get_post_meta($p->ID, '_from', true),
'date' => $p->post_date, 'channel' => wp_get_post_terms($p->ID, 'flamingo_inbound_channel', ['fields' => 'names'])];
}
return ['supported' => true, 'count' => count($out), 'submissions' => $out];
}
}