'array', 'sanitize_callback' => [ $this, 'sanitize' ], 'default' => Plugin::default_settings(), ] ); } public function on_settings_updated( $old_value, $new_value ): void { $old_key = is_array( $old_value ) ? (string) ( $old_value['api_key'] ?? '' ) : ''; $new_key = is_array( $new_value ) ? (string) ( $new_value['api_key'] ?? '' ) : ''; if ( $old_key !== $new_key ) { Api_Client::clear_cache(); } } public function sanitize( $input ): array { if ( ! is_array( $input ) ) { $input = []; } $current = Plugin::get_settings(); $clean = []; $clean['api_base'] = isset( $input['api_base'] ) ? untrailingslashit( esc_url_raw( trim( (string) $input['api_base'] ) ) ) : AGENTSIA_AGENTS_DEFAULT_API_BASE; if ( $clean['api_base'] === '' ) { $clean['api_base'] = AGENTSIA_AGENTS_DEFAULT_API_BASE; } $submitted_key = isset( $input['api_key'] ) ? trim( (string) $input['api_key'] ) : ''; if ( $submitted_key === '' ) { $clean['api_key'] = $current['api_key']; } else { $clean['api_key'] = preg_match( '/^agsk_[a-f0-9]{48}$/', $submitted_key ) ? $submitted_key : $current['api_key']; if ( ! preg_match( '/^agsk_[a-f0-9]{48}$/', $submitted_key ) ) { add_settings_error( Plugin::OPTION_KEY, 'agentsia_invalid_api_key', __( 'Clé API invalide. Elle doit commencer par « agsk_ » et contenir 53 caractères.', 'agentsia-agents' ) ); } } $clean['chatbot_config_id'] = isset( $input['chatbot_config_id'] ) ? sanitize_text_field( (string) $input['chatbot_config_id'] ) : ''; $clean['chatbot_version'] = isset( $input['chatbot_version'] ) ? sanitize_text_field( (string) $input['chatbot_version'] ) : ''; $clean['chatbot_version_url'] = (string) ( $current['chatbot_version_url'] ?? '' ); $clean['widget_token'] = isset( $input['widget_token'] ) ? sanitize_text_field( (string) $input['widget_token'] ) : ''; $clean['enable_global'] = ! empty( $input['enable_global'] ) ? 1 : 0; $clean['exclude_paths'] = isset( $input['exclude_paths'] ) ? sanitize_textarea_field( (string) $input['exclude_paths'] ) : ''; $clean['mode'] = in_array( $input['mode'] ?? 'bubble', [ 'bubble', 'modal', 'fullpage' ], true ) ? $input['mode'] : 'bubble'; $clean['position'] = in_array( $input['position'] ?? 'right', [ 'right', 'left' ], true ) ? $input['position'] : 'right'; $clean['bubble_label'] = isset( $input['bubble_label'] ) ? sanitize_text_field( (string) $input['bubble_label'] ) : ''; $clean['header_text'] = isset( $input['header_text'] ) ? sanitize_text_field( (string) $input['header_text'] ) : ''; $clean['show_bubble'] = ! empty( $input['show_bubble'] ) ? 1 : 0; $default_scenario_raw = isset( $input['default_scenario'] ) ? sanitize_text_field( (string) $input['default_scenario'] ) : ''; // Keep accents/unicode; only normalise whitespace to dashes (mirrors the backend). $clean['default_scenario'] = (string) mb_substr( (string) preg_replace( '/\s+/u', '-', $default_scenario_raw ), 0, 80 ); foreach ( [ 'accent_color', 'header_bg_color', 'header_text_color', 'user_bubble_bg', 'user_bubble_text', 'assistant_bubble_bg', 'assistant_bubble_text' ] as $color_key ) { $clean[ $color_key ] = isset( $input[ $color_key ] ) ? $this->sanitize_hex_color( (string) $input[ $color_key ] ) : ''; } $clean['default_booking_slug'] = isset( $input['default_booking_slug'] ) ? Shortcodes::normalize_booking_ref( (string) $input['default_booking_slug'] ) : ''; $clean['quota_exceeded_message'] = isset( $input['quota_exceeded_message'] ) ? sanitize_textarea_field( (string) $input['quota_exceeded_message'] ) : ''; $clean['toggle_selector'] = isset( $input['toggle_selector'] ) ? (string) substr( sanitize_text_field( (string) $input['toggle_selector'] ), 0, 300 ) : ''; $clean['toggle_inactive'] = in_array( $input['toggle_inactive'] ?? 'disable', [ 'disable', 'hide', 'none' ], true ) ? (string) $input['toggle_inactive'] : 'disable'; $clean['webhook_enabled'] = ! empty( $input['webhook_enabled'] ) ? 1 : 0; $clean['webhook_secret'] = isset( $input['webhook_secret'] ) ? (string) preg_replace( '/[^A-Za-z0-9]/', '', (string) $input['webhook_secret'] ) : (string) ( $current['webhook_secret'] ?? '' ); // Génère un secret la première fois que le webhook est activé. if ( $clean['webhook_enabled'] && $clean['webhook_secret'] === '' ) { $clean['webhook_secret'] = bin2hex( random_bytes( 32 ) ); } $clean['allow_editor_creation'] = ! empty( $input['allow_editor_creation'] ) ? 1 : 0; if ( $clean['chatbot_config_id'] !== '' && $clean['api_key'] !== '' ) { $chatbots = Api_Client::list_chatbots( true ); foreach ( $chatbots as $bot ) { if ( isset( $bot['chatbotConfigId'] ) && $bot['chatbotConfigId'] === $clean['chatbot_config_id'] ) { $clean['widget_token'] = isset( $bot['widgetToken'] ) ? (string) $bot['widgetToken'] : $clean['widget_token']; $clean['chatbot_version_url'] = ''; if ( $clean['chatbot_version'] !== '' && isset( $bot['versions'] ) && is_array( $bot['versions'] ) ) { foreach ( $bot['versions'] as $v ) { if ( isset( $v['versionNumber'] ) && (string) $v['versionNumber'] === $clean['chatbot_version'] && ! empty( $v['publicUrl'] ) ) { $clean['chatbot_version_url'] = (string) $v['publicUrl']; break; } } } break; } } } return $clean; } /** « Auteurs du site » : what Agentsia may create on this site (administrators of the site only). */ private static function render_site_members_fields( array $settings ): void { $opt = Plugin::OPTION_KEY; ?>
|
|
render_card(); ?>
[agentsia_chatbot] — [agentsia_chatbot mode="bubble" position="right"][agentsia_chatbot scenario="sos" context_hint="Page urgence climatisation"][agentsia_chatbot hide_bubble="1"] — [agentsia_booking] — [agentsia_booking slug="nom-du-calendrier" height="780"][agentsia_booking slug="espace/nom-du-calendrier"] : [agentsia_demo label="Réserver ma démo"]
window.agentsia.open() / close() / toggle() — window.agentsia.sendMessage("…") — window.agentsia.setWelcomeMessage("Bonjour Jean !")window.agentsia.setQuickReplies(["Voir devis", "RDV"])window.agentsia.setTeaserMessage("…") / setTeaserDelay(3) / setAutoOpenDelay(0)window.agentsia.setScenario("sos") — window.agentsia.setPageContext("…") — window.agentsia.setContext({ customerId: "X" }) — window.agentsia.setAppearance({ accentColor: "#c1121f" })window.agentsia.apply({ ...batch }) —