From 671750eb11cb518c0f37839c471ee3f10dc614bb Mon Sep 17 00:00:00 2001 From: colorlink Date: Fri, 16 Oct 2026 04:44:20 +0000 Subject: [PATCH] Batch36: FluentCRM tools (guarded, net-new) Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01Q8t9GEyyLZbUQu26na1JQg --- includes/Extensions/Batch36.php | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 includes/Extensions/Batch36.php diff --git a/includes/Extensions/Batch36.php b/includes/Extensions/Batch36.php new file mode 100644 index 0000000..0f756d7 --- /dev/null +++ b/includes/Extensions/Batch36.php @@ -0,0 +1,85 @@ + 'object', 'properties' => $props ?: new \stdClass()]; + if ($required) $schema['required'] = $required; + return ['name' => $name, 'description' => $desc, 'scope' => $scope, + 'handler' => 'AskTamMCP\\Extensions\\Batch36::' . $method, 'inputSchema' => $schema]; + }; + + $registry->register($def('fluentcrm_detect', 'Report whether FluentCRM is active and its version.', [], [], 'read', 'detect')); + $registry->register($def('fluentcrm_list_lists', 'List FluentCRM lists (id, title, slug, subscriber count).', [], [], 'read', 'listLists')); + $registry->register($def('fluentcrm_list_tags', 'List FluentCRM tags (id, title, slug).', [], [], 'read', 'listTags')); + $registry->register($def('fluentcrm_list_subscribers', 'List FluentCRM subscribers (newest first): id, email, status, name. Optionally filter by status.', + ['status' => ['type' => 'string'], 'per_page' => ['type' => 'integer', 'default' => 50]], [], 'read', 'listSubscribers')); + $registry->register($def('fluentcrm_stats', 'FluentCRM counts: subscribers by status, lists, tags.', [], [], 'read', 'stats')); + } + + public static function detect(array $args): array { + return ['active' => defined('FLUENTCRM'), 'version' => (defined('FLUENTCRM') && is_string(FLUENTCRM)) ? FLUENTCRM : (defined('FLUENTCRM_PLUGIN_VERSION') ? FLUENTCRM_PLUGIN_VERSION : null)]; + } + + private static function tbl(string $name): string { global $wpdb; return $wpdb->prefix . $name; } + 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('fc_lists'); $s = self::tbl('fc_subscriber_pivot'); + if (!self::has($t)) return ['supported' => false, 'note' => 'fc_lists table not found.', 'lists' => []]; + $rows = $wpdb->get_results("SELECT id, title, slug FROM {$t} ORDER BY id ASC LIMIT 200", ARRAY_A); + $out = []; + foreach ((array) $rows as $r) { + $c = self::has($s) ? (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$s} WHERE object_type = 'FluentCrm\\\\App\\\\Models\\\\Lists' AND object_id = %d", $r['id'])) : null; + $out[] = ['id' => (int) $r['id'], 'title' => $r['title'], 'slug' => $r['slug'], 'subscribers' => $c]; + } + return ['supported' => true, 'count' => count($out), 'lists' => $out]; + } + + public static function listTags(array $args): array { + global $wpdb; + $t = self::tbl('fc_tags'); + if (!self::has($t)) return ['supported' => false, 'note' => 'fc_tags table not found.', 'tags' => []]; + $rows = $wpdb->get_results("SELECT id, title, slug FROM {$t} ORDER BY id ASC LIMIT 300", ARRAY_A); + $out = []; + foreach ((array) $rows as $r) $out[] = ['id' => (int) $r['id'], 'title' => $r['title'], 'slug' => $r['slug']]; + return ['supported' => true, 'count' => count($out), 'tags' => $out]; + } + + public static function listSubscribers(array $args): array { + global $wpdb; + $t = self::tbl('fc_subscribers'); + if (!self::has($t)) return ['supported' => false, 'note' => 'fc_subscribers table not found.', 'subscribers' => []]; + $per = max(1, (int) ($args['per_page'] ?? 50)); + $where = ''; + if (!empty($args['status'])) $where = $wpdb->prepare('WHERE 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 stats(array $args): array { + global $wpdb; + $sub = self::tbl('fc_subscribers'); + if (!self::has($sub)) return ['supported' => false, 'note' => 'FluentCRM tables not found.']; + $byStatus = $wpdb->get_results("SELECT status, COUNT(*) c FROM {$sub} GROUP BY status", ARRAY_A); + $s = []; + foreach ((array) $byStatus as $r) $s[$r['status']] = (int) $r['c']; + return ['supported' => true, 'subscribers_by_status' => $s, + 'lists' => self::has(self::tbl('fc_lists')) ? (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . self::tbl('fc_lists')) : null, + 'tags' => self::has(self::tbl('fc_tags')) ? (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . self::tbl('fc_tags')) : null]; + } +}