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

74 lines
4.1 KiB
PHP

<?php
/**
* Batch 44 — Rank Math SEO tools (post-meta based). Net-new, GPL-2.0-or-later. See NOTICE.md.
* Inert unless Rank Math (RANK_MATH_VERSION) is active.
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch44 {
public static function register($registry): void {
if (!defined('RANK_MATH_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\\Batch44::' . $method, 'inputSchema' => $schema];
};
$registry->register($def('rankmath_detect', 'Report whether Rank Math is active and its version.', [], [], 'read', 'detect'));
$registry->register($def('rankmath_get_meta', 'Get Rank Math SEO meta for a post: title, description, focus keyword, canonical, robots.',
['post_id' => ['type' => 'integer']], ['post_id'], 'read', 'getMeta'));
$registry->register($def('rankmath_update_meta', 'Update Rank Math SEO meta for a post. Any of: title, description, focus_keyword, canonical.',
['post_id' => ['type' => 'integer'], 'title' => ['type' => 'string'], 'description' => ['type' => 'string'],
'focus_keyword' => ['type' => 'string'], 'canonical' => ['type' => 'string']], ['post_id'], 'write', 'updateMeta'));
$registry->register($def('rankmath_bulk_audit', 'Audit recent posts for missing Rank Math title/description/focus keyword.',
['post_type' => ['type' => 'string', 'default' => 'post'], 'per_page' => ['type' => 'integer', 'default' => 50]], [], 'read', 'bulkAudit'));
}
public static function detect(array $args): array {
return ['active' => defined('RANK_MATH_VERSION'), 'version' => defined('RANK_MATH_VERSION') ? RANK_MATH_VERSION : null];
}
public static function getMeta(array $args): array {
$id = (int) ($args['post_id'] ?? 0);
if (!get_post($id)) throw new \RuntimeException("Post not found: {$id}");
return ['post_id' => $id,
'title' => get_post_meta($id, 'rank_math_title', true),
'description' => get_post_meta($id, 'rank_math_description', true),
'focus_keyword' => get_post_meta($id, 'rank_math_focus_keyword', true),
'canonical' => get_post_meta($id, 'rank_math_canonical_url', true),
'robots' => get_post_meta($id, 'rank_math_robots', true)];
}
public static function updateMeta(array $args): array {
$id = (int) ($args['post_id'] ?? 0);
if (!get_post($id)) throw new \RuntimeException("Post not found: {$id}");
$map = ['title' => 'rank_math_title', 'description' => 'rank_math_description',
'focus_keyword' => 'rank_math_focus_keyword', 'canonical' => 'rank_math_canonical_url'];
$updated = [];
foreach ($map as $arg => $meta) {
if (array_key_exists($arg, $args)) { update_post_meta($id, $meta, (string) $args[$arg]); $updated[] = $arg; }
}
return ['post_id' => $id, 'updated' => $updated];
}
public static function bulkAudit(array $args): array {
$pt = (string) ($args['post_type'] ?? 'post');
$posts = get_posts(['post_type' => $pt, 'post_status' => 'publish', 'numberposts' => (int) ($args['per_page'] ?? 50)]);
$issues = [];
foreach ($posts as $p) {
$miss = [];
if (!get_post_meta($p->ID, 'rank_math_title', true)) $miss[] = 'title';
if (!get_post_meta($p->ID, 'rank_math_description', true)) $miss[] = 'description';
if (!get_post_meta($p->ID, 'rank_math_focus_keyword', true)) $miss[] = 'focus_keyword';
if ($miss) $issues[] = ['id' => $p->ID, 'title' => $p->post_title, 'missing' => $miss];
}
return ['post_type' => $pt, 'checked' => count($posts), 'offenders' => count($issues), 'issues' => $issues];
}
}