data-* attributes, consumed by the script_loader_tag filter. */ private array $script_attrs = []; /** Incrementing counter used to build unique script handles. */ private int $counter = 0; public static function instance(): self { if ( self::$instance === null ) { self::$instance = new self(); } return self::$instance; } public function register(): void { // Enqueue the global floating widget in the footer (priority 10 = before the // footer script printer at 20). Running in the footer lets us skip the global // instance when a shortcode/block already rendered one earlier in the content. add_action( 'wp_footer', [ $this, 'maybe_enqueue_global' ], 10 ); add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_trigger' ] ); add_filter( 'script_loader_tag', [ $this, 'filter_script_tag' ], 10, 3 ); } public function maybe_enqueue_global(): void { if ( is_admin() || $this->rendered ) { return; } $settings = Plugin::get_settings(); if ( empty( $settings['enable_global'] ) || empty( $settings['widget_token'] ) ) { return; } if ( $this->is_current_url_excluded( $settings['exclude_paths'] ) ) { return; } // Per-page overrides set via the "Agentsia — Chatbot" meta box on the // current post/page take precedence over the global defaults. $overrides = [ 'mode' => $settings['mode'] ?? 'bubble', 'position' => $settings['position'] ?? 'right', ]; $post_overrides = Post_Meta::instance()->resolve_overrides_for_current_request(); if ( ! empty( $post_overrides ) ) { $overrides = array_merge( $overrides, $post_overrides ); } $this->enqueue_widget( $overrides ); } /** * Enqueue the host-page trigger script when a custom open/close selector is * configured. It binds the selector to window.agentsia.toggle() and greys * out / hides the element(s) while the assistant is not active. */ public function maybe_enqueue_trigger(): void { if ( is_admin() ) { return; } $settings = Plugin::get_settings(); $selector = trim( (string) ( $settings['toggle_selector'] ?? '' ) ); if ( $selector === '' ) { return; } $handle = 'agentsia-chatbot-trigger'; wp_register_script( $handle, AGENTSIA_AGENTS_URL . 'assets/js/chatbot-trigger.js', [], AGENTSIA_AGENTS_VERSION, true ); wp_localize_script( $handle, 'AgentsiaTrigger', [ 'selector' => $selector, 'inactive' => $settings['toggle_inactive'] ?? 'disable', 'readyTimeoutMs' => 8000, ] ); wp_enqueue_script( $handle ); } /** * Register and enqueue the embed.js widget loader for one instance. The * per-instance configuration is attached to the resulting