From 18e7e415339ccc9adf8ade562f93abef606da16d Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 26 Aug 2026 22:24:18 +0000
Subject: [PATCH] =Cache::connects throughout codebase refactored
---
inc/rest/routes/NotificationsRoutes.php | 63 ++++++++++++-------------------
1 files changed, 25 insertions(+), 38 deletions(-)
diff --git a/inc/rest/routes/NotificationsRoutes.php b/inc/rest/routes/NotificationsRoutes.php
index eaf3e7f..fd04460 100644
--- a/inc/rest/routes/NotificationsRoutes.php
+++ b/inc/rest/routes/NotificationsRoutes.php
@@ -1,6 +1,7 @@
<?php
namespace JVBase\rest\routes;
+use JVBase\Instance;
use JVBase\managers\Cache;
use JVBase\managers\CustomTable;
use JVBase\registrar\Registrar;
@@ -23,6 +24,7 @@
*/
class NotificationsRoutes extends Rest
{
+ use Instance;
protected int $user_id;
protected array $notification_types = [];
protected object $manager;
@@ -107,10 +109,7 @@
$this->cacheName = 'notifications';
$this->cacheTtl = HOUR_IN_SECONDS;
parent::__construct();
-
- // Connect notifications cache to user cache
- // When user data changes, notification cache should invalidate
- $this->cache->connect('user');
+ $this->cache = Cache::for($this->cacheName, 'user', $this->cacheTtl);
$this->notifications = CustomTable::for('notifications');
$this->metrics = CustomTable::for('notification_metrics');
@@ -133,7 +132,7 @@
*/
public function init(): void
{
- $this->manager = JVB()->notification();
+ $this->manager = NotificationManager::getInstance();
$this->notification_types = $this->manager->getNotificationTypes(true);
}
@@ -146,7 +145,6 @@
Route::for('notifications')
->get([$this, 'getNotifications'])
->args([
- 'user' => 'integer|required',
'type' => 'string',
'status' => 'string|enum:unread,read,actioned,dismissed',
'limit' => 'integer|default:20|min:1|max:100',
@@ -160,7 +158,6 @@
Route::for('notifications/read')
->post([$this, 'markRead'])
->args([
- 'user' => 'integer|required',
'notification_id' => 'integer|required',
])
->auth('user')
@@ -171,7 +168,6 @@
Route::for('notifications/read-all')
->post([$this, 'markAllRead'])
->args([
- 'user' => 'integer|required',
'type' => 'string',
])
->auth('user')
@@ -182,7 +178,6 @@
Route::for('notifications/action')
->post([$this, 'markActioned'])
->args([
- 'user' => 'integer|required',
'notification_id' => 'integer|required',
])
->auth('user')
@@ -193,7 +188,6 @@
Route::for('notifications/dismiss')
->post([$this, 'markDismissed'])
->args([
- 'user' => 'integer|required',
'notification_id' => 'integer|required',
])
->auth('user')
@@ -204,7 +198,6 @@
Route::for('notifications/count')
->get([$this, 'getUnreadCount'])
->args([
- 'user' => 'integer|required',
'type' => 'string',
])
->auth('user')
@@ -221,16 +214,15 @@
*/
public function getNotifications(WP_REST_Request $request): WP_REST_Response
{
- $user_id = absint($request->get_param('user'));
+ $user_id = get_current_user_id();
+ if (!$user_id) {
+ return $this->unauthorized();
+ }
$type = sanitize_text_field($request->get_param('type') ?? '');
$status = sanitize_text_field($request->get_param('status') ?? '');
$limit = absint($request->get_param('limit'));
$offset = absint($request->get_param('offset'));
- if (!$this->checkUser($user_id)) {
- return $this->unauthorized();
- }
-
$cacheKey = compact('user_id', 'type', 'status', 'limit', 'offset');
$result = $this->cache->remember($cacheKey, function() use ($user_id, $type, $status, $limit, $offset) {
@@ -265,12 +257,11 @@
*/
public function getUnreadCount(WP_REST_Request $request): WP_REST_Response
{
- $user_id = absint($request->get_param('user'));
- $type = sanitize_text_field($request->get_param('type') ?? '');
-
- if (!$this->checkUser($user_id)) {
+ $user_id = get_current_user_id();
+ if (!$user_id) {
return $this->unauthorized();
}
+ $type = sanitize_text_field($request->get_param('type') ?? '');
$cacheKey = compact('user_id', 'type');
@@ -647,12 +638,11 @@
*/
public function markRead(WP_REST_Request $request): WP_REST_Response
{
- $user_id = absint($request->get_param('user'));
- $notification_id = absint($request->get_param('notification_id'));
-
- if (!$this->checkUser($user_id)) {
+ $user_id = get_current_user_id();
+ if (!$user_id) {
return $this->unauthorized();
}
+ $notification_id = absint($request->get_param('notification_id'));
try {
$result = $this->notifications->transaction(function($table) use ($notification_id, $user_id) {
@@ -695,12 +685,11 @@
*/
public function markAllRead(WP_REST_Request $request): WP_REST_Response
{
- $user_id = absint($request->get_param('user'));
- $type = sanitize_text_field($request->get_param('type') ?? '');
-
- if (!$this->checkUser($user_id)) {
+ $user_id = get_current_user_id();
+ if (!$user_id) {
return $this->unauthorized();
}
+ $type = sanitize_text_field($request->get_param('type') ?? '');
try {
$where = ['owner_id' => $user_id, 'status' => 'unread'];
@@ -730,12 +719,11 @@
*/
public function markActioned(WP_REST_Request $request): WP_REST_Response
{
- $user_id = absint($request->get_param('user'));
- $notification_id = absint($request->get_param('notification_id'));
-
- if (!$this->checkUser($user_id)) {
+ $user_id = get_current_user_id();
+ if (!$user_id) {
return $this->unauthorized();
}
+ $notification_id = absint($request->get_param('notification_id'));
try {
$result = $this->notifications->transaction(function($table) use ($notification_id, $user_id) {
@@ -779,12 +767,11 @@
*/
public function markDismissed(WP_REST_Request $request): WP_REST_Response
{
- $user_id = absint($request->get_param('user'));
- $notification_id = absint($request->get_param('notification_id'));
-
- if (!$this->checkUser($user_id)) {
+ $user_id = get_current_user_id();
+ if (!$user_id) {
return $this->unauthorized();
}
+ $notification_id = absint($request->get_param('notification_id'));
try {
$result = $this->notifications->transaction(function($table) use ($notification_id, $user_id) {
@@ -979,7 +966,7 @@
}
if (!empty($queueData)) {
- JVB()->queue()->queueOperation(
+ Queue::getInstance()->queueOperation(
'notification_' . $action,
$args['user_id'],
$queueData
@@ -1318,7 +1305,7 @@
'status' => in_array($params['status'] ?? '', ['all', 'unread', 'expired'])
? $params['status']
: 'unread',
- 'user_id' => absint($params['user'] ?? get_current_user_id()),
+ 'user_id' => get_current_user_id(),
'page' => absint($params['page'] ?? 1),
'type' => in_array($params['type'] ?? '', array_keys($this->manager->notification_types ?? []))
? $params['type']
--
Gitblit v1.10.0