Files

86 lines
5.1 KiB
PHP

<?php
/**
* Batch 36 — FluentCRM tools (reads the plugin's own tables via $wpdb).
* Net-new, GPL-2.0-or-later. See NOTICE.md. Inert unless FluentCRM (FLUENTCRM) is active.
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch36 {
public static function register($registry): void {
if (!defined('FLUENTCRM')) 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\\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];
}
}