From 3baf3d2545ba6ece6b74a64c0def59bd0774cf54 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 10 Jun 2026 16:34:12 +0000
Subject: [PATCH] =Laid the groundwork for an improved DashboardManager.php setup. Have to put it aside so I can get the dang Northeh done though.
---
inc/rest/routes/SettingsRoutes.php | 73 ++++++++++++++++--------------------
1 files changed, 32 insertions(+), 41 deletions(-)
diff --git a/inc/rest/routes/SettingsRoutes.php b/inc/rest/routes/SettingsRoutes.php
index f02fad9..b5d4926 100644
--- a/inc/rest/routes/SettingsRoutes.php
+++ b/inc/rest/routes/SettingsRoutes.php
@@ -2,10 +2,12 @@
namespace JVBase\rest\routes;
use JVBase\JVB;
-use JVBase\rest\RestRouteManager;
-use JVBase\managers\CacheManager;
-use JVBase\meta\MetaManager;
-use JVBase\meta\MetaSanitizer;
+use JVBase\rest\PermissionHandler;
+use JVBase\rest\Rest;
+use JVBase\managers\Cache;
+use JVBase\meta\Meta;
+use JVBase\meta\Sanitizer;
+use JVBase\rest\Route;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
@@ -14,15 +16,15 @@
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
-class SettingsRoutes extends RestRouteManager
+class SettingsRoutes extends Rest
{
protected int $count;
public function __construct()
{
- $this->cache_name = 'user_settings';
+ $this->cacheName = 'user_settings';
parent::__construct();
- $this->action = 'dash-';
- $this->operation_type = 'user_settings';
+// $this->action = 'dash-';
+// $this->operation_type = 'user_settings';
$this->count = 1;
add_filter(BASE.'handle_bulk_operation', [$this, 'processOperation'], 10, 3);
}
@@ -33,12 +35,11 @@
*/
public function registerRoutes():void
{
- register_rest_route($this->namespace, '/settings', [
- [
- 'methods' => 'POST',
- 'callback' => [$this, 'saveSettings'],
- 'permission_callback' => [$this, 'checkPermission']
- ]]);
+ Route::for('settings')
+ ->post([$this, 'saveSettings'])
+ ->auth(PermissionHandler::combine(['admin', ['actionNonce' => 'dash-']]))
+ ->rateLimit(20)
+ ->register();
}
/**
@@ -48,25 +49,18 @@
*/
public function saveSettings(WP_REST_Request $request):WP_REST_Response
{
- $this->queue = JVB()->queue();
$data = $request->get_params();
-
+ error_log('User: '.print_r($data['user'], true));
error_log('Settings routes data: '.print_r($data, true));
- $user_id = (int)$data['user'];
- if (!$this->userCheck($user_id)) {
- return new WP_REST_Response([
- 'success' => false,
- 'message' => 'Looks like you are not who you say you are. Bot?'
- ]);
+ $user_id = absint($data['user']??0);
+ if ($user_id === 0) {
+ return $this->unauthorized();
}
- $operation_id = $data['id'];
- unset($data['id']);
-
$fields = JVB()->getFields('user');
- $meta = new MetaSanitizer();
+ $meta = new Sanitizer();
$add = [];
global $types;
foreach ($data as $name => $value) {
@@ -83,8 +77,7 @@
//Sanitize values
$data[$name] = $meta->sanitize($value, $fields[$name]);
if ($name === 'notify') {
- $cache = new CacheManager('usernames');
- $cache->invalidate($user_id);
+ Cache::for('usernames')->forget($user_id);
}
}
}
@@ -92,21 +85,17 @@
$data['notification_preferences'] = $add;
}
- $this->queue->queueOperation(
- $this->type,
+ JVB()->queue()->queueOperation(
+ 'user_settings',
$user_id,
$data,
[
'count' => 1,
- 'operation_id' => $operation_id
]
);
- // Return standardized response
- return new WP_REST_Response([
- 'success' =>true,
+ return $this->success([
'message' => 'Request received and queued',
- 'operation_id' => $operation_id,
'status' => 'queued'
]);
}
@@ -126,9 +115,9 @@
$user_id = $operation->user_id;
- $meta = new MetaManager($user_id, 'user');
+ $meta = Meta::forUser($user_id);
$results = [];
- $tempMeta = new MetaManager(null, 'options');
+ $tempMeta = Meta::forOptions('options');
foreach ($data as $name => $value) {
if ($name == 'notification_preferences') {
return $this->saveNotificationPreferences($user_id, $value);
@@ -148,14 +137,15 @@
if ($name !== 'digest_override') { // digest_override should always reset to 'off'
if(array_key_exists($name, JVB_OPTIONS)) {
- $results[] = $tempMeta->updateValue($name, $value);
+ $results[] = $tempMeta->set($name, $value);
} else {
- $results[] = $meta->updateValue($name, $value);
+ $results[] = $meta->set($name, $value);
}
}
- CacheManager::invalidateGroup($this->cache_name);
+ $this->cache->flush();
}
+
return [
'success' => true,
'result' => $results,
@@ -170,6 +160,7 @@
*/
protected function saveNotificationPreferences(int $user_id, array $data):array|WP_Error
{
+ //TODO: This should be in with Notifications.php manager
//TODO: Check what cache we are using to store user's preferences for the daily/weekly/monthly sendouts
global $wpdb;
$table = $wpdb->prefix . BASE . 'notification_preferences';
@@ -223,7 +214,7 @@
// Success - commit transaction
$wpdb->query('COMMIT');
- CacheManager::invalidateGroup($this->cache_name);
+ $this->cache->flush();
return [
'success' => true,
'result' => 'Notification preferences updated successfully'
--
Gitblit v1.10.0