'', 'enabled' => 1, 'lang' => 'dashboard', 'brand' => 0, ); } /** * Language choices for the chat window. "dashboard" means: use the setting from the AsistentiAI dashboard. * * @return array Map of value => label. */ function asistentiai_chat_languages() { return array( 'dashboard' => __( 'Use the setting from my AsistentiAI dashboard', 'asistentiai-chat' ), 'sq' => __( 'Albanian', 'asistentiai-chat' ), 'en' => __( 'English', 'asistentiai-chat' ), 'auto' => __( "Follow the visitor's browser (English for English browsers, Albanian otherwise)", 'asistentiai-chat' ), ); } /** * Saved settings merged with the defaults. * * @return array */ function asistentiai_chat_get_settings() { $saved = get_option( 'asistentiai_chat_settings', array() ); if ( ! is_array( $saved ) ) { $saved = array(); } return wp_parse_args( $saved, asistentiai_chat_defaults() ); } /** * Cleans the business code: lowercase letters, digits and hyphens only, at most 64 characters. * If the whole embed code from the dashboard was pasted, only the value of data-tenant is kept. * * @param mixed $value Raw value. * @return string */ function asistentiai_chat_sanitize_code( $value ) { $value = is_string( $value ) ? $value : ''; if ( preg_match( '/data-tenant\s*=\s*["\']([^"\']*)["\']/i', $value, $matches ) ) { $value = $matches[1]; } $value = (string) preg_replace( '/[^a-z0-9-]/', '', strtolower( trim( $value ) ) ); return substr( $value, 0, 64 ); } /** * Sanitize callback for the settings option. * * @param mixed $input Submitted values. * @return array */ function asistentiai_chat_sanitize_settings( $input ) { if ( ! is_array( $input ) ) { return asistentiai_chat_defaults(); } $lang = isset( $input['lang'] ) ? sanitize_key( $input['lang'] ) : 'dashboard'; if ( ! array_key_exists( $lang, asistentiai_chat_languages() ) ) { $lang = 'dashboard'; } return array( 'code' => asistentiai_chat_sanitize_code( isset( $input['code'] ) ? $input['code'] : '' ), 'enabled' => empty( $input['enabled'] ) ? 0 : 1, 'lang' => $lang, 'brand' => empty( $input['brand'] ) ? 0 : 1, ); } /** * URL of the plugin's settings page. * * @return string */ function asistentiai_chat_settings_url() { return admin_url( 'options-general.php?page=asistentiai-chat' ); } /** * Registers the setting, the section and the fields (Settings API). */ function asistentiai_chat_register_settings() { register_setting( 'asistentiai_chat', 'asistentiai_chat_settings', array( 'type' => 'array', 'sanitize_callback' => 'asistentiai_chat_sanitize_settings', 'default' => asistentiai_chat_defaults(), ) ); add_settings_section( 'asistentiai_chat_main', esc_html__( 'Chat on your site', 'asistentiai-chat' ), 'asistentiai_chat_render_section', 'asistentiai-chat' ); add_settings_field( 'asistentiai_chat_code', esc_html__( 'Business code', 'asistentiai-chat' ), 'asistentiai_chat_render_code_field', 'asistentiai-chat', 'asistentiai_chat_main', array( 'label_for' => 'asistentiai_chat_code' ) ); add_settings_field( 'asistentiai_chat_enabled', esc_html__( 'Show the chat', 'asistentiai-chat' ), 'asistentiai_chat_render_enabled_field', 'asistentiai-chat', 'asistentiai_chat_main', array( 'label_for' => 'asistentiai_chat_enabled' ) ); add_settings_field( 'asistentiai_chat_lang', esc_html__( 'Chat window language', 'asistentiai-chat' ), 'asistentiai_chat_render_lang_field', 'asistentiai-chat', 'asistentiai_chat_main', array( 'label_for' => 'asistentiai_chat_lang' ) ); add_settings_field( 'asistentiai_chat_brand', esc_html__( '"Powered by" link', 'asistentiai-chat' ), 'asistentiai_chat_render_brand_field', 'asistentiai-chat', 'asistentiai_chat_main', array( 'label_for' => 'asistentiai_chat_brand' ) ); } add_action( 'admin_init', 'asistentiai_chat_register_settings' ); /** * Adds Settings → AsistentiAI Chat. */ function asistentiai_chat_add_settings_page() { add_options_page( esc_html__( 'AsistentiAI Chat', 'asistentiai-chat' ), esc_html__( 'AsistentiAI Chat', 'asistentiai-chat' ), 'manage_options', 'asistentiai-chat', 'asistentiai_chat_render_settings_page' ); } add_action( 'admin_menu', 'asistentiai_chat_add_settings_page' ); /** * Help text above the fields: how to get an account and where the business code is. */ function asistentiai_chat_render_section() { echo '
'; printf( /* translators: %s: link to the page where a free trial can be requested. */ esc_html__( 'The chat works with an AsistentiAI account. No account yet? %s: 50 agent replies, no credit card, and our team opens your account within the day.', 'asistentiai-chat' ), '' . esc_html__( 'Request a free trial', 'asistentiai-chat' ) . '' ); echo '
'; esc_html_e( 'Your business code is in the AsistentiAI dashboard, under Channels → Website widget. It is the value of data-tenant in the embed code shown there. You can also paste the whole embed code below; only the code is kept.', 'asistentiai-chat' ); echo '
'; } /** * Business code field. */ function asistentiai_chat_render_code_field() { $settings = asistentiai_chat_get_settings(); printf( '', esc_attr( $settings['code'] ) ); echo '' . esc_html__( 'Lowercase letters, numbers and hyphens. Use the code from your own dashboard, not one copied from another site.', 'asistentiai-chat' ) . '
'; } /** * On/off field. */ function asistentiai_chat_render_enabled_field() { $settings = asistentiai_chat_get_settings(); printf( '', checked( 1, (int) $settings['enabled'], false ), esc_html__( 'Show the chat on my site', 'asistentiai-chat' ) ); echo '' . esc_html__( 'Untick to hide the chat without losing these settings. To pause only the agent, use your AsistentiAI dashboard.', 'asistentiai-chat' ) . '
'; } /** * Language field. */ function asistentiai_chat_render_lang_field() { $settings = asistentiai_chat_get_settings(); echo ''; echo '' . esc_html__( 'Language of the buttons and labels in the chat window. The agent itself always replies in the language the visitor writes in.', 'asistentiai-chat' ) . '
'; } /** * "Powered by AsistentiAI" field. Off by default. */ function asistentiai_chat_render_brand_field() { $settings = asistentiai_chat_get_settings(); printf( '', checked( 1, (int) $settings['brand'], false ), esc_html__( 'Show "Powered by AsistentiAI" at the bottom of the chat window', 'asistentiai-chat' ) ); echo '' . esc_html__( 'Off by default. When on, the line links to asistentiai.com.', 'asistentiai-chat' ) . '
'; } /** * The settings page. */ function asistentiai_chat_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = asistentiai_chat_get_settings(); ?>%s %s