From 772462eeca3002a1d52508aeba485aab2b4742ad Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Tue, 03 Mar 2026 19:06:19 +0000
Subject: [PATCH] =MAJOR OVERHAUL. Likely should have made a new branch ages ago. Key changes: Registrar.php is the base for custom post types, taxonomies, and user roles. Replaces JVB_CONTENT, JVB_TAXONOMY, and JVB_USER constants, eliminates most of Features.php (except for JVB_SITE, JVB_MEMBERSHIP), and has built in sanitizing and validation via sub-classes. Also started a major overhaul of the Schema output. Created a shit ton of property traits and classes to help sanitize and ensure proper data for different schema types. Still a bunch to do, but better to be starting committing changes here on this other branch.

---
 inc/integrations/Square.php |   57 ++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/inc/integrations/Square.php b/inc/integrations/Square.php
index d889bfa..85032f3 100644
--- a/inc/integrations/Square.php
+++ b/inc/integrations/Square.php
@@ -4,6 +4,7 @@
 use JVBase\meta\Form;
 use JVBase\meta\Meta;
 use Exception;
+use JVBase\registrar\Registrar;
 use JVBase\registry\PostTypeRegistrar;
 use WP_Error;
 use JVBase\ui\Checkout;
@@ -1384,7 +1385,12 @@
 	 */
 	protected function getVariationMapping(string $post_type): array
 	{
-		$product_type = JVB_CONTENT[jvbNoBase($post_type)]['integrations']['square']['content_type'] ?? 'REGULAR';
+		$registrar = Registrar::getInstance($post_type));
+		if (!$registrar) {
+			return [];
+		}
+		$config = $registrar->getIntegrationConfig($this->service_name);
+		$product_type = $config['content_type']??'REGULAR';
 		$valid_fields = $this->getValidFieldsForProductType($product_type);
 
 		$defaults = [
@@ -1453,7 +1459,12 @@
 	 */
 	protected function getFieldMapping(string $post_type): array
 	{
-		$product_type = JVB_CONTENT[jvbNoBase($post_type)]['integrations']['square']['content_type'] ?? 'REGULAR';
+		$registrar = Registrar::getInstance($post_type));
+		if (!$registrar) {
+			return [];
+		}
+		$config = $registrar->getIntegrationConfig($this->service_name);
+		$product_type = $config['content_type']??'REGULAR';
 		$valid_fields = $this->getValidFieldsForProductType($product_type);
 
 		$defaults = [
@@ -1733,11 +1744,11 @@
 	public function trackUserLogin(string $user_login, \WP_User $user): void
 	{
 		// Check if user has Square integration
-		$roles = array_keys(JVB_USER);
-		$user_roles = $user->roles;
-
-		foreach ($user_roles as $role) {
-			if (isset(JVB_USER[$role]['integrations']['square']['is_customer'])) {
+		$role = jvbUserRole($user->ID);
+		$registrar = Registrar::getInstance($role);
+		if ($registrar) {
+			$config = $registrar->getIntegration($this->service_name);
+			if ($config->isCustomer()) {
 				$login_count = (int)get_user_meta($user->ID, BASE . '_square_login_count', true);
 				$login_count++;
 
@@ -1748,8 +1759,6 @@
 				if ($login_count % self::PASSWORD_RESET_INTERVAL === 0) {
 					$this->schedulePasswordReset($user->ID);
 				}
-
-				break;
 			}
 		}
 	}
@@ -2236,16 +2245,21 @@
 	 */
 	private function importSquareItem(array $item): bool|int
 	{
+		//TODO: We need to add the post type to custom meta for Square, this is not good if we have multiple post types with the same product type
 		// Find matching content type
 		$product_type = $item['item_data']['product_type'] ?? 'REGULAR';
 		$post_type = null;
 
-		foreach (JVB_CONTENT as $key => $config) {
-			if (isset($config['integrations']['square']['content_type']) &&
-				$config['integrations']['square']['content_type'] === $product_type) {
-				$post_type = jvbCheckBase($key);
+		foreach (Registrar::getRegistered() as $registrar) {
+			if (!$registrar->hasIntegration($this->service_name)) {
+				continue;
+			}
+			$config = $registrar->getIntegration($this->service_name);
+			if ($config->getContent_type() && $config->getContent_type() === $product_type) {
+				$post_type = jvbCheckBase($registrar->getSlug());
 				break;
 			}
+
 		}
 
 		if (!$post_type) {
@@ -2746,6 +2760,23 @@
 			'GIFT_CARD'	=> array_merge($this->setGiftCardFields())
 		];
 	}
+	public function getAdditionalFields(?string $content_type = null):array {
+		if ($content_type && array_key_exists($content_type, $this->contentTypes)){
+			$array = $this->contentTypes[$content_type];
+			return array_combine(
+				array_map(fn($k) => 'sq_' . $k, array_keys($array)),
+				$array
+			);
+		} else if ($content_type && !array_key_exists($content_type, $this->contentTypes)) {
+			error_log('Could not get default fields for '.$this->service_name.' content type: '.$content_type);
+			return [];
+		}
+		$array = $this->setBaseFields();
+		return array_combine(
+			array_map(fn($k) => 'sq_' . $k, array_keys($array)),
+			$array
+		);
+	}
 	protected function setBaseFields():array
 	{
 		return [

--
Gitblit v1.10.0