72ac246630
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q8t9GEyyLZbUQu26na1JQg
79 lines
4.8 KiB
PHP
79 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* Batch 37 — MailPoet tools (reads the plugin's own tables via $wpdb).
|
|
* Net-new, GPL-2.0-or-later. See NOTICE.md.
|
|
* Inert unless MailPoet is active. NOTE: MailPoet does not define MAILPOET_VERSION
|
|
* as a plain constant at load; MAILPOET_MINIMUM_REQUIRED_WP_VERSION is defined whenever
|
|
* mailpoet.php loads, so we gate on either (verified live on mainwp 2026-10-16).
|
|
*/
|
|
|
|
namespace AskTamMCP\Extensions;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class Batch37 {
|
|
|
|
public static function register($registry): void {
|
|
if (!defined('MAILPOET_VERSION') && !defined('MAILPOET_MINIMUM_REQUIRED_WP_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\\Batch37::' . $method, 'inputSchema' => $schema];
|
|
};
|
|
|
|
$registry->register($def('mailpoet_detect', 'Report whether MailPoet is active and its version.', [], [], 'read', 'detect'));
|
|
$registry->register($def('mailpoet_list_lists', 'List MailPoet lists/segments (id, name, type, subscriber count).', [], [], 'read', 'listLists'));
|
|
$registry->register($def('mailpoet_list_subscribers', 'List MailPoet subscribers (newest first): id, email, status. Optionally filter by status.',
|
|
['status' => ['type' => 'string'], 'per_page' => ['type' => 'integer', 'default' => 50]], [], 'read', 'listSubscribers'));
|
|
$registry->register($def('mailpoet_list_newsletters', 'List MailPoet newsletters (id, subject, type, status).',
|
|
['per_page' => ['type' => 'integer', 'default' => 50]], [], 'read', 'listNewsletters'));
|
|
}
|
|
|
|
public static function detect(array $args): array {
|
|
$active = defined('MAILPOET_VERSION') || defined('MAILPOET_MINIMUM_REQUIRED_WP_VERSION');
|
|
return ['active' => $active, 'version' => defined('MAILPOET_VERSION') ? MAILPOET_VERSION : null];
|
|
}
|
|
|
|
private static function tbl(string $n): string { global $wpdb; return $wpdb->prefix . $n; }
|
|
private static function has(string $t): bool { global $wpdb; return $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $t)) === $t; }
|
|
|
|
public static function listLists(array $args): array {
|
|
global $wpdb;
|
|
$t = self::tbl('mailpoet_segments'); $sub = self::tbl('mailpoet_subscriber_segment');
|
|
if (!self::has($t)) return ['supported' => false, 'note' => 'mailpoet_segments table not found.', 'lists' => []];
|
|
$rows = $wpdb->get_results("SELECT id, name, type FROM {$t} WHERE deleted_at IS NULL ORDER BY id ASC LIMIT 200", ARRAY_A);
|
|
$out = [];
|
|
foreach ((array) $rows as $r) {
|
|
$c = self::has($sub) ? (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$sub} WHERE segment_id = %d AND status = 'subscribed'", $r['id'])) : null;
|
|
$out[] = ['id' => (int) $r['id'], 'name' => $r['name'], 'type' => $r['type'], 'subscribers' => $c];
|
|
}
|
|
return ['supported' => true, 'count' => count($out), 'lists' => $out];
|
|
}
|
|
|
|
public static function listSubscribers(array $args): array {
|
|
global $wpdb;
|
|
$t = self::tbl('mailpoet_subscribers');
|
|
if (!self::has($t)) return ['supported' => false, 'note' => 'mailpoet_subscribers table not found.', 'subscribers' => []];
|
|
$per = max(1, (int) ($args['per_page'] ?? 50));
|
|
$where = 'WHERE deleted_at IS NULL';
|
|
if (!empty($args['status'])) $where .= $wpdb->prepare(' AND status = %s', (string) $args['status']);
|
|
$rows = $wpdb->get_results("SELECT id, email, status, first_name, last_name FROM {$t} {$where} ORDER BY id DESC LIMIT {$per}", ARRAY_A);
|
|
$out = [];
|
|
foreach ((array) $rows as $r) $out[] = ['id' => (int) $r['id'], 'email' => $r['email'], 'status' => $r['status'], 'name' => trim(($r['first_name'] ?? '') . ' ' . ($r['last_name'] ?? ''))];
|
|
return ['supported' => true, 'count' => count($out), 'subscribers' => $out];
|
|
}
|
|
|
|
public static function listNewsletters(array $args): array {
|
|
global $wpdb;
|
|
$t = self::tbl('mailpoet_newsletters');
|
|
if (!self::has($t)) return ['supported' => false, 'note' => 'mailpoet_newsletters table not found.', 'newsletters' => []];
|
|
$per = max(1, (int) ($args['per_page'] ?? 50));
|
|
$rows = $wpdb->get_results("SELECT id, subject, type, status FROM {$t} WHERE deleted_at IS NULL ORDER BY id DESC LIMIT {$per}", ARRAY_A);
|
|
$out = [];
|
|
foreach ((array) $rows as $r) $out[] = ['id' => (int) $r['id'], 'subject' => $r['subject'], 'type' => $r['type'], 'status' => $r['status']];
|
|
return ['supported' => true, 'count' => count($out), 'newsletters' => $out];
|
|
}
|
|
}
|