From 88d9e0e2b7997eb0c96dc737082c91b4e3f7ca6e Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 17 Jun 2026 15:10:37 +0000
Subject: [PATCH] =Fixed for single image upload fields populating the image meta. Need to check galleries now.

---
 inc/managers/queue/executors/UploadExecutor.php |   45 ++++++++++++++++++++++++++++++---------------
 1 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/inc/managers/queue/executors/UploadExecutor.php b/inc/managers/queue/executors/UploadExecutor.php
index ac659a4..b825232 100644
--- a/inc/managers/queue/executors/UploadExecutor.php
+++ b/inc/managers/queue/executors/UploadExecutor.php
@@ -288,6 +288,7 @@
 		error_log('Processing Meta Update with data: '.print_r($data, true));
 		foreach ($data as $uploadId => $info) {
 			if (!is_array($info)) {
+				error_log('Not an array for processing meta update: '.print_r($info, true));
 				continue;
 			}
 			$success = true;
@@ -313,8 +314,6 @@
 					continue;
 				}
 
-
-
 				$this->applyMeta($attachmentId, $info);
 				$updatedCount++;
 				$progress->advance(1);
@@ -325,7 +324,7 @@
 				$errors[] = $e->getMessage();
 			}
 
-			if ($success) {
+			if ($success && $attachmentId) {
 				$postID = wp_get_post_parent_id($attachmentId);
 				if ($postID && !in_array($postID, $postsAttachedTo)){
 					$postsAttachedTo[] = $postID;
@@ -698,24 +697,40 @@
 
 	private function applyMeta(int $attachmentId, array $metadata): void
 	{
-		if (array_key_exists('image-alt-text', $metadata)) {
-			update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['image-alt-text']));
-		}
-
 		$postUpdates = [];
+		foreach ($metadata as $key => $value) {
+			//Remove the image_data| from the  meta's group formatting
+			$base = 'image_data|';
+			$key = str_replace($base, '', $key);
+			if ($key === 'image-alt-text') {
+				error_log('Updating Image Alt Text: '.print_r($value, true));
+				update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($value));
+				continue;
+			}
 
-		if (array_key_exists('image-title', $metadata)) {
-			$postUpdates['post_title'] = sanitize_text_field($metadata['image-title']);
+			$k = match ($key) {
+				'image-title'	=> 'post_title',
+				'image-caption' => 'post_excerpt',
+				'image-description' => 'post_content',
+				default => false
+			};
+			if (!$k) {
+				error_log('No matching key for '.$key.' with value: '.print_r($value, true));
+				continue;
+			}
+			$v = match ($key) {
+				'image-title'	=> sanitize_text_field($value),
+				default			=> sanitize_textarea_field($value)
+			};
+			$postUpdates[$k] = $v;
+
 		}
 
-		if (array_key_exists('image-caption', $metadata)) {
-			$postUpdates['post_excerpt'] = sanitize_textarea_field($metadata['image-caption']);
-		}
-		if (array_key_exists('image-description', $metadata)) {
-			$postUpdates['post_excerpt']= sanitize_textarea_field($metadata['image-caption']);
-		}
 		if (!empty($postUpdates)){
 			$postUpdates['ID'] = $attachmentId;
+			$postUpdates['post_type'] = 'attachment';
+
+			error_log('Updating Image post with data: '.print_r($postUpdates, true));
 			wp_update_post($postUpdates);
 		}
 	}

--
Gitblit v1.10.0