Jake Vanderwerf
5 days ago a9b3b28d001941921aa70d37fdc87c758a163a44
inc/rest/routes/UploadRoutes.php
@@ -1,11 +1,15 @@
<?php
namespace JVBase\rest\routes;
use JVBase\JVB;
use JVBase\rest\RestRouteManager;
use JVBase\meta\MetaManager;
use JVBase\managers\queue\executors\UploadExecutor;
use JVBase\managers\queue\mergers\UploadMerger;
use JVBase\managers\queue\TypeConfig;
use JVBase\registrar\Registrar;
use JVBase\rest\PermissionHandler;
use JVBase\rest\Rest;
use JVBase\meta\Meta;
use JVBase\managers\UploadManager;
use JVBase\utility\Features;
use JVBase\rest\Route;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
@@ -14,66 +18,107 @@
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
class UploadRoutes extends RestRouteManager
class UploadRoutes extends Rest
{
    public function __construct()
    {
      $this->action = 'dash-';
        parent::__construct();
        add_filter(BASE.'handle_bulk_operation', [$this, 'processOperation'], 10, 3);
      add_action('init', [$this, 'registerUploadExecutors'], 5);
    }
   /**
    * Register upload operation types with the queue's TypeRegistry
    */
   public function registerUploadExecutors(): void
   {
      $registry = JVB()->queue()->registry();
      $executor = new UploadExecutor();
      $merger = new UploadMerger('secured_files');
      // Image uploads - chunked at 5 files
      $registry->register('image_upload', new TypeConfig(
         mergeable: $merger,
         executor: $executor,
         chunkKey: 'secured_files',
         chunkSize: 3
      ));
      // Video uploads - one at a time (heavy processing)
      $registry->register('video_upload', new TypeConfig(
         mergeable: $merger,
         executor: $executor,
         chunkKey: 'secured_files',
         chunkSize: 1
      ));
      // Document uploads - chunked at 10
      $registry->register('document_upload', new TypeConfig(
         mergeable: $merger,
         executor: $executor,
         chunkKey: 'secured_files',
         chunkSize: 5
      ));
      // Metadata updates
      $registry->register('update_image_meta', new TypeConfig(
         executor: $executor
      ));
      // Cleanup - chunked at 5
      $registry->register('temporary_cleanup', new TypeConfig(
         executor: $executor,
         chunkKey: 'files',
         chunkSize: 5
      ));
      // Attach to content (depends on upload completing)
      $registry->register('attach_upload_to_content', new TypeConfig(
         executor: $executor
      ));
      // Process upload groups into posts
      $registry->register('process_upload_groups', new TypeConfig(
         executor: $executor,
         chunkKey: 'posts',
         chunkSize: 5
      ));
   }
    /**
     * Registers upload routes
     * @return void
     */
    public function registerRoutes():void
    {
      // Main upload endpoint
      register_rest_route($this->namespace, '/uploads', [
         'methods' => 'POST',
         'callback' => [$this, 'handleUpload'],
         'permission_callback' => [$this, 'checkPermission']
      ]);
      Route::for('uploads')
         ->post([$this, 'handleUpload'])
         ->auth(PermissionHandler::combine(['nonce']))
         ->rateLimit(30)
         ->register();
      register_rest_route($this->namespace, '/uploads/groups', [
         'methods' => 'POST',
         'callback' => [$this, 'handleGroupingRequest'],
         'permission_callback' => [$this, 'checkPermission'],
         'args' => [
            'id' => [
               'required' => true,
               'type' => 'string',
               'description' => 'Original upload operation ID'
            ],
            'content' => [
               'required' => true,
               'type' => 'string'
            ],
            'user' => [
               'required' => true,
               'type' => 'integer'
            ],
         ]
      ]);
      Route::for('uploads/groups')
         ->post([$this, 'handleGroupingRequest'])
         ->auth(PermissionHandler::combine(['nonce']))
         ->rateLimit(30)
         ->args([
            'id'     => 'string|required',
            'content'   => 'string|required',
            'user'      => 'int|required'
         ])
         ->register();
      register_rest_route($this->namespace, '/uploads/meta', [
         'methods' => 'POST',
         'callback' => [$this, 'handleMetadataUpdate'],
         'permission_callback' => [$this, 'checkPermission'],
         'args' => [
            'user' => [
               'type' => 'integer',
               'required' => true
            ],
            'items' => [
               'type' => 'array',
               'required' => true,
               'description' => 'Direct attachment IDs (for updates after completion)',
            ],
         ]
      ]);
      Route::for('uploads/meta')
         ->post([$this, 'handleMetadataUpdate'])
         ->auth(PermissionHandler::combine(['nonce']))
         ->rateLimit(30)
         ->args([
            'user'   => 'int|required',
            'items'  => 'array|required',
            'id'    => 'string'
         ])
         ->register();
    }
   /**
@@ -85,12 +130,37 @@
    {
        $data = $request->get_params();
      $args = [];
      $registrar = Registrar::getInstance($data['content']??'');
      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 ($registrar) {
                     switch ($registrar->getType()) {
                        case 'post':
                           $args['post_id'] = absint($value);
                           break;
                        case 'term':
                           $args['term_id'] = absint($value);
                           break;
                        case 'user':
                           $args['user_id'] = absint($value);
                           break;
                     }
                  }
               }
               break;
            // Post Type/Taxonomy
            case 'content':
               $key = str_replace('-', '_', $key);
               if ($value === 'options' || array_key_exists($value, JVB_CONTENT) || Features::forTaxonomy($key)->has('is_content')) {
               $value = str_replace('-', '_', $value);
               if ($value === 'options' || $registrar) {
                  $args['content'] = $value;
               }
               break;
@@ -106,8 +176,11 @@
            case 'user':
               if ($this->userCheck($value)) {
                  $args['user'] = (int) $value;
                  if (!array_key_exists('post_id', $data) && !array_key_exists('term_id', $data)) {
                     $args['post_id'] = (int)get_user_meta((int) $value, BASE.'link', true);
                  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.'profile_link', true);
                  }
               }
               break;
@@ -131,7 +204,7 @@
                  $args['term_id'] = absint($value);
               }
               break;
            // Field Name, as defined for MetaManager.php
            // Field Name, as defined for Meta.php
            case 'field_name':
               if (is_string($value)) {
                  $args['field_name'] = sanitize_text_field($value);
@@ -213,13 +286,12 @@
         $files = $request->get_file_params();
         $args = $this->buildUploadArgs($request);
         if (!$args['content'] || !$args['user']) {
            $this->logError('Missing required data');
            return new WP_REST_Response([
               'success'   => false,
               'message'   => 'Missing required data'
            ]);
         if (!$args['user']) {
             return $this->unauthorized();
         }
         if (!$args['content']) {
            return $this->validationError(['message' => 'Missing content']);
         }
         // Step 1: Secure all uploaded files
@@ -227,21 +299,13 @@
         if (empty($secured_files)) {
            $this->logError('No valid files to upload');
            return new WP_REST_Response([
               'success'   => false,
               'message'   => 'No valid files to upload'
            ]);
            return $this->error('No valid files to upload');
         }
         // Step 2: Queue for processing via OperationQueue
         $operation_id = $this->queueProcessing($secured_files, $args);
         return new WP_REST_Response([
            'success' => true,
            'operation_id' => $operation_id,
            'file_count' => count($secured_files),
            'message' => 'Files secured and queued for processing'
         ], 200);
         return $this->queued($operation_id, 'Files secured and queued for processing');
      } catch (Exception $e) {
         // Error handling...
@@ -254,12 +318,7 @@
               'trace' => $e->getTraceAsString()
            ]
         );
         return $this->sendResponse(
            false,
            ['error_code' => 'upload_failed'],
            'Upload processing failed: ' . $e->getMessage()
         );
         return $this->error($e->getMessage());
      }
   }
@@ -270,26 +329,27 @@
   {
      $uploader = new UploadManager();
      $secured_files = [];
      $upload_map = [];
      $errors = [];
      $context = $args;
      unset($context['upload_ids']);
      $config = $this->getFieldConfig($args);
      $file_array = $files['files'] ?? $files;
      $tmp_names = isset($file_array['tmp_name'][0]) && is_array($file_array['tmp_name'][0])
         ? $file_array['tmp_name'][0]
         : $file_array['tmp_name'];
      if (!is_array($file_array['tmp_name'])) {
         $file_array = [
            'name' => [$file_array['name']],
            'type' => [$file_array['type']],
            'tmp_name' => [$file_array['tmp_name']],
            'error' => [$file_array['error']],
            'size' => [$file_array['size']]
         ];
      }
      $tmp_names = $file_array['tmp_name'] ?? [];
      foreach ($tmp_names as $index => $tmp_name) {
         $file_data = isset($file_array['tmp_name'][0]) && is_array($file_array['tmp_name'][0]) ? [
            'name' => $file_array['name'][0][$index],
            'type' => $file_array['type'][0][$index],
            'tmp_name' => $tmp_name,
            'error' => $file_array['error'][0][$index],
            'size' => $file_array['size'][0][$index]
         ] : [
         $file_data = [
            'name' => $file_array['name'][$index],
            'type' => $file_array['type'][$index],
            'tmp_name' => $tmp_name,
@@ -309,8 +369,8 @@
               throw new Exception('Failed to secure file');
            }
            $frontend_id = $args['upload_ids'][$index] ?? 'upload_' . $index;
            $upload_map[$index] = $frontend_id;
            // Embed upload_id directly in the secured file data
            $secured['upload_id'] = $args['upload_ids'][$index] ?? 'upload_' . $index;
            $secured_files[] = $secured;
         } catch (Exception $e) {
@@ -320,7 +380,6 @@
      return [
         'files' => $secured_files,
         'upload_map' => $upload_map,
         'errors' => $errors
      ];
   }
@@ -343,7 +402,7 @@
   /**
    * Queue files for processing
    */
   protected function queueProcessing(array $secured_data, array $args):string
   protected function queueProcessing(array $secured_data, array $args): string
   {
      $operation_type = $this->determineOperationType($secured_data['files'][0] ?? []);
@@ -353,50 +412,61 @@
      } elseif ($operation_type === 'document') {
         $chunkSize = 10;
      }
      JVB()->queue()->queueOperation(
      error_log('Queueing Operation: '.print_r($operation_type, true));
      error_log('With ID: '.print_r($args['upload'], true));
      $queuedProcessing = JVB()->queue()->queueOperation(
         $operation_type,
         $args['user'],
         array_merge(
            [
               'secured_files'   => $secured_data['files'],
               'upload_map'      => $secured_data['upload_map'],
            ],
            ['secured_files' => $secured_data['files']],
            $args
         ),
         [
            'operation_id' => $args['upload'],
            'chunk_key'    => 'secured_files',
            'chunk_size'   => $chunkSize
            'operation_id'  => $args['upload'],
            'chunk_key'     => 'secured_files',
            'chunk_size'    => $chunkSize
         ]
      );
      error_log('queuedProcessing operation: '.print_r($queuedProcessing, true));
      $uploadOpId = $queuedProcessing['operation_id'];
      if ($args['mode'] !== 'selection') {
         JVB()->queue()->queueOperation(
            'attach_upload_to_content',
            $args['user'],
            $args,
            [
               'priority'     => 'high',
               'operation_id' => $args['id'],
               'depends_on'   => $args['upload']
            ]
         );
         // Only create attach_upload_to_content if the upload was NOT merged.
         // When merged, the original upload's attach_upload_to_content
         // will handle all files after the merged image_upload completes.
         if (!$queuedProcessing['updated_existing']) {
            JVB()->queue()->queueOperation(
               'attach_upload_to_content',
               $args['user'],
               $args,
               [
                  'priority'      => 'high',
                  'operation_id'  => $args['id'],
                  'depends_on'    => [$uploadOpId]
               ]
            );
         }
      }
      JVB()->queue()->queueOperation(
         'temporary_cleanup',
         $args['user'],
         [
            'files'     => $secured_data['files'],
            'context'   => $args,
            'files'     => $secured_data['files'],
            'context'   => $args,
         ],
         [
            'priority'     => 'low',
            'chunk_size'   => 5,
            'chunk_key'    => 'files',
            'depends_on'   => $args['upload']
            'priority'      => 'low',
            'chunk_size'    => 5,
            'chunk_key'     => 'files',
            'depends_on'    => [$uploadOpId]
         ]
      );
      return $args['id'];
   }
@@ -502,6 +572,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);
@@ -548,23 +635,23 @@
      $attachment_ids = array_column($results, 'attachment_id');
      if (array_key_exists('post_id', $data)) {
         $meta = new MetaManager($data['post_id'], 'post');
         $meta = Meta::forPost($data['post_id']);
      } elseif (array_key_exists('term_id', $data)) {
         $meta = new MetaManager($data['term_id'], 'term');
         $meta = Meta::forTerm($data['term_id']);
      } else {
         $link = (int)get_user_meta($data['user'], BASE.'link');
         $meta = new MetaManager($link, 'post');
         $link = (int)get_user_meta($data['user'], BASE.'profile_link');
         $meta = Meta::forPost($link);
      }
      // Get existing value
      $existing = $meta->getValue($data['field_name']);
      $existing = $meta->get($data['field_name']);
      $existing_ids = !empty($existing) ? explode(',', $existing) : [];
      // Merge with new IDs
      $all_ids = array_unique(array_merge($existing_ids, $attachment_ids));
      // Update with comma-separated string
      $meta->updateValue($data['field_name'], implode(',', $all_ids));
      $meta->set($data['field_name'], implode(',', $all_ids));
   }
   /**
@@ -579,9 +666,8 @@
         $args = $data;
         unset($args['secured_files']);
         unset($args['upload_map']);
         foreach ($data['secured_files'] as $index => $secured_file) {
         foreach ($data['secured_files'] as $secured_file) {
            $result = $uploader->processUpload(
               $secured_file['temp_path'],
               array_merge(
@@ -593,7 +679,7 @@
                     'term_id' => (int)($data['term_id'] ?? 0),
                     'original_name' => $secured_file['original_name'],
                     'mime_type' => $secured_file['mime_type'],
                     'content' => $data['content'] ?? '', // For directory pattern
                     'content' => $data['content'] ?? '',
                  ]
               )
            );
@@ -601,20 +687,19 @@
            if (!is_wp_error($result)) {
               $standardized = $this->standardizeResult($result);
               // Map to frontend upload ID
               if (isset($data['upload_map'][$index])) {
                  $standardized['upload_id'] = $data['upload_map'][$index];
               // Use embedded upload_id from the secured file itself
               $standardized['upload_id'] = $secured_file['upload_id'] ?? null;
               if ($standardized['upload_id']) {
                  $processed_results[$standardized['upload_id']] = $standardized;
               } else {
                  $processed_results[] = $standardized;
               }
               // Apply frontend metadata if provided
               if (!empty($data['frontend_metadata'][$index])) {
                  $this->applyMeta(
                     $standardized['attachment_id'],
                     $data['frontend_metadata'][$index]
                  );
               if (!empty($secured_file['metadata'])) {
                  $this->applyMeta($standardized['attachment_id'], $secured_file['metadata']);
               }
               $processed_results[$standardized['upload_id']] = $standardized;
            }
         }
@@ -639,8 +724,8 @@
            ]
         );
         return [
            'success'   => false,
            'result' => $e->getMessage()
            'success'   => false,
            'result'    => $e->getMessage()
         ];
      }
   }
@@ -731,14 +816,8 @@
      try {
         $data = $request->get_params();
         // Validate user permissions
         if (!$this->userCheck($data['user'])) {
            return $this->sendResponse(
               false,
               ['error_code' => 'invalid_user'],
               'Invalid user specified'
            );
         }
         error_log('Received data for meta change: '.print_r($data, true));
         $items = $data['items']??false;
         if (!$items) {
@@ -751,23 +830,15 @@
         }
         $pending = [];
         $attachments = array_filter($items, function ($item) {
            return is_numeric($item);
         }, ARRAY_FILTER_USE_KEY);
         if (count($attachments) !== count($items)) {
            $pending = array_filter($items, function ($item) {
               return !is_numeric($item);
            }, ARRAY_FILTER_USE_KEY);
         }
            return array_key_exists('attachmentId', $item) || array_key_exists('uploadId', $item);
         });
         if (!empty($attachments)) {
            // Phase 2B: Direct attachment update (images already processed)
            return $this->updateMeta($attachments, $data['user']);
            error_log('Attachments: '.print_r($attachments, true));
            return $this->queueMetaUpdate($attachments, absint($data['user']), sanitize_text_field($data['id']??''));
         }
         elseif (!empty($pending)) {
            // Phase 2A: Queue metadata update with dependency on upload operation
            return $this->queueMetaUpdate($pending, $data['user']);
         }
         return $this->sendResponse(
            false,
@@ -799,9 +870,13 @@
   {
      $updated_count = 0;
      $errors = [];
      foreach ($data as $attachment_id => $info) {
      $ids = [];
      foreach ($data as $info) {
         try {
            $attachment_id = $info['attachmentId'];
            error_log('Updating attachment ID:'.print_r($attachment_id,true));
            $ids[] = $attachment_id;
            unset($info['attachmentId']);
            // Verify attachment exists and user has permission
            if (!$this->verifyAttachmentAccess($attachment_id, $user)) {
               $errors[] = "No permission to edit attachment {$attachment_id}";
@@ -821,7 +896,7 @@
         [
            'updated_count' => $updated_count,
            'errors' => $errors,
            'attachment_ids' => $data['attachment_ids']
            'attachment_ids' => $ids
         ],
         $updated_count > 0 ?
            "Updated metadata for {$updated_count} attachment(s)" :
@@ -831,36 +906,34 @@
   /**
    * Queue metadata update with dependency on upload operation
    */
   protected function queueMetaUpdate(array $data, int $user): WP_REST_Response
   protected function queueMetaUpdate(array $data, int $user, ?string $operationId = null): WP_REST_Response
   {
      $queue = JVB()->queue();
      $depends_on = [];
      $errors = [];
      $original = count($data);
      foreach ($data as $uploadID => $info) {
         if (!array_key_exists('depends_on', $info)) {
            unset($data[$uploadID]);
            $errors[$uploadID] = $info;
            continue;
         }
         if (!in_array($info['depends_on'], $depends_on)) {
         if (array_key_exists('depends_on', $info) && !in_array($info['depends_on'], $depends_on)) {
            $depends_on[] = $info['depends_on'];
         }
      }
      $queueData = [
         'depends_on' => $depends_on,
      ];
      if ($operationId) {
         $queueData['operation_id'] = $operationId;
      }
      $operationID = $queue->queueOperation(
         'update_metadata',
         'update_image_meta',
         $user,
         $data,
         [
            'depends_on' => $depends_on,
            'priority' => 'medium',
         ]
         $queueData
      );
      return $this->sendResponse(
         true,
         [
            'operation_id' => $operationID,
            'operation_id' => $operationID['operation_id']??$operationId,
            'message'      => "Successfully queued ".count($data)." of {$original} meta updates"
         ],
         'Metadata update queued - will apply after upload completes'
@@ -880,7 +953,7 @@
         $errors = [];
         foreach ($operation->depends_on as $dependency) {
            $operationData = JVB()->queue()->getOperation($dependency);
            if (!$operationData || $operationData->status !== 'completed') {
            if (!$operationData || $operationData->state !== 'completed') {
               throw new Exception('Original upload operation not found or not completed');
            }
@@ -941,18 +1014,18 @@
   protected function applyMeta(int $attachment_id, array $metadata): void
   {
      // Update alt text
      if (!empty($metadata['alt'])) {
         update_post_meta($attachment_id, '_wp_attachment_image_alt', sanitize_text_field($metadata['alt']));
      if (!empty($metadata['image-alt-text'])) {
         update_post_meta($attachment_id, '_wp_attachment_image_alt', sanitize_text_field($metadata['image-alt-text']));
      }
      $postUpdates = [];
      // Update title
      if (!empty($metadata['title'])) {
         $postUpdates['post_title'] = $metadata['title'];
      if (!empty($metadata['image-title'])) {
         $postUpdates['post_title'] = $metadata['image-title'];
      }
      // Update caption
      if (!empty($metadata['caption'])) {
         $postUpdates['post_excerpt'] = sanitize_textarea_field($metadata['caption']);
      if (!empty($metadata['image-caption'])) {
         $postUpdates['post_excerpt'] = sanitize_textarea_field($metadata['image-caption']);
      }
      if (!empty($postUpdates)) {
@@ -1028,9 +1101,9 @@
      if (!empty($args['content']) && !empty($args['field_name'])) {
         $content_type = $args['content'];
         $field_name = $args['field_name'];
         if (array_key_exists($content_type, JVB_CONTENT)) {
            $content_fields = JVB_CONTENT[$content_type]['fields'] ?? [];
         $registrar = Registrar::getInstance($content_type);
         if ($registrar) {
            $content_fields = $registrar->getFields();
            if (array_key_exists($field_name, $content_fields)) {
               $field_def = $content_fields[$field_name];
@@ -1077,40 +1150,30 @@
         $response['operation_id'] = $operation_id;
      }
      return new WP_REST_Response($response);
      return $this->success($response);
   }
   public function handleGroupingRequest(WP_REST_Request $request): WP_REST_Response
   {
      try {
         $files = $request->get_file_params();
         $args = $this->buildUploadArgs($request);
         $data = $request->get_params();
         global $_FILES;
         if (!$args['content'] || !$args['user'] || !$args['posts']) {
            $this->logError('Missing required data');
            return new WP_REST_Response([
               'success'   => false,
               'message'   => 'Missing required data'
            ]);
         if (!array_key_exists('user', $args) || $args['user'] === 0){
            return $this->unauthorized();
         }
         if (!array_key_exists('content', $args) || empty($args['content'])) {
            return $this->validationError(['message'=>'Missing required content']);
         }
         if (!array_key_exists('posts', $args) || empty($args['posts'])) {
            return $this->validationError(['message' => 'Missing posts required']);
         }
         // Secure files to temporary storage
         $secured_files = $this->secureFiles($files, $args);
         if (empty($secured_files['files'])) {
            return $this->sendResponse(
               false,
               ['error_code' => 'no_files'],
               'No valid files to upload'
            );
            return $this->error('No valid files to upload');
         }
         // Queue file upload operation
@@ -1126,10 +1189,7 @@
            $operation_type,
            $args['user'],
            array_merge(
               [
                  'secured_files' => $secured_files['files'],
                  'upload_map' => $secured_files['upload_map'],
               ],
               ['secured_files' => $secured_files['files']],
               $args
            ),
            [
@@ -1139,28 +1199,19 @@
            ]
         );
         JVB()->queue()->queueOperation(
         $ID = JVB()->queue()->queueOperation(
            'process_upload_groups',
            $args['user'],
            $args,
            [
               'operation_id' => $args['id'],
               'depends_on' => [$args['upload']],
               'priority' => 'high'
               'priority' => 'high',
               'chunk_key' => 'posts'
            ]
         );
         return $this->sendResponse(
            true,
            [
               'operation_id' => $args['id'],
               'upload_operation_id' => $args['upload'],
               'post_count' => count($args['posts']),
               'file_count' => count($secured_files['files'])
            ],
            'Files uploaded and posts queued for creation'
         );
         return $this->queued($ID['operation_id'], 'Files uploaded and posts queued for creation');
      } catch (Exception $e) {
         JVB()->error()->log(
            '[UploadRoutes]:handleGroupingRequest',
@@ -1170,672 +1221,7 @@
               'trace' => $e->getTraceAsString()
            ]
         );
         return $this->sendResponse(
            false,
            ['error_code' => 'grouping_failed'],
            'Grouping operation failed: ' . $e->getMessage()
         );
      }
   }
   protected function processUploadGroups(WP_Error|array $result, object $operation, array $data): WP_Error|array
   {
      try {
         $queue = JVB()->queue();
         $all_uploaded_images = [];
         // Get the upload operation ID from data
         $dependencies = json_decode($operation->dependencies, true);
         $uploads = [];
         foreach($dependencies as $dependency) {
            $op = $queue->getOperation($dependency);
            $res = json_decode($op->result, true);
            if ($op->status === 'completed') {
               $uploads = array_merge($uploads, $res);
            }
         }
         if (empty($uploads)) {
            return [
               'success'   => false,
               'result' => 'No uploads to process'
            ];
         }
         // Build map of upload_id => attachment_id
         foreach ($uploads as $img) {
            if (isset($img['upload_id'], $img['attachment_id'])) {
               $all_uploaded_images[$img['upload_id']] = [
                  'upload_id' => $img['upload_id'],
                  'attachment_id' => (int)$img['attachment_id']
               ];
            }
         }
         $content = jvbCheckBase($data['content']);
         if (Features::forContent($data['content'])->has('is_timeline')) {
            return $this->processTimelineUploads($data, $uploads, $all_uploaded_images, $operation);
         }
         $user = (int)$data['user'];
         $created_posts = [];
         $used_upload_ids = [];
         // Create posts from groups
         foreach ($data['posts'] as $index => $post) {
            $post_title = !empty($post['fields']['post_title'])
               ? sanitize_text_field($post['fields']['post_title'])
               : 'New ' . JVB_CONTENT[$data['content']]['singular'] . ' ' . ($index + 1);
            $post_excerpt = !empty($post['fields']['post_excerpt'])
               ? sanitize_textarea_field($post['fields']['post_excerpt'])
               : '';
            $args =[
               'post_type' => $content,
               'post_author' => $user,
               'post_status' => 'draft',
               'post_title' => $post_title,
               'post_excerpt' => $post_excerpt,
            ];
            $new_post_id = wp_insert_post($args);
            if ($new_post_id && !is_wp_error($new_post_id)) {
               $created_posts[] = $new_post_id;
               // Get featured image upload_id
               $featured_upload_id = array_key_exists('featured', $post['fields']) ? (int)$post['fields']['featured'] : null;
               $featured_attachment_id = null;
               $gallery_attachment_ids = [];
               // Process all images for this post
               foreach ($post['images'] as $key => $img) {
                  $upload_id = $img['upload_id'];
                  $used_upload_ids[] = $upload_id;
                  if (isset($all_uploaded_images[$upload_id])) {
                     $attachment_id = (int)$all_uploaded_images[$upload_id]['attachment_id'];
                     if ($upload_id === $featured_upload_id) {
                        $featured_attachment_id = $attachment_id;
                     } else {
                        $gallery_attachment_ids[] = $attachment_id;
                     }
                  }
               }
               // Set featured image
               if ($featured_attachment_id) {
                  set_post_thumbnail($new_post_id, (int)$featured_attachment_id);
               } elseif (!empty($gallery_attachment_ids)) {
                  // If no featured set, use first image
                  set_post_thumbnail($new_post_id, (int)$gallery_attachment_ids[0]);
                  array_shift($gallery_attachment_ids);
               }
               // Set gallery images
               if (!empty($gallery_attachment_ids)) {
                  $meta = new MetaManager($new_post_id, 'post');
                  $fields = jvbGetFields($content, 'post');
                  foreach ($fields as $name => $config) {
                     if ($config['type'] === 'gallery') {
                        $meta->updateValue($name, implode(',', $gallery_attachment_ids));
                        break;
                     }
                  }
               }
            }
         }
         return [
            'success' => true,
            'result' => [
               'created_posts' => $created_posts,
               'total_posts' => count($created_posts),
               'used_images' => count($used_upload_ids),
               'message' => "Created " . count($created_posts) . " post(s) from uploads"
            ]
         ];
      } catch (Exception $e) {
         JVB()->error()->log(
            '[UploadRoutes]:processUploadGroups',
            $e->getMessage(),
            [
               'operation_id' => $operation->id,
               'user_id' => $operation->user_id
            ]
         );
         return [
            'success' => false,
            'result' => $e->getMessage()
         ];
      }
   }
   protected function processTimelineUploads(array $data, array $uploads, array $uploadMap, object $operation):array
   {
      try {
         $user = (int)$data['user'];
         $created_posts = [];
         $used_upload_ids = [];
         $content = jvbCheckBase($data['content']);
         $config = Features::getConfig($content);
         $defaultTitle = 'New '.$config['singular']. ' ';
         foreach ($data['posts'] as $index=> $post) {
            $title = !empty($post['fields']['post_title'])
               ? sanitize_text_field($post['fields']['post_title'])
               : $defaultTitle.($index + 1);
            $excerpt = !empty($post['fields']['post_excerpt'])
               ? sanitize_textarea_field($post['fields']['post_excerpt'])
               : '';
            $args =[
               'post_type'    => $content,
               'post_author'  => $user,
               'post_status'  => 'draft',
               'post_title'   => $title,
               'post_excerpt' => $excerpt
            ];
            $parent = wp_insert_post($args);
            if ($parent && !is_wp_error($parent)) {
               //Get the attachment IDs first
               $childPosts = [];
               $featured = $post['fields']['featured']??null;
               $featuredID = null;
               foreach ($post['images'] as $key => $img) {
                  $upload_id = $img['upload_id'];
                  $used_upload_ids[] = $upload_id;
                  if (isset($uploadMap[$upload_id])) {
                     $attachment_id = (int)$uploadMap[$upload_id]['attachment_id'];
                     if ($upload_id === $featured) {
                        $featuredID = $attachment_id;
                     } else {
                        $childPosts[] = $attachment_id;
                     }
                  }
               }
               // Set the featured image for the parent
               if ($featuredID) {
                  set_post_thumbnail($parent, $featuredID);
               } elseif (!empty($childPosts)) {
                  //use first image if no set featured
                  set_post_thumbnail($parent, (int)$childPosts[0]);
                  array_shift($childPosts);
               }
               //Create Child Posts
               if (!empty($childPosts)) {
                  $args['post_parent'] = $parent;
                  $created_posts[$parent] = [];
                  foreach ($childPosts as $i => $imgID) {
                     $treatment = $i + 1;
                     $childTitle = $title.' - Treatment '.$treatment;
                     $childDesc = '';
                     $args['post_title'] = $childTitle;
                     $args['post_excerpt'] = $childDesc;
                     $child = wp_insert_post($args);
                     if ($child && !is_wp_error($child)) {
                        $created_posts[$parent][] = $child;
                        set_post_thumbnail($child, $imgID);
                     }
                  }
               }
            }
         }
         return [
            'success'   => true,
            'result' => [
               'created_posts'   => $created_posts,
               'used_images'  => $used_upload_ids
            ]
         ];
      } catch (Exception $e) {
         JVB()->error()->log(
            '[UploadRoutes]:processTimelineUploads',
            $e->getMessage(),
            [
               'operation_id' => $operation->id,
               'user_id' => $operation->user_id
            ]
         );
         return [
            'success' => false,
            'result' => $e->getMessage()
         ];
      }
   }
   protected function cleanupUnusedImages(array $unused_images): array
   {
      $cleaned_count = 0;
      $errors = [];
      foreach ($unused_images as $upload_id => $image_data) {
         try {
            $attachment_id = $image_data['attachment_id'];
            // Verify this attachment exists and wasn't already deleted
            if (get_post($attachment_id)) {
               // Delete the attachment and its files
               $deleted = wp_delete_attachment($attachment_id, true);
               if ($deleted) {
                  $cleaned_count++;
               } else {
                  $errors[] = "Failed to delete attachment {$attachment_id} for upload {$upload_id}";
               }
            } else {
               // Attachment already doesn't exist, count as cleaned
               $cleaned_count++;
            }
         } catch (Exception $e) {
            $errors[] = "Error cleaning up upload {$upload_id}: " . $e->getMessage();
         }
      }
      return [
         'cleaned_count' => $cleaned_count,
         'errors' => $errors
      ];
   }
   function getAttachmentID(array $image, array $storedResults): int|false
   {
      foreach ($storedResults as $operationID => $uploads) {
         foreach ($uploads as $upload) {
            // FIX: Handle both case variations
            $stored_upload_id = $upload['upload_id'];
            $search_upload_id = $image['upload_id'];
            if ($stored_upload_id === $search_upload_id) {
               return (int)$upload['attachment_id'];
            }
         }
      }
      return false;
   }
   function extractFeaturedItem(array &$items, string $meta_key = 'featured'): array
   {
      // Handle empty array
      if (empty($items)) {
         return [
            'featured' => null,
            'remaining' => []
         ];
      }
      $featured_index = null;
      // First pass: look for featured item
      foreach ($items as $index => $item) {
         if (isset($item['meta'][$meta_key])) {
            $featured_index = $index;
            break;
         }
      }
      // If no featured item found, use first item (index 0)
      if ($featured_index === null) {
         $featured_index = 0;
      }
      // Extract the featured/first item
      $featured = $items[$featured_index];
      // Remove the item from the original array
      unset($items[$featured_index]);
      // Re-index the array to maintain sequential indices
      $remaining = array_values($items);
      return [
         'featured' => $featured,
         'remaining' => $remaining
      ];
   }
   protected function mapUploadIdsToAttachments(array $uploadIds, array $uploadedImages): array
   {
      $mappedImages = [];
      foreach ($uploadIds as $uploadId) {
         $imageData = $this->findImageByUploadId($uploadId, $uploadedImages);
         if ($imageData) {
            $mappedImages[] = $imageData;
         }
      }
      return $mappedImages;
   }
   protected function findImageByUploadId(string $uploadId, array $uploadedImages): ?array
   {
      // Handle both flat array and grouped results
      foreach ($uploadedImages as $image) {
         if (is_array($image)) {
            // If it's a grouped result, check each image in the group
            if (isset($image[0]) && is_array($image[0])) {
               foreach ($image as $groupImage) {
                  if (isset($groupImage['upload_id']) && $groupImage['upload_id'] === $uploadId) {
                     return $groupImage;
                  }
               }
            } else {
               // Single image result
               if (isset($image['upload_id']) && $image['upload_id'] === $uploadId) {
                  return $image;
               }
            }
         }
      }
      return null;
   }
   protected function determineFeaturedImage(array $group, array $groupImages): ?int
   {
      // Check if user has starred a specific image
      if (!empty($group['featured_upload_id'])) {
         foreach ($groupImages as $image) {
            if (isset($image['upload_id']) && $image['upload_id'] === $group['featured_upload_id']) {
               return $image['attachment_id'];
            }
         }
      }
      // Default to first image
      return !empty($groupImages) ? $groupImages[0]['attachment_id'] : null;
   }
   protected function sanitizeGroupMetadata(array $metadata): array
   {
      $sanitized = [];
      foreach ($metadata as $key => $value) {
         $sanitizedKey = sanitize_key($key);
         if (is_string($value)) {
            $sanitized[$sanitizedKey] = sanitize_text_field($value);
         } elseif (is_array($value)) {
            $sanitized[$sanitizedKey] = array_map('sanitize_text_field', $value);
         } else {
            $sanitized[$sanitizedKey] = $value;
         }
      }
      return $sanitized;
   }
   protected function generatePostTitle(string $content, int $userId): string
   {
      $username = get_user_meta($userId, 'first_name', true) ?: get_user_meta($userId, 'display_name', true);
      $link = get_user_meta($userId, BASE.'link', true);
      $city = function_exists('jvbArtistCity') ? jvbArtistCity($link) : '';
      $title = ucfirst($content);
      if ($username) {
         $title .= ' by ' . $username;
      }
      if ($city) {
         $title .= ' from ' . $city;
      }
      return $title;
   }
   /**
    * Determine how to save uploaded files based on configuration
    */
   protected function handleUploadDestination(array $data, array $results): void
   {
      // Determine destination from config
      $destination = $data['destination'] ?? 'meta';
      switch ($destination) {
         case 'meta':
            // Save to post/term/user meta
            $this->saveToMeta($data, $results);
            break;
         case 'post':
            // Create individual posts for each file
            $this->createIndividualPosts($data, $results);
            break;
         case 'post_group':
            // Create posts with grouped files
            $this->createGroupedPosts($data, $results);
            break;
         default:
            // No destination specified - files processed but not attached
            break;
      }
   }
   /**
    * Infer destination from existing data (backward compatibility)
    */
   protected function inferDestination(array $data): string
   {
      // If field_name exists → saving to meta
      if (!empty($data['field_name'])) {
         return 'meta';
      }
      // If post_type exists without field_name → creating posts
      if (!empty($data['content'])) {
         return 'post';
      }
      // No destination
      return 'none';
   }
   /**
    * Save attachment IDs to meta field
    */
   protected 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 = new MetaManager($data['post_id'], 'post');
      } elseif (!empty($data['term_id'])) {
         $meta = new MetaManager($data['term_id'], 'term');
      } elseif (!empty($data['user'])) {
         $link = (int)get_user_meta($data['user'], BASE.'link', true);
         $meta = new MetaManager($link, 'post');
      } else {
         return;
      }
      // Get existing value
      $existing = $meta->getValue($data['field_name']);
      $existing_ids = !empty($existing) ? explode(',', $existing) : [];
      // Merge with new IDs
      $all_ids = array_unique(array_merge($existing_ids, $attachment_ids));
      // Update with comma-separated string
      $meta->updateValue($data['field_name'], implode(',', $all_ids));
   }
   /**
    * Create individual posts from uploads
    */
   protected function createIndividualPosts(array $data, array $results): array
   {
      if (empty($data['content'])) {
         return [];
      }
      $created_posts = [];
      foreach ($results as $result) {
         $attachment_id = $result['attachment_id'];
         $attachment = get_post($attachment_id);
         // Create post
         $post_data = [
            'post_type' => jvbCheckBase($data['content']),
            'post_title' => $attachment->post_title,
            'post_status' => 'draft',
            'post_author' => $data['user'] ?? get_current_user_id(),
         ];
         $post_id = wp_insert_post($post_data);
         if (!is_wp_error($post_id)) {
            // Set as featured image or attach to gallery
            $this->attachFileToPost($post_id, $attachment_id, $data);
            $created_posts[] = [
               'post_id' => $post_id,
               'attachment_id' => $attachment_id,
            ];
         }
      }
      return $created_posts;
   }
   /**
    * Create posts with grouped uploads
    */
   protected function createGroupedPosts(array $data, array $results): array
   {
      if (empty($data['content'])) {
         return [];
      }
      $id_map = [];
      foreach ($results as $result) {
         if (isset($result['upload_id'], $result['attachment_id'])) {
            $id_map[$result['upload_id']] = $result['attachment_id'];
         }
      }
      // Groups come from frontend as metadata
      $groups = $data['groups'] ?? [array_column($results, 'attachment_id')];
      $created_posts = [];
      foreach ($groups as $group_index => $group_upload_ids) {
         $group_attachment_ids = array_filter(
            array_map(fn($uid) => $id_map[$uid] ?? null, $group_upload_ids)
         );
         if (empty($group_attachment_ids)) continue;
         // Create post for this group
         $first_attachment = get_post($group_attachment_ids[0]);
         $post_data = [
            'post_type' => jvbCheckBase($data['content']),
            'post_title' => $data['group_titles'][$group_index] ?? $first_attachment->post_title,
            'post_status' => $data['post_status'] ?? 'draft',
            'post_author' => $data['user'] ?? get_current_user_id(),
         ];
         $post_id = wp_insert_post($post_data);
         if (!is_wp_error($post_id)) {
            // Attach all files in group
            foreach ($group_attachment_ids as $index => $attachment_id) {
               if ($index === 0) {
                  // First is featured
                  set_post_thumbnail($post_id, $attachment_id);
               } else {
                  // Others go to gallery
                  $meta = new MetaManager($post_id, 'post');
                  $existing = $meta->getValue('gallery');
                  $existing_ids = !empty($existing) ? explode(',', $existing) : [];
                  $existing_ids[] = $attachment_id;
                  $meta->updateValue('gallery', implode(',', $existing_ids));
               }
            }
            $created_posts[] = [
               'post_id' => $post_id,
               'attachment_ids' => $group_attachment_ids,
            ];
         }
      }
      return $created_posts;
   }
   /**
    * Attach file to post based on file type
    */
   protected function attachFileToPost(int $post_id, int $attachment_id, array $data): void
   {
      $attachment = get_post($attachment_id);
      $mime_type = $attachment->post_mime_type;
      // Determine file type
      if (str_starts_with($mime_type, 'image/')) {
         // Set as featured image
         set_post_thumbnail($post_id, $attachment_id);
      } elseif (str_starts_with($mime_type, 'video/')) {
         // Save to video field
         $meta = new MetaManager($post_id, 'post');
         $meta->updateValue('video', $attachment_id);
      } else {
         // Documents - save to documents field
         $meta = new MetaManager($post_id, 'post');
         $existing = $meta->getValue('documents');
         $existing_ids = !empty($existing) ? explode(',', $existing) : [];
         $existing_ids[] = $attachment_id;
         $meta->updateValue('documents', implode(',', $existing_ids));
         return $this->error('Grouping operation failed: '.$e->getMessage());
      }
   }
}