From e6672fe38ce5d99f3b3f026154f777aded7361de Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 29 Jan 2026 17:36:53 +0000
Subject: [PATCH] =Starting refactor of Meta and Routes to fluent-style
---
inc/rest/routes/UploadRoutes.php | 119 +++++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 79 insertions(+), 40 deletions(-)
diff --git a/inc/rest/routes/UploadRoutes.php b/inc/rest/routes/UploadRoutes.php
index 941207b..c0016bf 100644
--- a/inc/rest/routes/UploadRoutes.php
+++ b/inc/rest/routes/UploadRoutes.php
@@ -2,6 +2,8 @@
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\managers\UploadManager;
@@ -21,9 +23,62 @@
{
$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
+ ));
+ }
+
/**
* Registers upload routes
* @return void
@@ -728,14 +783,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) {
@@ -748,23 +797,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, $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,
@@ -796,9 +837,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}";
@@ -818,7 +863,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)" :
@@ -835,22 +880,16 @@
$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',
]
);
@@ -877,7 +916,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');
}
@@ -938,18 +977,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)) {
--
Gitblit v1.10.0