Jake Vanderwerf
2026-02-11 6110744a00d28db84216ceb5313fbb10fe7b5609
inc/managers/queue/executors/UploadExecutor.php
@@ -151,27 +151,43 @@
         throw new Exception('No upload operation ID provided');
      }
      // Get results from the dependency
      $uploadOp = JVB()->queue()->get($uploadOpId);
      if (!$uploadOp || $uploadOp->outcome !== 'success') {
      if (!$uploadOp || !in_array($uploadOp->outcome, ['success', 'partial'])) {
         throw new Exception("Upload operation {$uploadOpId} not completed successfully");
      }
      $uploadResults = $uploadOp->result ?? [];
      $uploadResults = $uploadOp->result['uploads'] ?? [];
      if (empty($uploadResults)) {
         throw new Exception('No upload results found');
      }
      // Attach to content via field
      // Resolve post_id from content_update dependency for new posts
      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');
         }
      }
      if (!empty($data['field_name'])) {
         $this->updateFieldValue($data, $uploadResults);
         $this->saveToMeta($data, $uploadResults);
      }
      $progress->advance(1);
      return new Result(
         outcome: 'success',
         result: ['attached' => count($uploadResults)]
         result: ['attached' => count($uploadResults), 'post_id' => $data['post_id']]
      );
   }
@@ -636,11 +652,18 @@
         return;
      }
      $existing = $meta->get($data['field_name']);
      $existingIds = !empty($existing) ? explode(',', $existing) : [];
      $allIds = array_unique(array_merge($existingIds, $attachmentIds));
      $fieldType = $data['field_type'] ?? 'single';
      $meta->set($data['field_name'], implode(',', $allIds));
      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));
      }
   }
   private function updateFieldValue(array $data, array $results): void