| | |
| | | <?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; |
| | |
| | | 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); |
| | | } |
| | |
| | | */ |
| | | 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) |
| | | ->register(); |
| | | |
| | | Route::for(Route::pattern('news/{id}')) |
| | | ->get([$this, 'getNewsItem']) |
| | | ->arg('id', 'integer|required') |
| | | ->auth(PermissionHandler::combine(['user','nonce', ['actionNonce'=>'dash-']])) |
| | | ->rateLimit(30) |
| | | ->register(); |
| | | } |
| | | |
| | | /** |
| | |
| | | { |
| | | $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']); |
| | |
| | | $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.' |
| | | ]); |
| | | } |
| | | |
| | | /** |
| | |
| | | $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'; |
| | | |
| | |
| | | |
| | | $this->cache->set($key, $results); |
| | | |
| | | return new WP_REST_Response($results); |
| | | return $this->success($results); |
| | | } |
| | | |
| | | /** |
| | |
| | | $args = $this->applyTaxonomyFilters($args, $data); |
| | | $args = $this->applyOrderFilters($args, $data); |
| | | $args = $this->applyDateFilters($args, $data); |
| | | $args = $this->applyWatchedFilter($args, $data); |
| | | |
| | | return $args; |
| | | return $this->applyWatchedFilter($args, $data); |
| | | } |
| | | |
| | | /** |
| | |
| | | { |
| | | if (array_key_exists('orderby', $data) && $data['orderby'] === 'random') { |
| | | // Handle random ordering |
| | | $current_seed = jvbGetRandomSeed(); |
| | | $current_seed = floor(time() / 1800); |
| | | $args['orderby'] = 'RAND(' . $current_seed . ')'; |
| | | unset($args['order']); |
| | | } else { |
| | |
| | | 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); |
| | | } |
| | | } |