get_contents( VELISCHAT_PATH . 'assets/images/velischat-menu-icon.svg' ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- the plugin's own menu icon, as a data URI 80 ); $hooks[] = add_submenu_page( 'velischat', __( 'Settings', 'velischat' ), __( 'Settings', 'velischat' ), 'manage_options', 'velischat', 'velischat_render_admin_page' ); $hooks[] = add_submenu_page( 'velischat', __( 'Instructions and content', 'velischat' ), __( 'Instructions and content', 'velischat' ), 'manage_options', 'velischat-contents', 'velischat_render_contents_page' ); $hooks[] = add_submenu_page( 'velischat', __( 'Design and texts', 'velischat' ), __( 'Design and texts', 'velischat' ), 'manage_options', 'velischat-design', 'velischat_render_design_page' ); $hooks[] = add_submenu_page( 'velischat', __( 'Contact form', 'velischat' ), __( 'Contact form', 'velischat' ), 'manage_options', 'velischat-contact-form', 'velischat_render_contact_form_page' ); $hooks[] = add_submenu_page( 'velischat', __( 'Setup wizard', 'velischat' ), __( 'Setup wizard', 'velischat' ), 'manage_options', 'velischat-wizard', 'velischat_render_wizard_page' ); $hooks[] = add_submenu_page( 'velischat', __( 'Support', 'velischat' ), __( 'Support', 'velischat' ), 'manage_options', 'velischat-support', 'velischat_render_support_page' ); // The upgrade banner at the bottom of every plugin page, after the page itself foreach( array_unique( array_filter( $hooks ) ) as $hook ){ add_action( $hook, 'velischat_render_upgrade_banner', 20 ); } } add_action( 'admin_menu', 'velischat_admin_menu' ); function velischat_admin_enqueue_scripts( $hook ){ // Only load assets on the plugin's admin pages if( strpos( $hook, 'velischat' ) === false ){ return; } wp_enqueue_style( 'velischat-admin', VELISCHAT_URL . 'assets/css/admin.css', array(), filemtime( VELISCHAT_PATH . 'assets/css/admin.css' ) ); wp_enqueue_script( 'velischat-admin', VELISCHAT_URL . 'assets/js/admin.js', array( 'jquery' ), filemtime( VELISCHAT_PATH . 'assets/js/admin.js' ), true ); // Fonts for the font choice and the color preview of the "Design e testi" page if( $hook === 'toplevel_page_velischat' || strpos( $hook, 'velischat-design' ) !== false ){ foreach( velischat_get_fonts() as $font_key => $config ){ wp_enqueue_style( 'velischat-font-' . $font_key, VELISCHAT_URL . $config[ 'css' ], array(), filemtime( VELISCHAT_PATH . $config[ 'css' ] ) ); } } } add_action( 'admin_enqueue_scripts', 'velischat_admin_enqueue_scripts' ); function velischat_sanitize_rate_limit_max( $value ){ // Between 1 and 1000 requests an hour, 30 when left empty $value = absint( $value ); return $value ? min( 1000, $value ) : 30; } function velischat_sanitize_max_chat_messages( $value ){ // Between 0 (no limit) and 1000 messages of one conversation, the default number when left empty return '' === trim( (string) $value ) ? '' : (string) min( 1000, absint( $value ) ); } function velischat_sanitize_checkbox( $value ){ // Store checkboxes as '1' or '' return $value ? '1' : ''; } function velischat_sanitize_display_mode( $value ){ // Only the two supported layouts are accepted return $value === 'compact' ? 'compact' : 'extended'; } function velischat_sanitize_compact_offset( $value ){ // Distance of the compact icon from the page edge, in px. Empty keeps the default distance if( $value === '' || $value === null ){ return ''; } return (string) min( absint( $value ), 1000 ); } function velischat_get_default_compact_icon(){ // Default launcher icon of the compact layout: the VelisChat logo in a blue circle return VELISCHAT_URL . 'assets/images/velischat-launcher.svg'; } function velischat_get_compact_icon(){ // Icon of the compact layout: the plugin's own, unless an add-on gives another one return (string) apply_filters( 'velischat_compact_icon', velischat_get_default_compact_icon() ); } function velischat_get_site_logo(){ // Logo at the top of the chat and of the conversation PDF: none, unless an add-on gives one return (string) apply_filters( 'velischat_site_logo', '' ); } function velischat_get_floating_image(){ // Icon next to the text field: the plugin's own, unless an add-on gives another one return (string) apply_filters( 'velischat_floating_image', velischat_get_default_floating_image() ); } function velischat_get_default_floating_image(){ // Default icon next to the text field: the VelisChat symbol return VELISCHAT_URL . 'assets/images/velischat-symbol.png'; } function velischat_format_dollars( $value ){ // Price in dollars written the Italian way: "0,50 $", "24 $" return number_format_i18n( $value, $value < 10 ? 2 : 0 ) . ' $'; } function velischat_render_ai_pricing_dialog( $providers ){ // Popup with the list prices of the AI services and how many conversations a million tokens are (admin.js opens it) $conversations = wp_list_pluck( wp_list_pluck( $providers, 'pricing' ), 'conversations' ); ?>


1

2

$config ){ ?> > 1 ){ ?>

Connectors and what to do if // not: only the chosen one is shown (admin.js switches them). Used in the settings and in the wizard $providers = velischat_get_ai_providers(); $provider = velischat_get_ai_provider(); $moved = isset( $_GET[ 'velischat_key' ] ) ? sanitize_key( wp_unslash( $_GET[ 'velischat_key' ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only says which message to show, after velischat_move_legacy_api_key() ?>
$config ){ ?>

$config ){ ?>
>

Connectors */ function velischat_get_legacy_api_key( $provider ){ // API key of the given service saved by VelisChat up to version 0.9, or an empty string return trim( (string) get_option( 'velischat_' . $provider . '_api_key', '' ) ); } function velischat_delete_legacy_api_key( $provider ){ // Forget a key saved by VelisChat up to version 0.9, with the mark of its verification delete_option( 'velischat_' . $provider . '_api_key' ); delete_option( 'velischat_' . $provider . '_api_key_verified' ); } function velischat_move_legacy_api_key(){ // The site owner clicked "Move the saved key to Connectors": the key goes to the connector of its service, if the // service accepts it, and VelisChat forgets it $provider = isset( $_GET[ 'provider' ] ) ? sanitize_key( wp_unslash( $_GET[ 'provider' ] ) ) : ''; check_admin_referer( 'velischat_move_api_key_' . $provider ); if( ! current_user_can( 'manage_options' ) ){ wp_die( esc_html__( 'You don\'t have permission to do this.', 'velischat' ) ); } $providers = velischat_get_ai_providers(); $back = wp_get_referer() ? wp_get_referer() : admin_url( 'admin.php?page=velischat' ); $result = 'failed'; if( isset( $providers[ $provider ] ) && velischat_get_ai_status( $provider ) === 'key' ){ $key = velischat_get_legacy_api_key( $provider ); $connector = wp_get_connector( $providers[ $provider ][ 'connector' ] ); $setting = $connector[ 'authentication' ][ 'setting_name' ] ?? ''; if( $key !== '' && $setting !== '' ){ // Verified as Settings > Connectors does when a key is entered there try { $registry = \WordPress\AiClient\AiClient::defaultRegistry(); $registry->setProviderRequestAuthentication( $providers[ $provider ][ 'connector' ], new \WordPress\AiClient\Providers\Http\DTO\ApiKeyRequestAuthentication( $key ) ); $valid = $registry->isProviderConfigured( $providers[ $provider ][ 'connector' ] ); } catch( Exception $e ){ $valid = false; } if( $valid ){ update_option( $setting, $key ); velischat_delete_legacy_api_key( $provider ); $result = 'moved'; } else { $result = 'invalid'; } } } wp_safe_redirect( add_query_arg( 'velischat_key', $result, remove_query_arg( 'velischat_key', $back ) ) . '#velischat-ai' ); exit; } add_action( 'admin_post_velischat_move_api_key', 'velischat_move_legacy_api_key' ); function velischat_cleanup_legacy_api_keys(){ // A key saved by VelisChat up to version 0.9 is no longer used once its service is connected in Settings > // Connectors: it's deleted, so no copy of it is left around foreach( array_keys( velischat_get_ai_providers() ) as $provider ){ if( get_option( 'velischat_' . $provider . '_api_key', '' ) !== '' && velischat_get_ai_status( $provider ) === 'ready' ){ velischat_delete_legacy_api_key( $provider ); } } } add_action( 'admin_init', 'velischat_cleanup_legacy_api_keys' ); function velischat_render_ai_connect_notice(){ // On every admin page, while the chosen AI service isn't connected and the chatbot was already set up (e.g. right // after the update to 1.0): visitors don't see it until then. Not on the plugin's settings and wizard, which // show what to do, nor in Settings > Connectors, where it's being done global $pagenow; if( ! current_user_can( 'manage_options' ) || velischat_ai_is_ready() || $pagenow === 'options-connectors.php' ){ return; } $page = isset( $_GET[ 'page' ] ) ? sanitize_key( wp_unslash( $_GET[ 'page' ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only reads which admin page is open if( in_array( $page, array( 'velischat', 'velischat-wizard' ), true ) ){ return; } if( velischat_get_legacy_api_key( velischat_get_ai_provider() ) === '' && ! velischat_has_instructions() ){ return; } ?>


>

array( 'name' => 'Fira Sans', 'family' => "'Fira Sans', sans-serif", 'css' => 'assets/fonts/fira-sans/fira-sans.css', ), 'open_sans' => array( 'name' => 'Open Sans', 'family' => "'Open Sans', sans-serif", 'css' => 'assets/fonts/open-sans/open-sans.css', ), 'lato' => array( 'name' => 'Lato', 'family' => "'Lato', sans-serif", 'css' => 'assets/fonts/lato/lato.css', ), 'montserrat' => array( 'name' => 'Montserrat', 'family' => "'Montserrat', sans-serif", 'css' => 'assets/fonts/montserrat/montserrat.css', ), 'nunito' => array( 'name' => 'Nunito', 'family' => "'Nunito', sans-serif", 'css' => 'assets/fonts/nunito/nunito.css', ), 'lora' => array( 'name' => 'Lora', 'family' => "'Lora', serif", 'css' => 'assets/fonts/lora/lora.css', ), ); } function velischat_get_font_config( $key ) { $fonts = velischat_get_fonts(); return $fonts[ $key ] ?? $fonts[ 'fira_sans' ]; } function velischat_enqueue_font() { // Load the selected font from the plugin files, without requests to Google Fonts if( ! velischat_can_use_chatbot() ){ return; } $font_key = get_option( 'velischat_font', 'fira_sans' ); $config = velischat_get_font_config( $font_key ); wp_enqueue_style( 'velischat-font', VELISCHAT_URL . $config[ 'css' ], array(), filemtime( VELISCHAT_PATH . $config[ 'css' ] ) ); } add_action( 'wp_enqueue_scripts', 'velischat_enqueue_font' ); // Colors of the chatbot: includes/colors.php