'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => Defaults::oauth_client_id(), ]); register_setting('agenized_chat_widget', Options::OPT_ENABLE_CONTEXT_EVENTS, [ 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'default' => true, ]); register_setting('agenized_chat_widget', Options::OPT_ENABLE_ORDER_WEBHOOKS, [ 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'default' => true, ]); } public static function render(): void { if (!current_user_can('manage_options')) { return; } $notice = null; // Handle OAuth callback if present. $oauth_result = OAuth::handle_callback_if_present(); if (is_array($oauth_result)) { $notice = [ 'type' => $oauth_result['ok'] ? 'success' : 'error', 'text' => (string) ($oauth_result['message'] ?? ''), ]; // Prevent stale OAuth query params from triggering callback handling again on refresh. $oauth_params = ['code', 'state', 'error', 'error_description', 'error_uri']; $has_oauth_params = false; foreach ($oauth_params as $param) { if (isset($_GET[$param])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $has_oauth_params = true; break; } } if ($has_oauth_params) { $redirect_url = remove_query_arg($oauth_params); $redirect_url = add_query_arg( [ 'agenized_notice' => $oauth_result['ok'] ? 'connected' : 'oauth_error', ], $redirect_url ); wp_safe_redirect($redirect_url); exit; } } // Show OAuth outcome after redirect cleanup. if (!is_array($oauth_result) && isset($_GET['agenized_notice'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $notice_key = sanitize_text_field(wp_unslash($_GET['agenized_notice'])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ($notice_key === 'connected') { $notice = ['type' => 'success', 'text' => __('Connected to Agenized', 'agenized-for-woocommerce')]; } elseif ($notice_key === 'oauth_error') { $notice = ['type' => 'error', 'text' => __('Authentication failed. Please try connecting again.', 'agenized-for-woocommerce')]; } } if (isset($_POST['agenized_setup_action'])) { check_admin_referer('agenized_setup_actions'); $action = sanitize_text_field(wp_unslash($_POST['agenized_setup_action'])); if ($action === 'disconnect') { OAuth::disconnect(); $notice = ['type' => 'success', 'text' => __('Disconnected.', 'agenized-for-woocommerce')]; } if ($action === 'save_channel') { $channel_id = isset($_POST['channel_id']) ? sanitize_text_field(wp_unslash($_POST['channel_id'])) : ''; if ($channel_id === '') { Options::delete(Options::OPT_CHANNEL_ID); Options::delete(Options::OPT_CHANNEL_PUBLIC_KEY); Options::update_bool(Options::OPT_ENABLE_WIDGET, false); $notice = ['type' => 'success', 'text' => __('Channel selection cleared. Widget disabled.', 'agenized-for-woocommerce')]; } else { $ch = AgenizedApi::get_channel($channel_id); if (!$ch['ok']) { $notice = ['type' => 'error', 'text' => __('Failed to load selected channel.', 'agenized-for-woocommerce')]; } else { $channel = $ch['channel'] ?? []; $public_key = is_array($channel) && isset($channel['public_key']) ? (string) $channel['public_key'] : ''; if ($public_key === '') { $notice = ['type' => 'error', 'text' => __('Selected channel has no public key.', 'agenized-for-woocommerce')]; } else { Options::update_string(Options::OPT_CHANNEL_ID, $channel_id); Options::update_string(Options::OPT_CHANNEL_PUBLIC_KEY, $public_key); $notice = ['type' => 'success', 'text' => __('Channel saved. The widget is active.', 'agenized-for-woocommerce')]; } } } } if ($action === 'toggle_widget') { $currently_enabled = Options::get_bool(Options::OPT_ENABLE_WIDGET, true); $next_enabled = !$currently_enabled; $public_key = Options::get_string(Options::OPT_CHANNEL_PUBLIC_KEY, ''); if ($next_enabled && $public_key === '') { $notice = ['type' => 'error', 'text' => __('Select and save a channel before enabling the widget.', 'agenized-for-woocommerce')]; } else { Options::update_bool(Options::OPT_ENABLE_WIDGET, $next_enabled); $notice = [ 'type' => 'success', 'text' => $next_enabled ? __('Widget enabled on your site.', 'agenized-for-woocommerce') : __('Widget disabled on your site.', 'agenized-for-woocommerce'), ]; } } if ($action === 'connect_woocommerce') { $result = self::connect_woocommerce_provider(); $notice = [ 'type' => $result['ok'] ? 'success' : 'error', 'text' => (string) ($result['message'] ?? ''), ]; } } $is_connected = OAuth::is_connected(); $connect_url = OAuth::begin_auth_url(); $signup_url = Defaults::signup_url(); $dashboard_url = Defaults::merchant_portal_base_url(); $store_url = rtrim(home_url(), '/'); $dev_mode_enabled = Defaults::is_dev_mode(); $oauth_client_id_value = Options::get_string(Options::OPT_OAUTH_CLIENT_ID, ''); if ($oauth_client_id_value === '') { $oauth_client_id_value = Defaults::oauth_client_id(); } $connected_email = ''; $connected_merchant = ''; $connected_account = ''; if ($is_connected) { $me_res = AgenizedApi::get_me_context(); if (!empty($me_res['ok']) && isset($me_res['me']) && is_array($me_res['me'])) { $me = $me_res['me']; $merchant = isset($me['merchant']) && is_array($me['merchant']) ? $me['merchant'] : []; $active_account = isset($me['active_account']) && is_array($me['active_account']) ? $me['active_account'] : []; $connected_merchant = isset($merchant['name']) ? (string) $merchant['name'] : ''; $connected_account = isset($active_account['name']) ? (string) $active_account['name'] : ''; } } $channels_res = $is_connected ? AgenizedApi::list_chat_widget_channels() : ['ok' => false, 'channels' => [], 'message' => '']; $channels = ($is_connected && !empty($channels_res['ok'])) ? (array) ($channels_res['channels'] ?? []) : []; $selected_channel_id = Options::get_string(Options::OPT_CHANNEL_ID, ''); $selected_channel_name = ''; $widget_enabled = Options::get_bool(Options::OPT_ENABLE_WIDGET, true); $has_widget_channel = Options::get_string(Options::OPT_CHANNEL_PUBLIC_KEY, '') !== ''; $widget_effective_enabled = $widget_enabled && $has_widget_channel; foreach ($channels as $channel) { if (!is_array($channel)) { continue; } $id = isset($channel['id']) ? (string) $channel['id'] : ''; if ($id === $selected_channel_id) { $selected_channel_name = isset($channel['name']) ? (string) $channel['name'] : $id; break; } } $has_woocommerce = class_exists('WooCommerce'); $wc_connected = false; $wc_linked_to_site = false; $wc_has_any_connection = false; $wc_connection_id = Options::get_string(Options::OPT_WC_ACTION_CONNECTION_ID, ''); if ($is_connected) { $providers_res = AgenizedApi::list_action_providers(); if (!empty($providers_res['ok']) && isset($providers_res['providers']) && is_array($providers_res['providers'])) { foreach ($providers_res['providers'] as $provider) { if (!is_array($provider)) { continue; } $key = isset($provider['provider']) ? (string) $provider['provider'] : ''; if ($key !== 'woocommerce') { continue; } $count = isset($provider['connection_count']) ? (int) $provider['connection_count'] : 0; if ($count > 0) { $wc_has_any_connection = true; } break; } } $list = AgenizedApi::list_provider_connections('woocommerce'); if (!empty($list['ok']) && isset($list['connections']) && is_array($list['connections'])) { foreach ($list['connections'] as $conn) { if (!is_array($conn)) { continue; } $id = isset($conn['id']) ? (string) $conn['id'] : ''; $config = isset($conn['config']) && is_array($conn['config']) ? $conn['config'] : []; $base = isset($config['base_url']) ? rtrim((string) $config['base_url'], '/') : ''; if ($id !== '') { $wc_has_any_connection = true; } if ($id !== '' && $wc_connection_id !== '' && $id === $wc_connection_id) { $wc_connected = true; } if ($base !== '' && $base === $store_url) { $wc_linked_to_site = true; $wc_connected = true; if ($wc_connection_id === '' && $id !== '') { Options::update_string(Options::OPT_WC_ACTION_CONNECTION_ID, $id); $wc_connection_id = $id; } } } } if (!$wc_connected && $wc_connection_id !== '') { Options::delete(Options::OPT_WC_ACTION_CONNECTION_ID); Options::delete(Options::OPT_WC_KEY_ID); } } $wc_status_connected = $wc_connected || $wc_has_any_connection; ?>
Actions in Agenized.', 'agenized-for-woocommerce'); ?> Actions', 'agenized-for-woocommerce'); ?>.
define('AGENIZED_DEV_MODE', true);