'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; ?>


.


.

'disabled'] : []); ?>
'disabled'] : []); ?>


Actions in Agenized.', 'agenized-for-woocommerce'); ?> Actions', 'agenized-for-woocommerce'); ?>.

'disabled'] : []); ?>


define('AGENIZED_DEV_MODE', true);

false, 'message' => __('WooCommerce is not installed/active.', 'agenized-for-woocommerce')]; } $store_url = rtrim(home_url(), '/'); $list = AgenizedApi::list_provider_connections('woocommerce'); if (empty($list['ok'])) { return ['ok' => false, 'message' => __('Failed to check existing WooCommerce connections.', 'agenized-for-woocommerce')]; } $connections = is_array($list['connections'] ?? null) ? $list['connections'] : []; foreach ($connections as $conn) { if (!is_array($conn)) { continue; } $config = isset($conn['config']) && is_array($conn['config']) ? $conn['config'] : []; $base = isset($config['base_url']) ? rtrim((string) $config['base_url'], '/') : ''; $connection_id = isset($conn['id']) ? (string) $conn['id'] : ''; if ($base === $store_url && $connection_id !== '') { Options::update_string(Options::OPT_WC_ACTION_CONNECTION_ID, $connection_id); return ['ok' => true, 'message' => __('WooCommerce connection already exists and is now linked.', 'agenized-for-woocommerce')]; } } $key_res = self::create_woocommerce_keys(); if (empty($key_res['ok'])) { return ['ok' => false, 'message' => (string) ($key_res['message'] ?? __('Failed to create WooCommerce keys.', 'agenized-for-woocommerce'))]; } $payload = [ 'name' => __('WooCommerce:', 'agenized-for-woocommerce') . ' ' . wp_parse_url($store_url, PHP_URL_HOST), 'auth_type' => 'api_key', 'config' => [ 'base_url' => $store_url, 'consumer_key' => (string) $key_res['consumer_key'], 'consumer_secret' => (string) $key_res['consumer_secret'], ], ]; $conn = AgenizedApi::create_provider_connection('woocommerce', $payload); if (empty($conn['ok'])) { return ['ok' => false, 'message' => __('Failed to create WooCommerce connection in Agenized.', 'agenized-for-woocommerce')]; } $connection = $conn['connection'] ?? []; $connection_id = is_array($connection) && isset($connection['id']) ? (string) $connection['id'] : ''; if ($connection_id === '') { return ['ok' => false, 'message' => __('WooCommerce connection created but missing ID.', 'agenized-for-woocommerce')]; } Options::update_string(Options::OPT_WC_ACTION_CONNECTION_ID, $connection_id); Options::update_string(Options::OPT_WC_KEY_ID, (string) $key_res['key_id']); return ['ok' => true, 'message' => __('WooCommerce connected to Agenized.', 'agenized-for-woocommerce')]; } /** * @return array{ok:bool,consumer_key?:string,consumer_secret?:string,key_id?:string,message?:string} */ private static function create_woocommerce_keys(): array { global $wpdb; $user_id = get_current_user_id(); if (!$user_id) { return ['ok' => false, 'message' => __('No current user.', 'agenized-for-woocommerce')]; } try { $rand_hash = function_exists('wc_rand_hash') ? wc_rand_hash() : bin2hex(random_bytes(20)); $consumer_key = 'ck_' . $rand_hash; $consumer_secret = 'cs_' . bin2hex(random_bytes(20)); $hashed_key = function_exists('wc_api_hash') ? wc_api_hash($consumer_key) : hash_hmac('sha256', $consumer_key, 'wc-api'); $table_name = $wpdb->prefix . 'woocommerce_api_keys'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->insert( $table_name, [ 'user_id' => $user_id, 'description' => __('Agenized Integration', 'agenized-for-woocommerce'), 'permissions' => 'read_write', 'consumer_key' => $hashed_key, 'consumer_secret' => $consumer_secret, 'truncated_key' => substr($consumer_key, -7), 'last_access' => null, ], ['%d', '%s', '%s', '%s', '%s', '%s', '%s'] ); if ($result === false) { return ['ok' => false, 'message' => __('Failed to insert WooCommerce API key into database:', 'agenized-for-woocommerce') . ' ' . $wpdb->last_error]; } return [ 'ok' => true, 'consumer_key' => $consumer_key, 'consumer_secret' => $consumer_secret, 'key_id' => (string) $wpdb->insert_id, ]; } catch (\Throwable $e) { return ['ok' => false, 'message' => __('Failed to create WooCommerce keys.', 'agenized-for-woocommerce')]; } } }