From 2127b1bdd73ecd2423e443992da4b442f5a3c1a3 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 04 Feb 2026 21:19:25 +0000
Subject: [PATCH] =Major overhaul of MetaManager.php -> Meta.php and RestRouteManager.php -> Rest.php. Seems to work for JakeVan
---
inc/rest/routes/NewsRoutes.php | 109 ++++++++++++++++++++++++++++++++++++++----------------
1 files changed, 77 insertions(+), 32 deletions(-)
diff --git a/inc/rest/routes/NewsRoutes.php b/inc/rest/routes/NewsRoutes.php
index 0811cec..bb9caae 100644
--- a/inc/rest/routes/NewsRoutes.php
+++ b/inc/rest/routes/NewsRoutes.php
@@ -1,11 +1,11 @@
<?php
namespace JVBase\rest\routes;
-use JVBase\JVB;
-use JVBase\rest\RestRouteManager;
-use JVBase\managers\CacheManager;
-use JVBase\meta\MetaManager;
+use JVBase\rest\PermissionHandler;
+use JVBase\rest\Rest;
+use JVBase\meta\Meta;
use JVBase\managers\NewsRelationships;
+use JVBase\rest\Route;
use WP_Query;
use WP_Error;
use WP_REST_Request;
@@ -15,16 +15,14 @@
exit; // Exit if accessed directly
}
-class NewsRoutes extends RestRouteManager
+class NewsRoutes extends Rest
{
protected int $per_page;
protected bool|object $manager = false;
public function __construct()
{
- $this->cache_name = 'news';
+ $this->cacheName = 'news';
parent::__construct();
- $this->action = 'dash-';
- $this->per_page = 20;
add_filter(BASE.'handle_bulk_operation', [$this, 'processOperation'], 10, 3);
}
@@ -35,18 +33,40 @@
*/
public function registerRoutes():void
{
- register_rest_route($this->namespace, '/news', [
- [
- 'methods' => 'GET',
- 'callback' => [$this, 'getNews'],
- 'permission_callback' => [$this, 'checkPermission']
- ],
- [
- 'methods' => 'POST',
- 'callback' => [$this, 'handleNewsOperation'],
- 'permission_callback' => [$this, 'checkPermission']
- ]
- ]);
+ Route::for('news')
+ ->get([$this, 'getNews'])
+ ->args([
+ 'page' => 'integer|default:1|min:1',
+ 'shop' => 'integer',
+ 'type' => 'integer',
+ 'artist' => 'array',
+ 'orderby' => 'string|enum:date,title,name,popularity,karma,random|default:date',
+ 'order' => 'string|enum:ASC,DESC|default:DESC',
+ 'date-filter' => 'string|enum:today,week,month,year',
+ 'per_page' => 'integer|default:20|min:1|max:100',
+ 'dateFrom' => 'string',
+ 'dateTo' => 'string',
+ 'watched' => 'boolean',
+ ])
+ ->auth(PermissionHandler::combine(['user','nonce', ['actionNonce'=>'dash-']]))
+ ->rateLimit(20)
+ ->post([$this, 'handleNewsOperation'])
+ ->args([
+ 'user' => 'integer|required',
+ 'id' => 'string|required',
+ 'post_title' => 'string|required',
+ 'post_excerpt' => 'string',
+ 'post_content' => 'string|required',
+ 'type' => 'integer',
+ ])
+ ->auth(PermissionHandler::combine(['user','nonce',['actionNonce'=>'dash-']]))
+ ->rateLimit(30);
+
+ Route::for(Route::pattern('news/{id}'))
+ ->get([$this, 'getNewsItem'])
+ ->arg('id', 'integer|required')
+ ->auth(PermissionHandler::combine(['user','nonce', ['actionNonce'=>'dash-']]))
+ ->rateLimit(30);
}
/**
@@ -58,8 +78,8 @@
{
$queue = JVB()->queue();
$data = $request->get_params();
- $user = $data['user'];
- $operationID = $data['id'];
+ $user = absint($data['user']);
+ $operationID = sanitize_text_field($data['id']);
unset($data['user']);
unset($data['id']);
@@ -68,16 +88,13 @@
$user,
$data,
[
- 'operation_id' => 'u'.$user.'_'.$operationID,
+ 'operation_id' => $operationID,
'priority' => 'high',
'notification' => true,
]
);
+ return $this->queued($operationID);
- return new WP_REST_Response([
- 'success' => true,
- 'message' => 'Queued for processing.'
- ]);
}
/**
@@ -91,7 +108,7 @@
$key = $this->cache->generateKey($args);
$cache = $this->cache->get($key);
if ($cache) {
- return new WP_REST_Response($cache);
+ return $this->success($cache);
}
$args['post_type'] = BASE.'news';
@@ -107,7 +124,7 @@
$this->cache->set($key, $results);
- return new WP_REST_Response($results);
+ return $this->success($results);
}
/**
@@ -369,18 +386,46 @@
unset($data['type']);
if ($ID) {
- $meta = new MetaManager($ID, 'post');
+ $meta = Meta::forPost($ID);
foreach ($data as $key => $value) {
- $m = $meta->updateValue($key, $value);
+ $m = $meta->set($key, $value);
$result[$key] = $m;
}
}
- CacheManager::invalidateGroup($this->cache_name);
+ $this->cache->flush();
return [
'success' => true,
'result' => $result
];
}
+
+ /**
+ * Get a single news item by ID
+ *
+ * @param WP_REST_Request $request
+ * @return WP_REST_Response
+ */
+ public function getNewsItem(WP_REST_Request $request): WP_REST_Response
+ {
+ $id = absint($request->get_param('id'));
+
+ $cache = $this->cache->get($id);
+ if ($cache) {
+ return $this->success($cache);
+ }
+
+ $post = get_post($id);
+
+ if (!$post || $post->post_type !== BASE.'news' || $post->post_status !== 'publish') {
+ return $this->error('News item not found', 'not_found', 404);
+ }
+
+ $item = $this->formatItem($post);
+
+ $this->cache->set($id, $item);
+
+ return $this->success($item);
+ }
}
--
Gitblit v1.10.0