| @@ -1,35 +1,67 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AcyMailing\Controllers\Configuration; |
| 4 | 4 | |
| 5 | +use AcyMailing\Controllers\DashboardController; | |
| 5 | 6 | use AcyMailing\Helpers\HeaderHelper; |
| 6 | 7 | |
| 7 | 8 | trait Subscription |
| 8 | 9 | { |
| 9 | - public function removeNotification() | |
| 10 | + public function removeNotification(): void | |
| 10 | 11 | { |
| 12 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 13 | + | |
| 11 | 14 | $whichNotification = acym_getVar('string', 'id'); |
| 12 | 15 | |
| 13 | - if ($whichNotification != 0 && empty($whichNotification)) { | |
| 16 | + if ($whichNotification !== '0' && empty($whichNotification)) { | |
| 14 | 17 | acym_sendAjaxResponse(acym_translation('ACYM_NOTIFICATION_NOT_FOUND'), [], false); |
| 15 | 18 | } |
| 16 | 19 | |
| 20 | + $notifications = json_decode($this->config->get('notifications', '[]'), true) ?? []; | |
| 21 | + $dashboardNotifications = json_decode($this->config->get('dashboard_notif', '[]'), true) ?? []; | |
| 22 | + | |
| 17 | 23 | if ('all' === $whichNotification) { |
| 18 | - $this->config->save(['notifications' => '[]']); | |
| 19 | 24 | $notifications = []; |
| 20 | 25 | } else { |
| 21 | - $notifications = json_decode($this->config->get('notifications', '[]'), true); | |
| 22 | - unset($notifications[$whichNotification]); | |
| 23 | - $this->config->save(['notifications' => json_encode($notifications)]); | |
| 26 | + $notifications = array_values(array_filter($notifications, fn($notif) => !isset($notif['id']) || $notif['id'] !== $whichNotification)); | |
| 27 | + | |
| 28 | + $dashboardNotifications = array_values(array_filter($dashboardNotifications, fn($notif) => !isset($notif['name']) || $notif['name'] !== $whichNotification)); | |
| 29 | + | |
| 30 | + if (is_numeric($whichNotification)) { | |
| 31 | + $notifications = array_values(array_filter($notifications, fn($notif, $key) => $key != $whichNotification, ARRAY_FILTER_USE_BOTH)); | |
| 32 | + } | |
| 24 | 33 | } |
| 25 | - $helperHeader = new HeaderHelper(); | |
| 26 | 34 | |
| 27 | - acym_sendAjaxResponse('', ['html' => $helperHeader->getNotificationCenterInner($notifications)]); | |
| 35 | + $this->config->saveConfig(['notifications' => json_encode($notifications)]); | |
| 36 | + $this->config->saveConfig(['dashboard_notif' => json_encode($dashboardNotifications)], false); | |
| 37 | + | |
| 38 | + $headerHelper = new HeaderHelper(); | |
| 39 | + $dashboardController = new DashboardController(); | |
| 40 | + | |
| 41 | + $data = [ | |
| 42 | + 'notifications' => $dashboardNotifications, | |
| 43 | + ]; | |
| 44 | + $dashboardController->getDashboardNotifications($data); | |
| 45 | + | |
| 46 | + | |
| 47 | + ob_start(); | |
| 48 | + $headerHelper->displayNotificationCenterInner($notifications); | |
| 49 | + $notificationCenter = ob_get_clean(); | |
| 50 | + | |
| 51 | + acym_sendAjaxResponse( | |
| 52 | + '', | |
| 53 | + [ | |
| 54 | + 'headerHtml' => $notificationCenter, | |
| 55 | + 'dashboardNotifications' => $data['dashboardNotifications'], | |
| 56 | + ] | |
| 57 | + ); | |
| 28 | 58 | } |
| 29 | 59 | |
| 30 | - public function markNotificationRead() | |
| 60 | + public function markNotificationRead(): void | |
| 31 | 61 | { |
| 62 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 63 | + | |
| 32 | 64 | $which = acym_getVar('string', 'id'); |
| 33 | 65 | |
| 34 | 66 | $notifications = json_decode($this->config->get('notifications', '[]'), true); |
| 35 | 67 | if (empty($notifications)) { |
| @@ -47,15 +79,17 @@ | ||
| 47 | 79 | } |
| 48 | 80 | } |
| 49 | 81 | |
| 50 | 82 | |
| 51 | - $this->config->save(['notifications' => json_encode($notifications)]); | |
| 83 | + $this->config->saveConfig(['notifications' => json_encode($notifications)]); | |
| 52 | 84 | |
| 53 | 85 | acym_sendAjaxResponse('', []); |
| 54 | 86 | } |
| 55 | 87 | |
| 56 | - public function addNotification() | |
| 88 | + public function addNotification(): void | |
| 57 | 89 | { |
| 90 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 91 | + | |
| 58 | 92 | $message = acym_getVar('string', 'message'); |
| 59 | 93 | $level = acym_getVar('string', 'level'); |
| 60 | 94 | |
| 61 | 95 | if (empty($message) || empty($level)) { |
| @@ -61,9 +95,9 @@ | ||
| 61 | 95 | if (empty($message) || empty($level)) { |
| 62 | 96 | acym_sendAjaxResponse(acym_translation('ACYM_ERROR'), [], false); |
| 63 | 97 | } |
| 64 | 98 | |
| 65 | - $helperHeader = new HeaderHelper(); | |
| 99 | + $headerHelper = new HeaderHelper(); | |
| 66 | 100 | |
| 67 | 101 | $newNotification = new \stdClass(); |
| 68 | 102 | $newNotification->message = $message; |
| 69 | 103 | $newNotification->level = $level; |
| @@ -69,14 +103,18 @@ | ||
| 69 | 103 | $newNotification->level = $level; |
| 70 | 104 | $newNotification->read = false; |
| 71 | 105 | $newNotification->date = time(); |
| 72 | 106 | |
| 73 | - $helperHeader->addNotification($newNotification); | |
| 107 | + $headerHelper->addNotification($newNotification); | |
| 74 | 108 | |
| 75 | - acym_sendAjaxResponse('', ['notificationCenter' => $helperHeader->getNotificationCenter()]); | |
| 109 | + ob_start(); | |
| 110 | + $headerHelper->displayNotificationCenter(); | |
| 111 | + $notificationCenter = ob_get_clean(); | |
| 112 | + | |
| 113 | + acym_sendAjaxResponse('', ['notificationCenter' => $notificationCenter]); | |
| 76 | 114 | } |
| 77 | 115 | |
| 78 | - private function loadSurveyAnswers(&$data) | |
| 116 | + private function loadSurveyAnswers(array &$data): void | |
| 79 | 117 | { |
| 80 | 118 | $surveyTexts = $this->config->get('unsub_survey', '{}'); |
| 81 | 119 | $data['surveyAnswers'] = json_decode($surveyTexts, true); |
| 82 | 120 | } |