Batch40: Polylang tools (guarded, net-new)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q8t9GEyyLZbUQu26na1JQg
This commit is contained in:
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Batch 40 — Polylang tools via its public template functions. Net-new, GPL-2.0-or-later. See NOTICE.md.
|
||||||
|
* Inert unless Polylang (POLYLANG_VERSION) is active.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace AskTamMCP\Extensions;
|
||||||
|
|
||||||
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
|
class Batch40 {
|
||||||
|
|
||||||
|
public static function register($registry): void {
|
||||||
|
if (!defined('POLYLANG_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\\Batch40::' . $method, 'inputSchema' => $schema];
|
||||||
|
};
|
||||||
|
|
||||||
|
$registry->register($def('polylang_detect', 'Report whether Polylang is active, its version, and the default language.', [], [], 'read', 'detect'));
|
||||||
|
$registry->register($def('polylang_list_languages', 'List Polylang languages (slug) and the default.', [], [], 'read', 'listLanguages'));
|
||||||
|
$registry->register($def('polylang_get_post_language', 'Get the language slug of a post.',
|
||||||
|
['post_id' => ['type' => 'integer']], ['post_id'], 'read', 'getPostLanguage'));
|
||||||
|
$registry->register($def('polylang_get_translations', 'Get the translation post ids of a post, keyed by language slug.',
|
||||||
|
['post_id' => ['type' => 'integer']], ['post_id'], 'read', 'getTranslations'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function detect(array $args): array {
|
||||||
|
return ['active' => defined('POLYLANG_VERSION'), 'version' => defined('POLYLANG_VERSION') ? POLYLANG_VERSION : null,
|
||||||
|
'default' => function_exists('pll_default_language') ? pll_default_language() : null];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function listLanguages(array $args): array {
|
||||||
|
$langs = function_exists('pll_languages_list') ? pll_languages_list() : [];
|
||||||
|
return ['count' => count((array) $langs), 'languages' => array_values((array) $langs),
|
||||||
|
'default' => function_exists('pll_default_language') ? pll_default_language() : null];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPostLanguage(array $args): array {
|
||||||
|
$id = (int) ($args['post_id'] ?? 0);
|
||||||
|
$lang = function_exists('pll_get_post_language') ? pll_get_post_language($id) : null;
|
||||||
|
return ['post_id' => $id, 'language' => $lang];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getTranslations(array $args): array {
|
||||||
|
$id = (int) ($args['post_id'] ?? 0);
|
||||||
|
$tr = function_exists('pll_get_post_translations') ? pll_get_post_translations($id) : [];
|
||||||
|
return ['post_id' => $id, 'translations' => (array) $tr];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user