Jake Vanderwerf
2026-02-10 172a8b4404ea4ef10801d4662a68c7503aef23e1
inc/rest/routes/UploadRoutes.php
@@ -124,6 +124,15 @@
      $args = [];
      foreach ($data as $key => $value) {
         switch ($key) {
            case 'item_id':
               if (is_numeric($value)) {
                  $args['item_id'] = absint($value);
                  // item_id IS the post/term being edited — use as post_id if not already set
                  if (!array_key_exists('post_id', $args)) {
                     $args['post_id'] = absint($value);
                  }
               }
               break;
            // Post Type/Taxonomy
            case 'content':
               $key = str_replace('-', '_', $key);
@@ -143,7 +152,7 @@
            case 'user':
               if ($this->userCheck($value)) {
                  $args['user'] = (int) $value;
                  if (!array_key_exists('post_id', $data) && !array_key_exists('term_id', $data)) {
                  if (!array_key_exists('post_id', $args) && !array_key_exists('term_id', $args) && !array_key_exists('item_id', $args)) {
                     $args['post_id'] = (int)get_user_meta((int) $value, BASE.'link', true);
                  }
               }
@@ -1669,38 +1678,49 @@
      return 'none';
   }
   private function getMetaManager(array $data): ?Meta
   {
      if (!empty($data['post_id'])) {
         return Meta::forPost($data['post_id']);
      }
      if (!empty($data['term_id'])) {
         return Meta::forTerm($data['term_id']);
      }
      if (!empty($data['user'])) {
         $link = (int)get_user_meta($data['user'], BASE . 'link', true);
         if ($link) {
            return Meta::forPost($link);
         }
      }
      return null;
   }
   /**
    * Save attachment IDs to meta field
    */
   protected function saveToMeta(array $data, array $results): void
   private function saveToMeta(array $data, array $results): void
   {
      if (empty($data['field_name'])) {
         return;
      }
      $attachment_ids = array_column($results, 'attachment_id');
      // Determine meta type
      if (!empty($data['post_id'])) {
         $meta = Meta::forPost($data['post_id']);
      } elseif (!empty($data['term_id'])) {
         $meta = Meta::forTerm($data['term_id']);
      } elseif (!empty($data['user'])) {
         $link = (int)get_user_meta($data['user'], BASE.'link', true);
         $meta = Meta::forPost($link);
      } else {
      $attachmentIds = array_column($results, 'attachment_id');
      $meta = $this->getMetaManager($data);
      if (!$meta) {
         return;
      }
      // Get existing value
      $existing = $meta->get($data['field_name']);
      $existing_ids = !empty($existing) ? explode(',', $existing) : [];
      $fieldType = $data['field_type'] ?? 'single';
      // Merge with new IDs
      $all_ids = array_unique(array_merge($existing_ids, $attachment_ids));
      // Update with comma-separated string
      $meta->set($data['field_name'], implode(',', $all_ids));
      if ($fieldType === 'single') {
         // Single field: replace with latest upload
         $meta->set($data['field_name'], end($attachmentIds));
      } else {
         // Multi field: merge with existing
         $existing = $meta->get($data['field_name']);
         $existingIds = !empty($existing) ? explode(',', $existing) : [];
         $allIds = array_unique(array_merge($existingIds, $attachmentIds));
         $meta->set($data['field_name'], implode(',', $allIds));
      }
   }
   /**