| | |
| | | <?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\TypeConfig; |
| | | use JVBase\rest\PermissionHandler; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\managers\UploadManager; |
| | | use JVBase\rest\Route; |
| | | use JVBase\utility\Features; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | |
| | | 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(); |
| | | |
| | | // Image uploads - chunked at 5 files |
| | | $registry->register('image_upload', new TypeConfig( |
| | | executor: $executor, |
| | | chunkKey: 'secured_files', |
| | | chunkSize: 5 |
| | | )); |
| | | |
| | | // Video uploads - one at a time (heavy processing) |
| | | $registry->register('video_upload', new TypeConfig( |
| | | executor: $executor, |
| | | chunkKey: 'secured_files', |
| | | chunkSize: 1 |
| | | )); |
| | | |
| | | // Document uploads - chunked at 10 |
| | | $registry->register('document_upload', new TypeConfig( |
| | | executor: $executor, |
| | | chunkKey: 'secured_files', |
| | | chunkSize: 10 |
| | | )); |
| | | |
| | | // 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' |
| | | ]) |
| | | ->register(); |
| | | } |
| | | |
| | | /** |
| | |
| | | $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); |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | $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); |
| | |
| | | $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 |
| | |
| | | |
| | | 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... |
| | |
| | | 'trace' => $e->getTraceAsString() |
| | | ] |
| | | ); |
| | | |
| | | return $this->sendResponse( |
| | | false, |
| | | ['error_code' => 'upload_failed'], |
| | | 'Upload processing failed: ' . $e->getMessage() |
| | | ); |
| | | return $this->error($e->getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | $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, |
| | |
| | | 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) { |
| | |
| | | |
| | | return [ |
| | | 'files' => $secured_files, |
| | | 'upload_map' => $upload_map, |
| | | 'errors' => $errors |
| | | ]; |
| | | } |
| | |
| | | /** |
| | | * 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] ?? []); |
| | | |
| | |
| | | } elseif ($operation_type === 'document') { |
| | | $chunkSize = 10; |
| | | } |
| | | |
| | | 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 |
| | | ] |
| | | ); |
| | | |
| | | 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'], |
| | | $args, |
| | | [ |
| | | 'priority' => 'high', |
| | | 'operation_id' => $args['id'], |
| | | 'depends_on' => $args['upload'] |
| | | 'priority' => 'high', |
| | | 'operation_id' => $args['id'], |
| | | 'depends_on' => $dependencies |
| | | ] |
| | | ); |
| | | } |
| | |
| | | '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' => $args['upload'] |
| | | ] |
| | | ); |
| | | |
| | | return $args['id']; |
| | | } |
| | | |
| | |
| | | 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); |
| | |
| | | |
| | | $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'); |
| | | $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)); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | $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( |
| | |
| | | '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'] ?? '', |
| | | ] |
| | | ) |
| | | ); |
| | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | |
| | | ] |
| | | ); |
| | | return [ |
| | | 'success' => false, |
| | | 'result' => $e->getMessage() |
| | | 'success' => false, |
| | | 'result' => $e->getMessage() |
| | | ]; |
| | | } |
| | | } |
| | |
| | | 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) { |
| | |
| | | } |
| | | $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, $data['user']); |
| | | } |
| | | elseif (!empty($pending)) { |
| | | // Phase 2A: Queue metadata update with dependency on upload operation |
| | | return $this->queueMetaUpdate($pending, $data['user']); |
| | | } |
| | | |
| | | |
| | | return $this->sendResponse( |
| | | false, |
| | |
| | | { |
| | | $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}"; |
| | |
| | | [ |
| | | 'updated_count' => $updated_count, |
| | | 'errors' => $errors, |
| | | 'attachment_ids' => $data['attachment_ids'] |
| | | 'attachment_ids' => $ids |
| | | ], |
| | | $updated_count > 0 ? |
| | | "Updated metadata for {$updated_count} attachment(s)" : |
| | |
| | | $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']; |
| | | } |
| | | } |
| | | $operationID = $queue->queueOperation( |
| | | 'update_metadata', |
| | | 'update_image_meta', |
| | | $user, |
| | | $data, |
| | | [ |
| | | 'depends_on' => $depends_on, |
| | | 'priority' => 'medium', |
| | | ] |
| | | ); |
| | | |
| | |
| | | $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'); |
| | | } |
| | | |
| | |
| | | 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)) { |
| | |
| | | $response['operation_id'] = $operation_id; |
| | | } |
| | | |
| | | return new WP_REST_Response($response); |
| | | return $this->success($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 |
| | |
| | | $operation_type, |
| | | $args['user'], |
| | | array_merge( |
| | | [ |
| | | 'secured_files' => $secured_files['files'], |
| | | 'upload_map' => $secured_files['upload_map'], |
| | | ], |
| | | ['secured_files' => $secured_files['files']], |
| | | $args |
| | | ), |
| | | [ |
| | |
| | | ] |
| | | ); |
| | | |
| | | 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', |
| | |
| | | 'trace' => $e->getTraceAsString() |
| | | ] |
| | | ); |
| | | |
| | | return $this->sendResponse( |
| | | false, |
| | | ['error_code' => 'grouping_failed'], |
| | | 'Grouping operation failed: ' . $e->getMessage() |
| | | ); |
| | | return $this->error('Grouping operation failed: '.$e->getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | $queue = JVB()->queue(); |
| | | $all_uploaded_images = []; |
| | | |
| | | // Get the upload operation ID from data |
| | | // Get the upload operation ID from dependencies |
| | | $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($dependencies)) { |
| | | return [ |
| | | 'success' => false, |
| | | 'result' => 'No dependencies found' |
| | | ]; |
| | | } |
| | | |
| | | // Collect uploads from all dependency operations |
| | | $uploads = []; |
| | | foreach ($dependencies as $dependency) { |
| | | // Use getOperationValue like ContentRoutes does |
| | | $res = $queue->getOperationValue($dependency, 'result'); |
| | | |
| | | if (empty($res)) { |
| | | continue; |
| | | } |
| | | |
| | | // Results are stored at root level, keyed by upload_id |
| | | // Filter to only include actual upload results (arrays with attachment_id) |
| | | foreach ($res as $key => $value) { |
| | | if (is_array($value) && isset($value['attachment_id'])) { |
| | | $uploads[$key] = $value; |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | if (empty($uploads)) { |
| | | return [ |
| | | 'success' => false, |
| | | 'result' => 'No uploads to process' |
| | | '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'] |
| | | ]; |
| | | } |
| | | foreach ($uploads as $upload_id => $img) { |
| | | $all_uploaded_images[$upload_id] = [ |
| | | 'upload_id' => $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 = []; |
| | |
| | | ? sanitize_textarea_field($post['fields']['post_excerpt']) |
| | | : ''; |
| | | |
| | | $args =[ |
| | | $args = [ |
| | | 'post_type' => $content, |
| | | 'post_author' => $user, |
| | | 'post_status' => 'draft', |
| | |
| | | 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; |
| | | // Get featured image upload_id - string, not int! |
| | | $featured_upload_id = $post['fields']['featured'] ?? null; |
| | | $featured_attachment_id = null; |
| | | $gallery_attachment_ids = []; |
| | | |
| | | // Process all images for this post |
| | | foreach ($post['images'] as $key => $img) { |
| | | foreach ($post['images'] as $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']; |
| | | $attachment_id = $all_uploaded_images[$upload_id]['attachment_id']; |
| | | |
| | | if ($upload_id === $featured_upload_id) { |
| | | $featured_attachment_id = $attachment_id; |
| | |
| | | |
| | | // Set featured image |
| | | if ($featured_attachment_id) { |
| | | set_post_thumbnail($new_post_id, (int)$featured_attachment_id); |
| | | set_post_thumbnail($new_post_id, $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]); |
| | | set_post_thumbnail($new_post_id, $gallery_attachment_ids[0]); |
| | | array_shift($gallery_attachment_ids); |
| | | } |
| | | |
| | | // Set gallery images |
| | | if (!empty($gallery_attachment_ids)) { |
| | | $meta = new MetaManager($new_post_id, 'post'); |
| | | $meta = Meta::forPost($new_post_id); |
| | | $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; |
| | | } |
| | | } |
| | |
| | | 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 = 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 { |
| | | $attachmentIds = array_column($results, 'attachment_id'); |
| | | $meta = $this->getMetaManager($data); |
| | | if (!$meta) { |
| | | return; |
| | | } |
| | | |
| | | // Get existing value |
| | | $existing = $meta->getValue($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->updateValue($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)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | set_post_thumbnail($post_id, $attachment_id); |
| | | } else { |
| | | // Others go to gallery |
| | | $meta = new MetaManager($post_id, 'post'); |
| | | $existing = $meta->getValue('gallery'); |
| | | $meta = Meta::forPost($post_id); |
| | | $existing = $meta->get('gallery'); |
| | | $existing_ids = !empty($existing) ? explode(',', $existing) : []; |
| | | $existing_ids[] = $attachment_id; |
| | | $meta->updateValue('gallery', implode(',', $existing_ids)); |
| | | $meta->set('gallery', implode(',', $existing_ids)); |
| | | } |
| | | } |
| | | |
| | |
| | | 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); |
| | | $meta = Meta::forPost($post_id); |
| | | $meta->set('video', $attachment_id); |
| | | } else { |
| | | // Documents - save to documents field |
| | | $meta = new MetaManager($post_id, 'post'); |
| | | $existing = $meta->getValue('documents'); |
| | | $meta = Meta::forPost($post_id); |
| | | $existing = $meta->get('documents'); |
| | | $existing_ids = !empty($existing) ? explode(',', $existing) : []; |
| | | $existing_ids[] = $attachment_id; |
| | | $meta->updateValue('documents', implode(',', $existing_ids)); |
| | | $meta->set('documents', implode(',', $existing_ids)); |
| | | } |
| | | } |
| | | } |