| @@ -1,0 +1,152 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Users; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\UserClass; | |
| 6 | + | |
| 7 | +trait Subscription | |
| 8 | +{ | |
| 9 | + public function resetSubscription(): void | |
| 10 | + { | |
| 11 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 12 | + | |
| 13 | + $userId = acym_getVar('int', 'userId'); | |
| 14 | + | |
| 15 | + if (empty($userId)) { | |
| 16 | + $this->listing(); | |
| 17 | + | |
| 18 | + return; | |
| 19 | + } | |
| 20 | + | |
| 21 | + $userClass = new UserClass(); | |
| 22 | + if (!$userClass->hasUserAccess($userId)) { | |
| 23 | + die('Access denied for subscription reset of this user'); | |
| 24 | + } | |
| 25 | + | |
| 26 | + $lists = json_decode(acym_getVar('string', 'acym__entity_select__selected', '[]'), true); | |
| 27 | + if (empty($lists)) { | |
| 28 | + $lists = []; | |
| 29 | + } | |
| 30 | + $userClass->resetSubscription([$userId], $lists); | |
| 31 | + | |
| 32 | + $this->edit(); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public function unsubscribeUser(): void | |
| 36 | + { | |
| 37 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 38 | + | |
| 39 | + $userId = acym_getVar('int', 'userId'); | |
| 40 | + if (empty($userId)) { | |
| 41 | + $this->listing(); | |
| 42 | + | |
| 43 | + return; | |
| 44 | + } | |
| 45 | + | |
| 46 | + $userClass = new UserClass(); | |
| 47 | + if (!$userClass->hasUserAccess($userId)) { | |
| 48 | + die('Access denied for unsubscribing this user'); | |
| 49 | + } | |
| 50 | + | |
| 51 | + $lists = json_decode(acym_getVar('string', 'acym__entity_select__selected', '[]'), true); | |
| 52 | + if (empty($lists)) { | |
| 53 | + $lists = []; | |
| 54 | + } | |
| 55 | + $userClass->unsubscribe([$userId], $lists); | |
| 56 | + | |
| 57 | + $this->edit(); | |
| 58 | + } | |
| 59 | + | |
| 60 | + public function unsubscribeUserFromAll(): void | |
| 61 | + { | |
| 62 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 63 | + | |
| 64 | + $userId = acym_getVar('int', 'userId'); | |
| 65 | + | |
| 66 | + if (empty($userId)) { | |
| 67 | + $this->listing(); | |
| 68 | + | |
| 69 | + return; | |
| 70 | + } | |
| 71 | + | |
| 72 | + $userClass = new UserClass(); | |
| 73 | + if (!$userClass->hasUserAccess($userId)) { | |
| 74 | + die('Access denied for unsubscribing this user'); | |
| 75 | + } | |
| 76 | + | |
| 77 | + $lists = []; | |
| 78 | + $subscriptions = $userClass->getSubscriptionStatus($userId); | |
| 79 | + foreach ($subscriptions as $i => $oneList) { | |
| 80 | + if ($oneList->status == 1) { | |
| 81 | + $lists[] = $oneList->list_id; | |
| 82 | + } | |
| 83 | + } | |
| 84 | + | |
| 85 | + $userClass->unsubscribe([$userId], $lists); | |
| 86 | + | |
| 87 | + $this->edit(); | |
| 88 | + } | |
| 89 | + | |
| 90 | + public function resubscribeUserToAll(): void | |
| 91 | + { | |
| 92 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 93 | + | |
| 94 | + $userId = acym_getVar('int', 'userId'); | |
| 95 | + if (empty($userId)) { | |
| 96 | + $this->listing(); | |
| 97 | + | |
| 98 | + return; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $userClass = new UserClass(); | |
| 102 | + if (!$userClass->hasUserAccess($userId)) { | |
| 103 | + die('Access denied for resubscribing this user'); | |
| 104 | + } | |
| 105 | + | |
| 106 | + $lists = []; | |
| 107 | + $subscriptions = $userClass->getSubscriptionStatus($userId); | |
| 108 | + foreach ($subscriptions as $i => $oneList) { | |
| 109 | + if ($oneList->status == 0) { | |
| 110 | + $lists[] = $oneList->list_id; | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + $userClass->subscribe([$userId], $lists); | |
| 115 | + | |
| 116 | + $this->edit(); | |
| 117 | + } | |
| 118 | + | |
| 119 | + public function subscribeUser(bool $returnOnEdit = true, array $lists = [], bool $frontCreation = false): void | |
| 120 | + { | |
| 121 | + // Called as a task (no argument) → require a CSRF token. Internal calls from apply() are already token-checked. | |
| 122 | + if (func_num_args() === 0) { | |
| 123 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 124 | + } | |
| 125 | + | |
| 126 | + $userId = acym_getVar('int', 'userId'); | |
| 127 | + if (empty($userId)) { | |
| 128 | + $this->listing(); | |
| 129 | + | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + | |
| 133 | + $userClass = new UserClass(); | |
| 134 | + if (!$frontCreation && !$userClass->hasUserAccess($userId)) { | |
| 135 | + die('Access denied for subscribing this user'); | |
| 136 | + } | |
| 137 | + | |
| 138 | + // Can be called from the user's edition page when re-subscribing to a list | |
| 139 | + if (empty($lists)) { | |
| 140 | + $lists = json_decode(acym_getVar('string', 'acym__entity_select__selected', '[]'), true); | |
| 141 | + if (empty($lists)) { | |
| 142 | + $lists = []; | |
| 143 | + } | |
| 144 | + } | |
| 145 | + | |
| 146 | + $userClass->subscribe([$userId], $lists); | |
| 147 | + | |
| 148 | + if ($returnOnEdit) { | |
| 149 | + $this->edit(); | |
| 150 | + } | |
| 151 | + } | |
| 152 | +} | |