diff --git a/includes/Extensions/Batch37.php b/includes/Extensions/Batch37.php new file mode 100644 index 0000000..2878d77 --- /dev/null +++ b/includes/Extensions/Batch37.php @@ -0,0 +1,74 @@ + '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 { + return ['active' => defined('MAILPOET_VERSION'), '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]; + } +}