| | |
| | | <?php |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\JVB; |
| | | use JVBase\managers\queue\executors\UploadExecutor; |
| | | use JVBase\managers\queue\TypeConfig; |
| | | use JVBase\rest\RestRouteManager; |
| | | use JVBase\meta\MetaManager; |
| | | 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_action('init', [$this, 'registerUploadExecutors'], 5); |
| | |
| | | */ |
| | | 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); |
| | | Route::for('uploads/groups') |
| | | ->post([$this, 'handleGroupingRequest']) |
| | | ->auth(PermissionHandler::combine(['nonce'])) |
| | | ->rateLimit(30) |
| | | ->args([ |
| | | 'id' => 'string|required', |
| | | 'content' => 'string|required', |
| | | 'user' => 'int|required' |
| | | ]); |
| | | |
| | | 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' |
| | | ], |
| | | ] |
| | | ]); |
| | | |
| | | 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' |
| | | ]); |
| | | } |
| | | |
| | | /** |
| | |
| | | $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()); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | $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)); |
| | | } |
| | | |
| | | /** |
| | |
| | | $response['operation_id'] = $operation_id; |
| | | } |
| | | |
| | | return new WP_REST_Response($response); |
| | | return $this->success($response); |
| | | } |
| | | |
| | | |
| | |
| | | $files = $request->get_file_params(); |
| | | $args = $this->buildUploadArgs($request); |
| | | |
| | | 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 |
| | |
| | | ] |
| | | ); |
| | | |
| | | JVB()->queue()->queueOperation( |
| | | $ID = JVB()->queue()->queueOperation( |
| | | 'process_upload_groups', |
| | | $args['user'], |
| | | $args, |
| | |
| | | ] |
| | | ); |
| | | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | // 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; |
| | | } |
| | | } |
| | |
| | | |
| | | // Determine meta type |
| | | if (!empty($data['post_id'])) { |
| | | $meta = new MetaManager($data['post_id'], 'post'); |
| | | $meta = Meta::forPost($data['post_id']); |
| | | } elseif (!empty($data['term_id'])) { |
| | | $meta = new MetaManager($data['term_id'], 'term'); |
| | | $meta = Meta::forTerm($data['term_id']); |
| | | } elseif (!empty($data['user'])) { |
| | | $link = (int)get_user_meta($data['user'], BASE.'link', true); |
| | | $meta = new MetaManager($link, 'post'); |
| | | $meta = Meta::forPost($link); |
| | | } else { |
| | | return; |
| | | } |
| | | |
| | | // 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)); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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)); |
| | | } |
| | | } |
| | | } |