| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers\Configuration; |
| 4 |
|
| 5 |
use AcyMailing\Controllers\DashboardController; |
| 6 |
use AcyMailing\Helpers\HeaderHelper; |
| 7 |
|
| 8 |
trait Subscription |
| 9 |
{ |
| 10 |
public function removeNotification(): void |
| 11 |
{ |
| 12 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 13 |
|
| 14 |
$whichNotification = acym_getVar('string', 'id'); |
| 15 |
|
| 16 |
if ($whichNotification !== '0' && empty($whichNotification)) { |
| 17 |
acym_sendAjaxResponse(acym_translation('ACYM_NOTIFICATION_NOT_FOUND'), [], false); |
| 18 |
} |
| 19 |
|
| 20 |
$notifications = json_decode($this->config->get('notifications', '[]'), true) ?? []; |
| 21 |
$dashboardNotifications = json_decode($this->config->get('dashboard_notif', '[]'), true) ?? []; |
| 22 |
|
| 23 |
if ('all' === $whichNotification) { |
| 24 |
$notifications = []; |
| 25 |
} else { |
| 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 |
} |
| 33 |
} |
| 34 |
|
| 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 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function markNotificationRead(): void |
| 61 |
{ |
| 62 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 63 |
|
| 64 |
$which = acym_getVar('string', 'id'); |
| 65 |
|
| 66 |
$notifications = json_decode($this->config->get('notifications', '[]'), true); |
| 67 |
if (empty($notifications)) { |
| 68 |
acym_sendAjaxResponse('', []); |
| 69 |
} |
| 70 |
|
| 71 |
if (empty($which)) { |
| 72 |
foreach ($notifications as $key => $notification) { |
| 73 |
$notifications[$key]['read'] = true; |
| 74 |
} |
| 75 |
} else { |
| 76 |
foreach ($notifications as $key => $notification) { |
| 77 |
if ($notification['id'] != $which) continue; |
| 78 |
$notifications[$key]['read'] = true; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
$this->config->saveConfig(['notifications' => json_encode($notifications)]); |
| 84 |
|
| 85 |
acym_sendAjaxResponse('', []); |
| 86 |
} |
| 87 |
|
| 88 |
public function addNotification(): void |
| 89 |
{ |
| 90 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 91 |
|
| 92 |
$message = acym_getVar('string', 'message'); |
| 93 |
$level = acym_getVar('string', 'level'); |
| 94 |
|
| 95 |
if (empty($message) || empty($level)) { |
| 96 |
acym_sendAjaxResponse(acym_translation('ACYM_ERROR'), [], false); |
| 97 |
} |
| 98 |
|
| 99 |
$headerHelper = new HeaderHelper(); |
| 100 |
|
| 101 |
$newNotification = new \stdClass(); |
| 102 |
$newNotification->message = $message; |
| 103 |
$newNotification->level = $level; |
| 104 |
$newNotification->read = false; |
| 105 |
$newNotification->date = time(); |
| 106 |
|
| 107 |
$headerHelper->addNotification($newNotification); |
| 108 |
|
| 109 |
ob_start(); |
| 110 |
$headerHelper->displayNotificationCenter(); |
| 111 |
$notificationCenter = ob_get_clean(); |
| 112 |
|
| 113 |
acym_sendAjaxResponse('', ['notificationCenter' => $notificationCenter]); |
| 114 |
} |
| 115 |
|
| 116 |
private function loadSurveyAnswers(array &$data): void |
| 117 |
{ |
| 118 |
$surveyTexts = $this->config->get('unsub_survey', '{}'); |
| 119 |
$data['surveyAnswers'] = json_decode($surveyTexts, true); |
| 120 |
} |
| 121 |
} |
| 122 |
|