c3bc965ee8
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q8t9GEyyLZbUQu26na1JQg
69 lines
3.5 KiB
PHP
69 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Batch 42 — TablePress tools. Net-new, GPL-2.0-or-later. See NOTICE.md.
|
|
* Inert unless TablePress (class TablePress) is active. Defensive about storage (CPT v2 or option v1).
|
|
*/
|
|
|
|
namespace AskTamMCP\Extensions;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class Batch42 {
|
|
|
|
public static function register($registry): void {
|
|
if (!class_exists('TablePress')) 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\\Batch42::' . $method, 'inputSchema' => $schema];
|
|
};
|
|
|
|
$registry->register($def('tablepress_detect', 'Report whether TablePress is active and its version.', [], [], 'read', 'detect'));
|
|
$registry->register($def('tablepress_list_tables', 'List TablePress tables (id, name, rows x columns).',
|
|
['per_page' => ['type' => 'integer', 'default' => 100]], [], 'read', 'listTables'));
|
|
$registry->register($def('tablepress_get_table', 'Get one TablePress table: name, description, and its cell data.',
|
|
['table_id' => ['type' => 'string']], ['table_id'], 'read', 'getTable'));
|
|
}
|
|
|
|
public static function detect(array $args): array {
|
|
$v = defined('TablePress::version') ? \TablePress::version : (class_exists('TablePress') && isset(\TablePress::$version) ? \TablePress::$version : null);
|
|
return ['active' => class_exists('TablePress'), 'version' => $v];
|
|
}
|
|
|
|
private static function load($id) {
|
|
if (!class_exists('TablePress') || !method_exists('TablePress', 'load_controller')) return null;
|
|
$ctrl = \TablePress::load_controller('frontend');
|
|
if (isset(\TablePress::$model_table) && is_object(\TablePress::$model_table)) return \TablePress::$model_table;
|
|
return null;
|
|
}
|
|
|
|
public static function listTables(array $args): array {
|
|
$model = self::load(null);
|
|
if (!$model || !method_exists($model, 'load_all') || !method_exists($model, 'load')) {
|
|
return ['supported' => false, 'note' => 'TablePress model unavailable.', 'tables' => []];
|
|
}
|
|
$ids = $model->load_all(false);
|
|
$per = max(1, (int) ($args['per_page'] ?? 100));
|
|
$out = [];
|
|
foreach (array_slice((array) $ids, 0, $per) as $tid) {
|
|
$t = $model->load($tid, false, false);
|
|
if (!is_array($t)) continue;
|
|
$rows = isset($t['data']) && is_array($t['data']) ? count($t['data']) : 0;
|
|
$cols = $rows && isset($t['data'][0]) && is_array($t['data'][0]) ? count($t['data'][0]) : 0;
|
|
$out[] = ['id' => $tid, 'name' => $t['name'] ?? '', 'rows' => $rows, 'columns' => $cols];
|
|
}
|
|
return ['supported' => true, 'count' => count($out), 'tables' => $out];
|
|
}
|
|
|
|
public static function getTable(array $args): array {
|
|
$model = self::load(null);
|
|
$id = (string) ($args['table_id'] ?? '');
|
|
if (!$model || !method_exists($model, 'load')) throw new \RuntimeException('TablePress model unavailable.');
|
|
$t = $model->load($id, true, true);
|
|
if (!is_array($t)) throw new \RuntimeException("TablePress table not found: {$id}");
|
|
return ['id' => $id, 'name' => $t['name'] ?? '', 'description' => $t['description'] ?? '', 'data' => $t['data'] ?? []];
|
|
}
|
|
}
|