Jake Vanderwerf
2026-02-11 fd3d3f0bc4679a3d82db100cb230c8e0a484c24c
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']]
      );
   }
@@ -442,8 +458,11 @@
         if (!$found) {
            error_log('Could not find a gallery upload field for post '.$ID);
         }
         $meta->save();
      }
      return [
         'ID'  => $ID,
         'usedUploads' => $uploadIds
@@ -636,11 +655,20 @@
         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));
      }
      $meta->save();
   }
   private function updateFieldValue(array $data, array $results): void
@@ -660,6 +688,7 @@
      $allIds = array_unique(array_merge($existingIds, $attachmentIds));
      $meta->set($data['field_name'], implode(',', $allIds));
      $meta->save();
   }
   private function getMetaManager(array $data): ?Meta
@@ -722,12 +751,14 @@
      } elseif (str_starts_with($mimeType, 'video/')) {
         $meta = Meta::forPost($postId);
         $meta->set('video', $attachmentId);
         $meta->save();
      } else {
         $meta = Meta::forPost($postId);
         $existing = $meta->get('documents');
         $existingIds = !empty($existing) ? explode(',', $existing) : [];
         $existingIds[] = $attachmentId;
         $meta->set('documents', implode(',', $existingIds));
         $meta->save();
      }
   }