settings = $settings; } /** * Whether the widget should be loaded. * * @return bool */ public static function should_load() { $load = mybot()->settings->is_enabled() && ! is_admin(); /** * Prevents the widget from loading on certain pages. * * @param bool $load The default result. */ return (bool) apply_filters( 'mybot_should_load', $load ); } /** * Data passed to JavaScript. * * @return array */ public static function get_config() { $plugin = mybot(); $languages = $plugin->languages; $default = $languages->default_code(); return array( 'chatUrl' => esc_url_raw( rest_url( MYBOT_REST_NAMESPACE . '/chat' ) ), 'feedbackUrl' => esc_url_raw( rest_url( MYBOT_REST_NAMESPACE . '/feedback' ) ), 'supportUrl' => esc_url_raw( rest_url( MYBOT_REST_NAMESPACE . '/support' ) ), /* * Where a visitor's own details are sent (1.17.0). * * A separate route from `/support` on purpose: that one stores a QUESTION for a human to answer, * this one stores a PERSON. The two have different rules — one needs a question and an email, the * other needs a name and one way to reach them, one is mirrored to nobody and the other is * exported through WordPress' privacy tools — and a route that has to guess which of the two a * request is would eventually guess wrong. */ 'contactUrl' => esc_url_raw( rest_url( MYBOT_REST_NAMESPACE . '/contact' ) ), /* * Used to mint a fresh nonce when a submission is refused for having a stale one. The * nonce below is written into the page, so a cached page hands the visitor a nonce from * whenever that copy was generated — and the widget can then recover on its own instead * of asking the visitor to reload a page they have every reason to think is fine. */ 'configUrl' => esc_url_raw( rest_url( MYBOT_REST_NAMESPACE . '/config' ) ), /* * The help page's search route (1.20.0). `[mybot_help]` needs an address to ask; it searches the * site's published content only, never the imported knowledge base. */ 'searchUrl' => esc_url_raw( rest_url( MYBOT_REST_NAMESPACE . '/search' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'botName' => $languages->string( $default, 'bot_name' ), 'welcome' => $languages->string( $default, 'welcome' ), 'placeholder' => $languages->string( $default, 'placeholder' ), 'color' => (string) $plugin->settings->get( 'primary_color' ), 'ragEnabled' => $plugin->settings->rag_available(), 'supportEnabled' => (bool) $plugin->settings->get( 'support_enabled', 0 ), /* * When the widget offers the visitor card (1.22.7). * * It used to be a constant 3 inside the script: three questions, then an offer carrying "Leave your details". * The owner asked for the offer to come WITHOUT waiting for the visitor to ask for a person — which means * sooner — so it is one message by default, and a site that finds that eager can raise the number or set it * to zero to switch the offer off. * * A FILTER rather than a setting, deliberately: this is a matter of taste, and an option would mean a new * string in twelve languages plus another row on a screen that already carries 57. * * @param int $after Number of visitor messages before the offer. 0 or less disables it. */ 'contactInviteAfter' => (int) apply_filters( 'mybot_contact_invite_after', 1 ), /* * Seconds before the invitation bubble appears; 0 means there is no bubble. The script * needs the number, and needs it to be a number: a string here would compare as truthy * and turn a switched-off bubble into one that appears immediately. */ 'teaserDelay' => (int) $plugin->settings->get( 'teaser_delay', 0 ), // Languages: every string is sent along, so switching language needs no server call. 'languages' => $languages->enabled(), 'languageOptions' => $languages->switch_options(), 'defaultLanguage' => $default, 'showSwitch' => $languages->show_switch(), 'stringsByLang' => $languages->widget_strings(), /* * What the browser's speech recognition should listen for, per language (1.13.0). * * The widget cannot guess this: the API wants `vi-VN` while the plugin's codes are `vi`, and * given a two-letter code it listens for nothing useful and returns nothing, with no error. * The tags live in MYBOT_Languages with the rest of the language data, and the whole map is * sent here so switching language needs no request. */ 'speechTags' => MYBOT_Languages::speech_tags(), /* * Whether the widget may offer the picture button (1.14.0), and the ceilings it must respect. * * The first value is the same question the chat route asks before it accepts a picture, asked * through the one safe capability path (`MYBOT_LLM_Factory::can()`, docs/08 muc 3.65): a model * that cannot read an image gets no image, so offering a button that ends in a refusal would * be the "visible button that does nothing" defect this project keeps recording (6.42). When * the question cannot be asked, the answer is false and the button stays hidden. * * The two numbers are sent rather than written into the script so the browser's ceiling and * the server's cannot drift apart — the same rule as the composer's height (6.31). */ 'canAttachImage' => MYBOT_LLM_Factory::can( MYBOT_LLM_Factory::chat_provider( $plugin->settings ), 'vision', (string) $plugin->settings->get( 'llm_model' ) ), 'attachMaxBytes' => MYBOT_Attachments::MAX_BYTES, 'attachMaxSide' => MYBOT_Attachments::MAX_SIDE, 'strings' => array( 'send' => __( 'Send', 'aihouse' ), 'open' => __( 'Open chat', 'aihouse' ), 'close' => __( 'Close chat', 'aihouse' ), 'thinking' => __( 'Finding an answer...', 'aihouse' ), 'searching' => __( 'Searching the knowledge base...', 'aihouse' ), 'error' => __( 'Something went wrong. Please try again.', 'aihouse' ), 'sources' => __( 'Sources', 'aihouse' ), 'helpful' => __( 'Helpful', 'aihouse' ), 'notHelpful' => __( 'Not helpful', 'aihouse' ), 'thanks' => __( 'Thanks for your feedback!', 'aihouse' ), 'language' => __( 'Language', 'aihouse' ), /* * The picture button (1.14.0). English defaults only: the twelve real translations live in * MYBOT_Languages::UI_STRINGS, which is where every visitor-facing word in this widget is * kept — a .po catalogue is loaded for the ADMIN's language, and these are read by the * visitor in the language they chose. */ 'attach' => __( 'Send a picture', 'aihouse' ), 'attachRemove' => __( 'Remove the picture', 'aihouse' ), 'attachFailed' => __( 'That picture could not be prepared. Please use a JPEG, PNG or WebP picture.', 'aihouse' ), /* * A picture with no question is not sent, and says why. * * Retrieval searches by TEXT (docs/16), so a picture on its own has nothing to look up and * would produce a confident answer about a photo nobody described. Refusing silently was * the other option, and it is the worse one: a visitor who attaches a photo, presses Send * and sees nothing happen has no way to find out that the question is what is missing. */ 'attachNeeds' => __( 'Please type your question as well — the picture is sent together with it.', 'aihouse' ), /* * English defaults for the hand-over form. Per-language versions live in * MYBOT_Languages::support_strings() and win when present; these keep the form * readable instead of showing empty labels on a language nobody has translated yet. */ 'supportTitle' => __( 'This question needs a person', 'aihouse' ), 'supportIntro' => __( 'I am sorry, I do not have the information to answer this one. Leave your email and someone from our team will reply to you directly.', 'aihouse' ), 'supportName' => __( 'Your name', 'aihouse' ), 'supportEmail' => __( 'Email (required)', 'aihouse' ), 'supportPhone' => __( 'Phone (optional)', 'aihouse' ), 'supportNote' => __( 'Anything you would like to add? (optional)', 'aihouse' ), 'supportSend' => __( 'Send my question to the team', 'aihouse' ), 'supportSending' => __( 'Sending...', 'aihouse' ), 'supportDone' => __( 'Thank you. Your question has been sent to our team, and we will reply by email.', 'aihouse' ), 'supportMissing' => __( 'Please enter an email address so we can reply to you.', 'aihouse' ), 'supportError' => __( 'The question could not be sent. Please try again in a few minutes.', 'aihouse' ), 'supportLater' => __( 'Not now', 'aihouse' ), ), ); } /** * Builds the chat widget markup. * * @param array $args Display arguments. * @return string */ public static function get_widget_markup( $args = array() ) { $plugin = mybot(); $languages = $plugin->languages; $default = $languages->default_code(); $args = wp_parse_args( $args, array( 'title' => '', 'height' => 480, 'theme' => 'light', 'position' => 'inline', 'mode' => 'auto', 'lang' => $default, 'titleLock' => false, ) ); /** Filters the widget arguments. */ $args = apply_filters( 'mybot_widget_args', $args ); /* * The language this markup is PAINTED in, and it is the same value `data-mybot-lang` declares * below. * * Until 1.9.1 everything here was painted from `$default` while the attribute said * `resolve( $args['lang'] )`, so `[mybot lang="ja"]` produced a widget that announced Japanese * and was painted in English. A sighted visitor never saw it, because the script repaints the * whole widget from the visitor's own choice — which is also why nobody noticed that the * attribute had stopped doing anything (docs/08 muc 6.40). */ $code = $languages->resolve( $args['lang'] ); $ui = MYBOT_Languages::ui_strings( $code ); /* * An empty title means "the bot's name, in this widget's language". A title the author typed * is kept, and `titleLock` travels to the script so the language repaint cannot overwrite it * (docs/08 muc 6.41). */ $title = '' !== trim( (string) $args['title'] ) ? (string) $args['title'] : $languages->string( $code, 'bot_name' ); ++self::$instance_count; $widget_id = 'mybot-widget-' . self::$instance_count; $classes = array( 'mybot-widget', 'mybot-widget--' . ( 'dark' === $args['theme'] ? 'dark' : 'light' ), 'mybot-widget--' . ( 'inline' === $args['position'] ? 'inline' : 'floating' ), ); $show_switch = $languages->show_switch(); ob_start(); ?>
' . esc_html( $atts['subtitle'] ) . '
'; } $html .= ''; $html .= ''; $html .= '