name, locale and flag. Empty without a multilingual // plugin. Kept for the request once the multilingual plugin has loaded them static $cache = null; if( $cache !== null ){ return $cache; } $languages = array(); $default = ''; $plugin = velischat_get_multilingual_plugin(); if( $plugin === 'wpml' ){ $list = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API $default = (string) apply_filters( 'wpml_default_language', null ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API foreach( is_array( $list ) ? $list : array() as $item ){ $code = $item[ 'code' ] ?? ( $item[ 'language_code' ] ?? '' ); if( $code === '' ){ continue; } $languages[ $code ] = array( 'code' => $code, 'name' => $item[ 'native_name' ] ?? $code, 'locale' => $item[ 'default_locale' ] ?? '', 'flag' => $item[ 'country_flag_url' ] ?? '', ); } } elseif( $plugin === 'polylang' ){ $default = (string) pll_default_language(); foreach( (array) pll_languages_list( array( 'fields' => '' ) ) as $item ){ if( ! is_object( $item ) || empty( $item->slug ) ){ continue; } $languages[ $item->slug ] = array( 'code' => $item->slug, 'name' => $item->name, 'locale' => $item->locale, 'flag' => $item->flag_url ?? '', ); } } if( isset( $languages[ $default ] ) ){ $languages = array( $default => $languages[ $default ] ) + $languages; } // Too early (before the multilingual plugin set up its languages): don't remember the empty list if( $languages || $plugin === '' || did_action( 'init' ) ){ $cache = $languages; } return $languages; } function velischat_is_multilingual(){ // Whether the site has more than one language return count( velischat_get_languages() ) > 1; } function velischat_get_default_language(){ // Code of the site's main language, '' on a site with one language $languages = velischat_get_languages(); return velischat_is_multilingual() ? (string) array_key_first( $languages ) : ''; } function velischat_get_current_language(){ // Language of the page the visitor is on: the one sent by the chatbot in its requests, otherwise the one of the // multilingual plugin. '' on a site with one language if( ! velischat_is_multilingual() ){ return ''; } $languages = velischat_get_languages(); $lang = $GLOBALS[ 'velischat_request_language' ] ?? ''; if( $lang === '' ){ $lang = velischat_get_multilingual_plugin() === 'polylang' ? (string) pll_current_language() : (string) apply_filters( 'wpml_current_language', null ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API } return isset( $languages[ $lang ] ) ? $lang : velischat_get_default_language(); } /** * * * Texts written by the site owner, in each language */ function velischat_get_translatable_texts(){ // The texts of the plugin's pages that visitors read, with the plugin's own default. "source" is the default as // written in the code (in Italian until version 0.7): a saved text equal to it, or to the default in any language // the settings page was saved in, was never changed, so the default is shown in the visitor's language $texts = array( 'compact_label' => array( 'source' => 'Parla con noi!', 'default' => __( 'Talk to us!', 'velischat' ) ), 'text_1' => array( 'source' => 'Ciao!', 'default' => __( 'Hi!', 'velischat' ) ), 'text_2' => array( 'source' => 'Scrivici quello che vuoi.', 'default' => __( 'Write us anything you like.', 'velischat' ) ), // An empty placeholder is the default one, not an empty field 'input_placeholder' => array( 'source' => 'Chiedici qualcosa!', 'default' => __( 'Ask us anything!', 'velischat' ), 'empty_is_default' => true ), ); for( $i = 1; $i <= 6; $i++ ){ $texts[ 'example_question_' . $i ] = array( 'source' => '', 'default' => '' ); } $texts[ 'choice_label' ] = array( 'source' => 'Seleziona un servizio', 'default' => __( 'Select a service', 'velischat' ) ); $texts[ 'choice_options' ] = array( 'source' => array(), 'default' => array() ); return $texts; } function velischat_get_main_text( $key ){ // A text as saved for the site's main language, null if it was never saved if( in_array( $key, array( 'choice_label', 'choice_options' ), true ) ){ $saved = get_option( 'velischat_contact_form', array() ); $field = $key === 'choice_label' ? 'label' : 'options'; return isset( $saved[ 'choice' ][ $field ] ) ? $saved[ 'choice' ][ $field ] : null; } return get_option( 'velischat_' . $key, null ); } function velischat_get_untranslated_text( $key ){ // What a language without its own version shows: the main language's text, or the plugin's default (in the // current language) while that text was never changed $texts = velischat_get_translatable_texts(); $text = $texts[ $key ]; $main = velischat_get_main_text( $key ); if( $main === null || $main === $text[ 'source' ] || $main === $text[ 'default' ] || ( ! empty( $text[ 'empty_is_default' ] ) && $main === '' ) ){ return $text[ 'default' ]; } return $main; } function velischat_get_text( $key, $lang = null ){ // A text for visitors in the given language (by default the one of the page): the main language's text, unless an // add-on has a version of it for that language $lang = $lang ?? velischat_get_current_language(); return apply_filters( 'velischat_text', velischat_get_untranslated_text( $key ), $key, $lang ); } /** * * * Language of the chatbot's requests */ function velischat_set_request_language(){ // The chatbot's requests go to admin-ajax.php, which doesn't know the language of the page: the script sends it, // and texts, links and products follow it. Without this, the privacy notice could be built in another language // than the page's, and asked again at every message if( ! wp_doing_ajax() || ! isset( $_POST[ 'velischat_lang' ] ) ){ // phpcs:ignore WordPress.Security.NonceVerification.Missing -- only picks one of the site's languages; the handler checks the nonce return; } $lang = sanitize_key( wp_unslash( $_POST[ 'velischat_lang' ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- only picks one of the site's languages; the handler checks the nonce $languages = velischat_get_languages(); if( ! isset( $languages[ $lang ] ) ){ return; } $GLOBALS[ 'velischat_request_language' ] = $lang; // The multilingual plugin's own language: posts, links and products in the visitor's language. Polylang chose // it already, through velischat_polylang_request_language() if( velischat_get_multilingual_plugin() === 'wpml' ){ do_action( 'wpml_switch_language', $lang ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API } velischat_use_locale( $languages[ $lang ][ 'locale' ] ); } add_action( 'admin_init', 'velischat_set_request_language', 1 ); function velischat_polylang_request_language( $language ){ // Polylang picks the language of a request once, early: the chatbot's requests get the one of their page if( ! wp_doing_ajax() || ! isset( $_POST[ 'velischat_lang' ] ) || ! function_exists( 'PLL' ) || ! isset( PLL()->model->languages ) ){ // phpcs:ignore WordPress.Security.NonceVerification.Missing -- only picks one of the site's languages; the handler checks the nonce return $language; } $requested = PLL()->model->languages->get( sanitize_key( wp_unslash( $_POST[ 'velischat_lang' ] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- only picks one of the site's languages; the handler checks the nonce return $requested ? $requested : $language; } add_filter( 'pll_get_current_language', 'velischat_polylang_request_language' ); function velischat_use_locale( $locale ){ // Translate the plugin's texts in the given locale for the rest of the request. WordPress can switch only to the // languages it has files for: otherwise the locale is forced, so at least the plugin's own translations load static $forced = null; if( $forced ){ remove_filter( 'locale', $forced, PHP_INT_MAX ); remove_filter( 'determine_locale', $forced, PHP_INT_MAX ); $forced = null; } if( $locale === '' ){ return; } $switched = determine_locale() !== $locale && switch_to_locale( $locale ); if( ! $switched && determine_locale() !== $locale ){ $forced = function() use ( $locale ){ return $locale; }; add_filter( 'locale', $forced, PHP_INT_MAX ); add_filter( 'determine_locale', $forced, PHP_INT_MAX ); } if( ! $switched ){ // Names of months and days (wp_date) and WordPress's own texts: switch_to_locale() reloads them, while // Polylang and WPML change the locale without doing it load_default_textdomain( $locale ); $GLOBALS[ 'wp_locale' ] = new WP_Locale(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- what switch_to_locale() does too, for the multilingual plugins that skip it } // WordPress loads the plugin's texts again, in the new locale, the next time one is needed. Add-ons that bring // their own translation files load them here unload_textdomain( 'velischat', true ); do_action( 'velischat_locale_changed', $locale ); } function velischat_switch_to_main_language(){ // Back to the site's main language for what the site owner reads, like the contact email if( empty( $GLOBALS[ 'velischat_request_language' ] ) ){ return; } $languages = velischat_get_languages(); $main = velischat_get_default_language(); if( ! isset( $languages[ $main ] ) ){ return; } // The multilingual plugins also pick the translation files of their current language if( velischat_get_multilingual_plugin() === 'wpml' ){ do_action( 'wpml_switch_language', $main ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API } elseif( function_exists( 'PLL' ) && isset( PLL()->model->languages ) ){ $language = PLL()->model->languages->get( $main ); if( $language ){ PLL()->curlang = $language; } } velischat_use_locale( $languages[ $main ][ 'locale' ] ); } function velischat_get_language_label( $locale, $name = '' ){ // A language for the AI, e.g. "Italiano (it_IT)": the name given by the multilingual plugin, otherwise the English // name when PHP knows it, and the locale, which the AI services understand in any case if( $name === '' && $locale !== '' && class_exists( 'Locale' ) ){ $name = (string) Locale::getDisplayLanguage( $locale, 'en' ); } if( $locale === '' ){ return $name; } return $name !== '' && $name !== $locale ? $name . ' (' . $locale . ')' : $locale; } function velischat_get_main_language_label(){ // The site's main language, the one the site owner reads (e.g. in the contact emails) $main = velischat_get_default_language(); if( $main !== '' ){ $language = velischat_get_languages()[ $main ]; return velischat_get_language_label( $language[ 'locale' ], $language[ 'name' ] ); } return velischat_get_language_label( get_locale() ); } function velischat_get_page_language_label(){ // The language of the page the visitor is on, for the AI ("Italiano (it_IT)"), and whether it is the page's // (multilingual site) or the site's $lang = velischat_get_current_language(); if( $lang !== '' ){ $language = velischat_get_languages()[ $lang ]; return array( velischat_get_language_label( $language[ 'locale' ], $language[ 'name' ] ), true ); } return array( velischat_get_language_label( get_locale() ), false ); } function velischat_get_language_instructions(){ // Which language the answers are in. The instructions are in English and the site owner's texts in any language: // the visitor's language counts, and when the message doesn't show it (e.g. an example question clicked) the // language of the page, or of the site list( $label, $is_page ) = velischat_get_page_language_label(); $where = $is_page ? sprintf( 'The visitor is browsing the version of the site in %s.', $label ) : sprintf( 'The site is in %s.', $label ); return 'Language of your answers: ' . $where . ' Always answer in the language of the user\'s latest message, even when it differs from the language of the page, of the site, of these instructions or of your earlier answers, and also for short questions, for questions about you and when you refuse a request (for example, a user who writes "¿Qué modelo eres?" gets the answer in Spanish). Only when the latest message has no clear language (for example a single word, a name or a number) answer in ' . $label . '. The only exception: if the site\'s instructions explicitly require a specific language for the answers, follow them. The site\'s instructions and content may be written in another language: they still apply, and you translate what you take from them into the language of your answer.'; } function velischat_get_language_closing_rule(){ // The language once more, at the end of the instructions: the site owner's instructions come first and are often // in the site's language, and a rule given once in the list wasn't enough for Gemini and Claude return "Most important rule, above every other one: the language of your answer. Write your whole answer in the language of the user's latest message, even when the site's instructions, the document and the earlier messages are in another language: translate what you take from them. Only exception: the site's instructions explicitly require one language for every answer."; } function velischat_get_language_reminder(){ // Sent after the visitor's latest message, in that request only (never saved in the conversation): with the site's // contents in its own language, Gemini and Claude still answered in it about one time in four. Marked as a note of // the system: in English and right after the message, it was otherwise taken for the visitor's language. Without // the exception of the site's instructions, which is in the rules: here it made them answer in the site's language list( $label, $is_page ) = velischat_get_page_language_label(); return sprintf( 'Note from the system, not written by the user: write your whole answer in the language of the user\'s message above, even if it differs from %1$s, the language of the %2$s. Only if that message has no clear language (for example a single word, a name or a number) answer in %1$s.', $label, $is_page ? 'page' : 'site' ); } /** * * * Contents in the visitor's language */ function velischat_in_language( $lang, $callback ){ // Run the callback with the queries of the multilingual plugin in the given language, e.g. to read the products // of one language also from the admin, where the current language can be "all" if( $lang === '' ){ $callback(); return; } $plugin = velischat_get_multilingual_plugin(); if( $plugin === 'wpml' ){ $previous = apply_filters( 'wpml_current_language', null ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API do_action( 'wpml_switch_language', $lang ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API $callback(); do_action( 'wpml_switch_language', $previous ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API return; } if( $plugin === 'polylang' ){ // Polylang reads the "lang" argument of posts and terms queries $add_lang = function( $args ) use ( $lang ){ $args[ 'lang' ] = $lang; return $args; }; add_filter( 'woocommerce_product_data_store_cpt_get_products_query', $add_lang ); add_filter( 'get_terms_args', $add_lang ); $callback(); remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', $add_lang ); remove_filter( 'get_terms_args', $add_lang ); return; } $callback(); } function velischat_posts_in_current_language( $ids, $type ){ // Posts of any language as the ones of the visitor's language (the original when not translated), each once $lang = velischat_get_current_language(); if( $lang === '' ){ return $ids; } $plugin = velischat_get_multilingual_plugin(); $result = array(); foreach( $ids as $id ){ if( $plugin === 'wpml' ){ $translated = apply_filters( 'wpml_object_id', (int) $id, $type, true, $lang ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API } else { $translated = function_exists( 'pll_get_post' ) ? pll_get_post( (int) $id, $lang ) : 0; } $translated = $translated ? (int) $translated : (int) $id; if( ! in_array( $translated, $result, true ) ){ $result[] = $translated; } } return $result; } /** * * * Chatbot contents: one version of each, in the main language when there is one */ function velischat_get_post_language( $post ){ // Language code of a post, '' when unknown or on a site with one language if( ! velischat_is_multilingual() ){ return ''; } $post = get_post( $post ); if( ! $post ){ return ''; } if( velischat_get_multilingual_plugin() === 'wpml' ){ return (string) apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $post->ID, 'element_type' => $post->post_type ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API } return function_exists( 'pll_get_post_language' ) ? (string) pll_get_post_language( $post->ID ) : ''; } function velischat_get_post_translations( $post ){ // The versions of a post in each language, the post itself included: language code => post ID if( ! velischat_is_multilingual() ){ return array(); } $post = get_post( $post ); if( ! $post ){ return array(); } if( velischat_get_multilingual_plugin() === 'wpml' ){ $type = apply_filters( 'wpml_element_type', $post->post_type ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API $trid = apply_filters( 'wpml_element_trid', null, $post->ID, $type ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API if( ! $trid ){ return array(); } $translations = array(); foreach( (array) apply_filters( 'wpml_get_element_translations', null, $trid, $type ) as $code => $item ){ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own API if( ! empty( $item->element_id ) ){ $translations[ $code ] = (int) $item->element_id; } } return $translations; } return function_exists( 'pll_get_post_translations' ) ? array_map( 'intval', pll_get_post_translations( $post->ID ) ) : array(); } function velischat_pick_post_version( $post_id, $available ){ // The one version of a post the chatbot reads, among the available posts: the main language's, otherwise the first // language of the site that has one. The AI translates by itself, and the links follow the visitor's language $translations = array_intersect( velischat_get_post_translations( $post_id ), $available ); if( ! $translations ){ return (int) $post_id; } foreach( array_keys( velischat_get_languages() ) as $lang ){ if( isset( $translations[ $lang ] ) ){ return (int) $translations[ $lang ]; } } return (int) $post_id; } function velischat_one_version_per_post( $posts ){ // Posts (objects or IDs) with only one version of each translated content, as velischat_pick_post_version() if( ! velischat_is_multilingual() ){ return $posts; } $ids = array_map( function( $post ){ return is_object( $post ) ? (int) $post->ID : (int) $post; }, $posts ); $result = array(); foreach( $posts as $i => $post ){ if( velischat_pick_post_version( $ids[ $i ], $ids ) === $ids[ $i ] ){ $result[] = $post; } } return $result; } function velischat_render_post_languages( $post ){ // Under a content's title in the list: the languages it is translated into, or the only language it is written in if( ! velischat_is_multilingual() ){ return; } $languages = velischat_get_languages(); $lang = velischat_get_post_language( $post ); $translations = array_intersect_key( array_diff_key( velischat_get_post_translations( $post ), array( $lang => true ) ), $languages ); $is_only = $lang !== '' && $lang !== velischat_get_default_language() && isset( $languages[ $lang ] ); if( ! $translations && ! $is_only ){ return; } $flag = function( $code ) use ( $languages ){ if( empty( $languages[ $code ][ 'flag' ] ) ){ return ''; } $src = $languages[ $code ][ 'flag' ]; return ''; }; $flag_html = array( 'img' => array( 'src' => true, 'alt' => true ) ); $flag_protocols = array_merge( wp_allowed_protocols(), array( 'data' ) ); ?> ">