Files

65 lines
3.2 KiB
PHP

<?php
/**
* Batch 43 — Advanced Custom Fields tools via the public acf_* API. Net-new, GPL-2.0-or-later. See NOTICE.md.
* Inert unless ACF (class ACF) is active.
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch43 {
public static function register($registry): void {
if (!class_exists('ACF')) 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\\Batch43::' . $method, 'inputSchema' => $schema];
};
$registry->register($def('acf_detect', 'Report whether Advanced Custom Fields is active and its version.', [], [], 'read', 'detect'));
$registry->register($def('acf_list_field_groups', 'List ACF field groups (key, title, active).', [], [], 'read', 'listGroups'));
$registry->register($def('acf_get_group_fields', 'List the fields of one ACF field group (name, label, type).',
['group_key' => ['type' => 'string']], ['group_key'], 'read', 'getGroupFields'));
$registry->register($def('acf_get_field_value', 'Read one ACF field value for a given post. field = field name or key.',
['post_id' => ['type' => 'integer'], 'field' => ['type' => 'string']], ['post_id', 'field'], 'read', 'getFieldValue'));
}
public static function detect(array $args): array {
$v = function_exists('acf_get_setting') ? acf_get_setting('version') : (defined('ACF_VERSION') ? ACF_VERSION : null);
return ['active' => class_exists('ACF'), 'version' => $v];
}
public static function listGroups(array $args): array {
if (!function_exists('acf_get_field_groups')) return ['supported' => false, 'note' => 'acf_get_field_groups unavailable.', 'groups' => []];
$groups = acf_get_field_groups();
$out = [];
foreach ((array) $groups as $g) {
$out[] = ['key' => $g['key'] ?? '', 'title' => $g['title'] ?? '', 'active' => !empty($g['active'])];
}
return ['count' => count($out), 'groups' => $out];
}
public static function getGroupFields(array $args): array {
if (!function_exists('acf_get_fields')) throw new \RuntimeException('acf_get_fields unavailable.');
$key = (string) ($args['group_key'] ?? '');
$fields = acf_get_fields($key);
$out = [];
foreach ((array) $fields as $f) {
$out[] = ['name' => $f['name'] ?? '', 'label' => $f['label'] ?? '', 'type' => $f['type'] ?? '', 'key' => $f['key'] ?? ''];
}
return ['group_key' => $key, 'count' => count($out), 'fields' => $out];
}
public static function getFieldValue(array $args): array {
if (!function_exists('get_field')) throw new \RuntimeException('get_field unavailable.');
$id = (int) ($args['post_id'] ?? 0);
$field = (string) ($args['field'] ?? '');
$val = get_field($field, $id);
return ['post_id' => $id, 'field' => $field, 'value' => $val];
}
}