Files

68 lines
3.4 KiB
PHP

<?php
/**
* Batch 27 — Ninja Forms tools via the plugin's model API. Net-new, GPL-2.0-or-later. See NOTICE.md.
* Inert unless Ninja Forms (class Ninja_Forms) is active. Submissions are the nf_sub post type.
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch27 {
public static function register($registry): void {
if (!class_exists('Ninja_Forms')) 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\\Batch27::' . $method, 'inputSchema' => $schema];
};
$registry->register($def('nf_detect', 'Report whether Ninja Forms is active and its version.', [], [], 'read', 'detect'));
$registry->register($def('nf_list_forms', 'List Ninja Forms (id, title).', [], [], 'read', 'listForms'));
$registry->register($def('nf_get_form', 'Get one Ninja Form: title plus its fields (id, label, type).',
['form_id' => ['type' => 'integer']], ['form_id'], 'read', 'getForm'));
$registry->register($def('nf_list_subs', 'List Ninja Forms submissions for a form (newest first): id, date.',
['form_id' => ['type' => 'integer'], 'per_page' => ['type' => 'integer', 'default' => 25]], ['form_id'], 'read', 'listSubs'));
}
public static function detect(array $args): array {
return ['active' => class_exists('Ninja_Forms'), 'version' => defined('NF_PLUGIN_VERSION') ? NF_PLUGIN_VERSION : null];
}
public static function listForms(array $args): array {
$out = [];
$forms = \Ninja_Forms()->form()->get_forms();
foreach ((array) $forms as $f) {
if (!is_object($f) || !method_exists($f, 'get_id')) continue;
$out[] = ['id' => $f->get_id(), 'title' => method_exists($f, 'get_setting') ? (string) $f->get_setting('title') : ''];
}
return ['count' => count($out), 'forms' => $out];
}
public static function getForm(array $args): array {
$id = (int) ($args['form_id'] ?? 0);
$model = \Ninja_Forms()->form($id);
$fields = [];
foreach ((array) $model->get_fields() as $fld) {
if (!is_object($fld) || !method_exists($fld, 'get_id')) continue;
$fields[] = ['id' => $fld->get_id(), 'label' => (string) $fld->get_setting('label'), 'type' => (string) $fld->get_setting('type')];
}
$form = $model->get_form();
$title = (is_object($form) && method_exists($form, 'get_setting')) ? (string) $form->get_setting('title') : '';
return ['id' => $id, 'title' => $title, 'fields' => $fields];
}
public static function listSubs(array $args): array {
$fid = (int) ($args['form_id'] ?? 0);
$posts = get_posts(['post_type' => 'nf_sub', 'post_status' => 'any',
'numberposts' => (int) ($args['per_page'] ?? 25), 'orderby' => 'date', 'order' => 'DESC',
'meta_key' => '_form_id', 'meta_value' => $fid]);
$out = [];
foreach ($posts as $p) $out[] = ['id' => $p->ID, 'date' => $p->post_date];
return ['form_id' => $fid, 'count' => count($out), 'submissions' => $out];
}
}