Jake Vanderwerf
2026-02-11 1ee52f219a516d831b4be6bd05bce224afa28189
inc/rest/routes/UploadRoutes.php
@@ -75,7 +75,9 @@
      // Process upload groups into posts
      $registry->register('process_upload_groups', new TypeConfig(
         executor: $executor
         executor: $executor,
         chunkKey: 'posts',
         chunkSize: 5
      ));
   }
@@ -88,7 +90,9 @@
      Route::for('uploads')
         ->post([$this, 'handleUpload'])
         ->auth(PermissionHandler::combine(['nonce']))
         ->rateLimit(30);
         ->rateLimit(30)
         ->register();
      Route::for('uploads/groups')
         ->post([$this, 'handleGroupingRequest'])
         ->auth(PermissionHandler::combine(['nonce']))
@@ -97,7 +101,8 @@
            'id'     => 'string|required',
            'content'   => 'string|required',
            'user'      => 'int|required'
         ]);
         ])
         ->register();
      Route::for('uploads/meta')
         ->post([$this, 'handleMetadataUpdate'])
@@ -106,7 +111,8 @@
         ->args([
            'user'   => 'int|required',
            'items'  => 'array|required'
         ]);
         ])
         ->register();
    }
   /**
@@ -120,6 +126,19 @@
      $args = [];
      foreach ($data as $key => $value) {
         switch ($key) {
            case 'depends_on':
               if (is_string($value) && !empty($value)) {
                  $args['depends_on'] = sanitize_text_field($value);
               }
               break;
            case 'item_id':
               if (is_numeric($value)) {
                  $args['item_id'] = absint($value);
                  if (!array_key_exists('post_id', $args)) {
                     $args['post_id'] = absint($value);
                  }
               }
               break;
            // Post Type/Taxonomy
            case 'content':
               $key = str_replace('-', '_', $key);
@@ -139,7 +158,10 @@
            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('post_id', $data) &&
                     !array_key_exists('term_id', $data) &&
                     !array_key_exists('item_id', $data)) {
                     $args['post_id'] = (int)get_user_meta((int) $value, BASE.'link', true);
                  }
               }
@@ -388,6 +410,11 @@
      );
      if ($args['mode'] !== 'selection') {
         $dependencies = [$args['upload']];
         if (!empty($args['depends_on'])) {
            $dependencies[] = $args['depends_on'];
         }
         JVB()->queue()->queueOperation(
            'attach_upload_to_content',
            $args['user'],
@@ -395,7 +422,7 @@
            [
               'priority'      => 'high',
               'operation_id'  => $args['id'],
               'depends_on'    => $args['upload']
               'depends_on'    => $dependencies
            ]
         );
      }
@@ -520,6 +547,23 @@
            throw new Exception('No upload results found for operation: ' . $data['upload']);
         }
         if (empty($data['post_id']) || str_starts_with((string)($data['item_id'] ?? ''), 'new')) {
            foreach ($operation->dependencies as $depId) {
               $dep = JVB()->queue()->get($depId);
               if ($dep && $dep->type === 'content_update' && !empty($dep->result['new_posts'])) {
                  $itemId = $data['item_id'] ?? null;
                  if ($itemId && isset($dep->result['new_posts'][$itemId])) {
                     $data['post_id'] = $dep->result['new_posts'][$itemId];
                     break;
                  }
               }
            }
            if (empty($data['post_id'])) {
               throw new Exception('Could not resolve post_id from dependencies');
            }
         }
         // Now attach to the specified content
         if (!empty($data['field_name'])) {
            $this->updateFieldValue($data, $upload_results);
@@ -1137,7 +1181,8 @@
            [
               'operation_id' => $args['id'],
               'depends_on' => [$args['upload']],
               'priority' => 'high'
               'priority' => 'high',
               'chunk_key' => 'posts'
            ]
         );
@@ -1664,38 +1709,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));
      }
   }
   /**