| | |
| | | |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\managers\CustomTable; |
| | | use JVBase\Instance; |
| | | use JVBase\managers\Approvals; |
| | | use JVBase\managers\Cache; |
| | | |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\rest\PermissionHandler; |
| | | use JVBase\rest\Rest; |
| | |
| | | use JVBase\base\Site; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | | use Exception; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | |
| | | |
| | | class ApprovalRoutes extends Rest |
| | | { |
| | | use Instance; |
| | | protected array $userTypes; |
| | | protected array $termTypes; |
| | | protected array $allTypes; |
| | |
| | | $this->cacheName = 'approvals'; |
| | | $this->hasMemberApproval = Site::membership() && Site::membership()->has('member_verified'); |
| | | parent::__construct(); |
| | | $this->cache = Cache::for('approvals', 'custom', $this->cacheTtl); |
| | | |
| | | $this->initTypes(); |
| | | } |
| | |
| | | $this->termTypes = []; |
| | | $this->allTypes = []; |
| | | if ($this->hasMemberApproval) { |
| | | $this->userTypes = Registrar::getFeatured('approve_new', 'user'); |
| | | $this->userTypes = Registrar::withFeature('approve_new', 'user'); |
| | | $this->allTypes = $this->userTypes; |
| | | } |
| | | if (Site::has('term_approval')) { |
| | | $this->termTypes = Registrar::getFeatured('approve_new', 'term'); |
| | | $this->termTypes = Registrar::withFeature('approve_new', 'term'); |
| | | $this->allTypes[] = 'term'; |
| | | } |
| | | } |
| | |
| | | Route::for('approvals') |
| | | ->get([$this, 'getApprovals']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'type' => 'string', |
| | | 'status' => 'string|enum:pending,approved,rejected,expired', |
| | | ]) |
| | |
| | | ->rateLimit(30) |
| | | ->post([$this, 'handleAction']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'request_id' => 'integer|required', |
| | | 'action' => 'string|required|enum:approve,reject', |
| | | 'type' => 'string|required', |
| | |
| | | { |
| | | $data = $request->get_params(); |
| | | $request_id = absint($data['request_id']); |
| | | $user_id = absint($data['user']); |
| | | $user_id = get_current_user_id(); |
| | | $action = sanitize_text_field($data['action']); |
| | | $type = sanitize_text_field($data['type']); |
| | | $notes = sanitize_text_field($data['notes'] ?? ''); |
| | |
| | | if (!in_array($vote, ['approve', 'reject', 'dismiss'])) { |
| | | return false; |
| | | } |
| | | $result = JVB()->approvals()->markApproval($request_id, $user_id, $type, $vote, $notes); |
| | | $result = Approvals::getInstance()->markApproval($request_id, $user_id, $type, $vote, $notes); |
| | | return $result['success']; |
| | | } |
| | | |
| | |
| | | */ |
| | | public function getVerificationDetails(int $requestID, string $type): array|false |
| | | { |
| | | $request = JVB()->approvals()->getRequest($requestID, $type); |
| | | $request = Approvals::getInstance()->getRequest($requestID, $type); |
| | | if (!$request) { |
| | | return false; |
| | | } |
| | | $votes = JVB()->approvals()->getVotes($requestID, $type); |
| | | $votes = Approvals::getInstance()->getVotes($requestID, $type); |
| | | |
| | | // Join with user data for display names |
| | | foreach ($votes as &$vote) { |
| | |
| | | int $required_approvals = 3 |
| | | ): int|false { |
| | | |
| | | $result = JVB()->approvals()->createApproval( |
| | | $result = Approvals::getInstance()->createApproval( |
| | | $user_id, |
| | | $taxonomy, |
| | | $name, |
| | |
| | | return $result['success']; |
| | | } |
| | | |
| | | protected function getTableName(string $type, string $suffix): string |
| | | { |
| | | return match ($type) { |
| | | 'term' => "approval_term_{$suffix}", |
| | | default => "approval_{$type}_{$suffix}", |
| | | }; |
| | | } |
| | | |
| | | public function getApprovals(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $user_id = get_current_user_id(); |
| | | $type = sanitize_text_field($request->get_param('type') ?? 'all'); |
| | | $status = sanitize_text_field($request->get_param('status') ?? 'pending'); |
| | | |
| | | if (!$this->checkUser($user_id)) { |
| | | return $this->unauthorized(); |
| | | } |
| | | |
| | | $cacheKey = compact('user_id', 'type', 'status'); |
| | | |
| | | $result = $this->cache->remember($cacheKey, function() use ($type, $status) { |
| | | $data = []; |
| | | |
| | | if ($type === 'user' || $type === 'all') { |
| | | $data['user_approvals'] = $this->getUserApprovals($status); |
| | | } |
| | | |
| | | if ($type === 'term' || $type === 'all') { |
| | | $data['term_approvals'] = $this->getTermApprovals($status); |
| | | } |
| | | |
| | | return $data; |
| | | }); |
| | | $result = Approvals::getInstance()->getApprovalRequests($user_id, $type === 'all' ? null : $type); |
| | | |
| | | return $this->success($result); |
| | | } |
| | | |
| | | private function getUserApprovals(string $status = 'pending'): array |
| | | { |
| | | |
| | | $table = CustomTable::for($this->getTableName('artist', 'requests')); |
| | | |
| | | $query = $table; |
| | | |
| | | if ($status !== 'all') { |
| | | $query = $query->where(['status' => $status]); |
| | | } |
| | | |
| | | return $query->orderBy('created_at', 'DESC')->getResults(ARRAY_A); |
| | | } |
| | | |
| | | private function getTermApprovals(string $status = 'pending'): array |
| | | { |
| | | $table = CustomTable::for($this->getTableName('term', 'requests')); |
| | | |
| | | if ($status === 'all') { |
| | | return $table->orderBy('created_at', 'DESC')->getResults(ARRAY_A); |
| | | } |
| | | |
| | | return $table |
| | | ->where(['status' => $status]) |
| | | ->orderBy('created_at', 'DESC') |
| | | ->getResults(ARRAY_A); |
| | | } |
| | | } |