| @@ -15,30 +15,29 @@ | ||
| 15 | 15 | $this->notificationService = $notificationService; |
| 16 | 16 | } |
| 17 | 17 | public function getAllNotifications(Request $request) |
| 18 | 18 | { |
| 19 | - $data = $request->all(); | |
| 20 | 19 | try { |
| 21 | - $per_page = isset($data['per_page']) ? $data['per_page'] : 20; | |
| 22 | - $page = isset($data['page']) ? $data['page'] : 1; | |
| 23 | - $data = $this->notificationService->getAllNotifications($per_page, $page); | |
| 20 | + $per_page = $request->getSafe('per_page', 'intval', 20); | |
| 21 | + $page = $request->getSafe('page', 'intval', 1); | |
| 22 | + $action = $request->getSafe('action', 'sanitize_text_field', 'all'); | |
| 23 | + $data = $this->notificationService->getAllNotifications($per_page, $page, $action); | |
| 24 | 24 | |
| 25 | 25 | return $this->sendSuccess($data, 200); |
| 26 | 26 | } catch (\Exception $e) { |
| 27 | - return $this->sendError($e->getMessage(), 404); | |
| 27 | + return $this->sendError($e->getMessage(), 400); | |
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | public function getAllUnreadNotifications(Request $request) |
| 31 | 31 | { |
| 32 | - $data = $request->all(); | |
| 33 | 32 | try { |
| 34 | - $per_page = isset($data['per_page']) ? $data['per_page'] : 20; | |
| 35 | - $page = isset($data['page']) ? $data['page'] : 1; | |
| 33 | + $per_page = $request->getSafe('per_page', 'intval', 20); | |
| 34 | + $page = $request->getSafe('page', 'intval', 1); | |
| 36 | 35 | return $this->sendSuccess( |
| 37 | 36 | $this->notificationService->getAllUnreadNotifications($per_page, $page), |
| 38 | 37 | 200); |
| 39 | 38 | } catch (\Exception $e) { |
| 40 | - return $this->sendError($e->getMessage(), 404); | |
| 39 | + return $this->sendError($e->getMessage(), 400); | |
| 41 | 40 | } |
| 42 | 41 | } |
| 43 | 42 | |
| 44 | 43 | public function newNotificationNumber() |
| @@ -49,54 +48,63 @@ | ||
| 49 | 48 | return $this->sendSuccess([ |
| 50 | 49 | 'total_unread' => $unread_notifications, |
| 51 | 50 | ], 200); |
| 52 | 51 | } catch (\Exception $e) { |
| 53 | - return $this->sendError($e->getMessage(), 404); | |
| 52 | + return $this->sendError($e->getMessage(), 400); | |
| 54 | 53 | } |
| 55 | 54 | } |
| 56 | 55 | |
| 56 | + /** | |
| 57 | + * Return the current user's board notification settings with safe defaults. | |
| 58 | + */ | |
| 57 | 59 | public function getBoardNotificationSettings($board_id) |
| 58 | 60 | { |
| 61 | + $board_id = absint($board_id); | |
| 59 | 62 | try { |
| 60 | 63 | $userId = get_current_user_id(); |
| 61 | - $currentSettings = []; | |
| 62 | - $boardSettings = $this->notificationService->getBoardNotificationSettingsOfUser($board_id, $userId); | |
| 63 | - if ($boardSettings && $boardSettings->preferences) { | |
| 64 | - $currentSettings = maybe_unserialize($boardSettings->preferences); | |
| 65 | - } | |
| 64 | + $currentSettings = $this->notificationService->getBoardNotificationSettingsWithDefaults($board_id, $userId); | |
| 66 | 65 | |
| 67 | 66 | return $this->sendSuccess([ |
| 68 | 67 | 'currentSettings' => $currentSettings, |
| 69 | 68 | ], 200); |
| 70 | 69 | } catch (\Exception $e) { |
| 71 | - return $this->sendError($e->getMessage(), 404); | |
| 70 | + return $this->sendError($e->getMessage(), 400); | |
| 72 | 71 | } |
| 73 | 72 | } |
| 74 | 73 | |
| 75 | 74 | public function updateBoardNotificationSettings(Request $request, $board_id) |
| 76 | 75 | { |
| 76 | + $board_id = absint($board_id); | |
| 77 | 77 | try { |
| 78 | - $newSettings = $request->get('updatedSettings'); | |
| 79 | - | |
| 80 | - | |
| 78 | + // updatedSettings is an array, sanitize each element | |
| 79 | + $rawSettings = $request->getSafe('updatedSettings'); | |
| 80 | + $newSettings = []; | |
| 81 | + if (is_array($rawSettings)) { | |
| 82 | + foreach ($rawSettings as $key => $value) { | |
| 83 | + // Sanitize key and value | |
| 84 | + $sanitizedKey = sanitize_text_field($key); | |
| 85 | + $sanitizedValue = sanitize_text_field($value); | |
| 86 | + $newSettings[$sanitizedKey] = $sanitizedValue; | |
| 87 | + } | |
| 88 | + } | |
| 81 | 89 | $this->notificationService->updateBoardNotificationSettings($newSettings, $board_id); |
| 82 | 90 | return $this->sendSuccess([ |
| 83 | - 'message' => __('Board notification settings has been updated', 'fluent-boards'), | |
| 91 | + 'message' => __('Board notification settings have been updated', 'fluent-boards'), | |
| 84 | 92 | ], 200); |
| 85 | 93 | } catch (\Exception $e) { |
| 86 | - return $this->sendError($e->getMessage(), 404); | |
| 94 | + return $this->sendError($e->getMessage(), 400); | |
| 87 | 95 | } |
| 88 | 96 | } |
| 89 | 97 | |
| 90 | 98 | public function readNotification(Request $request) |
| 91 | 99 | { |
| 92 | - try{ | |
| 93 | - $notificationId = $request->getSafe('notification_id'); | |
| 100 | + try { | |
| 101 | + $notificationId = $request->getSafe('notification_id', 'intval'); | |
| 94 | 102 | |
| 95 | - if ( $notificationId ) { | |
| 96 | - $notification = $this->notificationService->markNotificationRead( $notificationId ); | |
| 103 | + if ($notificationId) { | |
| 104 | + $notification = $this->notificationService->markNotificationRead($notificationId); | |
| 97 | 105 | } else { |
| 98 | - $notification = $this->notificationService->markAllRead(); | |
| 106 | + $notification = $this->notificationService->markAllRead(); | |
| 99 | 107 | } |
| 100 | 108 | |
| 101 | 109 | return $this->sendSuccess([ |
| 102 | 110 | 'message' => __('Notification is updated', 'fluent-boards'), |
| @@ -103,8 +111,8 @@ | ||
| 103 | 111 | 'notification' => $notification |
| 104 | 112 | ], 200); |
| 105 | 113 | |
| 106 | 114 | } catch (\Exception $e) { |
| 107 | - return $this->sendError($e->getMessage(), 404); | |
| 115 | + return $this->sendError($e->getMessage(), 400); | |
| 108 | 116 | } |
| 109 | 117 | } |
| 110 | -} | |
| 118 | +} | |