flexible-shipping
/
vendor_prefixed
/
octolize
/
wp-octolize-docs-chat
/
src
/
Chat
/
ChatSettings.php
flexible-shipping
/
vendor_prefixed
/
octolize
/
wp-octolize-docs-chat
/
src
/
Chat
Last commit date
views
2 weeks ago
AjaxChatSettings.php
2 weeks ago
Assets.php
2 weeks ago
ChatContainer.php
2 weeks ago
ChatSettings.php
2 weeks ago
ChatSettingsProvider.php
2 weeks ago
ConsentSettings.php
2 weeks ago
DocsChat.php
2 weeks ago
HookableChatObjects.php
2 weeks ago
ChatSettings.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace FSVendor\Octolize\Docs\Chat; |
| 5 | |
| 6 | /** |
| 7 | * Configures the chat and its diagnostic metadata. |
| 8 | */ |
| 9 | class ChatSettings |
| 10 | { |
| 11 | private const WEBHOOK_URL = 'webhook_url'; |
| 12 | private const STREAMING = 'streaming'; |
| 13 | private const INITIAL_MESSAGES = 'initial_messages'; |
| 14 | private const TITLE = 'title'; |
| 15 | private const SUBTITLE = 'subtitle'; |
| 16 | private const FOOTER = 'footer'; |
| 17 | private const INPUT_PLACEHOLDER = 'input_placeholder'; |
| 18 | private const GET_STARTED = 'get_started'; |
| 19 | private const METADATA = 'metadata'; |
| 20 | private const CONSENT = 'consent'; |
| 21 | private const CONSENT_DISABLED = 'consent_disabled'; |
| 22 | private string $plugin = 'Octolize'; |
| 23 | private array $plugin_settings = []; |
| 24 | private array $plugin_settings_masked_fields = ['api_key', 'api_secret', 'client_id', 'client_secret', 'user_id', 'password', 'access_key', 'account_numer']; |
| 25 | private array $shipping_method_settings = []; |
| 26 | private string $current_page = 'Plugin settings'; |
| 27 | private array $settings = []; |
| 28 | private ?ConsentSettings $consent_settings = null; |
| 29 | public function set_webhook_url(string $url): void |
| 30 | { |
| 31 | $this->settings[self::WEBHOOK_URL] = $url; |
| 32 | } |
| 33 | public function set_streaming(bool $streaming): void |
| 34 | { |
| 35 | $this->settings[self::STREAMING] = $streaming; |
| 36 | } |
| 37 | public function set_initial_messages(array $messages): void |
| 38 | { |
| 39 | $this->settings[self::INITIAL_MESSAGES] = $messages; |
| 40 | } |
| 41 | public function set_title(string $title): void |
| 42 | { |
| 43 | $this->settings[self::TITLE] = $title; |
| 44 | } |
| 45 | public function set_subtitle(string $subtitle): void |
| 46 | { |
| 47 | $this->settings[self::SUBTITLE] = $subtitle; |
| 48 | } |
| 49 | public function set_footer(string $footer): void |
| 50 | { |
| 51 | $this->settings[self::FOOTER] = $footer; |
| 52 | } |
| 53 | public function set_input_placeholder(string $placeholder): void |
| 54 | { |
| 55 | $this->settings[self::INPUT_PLACEHOLDER] = $placeholder; |
| 56 | } |
| 57 | public function set_metadata(array $metadata): void |
| 58 | { |
| 59 | $this->settings[self::METADATA] = $metadata; |
| 60 | } |
| 61 | /** |
| 62 | * @param string $key |
| 63 | * @param $value |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function add_metadata(string $key, $value): void |
| 68 | { |
| 69 | $this->settings[self::METADATA][$key] = $value; |
| 70 | } |
| 71 | public function set_plugin_settings(array $settings): void |
| 72 | { |
| 73 | $this->plugin_settings = $settings; |
| 74 | } |
| 75 | public function set_get_started(string $get_started): void |
| 76 | { |
| 77 | $this->settings[self::GET_STARTED] = $get_started; |
| 78 | } |
| 79 | /** |
| 80 | * Enable or disable consent popup explicitly. |
| 81 | * When true, consent popup is disabled and chat can initialize without asking. |
| 82 | */ |
| 83 | public function set_consent_disabled(bool $disabled): void |
| 84 | { |
| 85 | $this->settings[self::CONSENT_DISABLED] = $disabled; |
| 86 | } |
| 87 | public function set_consent(array $consent): void |
| 88 | { |
| 89 | $this->consent_settings = ConsentSettings::from_array($consent); |
| 90 | } |
| 91 | public function set_consent_settings(ConsentSettings $consent_settings): void |
| 92 | { |
| 93 | $this->consent_settings = $consent_settings; |
| 94 | } |
| 95 | public function set_shipping_method_settings(array $settings): void |
| 96 | { |
| 97 | $this->shipping_method_settings = $settings; |
| 98 | } |
| 99 | public function set_plugin(string $plugin): void |
| 100 | { |
| 101 | $this->plugin = $plugin; |
| 102 | } |
| 103 | public function set_current_page(string $page): void |
| 104 | { |
| 105 | $this->current_page = $page; |
| 106 | } |
| 107 | public function get_settings(): array |
| 108 | { |
| 109 | return $this->add_metadata_to_settings($this->prepare_settings()); |
| 110 | } |
| 111 | private function prepare_settings(): array |
| 112 | { |
| 113 | $settings = $this->settings; |
| 114 | if (empty($settings[self::WEBHOOK_URL])) { |
| 115 | $settings[self::WEBHOOK_URL] = 'https://n8n.octolize.dev/webhook/5f60a382-151a-49bf-8baa-52b070c11179/chat'; |
| 116 | } |
| 117 | if (empty($settings[self::STREAMING])) { |
| 118 | $settings[self::STREAMING] = \false; |
| 119 | } |
| 120 | if (empty($settings[self::INITIAL_MESSAGES])) { |
| 121 | $settings[self::INITIAL_MESSAGES] = [__('Hi there! 👋 I’m here to help you set up and use the plugin.', 'flexible-shipping'), __('You can ask me about: |
| 122 | - settings and configuration, |
| 123 | - common errors and how to fix them, |
| 124 | - integrations with other tools. |
| 125 | |
| 126 | Just tell me what you need help with 🙂', 'flexible-shipping')]; |
| 127 | } |
| 128 | if (empty($settings[self::TITLE])) { |
| 129 | $settings[self::TITLE] = __('Hi there! 👋', 'flexible-shipping'); |
| 130 | } |
| 131 | if (empty($settings[self::SUBTITLE])) { |
| 132 | $settings[self::SUBTITLE] = __('I’m your AI Assistant. I’ll help you quickly configure the plugin and solve any issues.', 'flexible-shipping'); |
| 133 | } |
| 134 | if (empty($settings[self::INPUT_PLACEHOLDER])) { |
| 135 | $settings[self::INPUT_PLACEHOLDER] = __('Type your question to get instant answer..', 'flexible-shipping'); |
| 136 | } |
| 137 | if (empty($settings[self::FOOTER])) { |
| 138 | $settings[self::FOOTER] = __('AI can make mistakes. Octolize has access to the conversation held in this chat.', 'flexible-shipping'); |
| 139 | } |
| 140 | if (empty($settings[self::GET_STARTED])) { |
| 141 | $settings[self::GET_STARTED] = __('New conversation', 'flexible-shipping'); |
| 142 | } |
| 143 | $settings[self::CONSENT] = ($this->consent_settings ?? new ConsentSettings())->to_array(); |
| 144 | if (!array_key_exists(self::CONSENT_DISABLED, $settings)) { |
| 145 | $settings[self::CONSENT_DISABLED] = \false; |
| 146 | } |
| 147 | return $settings; |
| 148 | } |
| 149 | private function add_metadata_to_settings(array $settings): array |
| 150 | { |
| 151 | return array_merge($settings, [self::METADATA => $this->get_metadata()]); |
| 152 | } |
| 153 | private function get_metadata(): array |
| 154 | { |
| 155 | $metadata = $this->settings[self::METADATA] ?? []; |
| 156 | $metadata['plugin'] = $this->plugin; |
| 157 | $metadata['plugin_settings'] = $this->mask_settings($this->plugin_settings); |
| 158 | $metadata['shipping_method_settings'] = $this->shipping_method_settings; |
| 159 | $metadata['user_id'] = $this->user_id ?? md5(site_url()); |
| 160 | $metadata['current_page'] = $this->current_page; |
| 161 | $metadata['wc_store_address'] = $this->prepare_store_address(); |
| 162 | $metadata['wc_shipping_options'] = $this->prepare_shipping_options(); |
| 163 | return $metadata; |
| 164 | } |
| 165 | private function prepare_store_address(): array |
| 166 | { |
| 167 | return ['woocommerce_store_address' => get_option('woocommerce_store_address', ''), 'woocommerce_store_address_2' => get_option('woocommerce_store_address_2', ''), 'woocommerce_store_city' => get_option('woocommerce_store_city', ''), 'woocommerce_store_postcode' => get_option('woocommerce_store_postcode', ''), 'woocommerce_default_country' => get_option('woocommerce_default_country', '')]; |
| 168 | } |
| 169 | private function prepare_shipping_options(): array |
| 170 | { |
| 171 | return ['woocommerce_allowed_countries' => get_option('woocommerce_allowed_countries', ''), 'woocommerce_ship_to_countries' => get_option('woocommerce_ship_to_countries', ''), 'woocommerce_default_customer_address' => get_option('woocommerce_default_customer_address', '')]; |
| 172 | } |
| 173 | private function mask_settings(array $settings): array |
| 174 | { |
| 175 | foreach ($settings as $key => $value) { |
| 176 | if (in_array($key, $this->plugin_settings_masked_fields, \true)) { |
| 177 | $settings[$key] = '********'; |
| 178 | } |
| 179 | } |
| 180 | return $settings; |
| 181 | } |
| 182 | } |
| 183 |