From 2fd9c0c5a204ee209a0d5a47fe0cefcb44d85aae Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 11 Feb 2026 01:48:23 +0000
Subject: [PATCH] =added helper to CRUD.js to ignore upload field changes if an upload is in progress

---
 inc/rest/routes/UploadRoutes.php |   39 +++++++++++++++++++++++++++++++++++----
 1 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/inc/rest/routes/UploadRoutes.php b/inc/rest/routes/UploadRoutes.php
index c12fa92..4b2a4a6 100644
--- a/inc/rest/routes/UploadRoutes.php
+++ b/inc/rest/routes/UploadRoutes.php
@@ -90,7 +90,9 @@
 		Route::for('uploads')
 			->post([$this, 'handleUpload'])
 			->auth(PermissionHandler::combine(['nonce']))
-			->rateLimit(30);
+			->rateLimit(30)
+			->register();
+
 		Route::for('uploads/groups')
 			->post([$this, 'handleGroupingRequest'])
 			->auth(PermissionHandler::combine(['nonce']))
@@ -124,10 +126,14 @@
 		$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);
-						// item_id IS the post/term being edited — use as post_id if not already set
 						if (!array_key_exists('post_id', $args)) {
 							$args['post_id'] = absint($value);
 						}
@@ -152,7 +158,10 @@
 				case 'user':
 					if ($this->userCheck($value)) {
 						$args['user'] = (int) $value;
-						if (!array_key_exists('post_id', $args) && !array_key_exists('term_id', $args) && !array_key_exists('item_id', $args)) {
+						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);
 						}
 					}
@@ -401,6 +410,11 @@
 		);
 
 		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'],
@@ -408,7 +422,7 @@
 				[
 					'priority'      => 'high',
 					'operation_id'  => $args['id'],
-					'depends_on'    => $args['upload']
+					'depends_on'    => $dependencies
 				]
 			);
 		}
@@ -533,6 +547,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);

--
Gitblit v1.10.0