ameliabooking
/
vendor
/
melograno
/
usage-tracker
/
src
/
Collectors
/
Plugin
/
WpDataTablesCollector.php
AmeliaCollector.php
2 months ago
IvyFormsCollector.php
2 months ago
WpDataTablesCollector.php
2 months ago
WpDataTablesCollector.php
152 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AmeliaVendor\Melograno\UsageTracker\Collectors\Plugin; |
| 6 | |
| 7 | use AmeliaVendor\Melograno\UsageTracker\Collectors\BaseCollector; |
| 8 | |
| 9 | class WpDataTablesCollector extends BaseCollector |
| 10 | { |
| 11 | /** @var list<string> */ |
| 12 | private const ALLOWED_TABLES = [ |
| 13 | 'wpdatatables', |
| 14 | 'wpdatacharts', |
| 15 | ]; |
| 16 | |
| 17 | /** @var array<string, string> */ |
| 18 | private const LICENSE_TIER_MAP = [ |
| 19 | 'free' => 'free', |
| 20 | 'starter' => 'starter', |
| 21 | 'standard' => 'standard', |
| 22 | 'pro' => 'pro', |
| 23 | 'developer' => 'developer', |
| 24 | ]; |
| 25 | |
| 26 | /** @var array<string, string> */ |
| 27 | private const CONTENT_TYPE_TABLE_MAP = [ |
| 28 | 'table' => 'wpdatatables', |
| 29 | 'chart' => 'wpdatacharts', |
| 30 | ]; |
| 31 | |
| 32 | public function getPluginSlug(): string |
| 33 | { |
| 34 | return 'wpdatatables'; |
| 35 | } |
| 36 | |
| 37 | public function getConsentOptionName(): string |
| 38 | { |
| 39 | return 'wpdatatables_usage_tracking_consent'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Maps wpDataTables licence tier values to telemetry license slugs. |
| 44 | */ |
| 45 | public static function normalizeLicenseTier(?string $licenseTier): ?string |
| 46 | { |
| 47 | if ($licenseTier === null) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | $key = strtolower(trim($licenseTier)); |
| 52 | |
| 53 | return self::LICENSE_TIER_MAP[$key] ?? null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return array<string, mixed> |
| 58 | */ |
| 59 | protected function pluginPayload(): array |
| 60 | { |
| 61 | $data = [ |
| 62 | 'plugin_version' => defined('WDT_CURRENT_VERSION') ? WDT_CURRENT_VERSION : null, |
| 63 | ]; |
| 64 | |
| 65 | if (!function_exists('get_plugins')) { |
| 66 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 67 | } |
| 68 | |
| 69 | if (function_exists('get_plugin_data') && defined('WDT_ROOT_PATH')) { |
| 70 | $pluginData = get_plugin_data(WDT_ROOT_PATH . 'wpdatatables.php', false, false); |
| 71 | if (!empty($pluginData['Version'])) { |
| 72 | $data['plugin_version'] = $pluginData['Version']; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | $rawTier = $this->resolveLicenseTier(); |
| 77 | $data['license'] = self::normalizeLicenseTier($rawTier); |
| 78 | |
| 79 | $data['tables_count'] = $this->resolveContentCount('table'); |
| 80 | $data['charts_count'] = $this->resolveContentCount('chart'); |
| 81 | |
| 82 | $data['first_content_created_at'] = $this->resolveFirstContentCreatedAt(); |
| 83 | |
| 84 | $data = array_filter($data, static function ($value) { |
| 85 | return $value !== null; |
| 86 | }); |
| 87 | |
| 88 | return $data; |
| 89 | } |
| 90 | |
| 91 | protected function resolveContentCount(string $contentType): int |
| 92 | { |
| 93 | if (class_exists('WDTTools')) { |
| 94 | return (int) \WDTTools::getTablesCount($contentType); |
| 95 | } |
| 96 | |
| 97 | $table = self::CONTENT_TYPE_TABLE_MAP[$contentType] ?? ''; |
| 98 | |
| 99 | return $this->countPluginRows($table); |
| 100 | } |
| 101 | |
| 102 | protected function resolveFirstContentCreatedAt(): ?string |
| 103 | { |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | protected function resolveLicenseTier(): ?string |
| 108 | { |
| 109 | $this->ensureTierDetectionLoaded(); |
| 110 | |
| 111 | if (!function_exists('wdtmcp_detect_integrations') || !function_exists('wdtmcp_detect_tier')) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | return wdtmcp_detect_tier(wdtmcp_detect_integrations()); |
| 116 | } |
| 117 | |
| 118 | private function ensureTierDetectionLoaded(): void |
| 119 | { |
| 120 | if (function_exists('wdtmcp_detect_tier')) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (!defined('WDT_ROOT_PATH')) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $path = WDT_ROOT_PATH . 'Infrastructure/WP/MCP/Abilities/get-system-info.php'; |
| 129 | |
| 130 | if (is_file($path)) { |
| 131 | require_once $path; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private function countPluginRows(string $table): int |
| 136 | { |
| 137 | if (!in_array($table, self::ALLOWED_TABLES, true)) { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | global $wpdb; |
| 142 | |
| 143 | if (!isset($wpdb)) { |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | $count = $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->prefix . $table); |
| 148 | |
| 149 | return $count === null ? 0 : (int) $count; |
| 150 | } |
| 151 | } |
| 152 |