Jake Vanderwerf
2026-09-18 a6413da67a4579866f0fd0323ddb23a7b6b9654a
inc/rest/routes/ContentTermsRoutes.php
@@ -1,13 +1,16 @@
<?php
namespace JVBase\rest\routes;
use JVBase\Instance;
use JVBase\managers\Cache;
use JVBase\managers\queue\executors\ContentTermExecutor;
use JVBase\managers\queue\Queue;
use JVBase\managers\queue\TypeConfig;
use JVBase\managers\Roles;
use JVBase\registrar\Registrar;
use JVBase\rest\Rest;
use JVBase\rest\Route;
use JVBase\rest\PermissionHandler;
use JVBase\base\Site;
use JVBase\managers\CustomTable;
use WP_REST_Request;
use WP_REST_Response;
@@ -31,12 +34,13 @@
 */
class ContentTermsRoutes extends Rest
{
   use Instance;
   protected string $taxonomy;
   protected Registrar $registrar;
   protected ?CustomTable $historyTable = null;
   protected ?CustomTable $requestsTable = null;
   public function __construct(string $taxonomy = '')
   //TODO: This needs some work. We need to move most logic into it's own manager class
   protected function __construct(string $taxonomy = '')
   {
      $this->taxonomy = jvbNoBase($taxonomy);
@@ -44,8 +48,8 @@
         $this->registrar = Registrar::getInstance($this->taxonomy);
         $this->cacheName = $this->taxonomy;
         parent::__construct();
         $this->cache = Cache::for($this->taxonomy, 'term', DAY_IN_SECONDS);
         $this->setupTables();
         $this->setupCacheConnections();
      }
      add_action('init', [$this, 'registerContentTermsExecutors'], 5);
@@ -53,7 +57,7 @@
   public function registerContentTermsExecutors():void
   {
      $registry = JVB()->queue()->registry();
      $registry = Queue::getInstance()->registry();
      $executor = new ContentTermExecutor();
      $taxonomies = Registrar::withFeature('is_content', 'term');
@@ -97,13 +101,6 @@
      }
   }
   protected function setupCacheConnections(): void
   {
      $this->cache
         ->connect('taxonomy')
         ->connect('user');
   }
   public function registerRoutes(): void
   {
      if (!$this->registrar->hasFeature('is_content')) {
@@ -205,7 +202,7 @@
      }
      return user_can($userID, 'manage_options') ||
         JVB()->roles()->isManager($userID, $termID);
         Roles::getInstance()()->isManager($userID, $termID);
   }
   public function checkOwnerPermission(WP_REST_Request $request): bool
@@ -218,7 +215,7 @@
      }
      return user_can($userID, 'manage_options') ||
         JVB()->roles()->isOwner($userID, $termID);
         Roles::getInstance()()->isOwner($userID, $termID);
   }
   /**
@@ -233,7 +230,7 @@
      unset($data['user'], $data['term_id']);
      // Queue the update
      $op = JVB()->queue()->add(
      $op = Queue::getInstance()->add(
         "{$this->taxonomy}_update",
         $userID,
         array_merge(['term_id' => $termID], $data),
@@ -255,9 +252,8 @@
      $status = $request->get_param('status');
      $page = (int)$request->get_param('page');
      $cacheKey = $this->cache->generateKey(compact('termID', 'status', 'page'));
      return $this->cache->remember($cacheKey, function() use ($termID, $status, $page) {
      return $this->cache->remember(['id'=> $termID, 'status' => $status, 'page' => $page], function() use ($termID, $status, $page) {
         $perPage = 20;
         $offset = ($page - 1) * $perPage;
@@ -266,13 +262,13 @@
         if ($status === 'active') {
            $query = $query->where(['end_date' => null]);
         } elseif ($status === 'inactive') {
            $query = $query->whereNotNull('end_date');
            $query = $query->where(['end_date'=> ['NOT NULL', null]]);
         }
         $total = $query->countResults();
         $members = $query
            ->orderBy('is_primary', 'DESC')
            ->orderBy('is_primary')
            ->orderBy('created_at', 'ASC')
            ->limit($perPage, $offset)
            ->getResults(ARRAY_A);
@@ -306,7 +302,7 @@
      $targetUserID = (int)$request->get_param('target_user');
      // Queue the operation
      $op = JVB()->queue()->add(
      $op = Queue::getInstance()->add(
         "{$this->taxonomy}_member_{$action}",
         $userID,
         [
@@ -328,9 +324,7 @@
      $status = $request->get_param('status');
      $page = (int)$request->get_param('page');
      $cacheKey = $this->cache->generateKey(compact('termID', 'status', 'page'));
      return $this->cache->remember($cacheKey, function() use ($termID, $status, $page) {
      return $this->cache->remember(['id'=>$termID, 'status' => $status,'page' => $page], function() use ($termID, $status, $page) {
         $perPage = 20;
         $offset = ($page - 1) * $perPage;
@@ -343,7 +337,7 @@
         $total = $query->countResults();
         $requests = $query
            ->orderBy('created_date', 'DESC')
            ->orderBy('created_date')
            ->limit($perPage, $offset)
            ->getResults(ARRAY_A);
@@ -417,8 +411,7 @@
         return $this->error('Failed to create request');
      } catch (Exception $e) {
         $this->logError('createRequest', [
            'error' => $e->getMessage(),
         $this->logError($e->getMessage(),'createRequest', [
            'user_id' => $userID,
            'term_id' => $termID
         ]);
@@ -468,24 +461,23 @@
            }
         });
         // Notify user
         JVB()->notification()->addNotification(
            $requestData['user_id'],
            'request_' . $status,
            [
               'term_id' => $requestData['term_id'],
               'taxonomy' => $this->taxonomy,
               'notes' => $notes
            ]
         );
         // Notify user TODO
//       NotificationManager::getInstance()->notify(
//          $requestData['user_id'],
//          'request_' . $status,
//          [
//             'term_id' => $requestData['term_id'],
//             'taxonomy' => $this->taxonomy,
//             'notes' => $notes
//          ]
//       );
         $this->cache->flush();
         return $this->success(['message' => 'Request ' . $status]);
      } catch (Exception $e) {
         $this->logError('processRequest', [
            'error' => $e->getMessage(),
         $this->logError($e->getMessage(), 'processRequest', [
            'request_id' => $requestID,
            'action' => $action
         ]);
@@ -508,7 +500,7 @@
         return $this->error('Invalid target user');
      }
      $roleManager = JVB()->roles();
      $roleManager = Roles::getInstance()();
      try {
         $result = match($role) {
@@ -532,8 +524,7 @@
         return $this->error('Failed to update permissions');
      } catch (Exception $e) {
         $this->logError('updatePermissions', [
            'error' => $e->getMessage(),
         $this->logError($e->getMessage(),'updatePermissions', [
            'term_id' => $termID,
            'target_user' => $targetUserID,
            'role' => $role
@@ -548,22 +539,23 @@
    */
   protected function notifyTermManagers(int $termID, int $actingUserID, string $notificationType): void
   {
      $roleManager = JVB()->roles();
      $roleManager = Roles::getInstance()();
      $managers = $roleManager->getManagedTerms($actingUserID, $this->taxonomy);
      $term = get_term($termID, jvbCheckBase($this->taxonomy));
      foreach ($managers as $managerID) {
         JVB()->notification()->addNotification(
            $managerID,
            $notificationType,
            [
               'user_id' => $actingUserID,
               'term_id' => $termID,
               'term_name' => $term->name ?? 'Unknown',
               'taxonomy' => $this->taxonomy
            ]
         );
      }
      //TODO
//    foreach ($managers as $managerID) {
//       NotificationManager::getInstance()->addNotification(
//          $managerID,
//          $notificationType,
//          [
//             'user_id' => $actingUserID,
//             'term_id' => $termID,
//             'term_name' => $term->name ?? 'Unknown',
//             'taxonomy' => $this->taxonomy
//          ]
//       );
//    }
   }
}