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/managers/queue/executors/UploadExecutor.php |   78 +++++++++++++++++++++++++++++----------
 1 files changed, 58 insertions(+), 20 deletions(-)

diff --git a/inc/managers/queue/executors/UploadExecutor.php b/inc/managers/queue/executors/UploadExecutor.php
index c71e680..ab2f992 100644
--- a/inc/managers/queue/executors/UploadExecutor.php
+++ b/inc/managers/queue/executors/UploadExecutor.php
@@ -14,7 +14,7 @@
 /**
  * Executor for upload-related queue operations.
  * Handles: image_upload, video_upload, document_upload,
- *          update_metadata, temporary_cleanup, attach_upload_to_content, process_upload_groups
+ *          update_image_meta, temporary_cleanup, attach_upload_to_content, process_upload_groups
  */
 final class UploadExecutor implements Executor
 {
@@ -22,7 +22,7 @@
 		'image_upload',
 		'video_upload',
 		'document_upload',
-		'update_metadata',
+		'update_image_meta',
 		'temporary_cleanup',
 		'attach_upload_to_content',
 		'process_upload_groups'
@@ -41,7 +41,7 @@
 				'image_upload'            => $this->processFileUpload($operation, $data, 'image', $progress),
 				'video_upload'            => $this->processFileUpload($operation, $data, 'video', $progress),
 				'document_upload'         => $this->processFileUpload($operation, $data, 'document', $progress),
-				'update_metadata'         => $this->processMetadataUpdate($operation, $data, $progress),
+				'update_image_meta'         => $this->processMetaUpdate($operation, $data, $progress),
 				'temporary_cleanup'       => $this->processTemporaryCleanup($operation, $data, $progress),
 				'attach_upload_to_content'=> $this->processAttachToContent($operation, $data, $progress),
 				'process_upload_groups'   => $this->processUploadGroups($operation, $data, $progress),
@@ -176,29 +176,39 @@
 	/**
 	 * Process metadata updates for attachments
 	 */
-	private function processMetadataUpdate(Operation $operation, array $data, Progress $progress): Result
+	private function processMetaUpdate(Operation $operation, array $data, Progress $progress): Result
 	{
 		$updatedCount = 0;
 		$errors = [];
 
 		foreach ($data as $uploadId => $info) {
-			if (!is_array($info) || empty($info['depends_on'])) {
+			if (!is_array($info)) {
 				continue;
 			}
 
 			try {
-				// Get the dependency operation to find attachment ID
-				$depOp = JVB()->queue()->get($info['depends_on']);
-				if (!$depOp || !$depOp->result) {
-					$errors[] = "Dependency {$info['depends_on']} not found or has no result";
+				if (array_key_exists('depends_on', $info)) {
+					// Get the dependency operation to find attachment ID
+					$depOp = JVB()->queue()->get($info['depends_on']);
+					if (!$depOp || !$depOp->result) {
+						$errors[] = "Dependency {$info['depends_on']} not found or has no result";
+						continue;
+					}
+					$attachmentId = $this->findAttachmentByUploadId($uploadId, $depOp->result);
+					if (!$attachmentId) {
+						$errors[] = "No attachment found for upload ID: {$uploadId}";
+						continue;
+					}
+				} else {
+					$attachmentId = $info['attachmentId']??false;
+				}
+
+				if (!$attachmentId) {
+					$errors[] = "No attachment found for: ".print_r($info, true);
 					continue;
 				}
 
-				$attachmentId = $this->findAttachmentByUploadId($uploadId, $depOp->result);
-				if (!$attachmentId) {
-					$errors[] = "No attachment found for upload ID: {$uploadId}";
-					continue;
-				}
+
 
 				$this->applyMeta($attachmentId, $info);
 				$updatedCount++;
@@ -385,6 +395,7 @@
 
 		$defaultTitle = 'New '.$config['singular']. ' ';
 		foreach($data['posts'] as $index => $post) {
+			$progress->advance();
 			$title = array_key_exists('post_title', $post['fields'])
 				? sanitize_text_field($post['fields']['post_title'])
 				: $defaultTitle . ($index + 1);
@@ -405,6 +416,7 @@
 			$parent = wp_insert_post($args);
 			$progress->advance();
 			if ($parent && !is_wp_error($parent)) {
+
 				$childPosts = [];
 				$featured = $post['fields']['featured']??null;
 				$featuredID = null;
@@ -442,6 +454,8 @@
 						}
 					}
 				}
+
+				$this->updateTimelineMetadata($parent);
 			}
 		}
 		return new Result(
@@ -450,27 +464,51 @@
 		);
 	}
 
+	/**
+	 * Update timeline parent post with count and latest date
+	 * @param int $parentId Parent timeline post ID
+	 */
+	private function updateTimelineMetadata(int $parentId): void
+	{
+		// Get all child posts
+		$children = get_children([
+			'post_parent' => $parentId,
+			'post_type' => get_post_type($parentId),
+			'post_status' => ['publish', 'draft'],
+			'orderby' => 'date',
+			'order' => 'DESC',
+			'fields' => 'ids'
+		]);
+
+		// Count includes parent + children
+		$number = count($children) + 1;
+
+		// Update both meta fields
+		update_post_meta($parentId, BASE . 'number', $number);
+		update_post_meta($parentId, BASE . 'latest_date', time());
+	}
+
 	// ─────────────────────────────────────────────────────────────
 	// Helper methods
 	// ─────────────────────────────────────────────────────────────
 
 	private function applyMeta(int $attachmentId, array $metadata): void
 	{
-		if (!empty($metadata['title'])) {
+		if (!empty($metadata['image-title'])) {
 			wp_update_post([
 				'ID'         => $attachmentId,
-				'post_title' => sanitize_text_field($metadata['title']),
+				'post_title' => sanitize_text_field($metadata['image-title']),
 			]);
 		}
 
-		if (!empty($metadata['alt'])) {
-			update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['alt']));
+		if (!empty($metadata['image-alt-text'])) {
+			update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['image-alt-text']));
 		}
 
-		if (!empty($metadata['caption'])) {
+		if (!empty($metadata['image-caption'])) {
 			wp_update_post([
 				'ID'           => $attachmentId,
-				'post_excerpt' => sanitize_textarea_field($metadata['caption']),
+				'post_excerpt' => sanitize_textarea_field($metadata['image-caption']),
 			]);
 		}
 	}

--
Gitblit v1.10.0