|
2 | 2 | namespace OpenProvider\WhmcsRegistrar\Controllers\Hooks; |
3 | 3 |
|
4 | 4 | use OpenProvider\WhmcsRegistrar\Controllers\Hooks\Widgets\BalanceWidget; |
| 5 | +use OpenProvider\WhmcsRegistrar\Controllers\Hooks\Widgets\CrossSellWidget; |
| 6 | +use WHMCS\Database\Capsule; |
5 | 7 |
|
6 | 8 | /** |
7 | 9 | * Class AdminWidgetController |
@@ -37,6 +39,112 @@ private function cleanupLegacyBalanceWidget(): void |
37 | 39 | null, |
38 | 40 | null |
39 | 41 | ); |
| 42 | + public function showCrossSellWidget() |
| 43 | + { |
| 44 | + return new CrossSellWidget(); |
| 45 | + } |
| 46 | + |
| 47 | + public function handleCrossSellDismiss($vars) |
| 48 | + { |
| 49 | + $isAjaxDismiss = isset($_GET['op_crosssell_ajax']) && $_GET['op_crosssell_ajax'] === '1'; |
| 50 | + |
| 51 | + if ( |
| 52 | + !isset($_GET['op_crosssell_action']) || $_GET['op_crosssell_action'] !== 'dismiss' |
| 53 | + || !isset($_GET['crosssell_product']) |
| 54 | + ) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!isset($_GET['token'])) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (function_exists('\\verify_token')) { |
| 63 | + if (!\verify_token('link', $_GET['token'])) { |
| 64 | + return; |
| 65 | + } |
| 66 | + } elseif (function_exists('\\check_token')) { |
| 67 | + try { |
| 68 | + \check_token('WHMCS.admin.default', true); |
| 69 | + } catch (\Throwable $e) { |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + $product = (string) $_GET['crosssell_product']; |
| 75 | + $validProducts = array_keys(CrossSellWidget::PRODUCTS); |
| 76 | + |
| 77 | + if (!in_array($product, $validProducts, true)) { |
| 78 | + if ($isAjaxDismiss) { |
| 79 | + header('Content-Type: application/json; charset=utf-8'); |
| 80 | + http_response_code(400); |
| 81 | + echo json_encode(['status' => 'error', 'message' => 'Invalid product']); |
| 82 | + exit; |
| 83 | + } |
| 84 | + header('Location: index.php'); |
| 85 | + exit; |
| 86 | + } |
| 87 | + |
| 88 | + $moduleName = CrossSellWidget::PRODUCTS[$product]['module_name']; |
| 89 | + |
| 90 | + CrossSellWidget::ensureDismissTableExists(); |
| 91 | + |
| 92 | + try { |
| 93 | + $existing = Capsule::table(CrossSellWidget::DISMISS_TABLE) |
| 94 | + ->where('module_name', $moduleName) |
| 95 | + ->first(); |
| 96 | + |
| 97 | + $now = date('Y-m-d H:i:s'); |
| 98 | + |
| 99 | + if ($existing) { |
| 100 | + Capsule::table(CrossSellWidget::DISMISS_TABLE) |
| 101 | + ->where('module_name', $moduleName) |
| 102 | + ->update([ |
| 103 | + 'dismissed' => 1, |
| 104 | + 'updated_at' => $now, |
| 105 | + ]); |
| 106 | + } else { |
| 107 | + Capsule::table(CrossSellWidget::DISMISS_TABLE) |
| 108 | + ->insert([ |
| 109 | + 'module_name' => $moduleName, |
| 110 | + 'dismissed' => 1, |
| 111 | + 'created_at' => $now, |
| 112 | + 'updated_at' => $now, |
| 113 | + ]); |
| 114 | + } |
| 115 | + } catch (\Exception $e) { |
| 116 | + // Silent fail |
| 117 | + } |
| 118 | + |
| 119 | + $this->invalidateCrossSellWidgetCache(); |
| 120 | + |
| 121 | + if ($isAjaxDismiss) { |
| 122 | + header('Content-Type: application/json; charset=utf-8'); |
| 123 | + echo json_encode(['status' => 'ok']); |
| 124 | + exit; |
| 125 | + } |
| 126 | + |
| 127 | + header('Location: index.php'); |
| 128 | + exit; |
| 129 | + } |
| 130 | + |
| 131 | + private function invalidateCrossSellWidgetCache() |
| 132 | + { |
| 133 | + try { |
| 134 | + $schema = Capsule::schema(); |
| 135 | + if (!$schema->hasTable('tbltransientdata')) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + Capsule::table('tbltransientdata') |
| 140 | + ->where(function ($query) { |
| 141 | + $query->where('name', 'like', '%OPCrossSellWidget%') |
| 142 | + ->orWhere('name', 'like', '%CrossSellWidget%') |
| 143 | + ->orWhere('name', 'like', '%OpenProvider\\\\WhmcsRegistrar\\\\Controllers\\\\Hooks\\\\Widgets\\\\CrossSellWidget%'); |
| 144 | + }) |
| 145 | + ->delete(); |
| 146 | + } catch (\Throwable $e) { |
| 147 | + // Silent fail |
40 | 148 | } |
41 | 149 | } |
42 | 150 | } |
0 commit comments