'array', 'sanitize_callback' => [self::class, 'sanitizeSettings'], 'default' => self::defaults(), ] ); add_settings_section( 'askanydocs_chatbot_connection', __('Widget connection', 'askanydocs-chatbot'), [self::class, 'renderConnectionSection'], self::SETTINGS_PAGE ); add_settings_field('askanydocs_chatbot_enabled', __('Enable widget', 'askanydocs-chatbot'), [self::class, 'renderEnabledField'], self::SETTINGS_PAGE, 'askanydocs_chatbot_connection'); add_settings_field('askanydocs_chatbot_bot_id', __('Bot ID', 'askanydocs-chatbot'), [self::class, 'renderBotIdField'], self::SETTINGS_PAGE, 'askanydocs_chatbot_connection'); add_settings_field('askanydocs_chatbot_share_user', __('Signed-in user', 'askanydocs-chatbot'), [self::class, 'renderShareUserField'], self::SETTINGS_PAGE, 'askanydocs_chatbot_connection'); } /** * @param mixed $input * @return array{enabled: bool, bot_id: string, share_user: bool} */ public static function sanitizeSettings($input): array { $previous = self::settings(); $input = is_array($input) ? $input : []; $botId = sanitize_text_field((string) ($input['bot_id'] ?? '')); if ($botId !== '' && !preg_match('/^[A-Za-z0-9._-]{1,128}$/', $botId)) { add_settings_error( self::OPTION_NAME, 'askanydocs_invalid_bot_id', __('The Bot ID may contain only letters, numbers, dots, underscores, and hyphens.', 'askanydocs-chatbot') ); $botId = $previous['bot_id']; } $enabled = !empty($input['enabled']); if ($enabled && $botId === '') { add_settings_error( self::OPTION_NAME, 'askanydocs_missing_bot_id', __('Enter a Bot ID before enabling the chatbot.', 'askanydocs-chatbot') ); $enabled = false; } return [ 'enabled' => $enabled, 'bot_id' => $botId, 'share_user' => !empty($input['share_user']), ]; } public static function addSettingsPage(): void { add_options_page( __('AskAnyDocs Chatbot', 'askanydocs-chatbot'), __('AskAnyDocs Chatbot', 'askanydocs-chatbot'), 'manage_options', self::SETTINGS_PAGE, [self::class, 'renderSettingsPage'] ); } public static function registerPrivacyPolicyContent(): void { if (!function_exists('wp_add_privacy_policy_content')) { return; } $content = '
' . esc_html__( 'This site uses the AskAnyDocs chatbot, an external service provided by AskAnyDocs.', 'askanydocs-chatbot' ) . '
'; $content .= '' . esc_html__( 'When the chatbot is enabled, the visitor browser loads the widget from app.askanydocs.com. Chat messages and technical metadata needed to operate the widget may be sent to AskAnyDocs, including the current page URL, referrer, browser language, platform, time zone, screen size, and viewport size. The widget uses browser local storage to preserve conversation and display state.', 'askanydocs-chatbot' ) . '
'; $content .= '' . esc_html__( 'If the site administrator enables signed-in user sharing, the WordPress user ID, display name, and email address are also sent to AskAnyDocs.', 'askanydocs-chatbot' ) . '
'; $content .= '' . sprintf( wp_kses( /* translators: 1: AskAnyDocs privacy policy URL, 2: AskAnyDocs terms URL. */ __('See the AskAnyDocs Privacy Policy and Terms of Service.', 'askanydocs-chatbot'), ['a' => ['href' => []]] ), esc_url(self::PRIVACY_URL), esc_url(self::TERMS_URL) ) . '
'; wp_add_privacy_policy_content( __('AskAnyDocs Chatbot', 'askanydocs-chatbot'), wp_kses_post(wpautop($content)) ); } public static function renderSettingsPage(): void { if (!current_user_can('manage_options')) { return; } $settings = self::settings(); $siteHost = (string) wp_parse_url(home_url('/'), PHP_URL_HOST); $registerUrl = self::SERVICE_URL . '/register'; $botsUrl = self::SERVICE_URL . '/app/bots'; $logoUrl = plugin_dir_url(__FILE__) . 'assets/icon-64x64.png'; ?>
%s there. Otherwise the chatbot will not appear on this site.', 'askanydocs-chatbot'), ['code' => []]), esc_html($siteHost) ); ?>
(string) $user->ID, 'email' => (string) $user->user_email, 'name' => (string) $user->display_name, ]; wp_add_inline_script( self::SCRIPT_HANDLE, 'window.AskAnyDocsUser = ' . wp_json_encode( $userData, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . ';', 'before' ); } } /** * @param string $tag * @param string $handle * @param string $src */ public static function addWidgetScriptAttributes($tag, $handle, $src): string { if ($handle !== self::SCRIPT_HANDLE) { return $tag; } $settings = self::settings(); return wp_get_script_tag([ 'src' => $src, 'data-bot-id' => $settings['bot_id'], 'data-api-base' => self::SERVICE_URL, 'defer' => true, ]); } /** * @param array