Jake Vanderwerf
2026-08-29 f859e64a57c624ff2daa2b21355f65a49fd4807d
inc/managers/Notifications/Notifications.php
@@ -46,8 +46,8 @@
            ]
         ]);
      }
      $contentTax = Registrar::getFeatured('is_content', 'term');
      $verifyEntry = Registrar::getFeatured('verify_entry', 'term');
      $contentTax = Registrar::withFeature('is_content', 'term');
      $verifyEntry = Registrar::withFeature('verify_entry', 'term');
      if (!empty(array_intersect($contentTax, $verifyEntry))) {
         $types = array_merge($types, [
            'entry_requested'  => [
@@ -69,7 +69,7 @@
            ]
         ]);
      }
      $invitable = Registrar::getFeatured('invitable');
      $invitable = Registrar::withFeature('invitable');
      if (!empty($invitable)) {
         $types = array_merge($types, [
            'invitation_requested' => [
@@ -97,12 +97,12 @@
         ]);
      }
      $approvals = Registrar::getFeatured('approve_new');
      $approvals = Registrar::withFeature('approve_new');
      if (!empty($approvals)) {
         $tmp = ['user', 'term', 'post'];
         $app = [];
         foreach ($tmp as $t) {
            $approvals = Registrar::getFeatured('approve_new', $t);
            $approvals = Registrar::withFeature('approve_new', $t);
            if (!empty($approvals)) {
               $app = array_merge($app, [
                  $t.'_new' => [
@@ -315,24 +315,58 @@
      return true;
   }
   public function getUserNotifications(int $user_id, array $where = []):array
   protected function buildArgs(array $args):array
   {
      $gotWhere = $where;
      $allowedKeys = ['from_user','target_id', 'target_type','type','status','priority','action_taken','created_at','read_at','actioned_at','updated_at'];
      $where = array_filter($gotWhere, function($key) use ($allowedKeys) {
         return in_array($key, $allowedKeys);
      $allowed = $this->notifications->getColumnKeys();
      $where = array_filter($args, function($key) use ($allowed) {
         return in_array($key, $allowed);
      }, ARRAY_FILTER_USE_KEY);
      $notAllowed = array_diff($gotWhere, $allowedKeys);
      if (!empty($notAllowed)){
         error_log('[Notifications]::getUserNotifications Invalid where arguments, removed from request: '.print_r($notAllowed, true));
      if ($where['type'] === 'all') {
         unset($where['type']);
      }
      return $this->notifications->getMany(array_merge([
         'for_user'  => $user_id,
         'status' => 'unread',   //default to unread, but the merge will override if different
      ], $where));
      $args['page'] = max(1, intval($args['page']??1));
      $order = wp_parse_args($args, [
         'orderby'   => 'created_at',
         'order'     => 'DESC',
         'per_page'  => 20,
         'page'      => 1,
      ]);
      return array_merge([
         'where'  => $where,
      ],$order);
   }
   public function getUserNotifications(int $user_id, array $args = []):array
   {
      $args = $this->buildArgs($args);
      $args['where']['for_user'] = $user_id;
      if (!array_key_exists('status', $args['where'])) {
         $args['where']['status'] = 'unread';
      }
      return $this->notifications->getMany($args, false);
   }
   public function countUserNotifications(int $user_id, array $args = []):int
   {
      $args = $this->buildArgs($args);
      $args['where']['for_user'] = $user_id;
      if (!array_key_exists('status', $args['where'])) {
         $args['where']['status'] = 'unread';
      }
      return $this->notifications->count($args);
   }
   public function markRead(int $userID, int|array $notification_id):bool
   {
      if (is_int($notification_id)) {