From fff721dd185f5b97f7ae7a6e64189e55887ff590 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 05 Jul 2026 18:36:57 +0000
Subject: [PATCH] =Cleaning up the Square integration (still a bit more to do yet). Also majorly overhauled /rest/ files to ignore a rest request 'user' paramater, and rely on get_current_user_id() instead.

---
 inc/ui/CRUDSkeleton.php |  205 +++++++++++++++++++++++++++++----------------------
 1 files changed, 116 insertions(+), 89 deletions(-)

diff --git a/inc/ui/CRUDSkeleton.php b/inc/ui/CRUDSkeleton.php
index d1d4834..87eec03 100644
--- a/inc/ui/CRUDSkeleton.php
+++ b/inc/ui/CRUDSkeleton.php
@@ -38,6 +38,7 @@
 	protected string $singular = '';
 	protected string $plural = '';
 	protected string $icon;
+	protected string $emptyState = '';
 
 	// Capabilities
 	protected array $caps = [];
@@ -145,6 +146,15 @@
 		return $this;
 	}
 
+	public function icon(string $icon):void
+	{
+		$this->icon = $icon;
+	}
+
+	public function getIcon():string
+	{
+		return $this->icon;
+	}
 	/**
 	 * Set content type information
 	 */
@@ -533,7 +543,7 @@
 	 * Render the CRUD interface
 	 */
 	public function render(): void {
-		$config = $this->build();
+
 		$classes = array_merge(['dashboard-page', $this->dataType], $this->additionalClasses);
 
 //		ob_start();
@@ -600,7 +610,7 @@
 		$dataIgnore = $this->useCRUDjs ? '' : ' data-ignore';
 		?>
 		<section class="items-list <?= esc_attr($this->dataType) ?> crud" data-content="<?= esc_attr($this->dataType) ?>" data-singular="<?=$this->singular?>" data-plural="<?=$this->plural?>" data-view="<?= $this->defaultView?>"<?=$dataIgnore?>>
-			<div class="wrap">
+			<div id="item-wrap">
 				<?php
 				$this->renderControlsAndFilters();
 
@@ -869,6 +879,7 @@
 				$form = ob_get_clean();
 				echo jvbNewModal(
 					'date-range',
+				'date-range',
 				'Filter Results by Date:',
 					$form
 				);
@@ -1101,15 +1112,23 @@
 	}
 	protected function renderEmptyState():string
 	{
-		ob_start();
-		?>
-		<div class="empty-state">
-			<h3><?=jvbDashIcon($this->icon)?>Nothing here<?=jvbDashIcon($this->icon)?></h3>
-			<p>It doesn't look like you have any <?=$this->plural ?> yet.</p>
-			<p><small><i>Add many by uploading images above.</i>, or click the "<?=jvbDashIcon('plus-square')?>" button to add one at a time.</small></p>
-		</div>
-		<?php
-		return ob_get_clean();
+
+		return empty($this->emptyState) ? sprintf(
+			'<div class="empty-state">
+				<h3>%sNothing here%s</h3>
+				<p>It doesn\'t look like you have any %s yet.</p>
+			<p><small><i>Add many by uploading images above.</i>, or click the "%s" button to add one at a time.</small></p>
+			</div>',
+			jvbDashIcon($this->icon),
+			jvbDashIcon($this->icon),
+			$this->plural,
+			jvbDashIcon('plus-square')
+		) : $this->emptyState;
+	}
+
+	public function setEmptyState(string $state):void
+	{
+		$this->emptyState = $state;
 	}
 
 	protected function renderGalleryPreviewTemplate():void
@@ -1217,7 +1236,6 @@
 		?>
 		<template class="contentTable">
 			<form class="table"
-				data-save="content"
 				data-content="<?= esc_attr($this->dataType) ?>"
 				data-form-id="content-table-<?= esc_attr($this->dataType) ?>"
 				<?= $permissions?>>
@@ -1255,9 +1273,9 @@
 					<?= $this->renderItemSelect() ?>
 				</td>
 				<?php
-					if (array_key_exists('status', $this->fields)){
+					if (array_key_exists('post_status', $this->fields)){
 						?>
-						<td class="status" data-field="post_status">
+						<td class="post_status" data-field="post_status" data-field-type="radio">
 							<?= $this->renderStatusRadios() ?>
 						</td>
 						<?php
@@ -1271,7 +1289,7 @@
 					'radio'
 				];
 				foreach ($this->fields as $name => $config):
-					if (array_key_exists('hidden', $config) || $name === 'status'){
+					if ((array_key_exists('hidden', $config) && $config['hidden'] === true) || $name === 'post_status'){
 						continue;
 					}
 					$makeThisDetailed = (in_array($config['type'], $makeDetails));
@@ -1311,7 +1329,6 @@
 		?>
 		<template class="contentTable">
 			<form class="table"
-				  data-save="content"
 				  data-content="<?= esc_attr($this->dataType) ?>"
 				  data-form-id="content-table-<?= esc_attr($this->dataType) ?>">
 				<?= jvbFormStatus() ?>
@@ -1384,7 +1401,7 @@
 				</td>
 				<?php
 				foreach ($this->fields as $name => $config) {
-					if(array_key_exists('hidden', $config) || $name === 'post_status') {
+					if((array_key_exists('hidden', $config) && $config['hidden'] === true) || $name === 'post_status') {
 						continue;
 					}
 					if (!in_array($name, $this->timelineUniqueFields)) {
@@ -1411,64 +1428,59 @@
 	 * Render table header
 	 */
 	protected function renderTableHeader(): string {
-		ob_start();
-
-		?>
-		<tr>
+		$out = '<tr>
 			<th scope="col" class="select-header">
 				<input type="checkbox" id="select-all" name="select-all">
 				<label for="select-all">All</label>
-			</th>
-			<?php if (array_key_exists('status', $this->fields)) { ?>
-				<th scope="col" class="status-header">Status</th>
-			<?php } ?>
-			<?php foreach ($this->fields as $name => $config):
-				if (array_key_exists('hidden', $config) || $name === 'status'){
-					continue;
-				}
-				?>
-				<th scope="col" class="show-<?= esc_attr($name) ?>"<?= (in_array($name, $this->stuck)) ? ' data-stuck':''?>>
-					<?= esc_html($config['label']) ?>
-				</th>
-			<?php endforeach;
-			if (!empty($this->itemActions)) {
-				?>
-				<th scope="col" class="show-actions">
-					Actions
-				</th>
-				<?php
-			}
-			?>
+			</th>';
 
-		</tr>
-		<?php
-		return ob_get_clean();
+		if (array_key_exists('post_status', $this->fields)){
+			$out .= '<th scope="col" class="status-header">Status</th>';
+		}
+
+		foreach ($this->fields as $name => $config) {
+			if ($name === 'post_status' || (array_key_exists('hidden', $config) && $config['hidden'] === true)) {
+				continue;
+			}
+			$out .= sprintf(
+				'<th scope="col" class="show-%s"%s>%s</th>',
+				esc_attr($name),
+				in_array($name, $this->stuck) ? ' data-stuck' : '',
+				esc_html($config['label'])
+			);
+		}
+
+		if (!empty($this->itemActions)) {
+			$out .= '<th scope="col" class="show-actions">Actions</th>';
+		}
+		$out .= '</tr>';
+
+		return $out;
 	}
 
 	protected function renderTimelineTableHeader(): string {
-		ob_start();
-
-		?>
-		<tr>
+		$out = '<tr>
 			<th scope="col" class="select-header">
 				<input type="checkbox" id="select-all" name="select-all">
 				<label for="select-all">All</label>
 			</th>
 			<th scope="col" class="show-post_status">
 				Status
-			</th>
-			<?php foreach ($this->fields as $name => $config):
-				if (array_key_exists('hidden', $config) || $name === 'post_status'){
-					continue;
-				}
-				?>
-				<th scope="col" class="show-<?= esc_attr($name) ?>"<?= (in_array($name, $this->stuck)) ? ' data-stuck':''?>>
-					<?= esc_html($config['label']) ?>
-				</th>
-			<?php endforeach; ?>
-		</tr>
-		<?php
-		return ob_get_clean();
+			</th>';
+
+		foreach ($this->fields as $name => $config) {
+			if ($name === 'post_status' || (array_key_exists('hidden', $config) && $config['hidden'] === true)) {
+				continue;
+			}
+			$out .= sprintf(
+				'<th scope="col" class="show-%s"%s>%s</th>th>',
+				esc_attr($name),
+				in_array($name, $this->stuck) ? ' data-stuck':'',
+				esc_html($config['label'])
+			);
+		}
+		$out .= '</tr>';
+		return $out;
 	}
 
 	/**
@@ -1496,29 +1508,35 @@
 		return ob_get_clean();
 	}
 
-	protected function renderStatusRadios(): string {
-		ob_start();
-		?>
-		<div class="radio-options status-options row" data-field="post_status" data-field-type="radio">
-			<?php foreach ($this->statuses as $status):
-				if ($status === 'all') continue;
-				if (!array_key_exists($status, $this->allowedStatuses)) continue;
-				$config = $this->allowedStatuses[$status];
-				?>
+	protected function renderStatusRadios(): string
+	{
+		$out = '<div class="radio-options status-options row">';
+		foreach ($this->statuses as $status) {
+			if ($status === 'all' || !array_key_exists($status, $this->allowedStatuses)) continue;
+			$config = $this->allowedStatuses[$status];
 
-				<input type="radio"
+			$out .= sprintf(
+				'<input type="radio"
 					   name="post_status"
-					   id="status-<?= esc_attr($status) ?>"
-					   value="<?= esc_attr($status) ?>">
-				<label for="status-<?= esc_attr($status) ?>">
-					<?= jvbDashIcon($config['icon']) ?>
-					<span class="screen-reader-text"><?= esc_html($config['label']) ?></span>
-				</label>
-			<?php endforeach; ?>
-			<span class="validation-message" hidden role="alert"></span>
-		</div>
-		<?php
-		return ob_get_clean();
+					   id="post_status-%s"
+					   class="btn"
+					   value="%s">
+				<label for="post_status-%s" title="%s">
+					%s
+					<span class="screen-reader-text">%s</span>
+				</label>',
+			 	esc_attr($status),
+			  	esc_attr($status),
+			 	esc_attr($status),
+				esc_html($config['label']),
+			 	jvbDashIcon($config['icon']),
+				esc_html($config['label'])
+			);
+		}
+		$out .= '<span class="validation-message" hidden role="alert"></span>
+		</div>';
+
+		return $out;
 	}
 
 
@@ -1564,6 +1582,7 @@
 	{
 		echo jvbNewModal(
 			'create',
+			'create',
 			'Creating <span class="count"></span> New '.$this->singular,
 			str_replace('edit-form"', 'create-form" data-noautosave', $this->editForm())
 		);
@@ -1572,14 +1591,16 @@
 	protected function editForm():string
 	{
 		ob_start();
+/*		<form class="edit-form" data-save="content" data-form-id="edit-<?=$this->dataType?>" data-autosave<?= ($this->isTimeline) ? ' data-timeline' : ''*/
 		?>
-		<form class="edit-form" data-save="content" data-form-id="edit-<?=$this->dataType?>" data-autosave<?= ($this->isTimeline) ? ' data-timeline' : ''?>>
+
+		<form class="edit-form" data-form-id="edit-<?=$this->dataType?>" data-autosave<?= ($this->isTimeline) ? ' data-timeline' : ''?>>
 			<?= jvbFormStatus() ?>
 			<input type="hidden" name="form-id" value="<?=uniqid('new-')?>" />
 			<input type="hidden" name="content" value="<?=$this->dataType?>" />
 			<div class="fields">
 				<?php
-				if (!empty($this->statuses)) {
+				if (empty($this->sections) && !empty($this->statuses)) {
 					echo Form::render('post_status', '', $this->getStatusFieldConfig('edit-'));
 				}
 
@@ -1593,8 +1614,9 @@
 								'icon'	=> $config['icon']
 							];
 						}
+
 						$tabs[$slug] = array_merge([
-							'title'	=> $config['label'],
+							'title'	=> $config['title'],
 							'content' => '',
 							'description' => $config['description']??'',
 						], $section);
@@ -1615,7 +1637,7 @@
 					foreach ($first as $f) {
 						if (array_key_exists($f, $fields)) {
 							if ($tabs) {
-								$tabs['basic']['content'] .= Form::render($f, '', $fields[$f]);
+								$tabs['main']['content'] .= Form::render($f, '', $fields[$f]);
 							} else {
 								echo Form::render($f, '', $fields[$f]);
 							}
@@ -1660,12 +1682,13 @@
 
 					$fields = $this->nonTimelineFields;
 				}
+
 				foreach ($fields as $n => $config) {
 					if (in_array($config['type'], ['taxonomy', 'selector'])) {
 						$config = array_merge($config, $this->taxConfig($config['taxonomy'], $config['label']));
 					}
 					if ($tabs) {
-						$section = (array_key_exists('section', $config)) ? $config['section'] : 'basic';
+						$section = (array_key_exists('section', $config)) && !empty($config['section']) ? $config['section'] : 'main';
 						$tabs[$section]['content'] .= Form::render($n, '', $config);
 					} else {
 						echo Form::render($n, '', $config);
@@ -1687,6 +1710,7 @@
 	{
 		echo jvbNewModal(
 			'edit',
+			'edit',
 			'Edit your '.$this->singular,
 			$this->editForm()
 		);
@@ -1696,8 +1720,10 @@
 	{
 		if (empty($this->bulkActions)) return;
 		ob_start();
+/*		<form class="bulk-edit-form" data-save="content" data-form-id="bulk-edit-<?=$this->dataType?>">*/
 		?>
-		<form class="bulk-edit-form" data-save="content" data-form-id="bulk-edit-<?=$this->dataType?>">
+
+		<form class="bulk-edit-form" data-form-id="bulk-edit-<?=$this->dataType?>">
 			<?= jvbFormStatus() ?>
 			<div class="selected"></div>
 			<p class="description">You can unselect items by clicking the image here.</p>
@@ -1737,6 +1763,7 @@
 		$form = ob_get_clean();
 		echo jvbNewModal(
 			'bulkEdit',
+			'bulkEdit',
 			'Bulk Edit <span class="selected"></span> '.$this->plural,
 			$form
 		);

--
Gitblit v1.10.0