From ba1e1ccf869b818f7a7a897264dfea05563a7796 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 07 Jun 2026 20:10:20 +0000
Subject: [PATCH] =Major overhaul of Integrations. Playing around with adding fields to post types through Registrar from an integrations' class file.

---
 inc/integrations/Square.php |   85 ++++++++++++++++--------------------------
 1 files changed, 32 insertions(+), 53 deletions(-)

diff --git a/inc/integrations/Square.php b/inc/integrations/Square.php
index ba37b92..a6346f1 100644
--- a/inc/integrations/Square.php
+++ b/inc/integrations/Square.php
@@ -26,6 +26,14 @@
  */
 class Square extends Integrations
 {
+	protected array $allowedContent = [
+		'REGULAR',
+		'FOOD_AND_BEV',
+		'APPOINTMENTS_SERVICE',
+		'DIGITAL',
+		'EVENT',
+		'DONATION'
+	];
 	/**
 	 * Square API Configuration
 	 */
@@ -45,6 +53,9 @@
 	 * OAuth Configuration
 	 */
 	protected bool $isOAuthService = true;
+
+	protected string $orderPostType = '_sq_order';
+	protected array $newOrder = [];
 	protected array $oauth = [
 		'authorize' => '',
 		'token' => '',
@@ -80,6 +91,11 @@
 
 		$this->refresh_interval = 7 * DAY_IN_SECONDS;
 
+		$this->newOrder = [
+			'post_type'		=> $this->orderPostType,
+			'post_status' 	=> 'PROPOSED',
+		];
+
 		// Define credential fields
 		$this->fields = [
 			'environment'	=> [
@@ -180,8 +196,7 @@
 				'sync_to_square' => 'Sync Site to Square',
 			]
 		);
-
-		add_action('init', [$this, 'registerSquarePostTypes']);
+		add_action('init', [$this, 'registerSquarePostTypes'], 5);
 	}
 
 	/**
@@ -228,24 +243,20 @@
 					'square_order_id' => [
 						'type' => 'text',
 						'label' => 'Square Order ID',
-						'readonly' => true
 					],
 					'square_payment_id' => [
 						'type' => 'text',
 						'label' => 'Square Payment ID',
-						'readonly' => true
 					],
 					'square_customer_id' => [
 						'type' => 'text',
 						'label' => 'Square Customer ID',
-						'readonly' => true
 					],
 					'amount' => [
 						'type' => 'number',
 						'label' => 'Total Amount (cents)',
-						'readonly' => true
 					],
-					'status' => [
+					'post_status' => [
 						'type' => 'select',
 						'label' => 'Order Status',
 						'options' => [
@@ -255,7 +266,6 @@
 							'COMPLETED' => 'Completed',
 							'CANCELED' => 'Canceled'
 						],
-						'readonly' => true
 					],
 					'fulfillment_status' => [
 						'type' => 'select',
@@ -268,7 +278,6 @@
 							'CANCELED' => 'Canceled',
 							'FAILED' => 'Failed'
 						],
-						'readonly' => true
 					],
 					'pickup_time' => [
 						'type' => 'datetime',
@@ -277,27 +286,22 @@
 					'customer_email' => [
 						'type' => 'email',
 						'label' => 'Customer Email',
-						'readonly' => true
 					],
 					'customer_name' => [
 						'type' => 'text',
 						'label' => 'Customer Name',
-						'readonly' => true
 					],
 					'customer_phone' => [
-						'type' => 'tel',
+						'type' => 'phone',
 						'label' => 'Customer Phone',
-						'readonly' => true
 					],
 					'special_instructions' => [
 						'type' => 'textarea',
 						'label' => 'Special Instructions',
-						'readonly' => true
 					],
 					'items' => [
 						'type' => 'repeater',
 						'label' => 'Order Items',
-						'readonly' => true,
 						'fields' => [
 							'name' => ['type' => 'text', 'label' => 'Item Name'],
 							'quantity' => ['type' => 'number', 'label' => 'Quantity'],
@@ -308,17 +312,14 @@
 					'receipt_url' => [
 						'type' => 'url',
 						'label' => 'Receipt URL',
-						'readonly' => true
 					],
 					'created_at' => [
 						'type' => 'datetime',
 						'label' => 'Created At',
-						'readonly' => true
 					],
 					'updated_at' => [
 						'type' => 'datetime',
 						'label' => 'Last Updated',
-						'readonly' => true
 					]
 				];
 	}
@@ -327,16 +328,14 @@
 	{
 		$orders = Registrar::forPost('_sq_orders', 'Square Order', 'Square Orders');
 		$orders->make([
-			'public'	=> false
-			]
-		);
+			'public'	=> true
+		]);
 		$orders->setAll(['system']);
 
 		$fields = $orders->fields();
 		foreach ($this->getOrderFields() as $fieldName => $config) {
 			$fields->addField($fieldName, $config);
 		}
-
 	}
 
 	/**
@@ -845,10 +844,9 @@
 		add_action('wp_login', [$this, 'trackUserLogin'], 10, 2);
 		add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
 
-		// Shared checkout UI (replaces outputCheckout)
 		add_filter('jvbAdditionalActions', [Checkout::class, 'render']);
 
-		// Square-specific checkout description
+
 		add_filter('jvb_checkout_description', function (string $desc, string $provider) {
 			if ($provider === 'square') {
 				return 'Securely checkout with your name, email, and payments processed by Square.';
@@ -902,31 +900,31 @@
 		$queue    = JVB()->queue();
 		$executor = new IntegrationExecutor();
 
-		$queue->registry()->register('square_sync_to', new TypeConfig(
+		$queue->registry()->register(self::$syncTo, new TypeConfig(
 			executor:   $executor,
 			chunkKey:   'items',
 			chunkSize:  50,
 			maxRetries: 3
 		));
 
-		$queue->registry()->register('square_delete_from', new TypeConfig(
+		$queue->registry()->register(self::$deleteFrom, new TypeConfig(
 			executor:   $executor,
 			chunkKey:   'external_ids',
 			chunkSize:  200,
 			maxRetries: 2
 		));
 
-		$queue->registry()->register('square_sync_from', new TypeConfig(
+		$queue->registry()->register(self::$syncFrom, new TypeConfig(
 			executor:   $executor,
 			maxRetries: 3
 		));
 
-		$queue->registry()->register('square_sync_customer', new TypeConfig(
+		$queue->registry()->register(self::$syncCustomer, new TypeConfig(
 			executor:   $executor,
 			maxRetries: 2
 		));
 
-		$queue->registry()->register('square_import', new TypeConfig(
+		$queue->registry()->register(self::$import, new TypeConfig(
 			executor:   $executor,
 			maxRetries: 3
 		));
@@ -941,7 +939,7 @@
 	 */
 	protected function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void
 	{
-		$this->queueOperation('sync_to', [
+		$this->queueOperation(self::$syncTo, [
 			'items'   => [$postID],
 			'user_id' => $this->userID,
 		], [
@@ -960,7 +958,7 @@
 		$square_id = get_post_meta($postID, BASE . '_square_catalog_id', true);
 
 		if ($square_id) {
-			$this->queueOperation('delete_from', [
+			$this->queueOperation(self::$deleteFrom, [
 				'external_ids' => [$square_id],
 				'post_id'      => $postID,
 			], [
@@ -970,26 +968,6 @@
 	}
 
 	/**
-	 * @deprecated IntegrationExecutor handles new operations via registerQueueTypes().
-	 * Kept for legacy-typed operations ('square_sync_to_square') already queued.
-	 * Safe to remove once all legacy operations have been processed.
-	 */
-	public function processOperation(WP_Error|array $result, object $operation, array $data): WP_Error|array
-	{
-		$base   = strtolower($this->service_name) . '_';
-		$square = array_key_exists('user', $data) ? new self((int) $data['user']) : $this;
-
-		return match ($operation->type) {
-			$base . 'sync_to_square'     => $square->processSyncToSquare($data),
-			$base . 'delete_from_square' => $square->processDeleteFromSquare($data),
-			$base . 'sync_from_square'   => $square->processSyncFromSquare($data),
-			$base . 'sync_customer'      => $square->processSyncCustomer($data),
-			default                      => $result,
-		};
-	}
-
-
-	/**
 	 * Process sync to Square
 	 */
 	private function processSyncToSquare(array $data): array
@@ -2764,17 +2742,18 @@
 			return [];
 		}
 		$array = $this->setBaseFields();
-		return array_combine(
+		$return = array_combine(
 			array_map(fn($k) => 'sq_' . $k, array_keys($array)),
 			$array
 		);
+
+		return $return;
 	}
 	protected function setBaseFields():array
 	{
 		return [
 			'price' => [
 				'type'        => 'number',
-				'bulkEdit'    => true,
 				'label'       => 'Price',
 				'step'        => 0.01,
 				'max'         => 99999,

--
Gitblit v1.10.0