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 | 88 ++++++++++++++++++++-----------------------
1 files changed, 41 insertions(+), 47 deletions(-)
diff --git a/inc/rest/routes/NotificationsRoutes.php b/inc/rest/routes/NotificationsRoutes.php
index f523b62..fd04460 100644
--- a/inc/rest/routes/NotificationsRoutes.php
+++ b/inc/rest/routes/NotificationsRoutes.php
@@ -1,8 +1,10 @@
<?php
namespace JVBase\rest\routes;
+use JVBase\Instance;
use JVBase\managers\Cache;
use JVBase\managers\CustomTable;
+use JVBase\registrar\Registrar;
use JVBase\rest\Rest;
use JVBase\rest\Route;
use WP_REST_Request;
@@ -22,6 +24,7 @@
*/
class NotificationsRoutes extends Rest
{
+ use Instance;
protected int $user_id;
protected array $notification_types = [];
protected object $manager;
@@ -106,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');
@@ -132,8 +132,8 @@
*/
public function init(): void
{
- $this->manager = JVB()->notification();
- $this->notification_types = $this->manager->getNotificationTypes();
+ $this->manager = NotificationManager::getInstance();
+ $this->notification_types = $this->manager->getNotificationTypes(true);
}
/**
@@ -145,64 +145,64 @@
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',
'offset' => 'integer|default:0',
])
->auth('user')
- ->rateLimit(30);
+ ->rateLimit(30)
+ ->register();
// Mark as read
Route::for('notifications/read')
->post([$this, 'markRead'])
->args([
- 'user' => 'integer|required',
'notification_id' => 'integer|required',
])
->auth('user')
- ->rateLimit(30);
+ ->rateLimit(30)
+ ->register();
// Mark all as read
Route::for('notifications/read-all')
->post([$this, 'markAllRead'])
->args([
- 'user' => 'integer|required',
'type' => 'string',
])
->auth('user')
- ->rateLimit(10);
+ ->rateLimit(10)
+ ->register();
// Mark as actioned
Route::for('notifications/action')
->post([$this, 'markActioned'])
->args([
- 'user' => 'integer|required',
'notification_id' => 'integer|required',
])
->auth('user')
- ->rateLimit(30);
+ ->rateLimit(30)
+ ->register();
// Dismiss notification
Route::for('notifications/dismiss')
->post([$this, 'markDismissed'])
->args([
- 'user' => 'integer|required',
'notification_id' => 'integer|required',
])
->auth('user')
- ->rateLimit(30);
+ ->rateLimit(30)
+ ->register();
// Get unread count
Route::for('notifications/count')
->get([$this, 'getUnreadCount'])
->args([
- 'user' => 'integer|required',
'type' => 'string',
])
->auth('user')
- ->rateLimit(60);
+ ->rateLimit()
+ ->register();
}
// =========================================================================
@@ -214,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) {
@@ -258,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');
@@ -548,7 +546,7 @@
$statusCondition = $wpdb->prepare("a.status = %s", $status);
}
- $approvals = jvbApprovalTypes();
+ $approvals = Registrar::withFeature('approve_new');
foreach ($approvals as $type => $config) {
$table = $wpdb->prefix . BASE . 'approval_' . $type . 'requests';
$votes = $wpdb->prefix . BASE . 'approval_' . $type . 'votes';
@@ -640,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) {
@@ -688,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'];
@@ -723,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) {
@@ -772,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) {
@@ -972,7 +966,7 @@
}
if (!empty($queueData)) {
- JVB()->queue()->queueOperation(
+ Queue::getInstance()->queueOperation(
'notification_' . $action,
$args['user_id'],
$queueData
@@ -1178,7 +1172,7 @@
/**
* Get notification actions
*/
- protected function getNotificationActions(string $type, array $data, object $notification = null): array
+ protected function getNotificationActions(string $type, array $data, ?object $notification = null): array
{
$actions = [];
@@ -1311,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