Jake Vanderwerf
2026-02-04 2127b1bdd73ecd2423e443992da4b442f5a3c1a3
inc/managers/queue/executors/UploadExecutor.php
@@ -3,7 +3,7 @@
use JVBase\managers\queue\{Executor, Operation, Progress, Result};
use JVBase\managers\UploadManager;
use JVBase\meta\MetaManager;
use JVBase\meta\Meta;
use Exception;
use JVBase\utility\Features;
@@ -14,7 +14,7 @@
/**
 * Executor for upload-related queue operations.
 * Handles: image_upload, video_upload, document_upload,
 *          update_metadata, temporary_cleanup, attach_upload_to_content, process_upload_groups
 *          update_image_meta, temporary_cleanup, attach_upload_to_content, process_upload_groups
 */
final class UploadExecutor implements Executor
{
@@ -22,7 +22,7 @@
      'image_upload',
      'video_upload',
      'document_upload',
      'update_metadata',
      'update_image_meta',
      'temporary_cleanup',
      'attach_upload_to_content',
      'process_upload_groups'
@@ -41,7 +41,7 @@
            'image_upload'            => $this->processFileUpload($operation, $data, 'image', $progress),
            'video_upload'            => $this->processFileUpload($operation, $data, 'video', $progress),
            'document_upload'         => $this->processFileUpload($operation, $data, 'document', $progress),
            'update_metadata'         => $this->processMetadataUpdate($operation, $data, $progress),
            'update_image_meta'         => $this->processMetaUpdate($operation, $data, $progress),
            'temporary_cleanup'       => $this->processTemporaryCleanup($operation, $data, $progress),
            'attach_upload_to_content'=> $this->processAttachToContent($operation, $data, $progress),
            'process_upload_groups'   => $this->processUploadGroups($operation, $data, $progress),
@@ -176,29 +176,39 @@
   /**
    * Process metadata updates for attachments
    */
   private function processMetadataUpdate(Operation $operation, array $data, Progress $progress): Result
   private function processMetaUpdate(Operation $operation, array $data, Progress $progress): Result
   {
      $updatedCount = 0;
      $errors = [];
      foreach ($data as $uploadId => $info) {
         if (!is_array($info) || empty($info['depends_on'])) {
         if (!is_array($info)) {
            continue;
         }
         try {
            // Get the dependency operation to find attachment ID
            $depOp = JVB()->queue()->get($info['depends_on']);
            if (!$depOp || !$depOp->result) {
               $errors[] = "Dependency {$info['depends_on']} not found or has no result";
            if (array_key_exists('depends_on', $info)) {
               // Get the dependency operation to find attachment ID
               $depOp = JVB()->queue()->get($info['depends_on']);
               if (!$depOp || !$depOp->result) {
                  $errors[] = "Dependency {$info['depends_on']} not found or has no result";
                  continue;
               }
               $attachmentId = $this->findAttachmentByUploadId($uploadId, $depOp->result);
               if (!$attachmentId) {
                  $errors[] = "No attachment found for upload ID: {$uploadId}";
                  continue;
               }
            } else {
               $attachmentId = $info['attachmentId']??false;
            }
            if (!$attachmentId) {
               $errors[] = "No attachment found for: ".print_r($info, true);
               continue;
            }
            $attachmentId = $this->findAttachmentByUploadId($uploadId, $depOp->result);
            if (!$attachmentId) {
               $errors[] = "No attachment found for upload ID: {$uploadId}";
               continue;
            }
            $this->applyMeta($attachmentId, $info);
            $updatedCount++;
@@ -356,11 +366,11 @@
            }
            if (!empty($gallery_attachment_ids)) {
               $meta = new MetaManager($newPostID, 'post');
               $meta = Meta::forPost($newPostID);
               $fields = jvbGetFields($content, 'post');
               foreach($fields as $name => $config) {
                  if ($config['type'] === 'gallery') {
                     $meta->updateValue($name, implode(',', $gallery_attachment_ids));
                     $meta->set($name, implode(',', $gallery_attachment_ids));
                     break;
                  }
               }
@@ -385,6 +395,7 @@
      $defaultTitle = 'New '.$config['singular']. ' ';
      foreach($data['posts'] as $index => $post) {
         $progress->advance();
         $title = array_key_exists('post_title', $post['fields'])
            ? sanitize_text_field($post['fields']['post_title'])
            : $defaultTitle . ($index + 1);
@@ -405,6 +416,7 @@
         $parent = wp_insert_post($args);
         $progress->advance();
         if ($parent && !is_wp_error($parent)) {
            $childPosts = [];
            $featured = $post['fields']['featured']??null;
            $featuredID = null;
@@ -442,6 +454,8 @@
                  }
               }
            }
            $this->updateTimelineMetadata($parent);
         }
      }
      return new Result(
@@ -450,27 +464,51 @@
      );
   }
   /**
    * Update timeline parent post with count and latest date
    * @param int $parentId Parent timeline post ID
    */
   private function updateTimelineMetadata(int $parentId): void
   {
      // Get all child posts
      $children = get_children([
         'post_parent' => $parentId,
         'post_type' => get_post_type($parentId),
         'post_status' => ['publish', 'draft'],
         'orderby' => 'date',
         'order' => 'DESC',
         'fields' => 'ids'
      ]);
      // Count includes parent + children
      $number = count($children) + 1;
      // Update both meta fields
      update_post_meta($parentId, BASE . 'number', $number);
      update_post_meta($parentId, BASE . 'latest_date', time());
   }
   // ─────────────────────────────────────────────────────────────
   // Helper methods
   // ─────────────────────────────────────────────────────────────
   private function applyMeta(int $attachmentId, array $metadata): void
   {
      if (!empty($metadata['title'])) {
      if (!empty($metadata['image-title'])) {
         wp_update_post([
            'ID'         => $attachmentId,
            'post_title' => sanitize_text_field($metadata['title']),
            'post_title' => sanitize_text_field($metadata['image-title']),
         ]);
      }
      if (!empty($metadata['alt'])) {
         update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['alt']));
      if (!empty($metadata['image-alt-text'])) {
         update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['image-alt-text']));
      }
      if (!empty($metadata['caption'])) {
      if (!empty($metadata['image-caption'])) {
         wp_update_post([
            'ID'           => $attachmentId,
            'post_excerpt' => sanitize_textarea_field($metadata['caption']),
            'post_excerpt' => sanitize_textarea_field($metadata['image-caption']),
         ]);
      }
   }
@@ -504,11 +542,11 @@
         return;
      }
      $existing = $meta->getValue($data['field_name']);
      $existing = $meta->get($data['field_name']);
      $existingIds = !empty($existing) ? explode(',', $existing) : [];
      $allIds = array_unique(array_merge($existingIds, $attachmentIds));
      $meta->updateValue($data['field_name'], implode(',', $allIds));
      $meta->set($data['field_name'], implode(',', $allIds));
   }
   private function updateFieldValue(array $data, array $results): void
@@ -523,25 +561,25 @@
         return;
      }
      $existing = $meta->getValue($data['field_name']);
      $existing = $meta->get($data['field_name']);
      $existingIds = !empty($existing) ? explode(',', $existing) : [];
      $allIds = array_unique(array_merge($existingIds, $attachmentIds));
      $meta->updateValue($data['field_name'], implode(',', $allIds));
      $meta->set($data['field_name'], implode(',', $allIds));
   }
   private function getMetaManager(array $data): ?MetaManager
   private function getMetaManager(array $data): ?Meta
   {
      if (!empty($data['post_id'])) {
         return new MetaManager($data['post_id'], 'post');
         return Meta::forPost($data['post_id']);
      }
      if (!empty($data['term_id'])) {
         return new MetaManager($data['term_id'], 'term');
         return Meta::forTerm($data['term_id']);
      }
      if (!empty($data['user'])) {
         $link = (int)get_user_meta($data['user'], BASE . 'link', true);
         if ($link) {
            return new MetaManager($link, 'post');
            return Meta::forPost($link);
         }
      }
      return null;
@@ -588,14 +626,14 @@
      if (str_starts_with($mimeType, 'image/')) {
         set_post_thumbnail($postId, $attachmentId);
      } elseif (str_starts_with($mimeType, 'video/')) {
         $meta = new MetaManager($postId, 'post');
         $meta->updateValue('video', $attachmentId);
         $meta = Meta::forPost($postId);
         $meta->set('video', $attachmentId);
      } else {
         $meta = new MetaManager($postId, 'post');
         $existing = $meta->getValue('documents');
         $meta = Meta::forPost($postId);
         $existing = $meta->get('documents');
         $existingIds = !empty($existing) ? explode(',', $existing) : [];
         $existingIds[] = $attachmentId;
         $meta->updateValue('documents', implode(',', $existingIds));
         $meta->set('documents', implode(',', $existingIds));
      }
   }