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(); ?>
switch_options() as $option ) : ?>
` with a label in the widget's language: a × that only * a mouse can reach is the kind of small exclusion this project writes down rather than ships. */ ?>
` * cannot be labelled, styled or translated, and on a phone it is a second, differently * worded "choose file" control sitting next to a Send button. */ ?> ` would arrive as a blank first line in the * field — and a textarea with content does not show its placeholder either. */ ?>
settings->get( 'position', 'bottom-right' ); if ( 'inline' === $position ) { return; } /* * The launcher sits OUTSIDE the widget root, so the widget's own language repaint cannot reach * it by walking up from the root — and it used to be printed with `__()`, which means it * followed the admin locale while the chat beside it followed the visitor. It is painted here * in the widget's default language and corrected by the script on load, from the language the * visitor actually has (docs/08 muc 6.39). */ $launcher_label = MYBOT_Languages::ui_strings( mybot()->languages->default_code() ); /* * The button's own words, and the bubble that invites a click (1.9.4). * * Both are the OWNER'S content, per language, and both default to empty: an empty label means * the button is drawn exactly as it was before this existed, and an empty text — or a delay of * zero — means no bubble is printed at all, so nothing appears that nobody asked for. * * The style ends up as a class name, and the class is only added when there is a label to * place: a class that says "put the text under the icon" on a button with no text is a rule * with nothing to do (docs/08 muc 6.47). */ $languages = mybot()->languages; $code = $languages->default_code(); $label = (string) $languages->string( $code, 'launcher_label' ); $teaser = (string) $languages->string( $code, 'teaser_text' ); $delay = (int) $this->settings->get( 'teaser_delay', 0 ); $style = ( 'beside' === (string) $this->settings->get( 'launcher_label_style', 'below' ) ) ? 'beside' : 'below'; $classes = 'mybot-launcher mybot-launcher--' . $position; if ( '' !== trim( $label ) ) { $classes .= ' mybot-launcher--label-' . $style; } /** * Filters whether the floating button draws attention to itself (1.22.5). * * The owner asked for a button that vibrates, so it is on by default. It is a FILTER rather * than a setting on purpose: a moving element on every page is a taste decision, not a * switch a site owner should have to hunt for, and a filter lets a site that disagrees turn * it off in one line without another option in the database. * * @param bool $vibrate Whether to add the animation class. Default true. */ $vibrate = (bool) apply_filters( 'mybot_launcher_vibrate', true ); ?>
0 ) : ?>
__( 'How can we help?', 'aihouse' ), 'subtitle' => '', 'placeholder' => __( 'Search the help pages', 'aihouse' ), 'button' => __( 'Search', 'aihouse' ), 'label' => __( 'Search the help pages', 'aihouse' ), 'ask' => __( 'Ask the assistant instead', 'aihouse' ), 'topics' => '', ), $atts, 'mybot_help' ); wp_enqueue_style( 'mybot-help', MYBOT_PLUGIN_URL . 'public/css/mybot-help.css', array( 'mybot-public' ), MYBOT_VERSION, 'all' ); /* * Depends on `mybot-public`: the nonce and the search URL arrive in `window.mybotConfig`, and a search * box that posts without a nonce fails on every cached page. */ wp_enqueue_script( 'mybot-help', MYBOT_PLUGIN_URL . 'public/js/mybot-help.js', array( 'mybot-public' ), MYBOT_VERSION, true ); $id = 'mybot-help-q-' . wp_unique_id(); $topics = ''; foreach ( explode( ',', (string) $atts['topics'] ) as $topic ) { $topic = trim( $topic ); if ( '' === $topic ) { continue; } $parts = explode( '|', $topic, 2 ); $name = trim( $parts[0] ); $url = isset( $parts[1] ) ? esc_url( trim( $parts[1] ) ) : ''; // `esc_url()` returns '' for a scheme it refuses, so a `javascript:` topic link cannot be built. if ( '' === $name || '' === $url ) { continue; } $topics .= '
  • ' . esc_html( $name ) . '
  • '; } $html = '
    '; $html .= '
    '; $html .= '

    ' . esc_html( $atts['title'] ) . '

    '; if ( '' !== trim( (string) $atts['subtitle'] ) ) { $html .= '

    ' . esc_html( $atts['subtitle'] ) . '

    '; } $html .= ''; $html .= '

    '; $html .= ''; if ( '' !== $topics ) { $html .= ''; } $html .= '

    '; $html .= '
    '; return $html; } /** * Renders the [mybot] shortcode. * * @param array $atts Shortcode attributes. * @return string */ public function render_shortcode( $atts ) { $default = mybot()->languages->default_code(); $raw = is_array( $atts ) ? $atts : array(); $atts = shortcode_atts( array( 'title' => '', 'height' => 480, 'theme' => 'light', 'mode' => 'auto', 'lang' => $default, ), $atts, 'mybot' ); $atts['height'] = absint( $atts['height'] ); $atts['theme'] = in_array( $atts['theme'], array( 'light', 'dark' ), true ) ? $atts['theme'] : 'light'; $atts['mode'] = in_array( $atts['mode'], array( 'auto', 'quick', 'deep' ), true ) ? $atts['mode'] : 'auto'; $atts['lang'] = mybot()->languages->resolve( $atts['lang'] ); /* * `[mybot title="Hỗ trợ"]` used to be painted and then replaced a moment later, when the * script repainted the header in the chat language — a documented attribute with no effect * (docs/08 muc 6.41). This flag is what tells the script to leave the author's title alone. */ $atts['titleLock'] = isset( $raw['title'] ) && '' !== trim( (string) $raw['title'] ); return self::get_widget_markup( $atts ); } /** * Enqueues the front-end CSS and JS. * * @return void */ public function enqueue_assets() { if ( ! self::should_load() ) { return; } wp_enqueue_style( 'mybot-public', MYBOT_PLUGIN_URL . 'public/css/mybot-public.css', array(), MYBOT_VERSION, 'all' ); wp_enqueue_script( 'mybot-public', MYBOT_PLUGIN_URL . 'public/js/mybot-public.js', array(), MYBOT_VERSION, true ); wp_localize_script( 'mybot-public', 'mybotConfig', self::get_config() ); } }