From a9b3b28d001941921aa70d37fdc87c758a163a44 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 05 Jun 2026 16:47:03 +0000
Subject: [PATCH] =Some hefty changes to FeedBlock. Transitioning to loading first page in php to save on extra requests. Got a bit to do yet, but I have to work on Northeh for a bit here.

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

diff --git a/inc/managers/queue/executors/UploadExecutor.php b/inc/managers/queue/executors/UploadExecutor.php
index cc828d5..ac659a4 100644
--- a/inc/managers/queue/executors/UploadExecutor.php
+++ b/inc/managers/queue/executors/UploadExecutor.php
@@ -6,7 +6,9 @@
 use JVBase\managers\UploadManager;
 use JVBase\meta\Meta;
 use Exception;
-use JVBase\utility\Features;
+use JVBase\registrar\Registrar;
+use JVBase\base\Site;
+use WP_Error;
 
 if (!defined('ABSPATH')) {
 	exit;
@@ -208,7 +210,43 @@
 		}
 
 		if (!empty($data['field_name'])) {
-			$this->saveToMeta($data, $uploadResults);
+			if ($data['field_name'] === 'timeline_gallery') {
+				$registrar = Registrar::getInstance($data['content']);
+				if ($registrar) {
+					switch ($registrar->getType()) {
+						case 'post':
+							$meta = Meta::forPost($data['item_id']);
+							break;
+						case 'term':
+							$meta = Meta::forTerm($data['item_id']);
+							break;
+						case 'user':
+							$meta = Meta::forUser($data['item_id']);
+							break;
+						default:
+							$meta = false;
+
+					}
+					if ($meta) {
+						$title = $meta->get('post_title');
+						$current = $meta->get('number');
+						$i = empty($current) ? 1 : $current + 1;
+						foreach ($data['upload_ids'] as $uploadID) {
+							if (!array_key_exists($uploadID, $uploadResults)) {
+								continue;
+							}
+							$imgID = $uploadResults[$uploadID]['attachment_id'] ?? false;
+							if (!$imgID) {
+								continue;
+							}
+							$this->createTimelinePoint($imgID, $data['item_id'], $data['user'], $data['content'], $title, $i);
+							$i++;
+						}
+					}
+				}
+			} else {
+				$this->saveToMeta($data, $uploadResults);
+			}
 		}
 
 		$progress->advance(1);
@@ -219,6 +257,25 @@
 		);
 	}
 
+		protected function createTimelinePoint(int $imgID, int $parentID, int $user, string $postType, string $baseTitle, int $index):int|WP_Error
+		{
+			$title = $baseTitle.' - Treatment #'.$index;
+			$args = [
+				'post_type'		=> jvbCheckBase($postType),
+				'post_author'	=> $user,
+				'post_status'	=> 'draft',
+				'post_parent'	=> $parentID,
+				'post_title'	=> $title,
+				'post_name'		=> sanitize_title($title)
+			];
+
+			$child = wp_insert_post($args);
+			if ($child && !is_wp_error($child)) {
+				set_post_thumbnail($child, $imgID);
+			}
+			return $child;
+		}
+
 	/**
 	 * Process metadata updates for attachments
 	 */
@@ -272,6 +329,7 @@
 				$postID = wp_get_post_parent_id($attachmentId);
 				if ($postID && !in_array($postID, $postsAttachedTo)){
 					$postsAttachedTo[] = $postID;
+					//TODO: is there a better way?
 				}
 			}
 
@@ -384,7 +442,8 @@
 		}
 
 		$content = jvbCheckBase($data['content']);
-		if (Features::forContent($data['content'])->has('is_timeline')) {
+		$registrar = Registrar::getInstance($data['content']);
+		if ($registrar && $registrar->hasFeature('is_timeline')) {
 			return $this->processTimelineUploads($operation, $data, $progress, $all_uploads);
 		}
 
@@ -438,14 +497,11 @@
 
 	protected function createPostFromGroup(array $post, int $index, string $content, array $uploads, Operation $op):array|false
 	{
-		$config = JVB_CONTENT[jvbNoBase($content)]??false;
-		if (!$config) {
-			throw new Exception('No config found for content: '.$content.'.');
-		}
+		$registrar = Registrar::getInstance($content);
 
 		$post_title = array_key_exists('post_title', $post['fields'])
 			? sanitize_text_field($post['fields']['post_title'])
-			: 'New '. $config['singular'].' '.($index + 1);
+			: ($registrar ? 'New '. $registrar->getSingular().' '.($index + 1) : 'New Item '.($index + 1));
 
 		$post_excerpt = array_key_exists('post_excerpt', $post['fields'])
 			? sanitize_textarea_field($post['fields']['post_excerpt'])
@@ -489,7 +545,7 @@
 
 		if (!empty($gallery)) {
 			$meta = Meta::forPost($ID);
-			$fields = jvbGetFields($content, 'post');
+			$fields = Registrar::getFieldsFor($content);
 			//add images to first found gallery field
 			$found = false;
 			foreach ($fields as $name =>$config) {
@@ -503,7 +559,6 @@
 				error_log('Could not find a gallery upload field for post '.$ID);
 			}
 
-			$meta->save();
 		}
 
 
@@ -521,9 +576,10 @@
 		$errors = [];
 
 		$content = jvbCheckBase($data['content']);
-		$config = Features::getConfig($content);
+		$registrar = Registrar::getInstance($data['content']);
 
-		$defaultTitle = 'New '.$config['singular']. ' ';
+
+		$defaultTitle = ($registrar) ? 'New '.$registrar->getSingular(). ' ' : 'New Item ';
 		foreach($data['posts'] as $index => $post) {
 			try {
 				$title = array_key_exists('post_title', $post['fields'])
@@ -539,7 +595,7 @@
 					'post_author'	=> $user,
 					'post_status'	=> 'draft',
 					'post_title'	=> $title,
-					'post_slug'		=> sanitize_title($title),
+					'post_name'		=> sanitize_title($title),
 					'post_excerpt'	=> $excerpt
 				];
 
@@ -583,12 +639,10 @@
 
 					foreach($childPosts as $i => $imgID) {
 						$treatment = $i + 1;
-						$args ['post_title'] = $title.' - Treatment #'.$treatment;
-						$child = wp_insert_post($args);
-						if ($child && !is_wp_error($child)) {
-							$createdChildren = $child;
+						$child = $this->createTimelinePoint($imgID, $parent, $args['post_author'], $args['post_type'], $title, $treatment);
+						if ($child && !is_wp_error($child) && $child> 0 ) {
+							$createdChildren[] = $child;
 							$usedUploads[] = $imgID;
-							set_post_thumbnail($child, $imgID);
 						}
 					}
 				}
@@ -628,14 +682,7 @@
 	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'
-		]);
+		$children = jvbTimelinePoints($parentId, get_post_type($parentId), ['any']);
 
 		// Count includes parent + children
 		$number = count($children) + 1;
@@ -715,7 +762,6 @@
 			$meta->set($data['field_name'], implode(',', $allIds));
 		}
 
-		$meta->save();
 	}
 
 	private function updateFieldValue(array $data, array $results): void
@@ -735,7 +781,6 @@
 		$allIds = array_unique(array_merge($existingIds, $attachmentIds));
 
 		$meta->set($data['field_name'], implode(',', $allIds));
-		$meta->save();
 	}
 
 	private function getMetaManager(array $data): ?Meta
@@ -798,14 +843,12 @@
 		} elseif (str_starts_with($mimeType, 'video/')) {
 			$meta = Meta::forPost($postId);
 			$meta->set('video', $attachmentId);
-			$meta->save();
 		} else {
 			$meta = Meta::forPost($postId);
 			$existing = $meta->get('documents');
 			$existingIds = !empty($existing) ? explode(',', $existing) : [];
 			$existingIds[] = $attachmentId;
 			$meta->set('documents', implode(',', $existingIds));
-			$meta->save();
 		}
 	}
 

--
Gitblit v1.10.0