From bad59c66549eb601fed963ed013f9b79305ca003 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 07 Jan 2026 20:09:14 +0000
Subject: [PATCH] =Feedblock integrated with refactored taxonomy selector

---
 inc/rest/routes/UploadRoutes.php |  121 ++++++++++++++++++++++++++++++++++++++--
 1 files changed, 115 insertions(+), 6 deletions(-)

diff --git a/inc/rest/routes/UploadRoutes.php b/inc/rest/routes/UploadRoutes.php
index e47b5e9..6052ab5 100644
--- a/inc/rest/routes/UploadRoutes.php
+++ b/inc/rest/routes/UploadRoutes.php
@@ -277,7 +277,7 @@
 		unset($context['upload_ids']);
 
 		$config = $this->getFieldConfig($args);
-
+		error_log('secureFiles: '.print_r($files, true));
 		$file_array = $files['files'] ?? $files;
 		$tmp_names = isset($file_array['tmp_name'][0]) && is_array($file_array['tmp_name'][0])
 			? $file_array['tmp_name'][0]
@@ -1092,6 +1092,10 @@
 			$args = $this->buildUploadArgs($request);
 			$data = $request->get_params();
 
+			error_log('get_file_params: '.print_r($files, true));
+			global $_FILES;
+			error_log('Global Files: '.print_r($_FILES, true));
+
 			if (!$args['content'] || !$args['user'] || !$args['posts']) {
 
 				$this->logError('Missing required data');
@@ -1113,7 +1117,7 @@
 			}
 
 			// Queue file upload operation
-			$operation_type = $this->determineOperationType($secured_data['files'][0] ?? []);
+			$operation_type = $this->determineOperationType($secured_files['files'][0] ?? []);
 			$chunkSize = 5;
 			if ($operation_type === 'video') {
 				$chunkSize = 1;
@@ -1215,27 +1219,33 @@
 			}
 
 			$content = jvbCheckBase($data['content']);
+			if (Features::forContent($data['content'])->has('is_timeline')) {
+				return $this->processTimelineUploads($data, $uploads, $all_uploaded_images, $operation);
+			}
 			$user = (int)$data['user'];
 			$created_posts = [];
 			$used_upload_ids = [];
 
+			error_log('Processing Group Data: '.print_r($data, true));
 			// Create posts from groups
 			foreach ($data['posts'] as $index => $post) {
 				$post_title = !empty($post['fields']['post_title'])
 					? sanitize_text_field($post['fields']['post_title'])
-					: 'New ' . JVB_CONTENT[$content]['singular'] . ' ' . ($index + 1);
+					: 'New ' . JVB_CONTENT[$data['content']]['singular'] . ' ' . ($index + 1);
 
 				$post_excerpt = !empty($post['fields']['post_excerpt'])
 					? sanitize_textarea_field($post['fields']['post_excerpt'])
 					: '';
 
-				$new_post_id = wp_insert_post([
+				$args =[
 					'post_type' => $content,
 					'post_author' => $user,
 					'post_status' => 'draft',
 					'post_title' => $post_title,
 					'post_excerpt' => $post_excerpt,
-				]);
+				];
+
+				$new_post_id = wp_insert_post($args);
 
 				if ($new_post_id && !is_wp_error($new_post_id)) {
 					$created_posts[] = $new_post_id;
@@ -1269,7 +1279,7 @@
 						set_post_thumbnail($new_post_id, (int)$gallery_attachment_ids[0]);
 						array_shift($gallery_attachment_ids);
 					}
-
+					error_log('Remaining Gallery images: '.print_r($gallery_attachment_ids, true));
 					// Set gallery images
 					if (!empty($gallery_attachment_ids)) {
 						$meta = new MetaManager($new_post_id, 'post');
@@ -1312,6 +1322,105 @@
 		}
 	}
 
+	protected function processTimelineUploads(array $data, array $uploads, array $uploadMap, object $operation):array
+	{
+		try {
+			$user = (int)$data['user'];
+			$created_posts = [];
+			$used_upload_ids = [];
+
+			$content = jvbCheckBase($data['content']);
+			error_log('[processTimelineUploads]Got content: '.print_r($content, true));
+			$config = Features::getConfig($content);
+			error_log('[processTimelineUploads]Got config: '.print_r($config, true));
+			$defaultTitle = 'New '.$config['singular']. ' ';
+			foreach ($data['posts'] as $index=> $post) {
+				$title = !empty($post['fields']['post_title'])
+					? sanitize_text_field($post['fields']['post_title'])
+					: $defaultTitle.($index + 1);
+				$excerpt = !empty($post['fields']['post_excerpt'])
+					? sanitize_textarea_field($post['fields']['post_excerpt'])
+					: '';
+
+				$args =[
+					'post_type'		=> $content,
+					'post_author'	=> $user,
+					'post_status'	=> 'draft',
+					'post_title'	=> $title,
+					'post_excerpt'	=> $excerpt
+				];
+				$parent = wp_insert_post($args);
+
+				if ($parent && !is_wp_error($parent)) {
+					//Get the attachment IDs first
+					$childPosts = [];
+					$featured = $post['fields']['featured']??null;
+					$featuredID = null;
+					foreach ($post['images'] as $key => $img) {
+						$upload_id = $img['upload_id'];
+						$used_upload_ids[] = $upload_id;
+
+						if (isset($uploadMap[$upload_id])) {
+							$attachment_id = (int)$uploadMap[$upload_id]['attachment_id'];
+							if ($upload_id === $featured) {
+								$featuredID = $attachment_id;
+							} else {
+								$childPosts[] = $attachment_id;
+							}
+						}
+					}
+					// Set the featured image for the parent
+					if ($featuredID) {
+						set_post_thumbnail($parent, $featuredID);
+					} elseif (!empty($childPosts)) {
+						//use first image if no set featured
+						set_post_thumbnail($parent, (int)$childPosts[0]);
+						array_shift($childPosts);
+					}
+
+					//Create Child Posts
+					if (!empty($childPosts)) {
+						$args['post_parent'] = $parent;
+						$created_posts[$parent] = [];
+						foreach ($childPosts as $i => $imgID) {
+							$treatment = $i + 1;
+							$childTitle = $title.' - Treatment '.$treatment;
+							$childDesc = '';
+							$args['post_title'] = $childTitle;
+							$args['post_excerpt'] = $childDesc;
+							$child = wp_insert_post($args);
+							if ($child && !is_wp_error($child)) {
+								$created_posts[$parent][] = $child;
+								set_post_thumbnail($child, $imgID);
+							}
+						}
+					}
+				}
+			}
+			return [
+				'success'	=> true,
+				'result'	=> [
+					'created_posts'	=> $created_posts,
+					'used_images'	=> $used_upload_ids
+				]
+			];
+		} catch (Exception $e) {
+			JVB()->error()->log(
+				'[UploadRoutes]:processTimelineUploads',
+				$e->getMessage(),
+				[
+					'operation_id' => $operation->id,
+					'user_id' => $operation->user_id
+				]
+			);
+
+			return [
+				'success' => false,
+				'result' => $e->getMessage()
+			];
+		}
+	}
+
 	protected function cleanupUnusedImages(array $unused_images): array
 	{
 		$cleaned_count = 0;

--
Gitblit v1.10.0