settings = new AICBC_Settings(); $this->settings->init(); $admin = new AICBC_Admin( $this->settings ); $admin->init(); $rest = new AICBC_REST(); $rest->init(); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_widget' ) ); add_filter( 'script_loader_tag', array( $this, 'hosted_script_attrs' ), 10, 2 ); } /** * Enqueue the chat widget on the public site. * * @return void */ public function enqueue_widget() { if ( ! AICBC_Settings::get( 'enabled' ) || is_admin() ) { return; } if ( 'hosted' === AICBC_Settings::get( 'mode' ) ) { $key = trim( (string) AICBC_Settings::get( 'hosted_site_key' ) ); if ( '' === $key ) { return; } $base = untrailingslashit( (string) AICBC_Settings::get( 'aicb_base', 'https://caimunicate.com' ) ); wp_enqueue_script( 'aicb-chat-hosted', $base . '/widget.js', array(), AICBC_VERSION, true ); return; } wp_enqueue_script( 'aicb-chat-widget', AICBC_URL . 'assets/js/aicb-chat-widget.js', array(), AICBC_VERSION, true ); wp_localize_script( 'aicb-chat-widget', 'AICBC_CFG', array( 'rest' => esc_url_raw( rest_url( AICBC_REST::NS ) ), 'title' => AICBC_Settings::bot_name(), 'greeting' => AICBC_Settings::greeting(), ) ); } /** * Add the data-site-key attribute + async to the hosted Caimunicate widget. * * @param string $tag Script tag HTML. * @param string $handle Script handle. * @return string */ public function hosted_script_attrs( $tag, $handle ) { if ( 'aicb-chat-hosted' !== $handle ) { return $tag; } $key = trim( (string) AICBC_Settings::get( 'hosted_site_key' ) ); return str_replace( ' src=', ' async data-site-key="' . esc_attr( $key ) . '" src=', $tag ); } }