| | |
| | | use JVBase\meta\Form; |
| | | use JVBase\meta\Meta; |
| | | use Exception; |
| | | use JVBase\registry\PostTypeRegistrar; |
| | | use JVBase\registrar\Fields; |
| | | use JVBase\registrar\Posts; |
| | | use JVBase\registrar\Registrar; |
| | | use WP_Error; |
| | | use JVBase\ui\Checkout; |
| | | use JVBase\managers\queue\TypeConfig; |
| | |
| | | |
| | | } |
| | | |
| | | public function getSquarePostConfig(string $post = 'all'):array |
| | | public function getOrderFields():array |
| | | { |
| | | $posts = [ |
| | | '_sq_orders' => [ |
| | | 'singular' => 'Square Order', |
| | | 'plural' => 'Square Orders', |
| | | 'public' => false, |
| | | 'fields' => [ |
| | | return [ |
| | | 'post_title' => [ |
| | | 'type' => 'text', |
| | | 'label' => 'Order Number' |
| | |
| | | 'label' => 'Last Updated', |
| | | 'readonly' => true |
| | | ] |
| | | ] |
| | | ] |
| | | ]; |
| | | |
| | | if ($post === 'all'){ |
| | | return $posts; |
| | | }elseif(array_key_exists($post, $posts)) { |
| | | return $posts[$post]; |
| | | } |
| | | return []; |
| | | ]; |
| | | } |
| | | |
| | | public function registerSquarePostTypes():void |
| | | { |
| | | $squarePostTypes = $this->getSquarePostConfig(); |
| | | foreach ($squarePostTypes as $slug => $config) { |
| | | $registrar = new PostTypeRegistrar($slug, $config); |
| | | $registrar->register(); |
| | | $orders = Registrar::forPost('_sq_orders', 'Square Order', 'Square Orders'); |
| | | $orders->make([ |
| | | 'public' => false |
| | | ] |
| | | ); |
| | | $orders->setAll(['system']); |
| | | |
| | | $fields = $orders->fields(); |
| | | foreach ($this->getOrderFields() as $fieldName => $config) { |
| | | $fields->addField($fieldName, $config); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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 = [ |
| | |
| | | */ |
| | | 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 = [ |
| | |
| | | |
| | | // Set user role (assuming you have a customer role defined) |
| | | $user = new \WP_User($user_id); |
| | | $user->set_role(BASE.'foodie'); // Or whatever role from JVB_USER |
| | | $user->set_role(BASE.'foodie'); // Or whatever role |
| | | |
| | | // Generate password reset key |
| | | $reset_key = get_password_reset_key($user); |
| | |
| | | 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++; |
| | | |
| | |
| | | if ($login_count % self::PASSWORD_RESET_INTERVAL === 0) { |
| | | $this->schedulePasswordReset($user->ID); |
| | | } |
| | | |
| | | break; |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | $meta->setAll($updates); |
| | | $meta->save(); |
| | | |
| | | // Trigger notification to customer if order is ready |
| | | if ($state === 'PREPARED') { |
| | |
| | | wp_enqueue_script('jvb-square-checkout'); |
| | | |
| | | wp_localize_script('jvb-square-checkout', 'squareConfig', [ |
| | | 'isOpen' => jvbIsOpen(), |
| | | //TODO 'isOpen' => jvbIsOpen(), |
| | | 'application_id' => $this->credentials['client_id'] ?? '', |
| | | 'location_id' => $this->locationId, |
| | | 'environment' => $this->environment, |
| | |
| | | */ |
| | | 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) { |
| | |
| | | |
| | | // Save all values at once |
| | | $meta->setAll($values_to_save); |
| | | $meta->save(); |
| | | } |
| | | |
| | | /** |
| | |
| | | '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 [ |
| | |
| | | |
| | | // Save all order meta |
| | | $meta = Meta::forPost($order_post_id); |
| | | $fields = $this->getSquarePostConfig('_sq_orders')['fields']; |
| | | $fields = $this->getOrderFields(); |
| | | unset($fields['post_title']); |
| | | |
| | | $meta->setAll([ |
| | |
| | | 'created_at' => current_time('mysql'), |
| | | 'updated_at' => current_time('mysql') |
| | | ]); |
| | | $meta->save(); |
| | | |
| | | // Index by Square order ID for quick webhook lookups |
| | | update_option(BASE . 'square_order_map_' . $order_data['square_order_id'], $order_post_id); |