load_dependencies(); $this->init_hooks(); } /** * Load required dependencies */ private function load_dependencies() { if ( is_admin() ) { require_once AIRGENTIC_PLUGIN_DIR . 'includes/admin-settings.php'; } } /** * Initialize WordPress hooks */ private function init_hooks() { // Front-end hooks add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_script' ) ); add_filter( 'script_loader_tag', array( $this, 'add_data_attributes_to_script' ), 10, 3 ); add_action( 'wp_footer', array( $this, 'output_widget_container' ) ); // Shortcode add_shortcode( 'airgentic_service_hub', array( $this, 'service_hub_shortcode' ) ); } /** * Get plugin options * * @return array */ private function get_options() { if ( empty( $this->options ) ) { $this->options = get_option( 'airgentic_options', array() ); } return $this->options; } /** * Enqueue the Airgentic script */ public function enqueue_script() { $options = $this->get_options(); // Check if plugin is enabled and configured if ( empty( $options['enable_on_front'] ) ) { return; } if ( empty( $options['account_id'] ) || empty( $options['service_id'] ) ) { return; } // Enqueue the Airgentic script wp_enqueue_script( self::SCRIPT_HANDLE, 'https://chat.airgentic.com/_js/airgentic-1.4.js', array(), AIRGENTIC_VERSION, array( 'in_footer' => false ) ); } /** * Add data attributes to the Airgentic script tag * * @param string $tag The script tag. * @param string $handle The script handle. * @param string $src The script source URL. * @return string Modified script tag with data attributes. */ public function add_data_attributes_to_script( $tag, $handle, $src ) { // Only modify our script if ( self::SCRIPT_HANDLE !== $handle ) { return $tag; } $options = $this->get_options(); // Build data attributes $data_attrs = 'data-account-id="' . esc_attr( $options['account_id'] ) . '" ' . 'data-service-id="' . esc_attr( $options['service_id'] ) . '"'; // Add optional search element IDs if ( ! empty( $options['search_input_id'] ) ) { $data_attrs .= ' data-search-input-id="' . esc_attr( $options['search_input_id'] ) . '"'; } if ( ! empty( $options['search_button_id'] ) ) { $data_attrs .= ' data-search-button-id="' . esc_attr( $options['search_button_id'] ) . '"'; } // Modify the WordPress-generated tag: // 1. Change ID from 'airgentic-widget-js' to 'airgentic-script' $tag = str_replace( 'id="airgentic-widget-js"', 'id="airgentic-script"', $tag ); $tag = str_replace( "id='airgentic-widget-js'", "id='airgentic-script'", $tag ); // 2. Add async attribute after opening script tag $tag = str_replace( '