Jake Vanderwerf
2026-07-05 fff721dd185f5b97f7ae7a6e64189e55887ff590
inc/rest/routes/ContentTermsRoutes.php
@@ -3,10 +3,11 @@
use JVBase\managers\queue\executors\ContentTermExecutor;
use JVBase\managers\queue\TypeConfig;
use JVBase\registrar\Registrar;
use JVBase\rest\Rest;
use JVBase\rest\Route;
use JVBase\rest\PermissionHandler;
use JVBase\utility\Features;
use JVBase\base\Site;
use JVBase\managers\CustomTable;
use WP_REST_Request;
use WP_REST_Response;
@@ -31,7 +32,7 @@
class ContentTermsRoutes extends Rest
{
   protected string $taxonomy;
   protected array $config;
   protected Registrar $registrar;
   protected ?CustomTable $historyTable = null;
   protected ?CustomTable $requestsTable = null;
@@ -39,8 +40,8 @@
   {
      $this->taxonomy = jvbNoBase($taxonomy);
      if ($taxonomy && isset(JVB_TAXONOMY[$this->taxonomy])) {
         $this->config = JVB_TAXONOMY[$this->taxonomy];
      if ($taxonomy !== '' && Registrar::getInstance($this->taxonomy)) {
         $this->registrar = Registrar::getInstance($this->taxonomy);
         $this->cacheName = $this->taxonomy;
         parent::__construct();
         $this->setupTables();
@@ -54,14 +55,15 @@
   {
      $registry = JVB()->queue()->registry();
      $executor = new ContentTermExecutor();
      $taxonomies = Features::getTypesWithFeature('is_content', 'taxonomy');
      $taxonomies = Registrar::withFeature('is_content', 'term');
      foreach($taxonomies as $taxonomy) {
         $registry->register("{$taxonomy}_update", new TypeConfig(
            executor: $executor,
         ));
         if (Features::forTaxonomy($taxonomy)->has('track_changes')) {
         $registrar = Registrar::getInstance($taxonomy);
         if ($registrar && $registrar->hasFeature('track_changes')) {
            $registry->register("{$taxonomy}_member_add", new TypeConfig(
               executor: $executor
            ));
@@ -76,9 +78,9 @@
   protected function setupTables(): void
   {
      $content = $this->config['for_content'] ?? [];
      $content = $this->registrar->registrar->for;
      if (Features::forTaxonomy($this->taxonomy)->has('track_changes') && !empty($content)) {
      if ($this->registrar->hasFeature('track_changes') && !empty($content)) {
         foreach ($content as $contentType) {
            $tableName = "history_{$contentType}_{$this->taxonomy}";
            $this->historyTable = CustomTable::for($tableName);
@@ -86,7 +88,7 @@
         }
      }
      if (Features::forTaxonomy($this->taxonomy)->has('verify_entry') && !empty($content)) {
      if ($this->registrar->hasFeature('verify_entry') && !empty($content)) {
         foreach ($content as $contentType) {
            $tableName = "{$contentType}_{$this->taxonomy}_requests";
            $this->requestsTable = CustomTable::for($tableName);
@@ -104,7 +106,7 @@
   public function registerRoutes(): void
   {
      if (!Features::forTaxonomy($this->taxonomy)->has('is_content')) {
      if (!$this->registrar->hasFeature('is_content')) {
         return;
      }
@@ -117,11 +119,12 @@
            'user' => 'int|required',
            'term_id' => 'int|required'
         ])
         ->auth(PermissionHandler::custom([$this, 'checkTermPermission']))
         ->rateLimit(10);
         ->auth(PermissionHandler::combine([[$this, 'checkTermPermission']]))
         ->rateLimit(10)
         ->register();
      // Member management (if track_changes enabled)
      if (Features::forTaxonomy($this->taxonomy)->has('track_changes')) {
      if ($this->registrar->hasFeature('track_changes')) {
         Route::for("{$base}/:term_id/members")
            ->get([$this, 'getMembers'])
            ->args([
@@ -139,12 +142,13 @@
               'target_user' => 'int|required',
               'action' => 'string|enum:add,remove|required'
            ])
            ->auth(PermissionHandler::custom([$this, 'checkTermPermission']))
            ->rateLimit(5);
            ->auth(PermissionHandler::combine([[$this, 'checkTermPermission']]))
            ->rateLimit(5)
            ->register();
      }
      // Membership requests (if verify_entry enabled)
      if (Features::forTaxonomy($this->taxonomy)->has('verify_entry')) {
      if ($this->registrar->hasFeature('verify_entry')) {
         Route::for("{$base}/:term_id/requests")
            ->get([$this, 'getRequests'])
            ->args([
@@ -153,8 +157,9 @@
               'status' => 'string|enum:requested,accepted,rejected,all|default:requested',
               'page' => 'int|default:1|min:1'
            ])
            ->auth(PermissionHandler::custom([$this, 'checkTermPermission']))
            ->rateLimit(20);
            ->auth(PermissionHandler::combine([[$this, 'checkTermPermission']]))
            ->rateLimit(20)
            ->register();
         Route::for("{$base}/request")
            ->post([$this, 'handleRequest'])
@@ -166,11 +171,12 @@
               'notes' => 'string'
            ])
            ->auth('verified')
            ->rateLimit(5);
            ->rateLimit(5)
            ->register();
      }
      // Ownership/management (if is_ownable enabled)
      if (Features::forTaxonomy($this->taxonomy)->has('is_ownable')) {
      if ($this->registrar->hasFeature('is_ownable')) {
         Route::for("{$base}/:term_id/permissions")
            ->post([$this, 'updatePermissions'])
            ->args([
@@ -180,8 +186,9 @@
               'role' => 'string|enum:owner,manager|required',
               'grant' => 'bool|required'
            ])
            ->auth(PermissionHandler::custom([$this, 'checkOwnerPermission']))
            ->rateLimit(5);
            ->auth(PermissionHandler::combine([[$this, 'checkTermPermission']]))
            ->rateLimit(5)
            ->register();
      }
   }
@@ -190,7 +197,7 @@
    */
   public function checkTermPermission(WP_REST_Request $request): bool
   {
      $userID = $request->get_param('user') ?? get_current_user_id();
      $userID = get_current_user_id();
      $termID = (int)$request->get_param('term_id');
      if (!$this->checkUser($userID) || !term_exists($termID, jvbCheckBase($this->taxonomy))) {
@@ -203,10 +210,10 @@
   public function checkOwnerPermission(WP_REST_Request $request): bool
   {
      $userID = $request->get_param('user') ?? get_current_user_id();
      $userID = get_current_user_id();
      $termID = (int)$request->get_param('term_id');
      if (!$this->checkUser($userID) || !term_exists($termID, jvbCheckBase($this->taxonomy))) {
      if (!term_exists($termID, jvbCheckBase($this->taxonomy))) {
         return false;
      }
@@ -220,7 +227,7 @@
   public function updateSettings(WP_REST_Request $request): WP_REST_Response
   {
      $termID = (int)$request->get_param('term_id');
      $userID = $request->get_param('user');
      $userID = get_current_user_id();
      $data = $request->get_params();
      unset($data['user'], $data['term_id']);
@@ -294,14 +301,10 @@
   public function manageMember(WP_REST_Request $request): WP_REST_Response
   {
      $action = $request->get_param('action');
      $userID = $request->get_param('user');
      $userID = get_current_user_id();
      $termID = (int)$request->get_param('term_id');
      $targetUserID = (int)$request->get_param('target_user');
      if (!$this->checkUser($targetUserID)) {
         return $this->error('Invalid target user');
      }
      // Queue the operation
      $op = JVB()->queue()->add(
         "{$this->taxonomy}_member_{$action}",
@@ -360,7 +363,7 @@
   public function handleRequest(WP_REST_Request $request): WP_REST_Response
   {
      $action = $request->get_param('action');
      $userID = $request->get_param('user');
      $userID = get_current_user_id();
      $termID = (int)$request->get_param('term_id');
      return match($action) {