'', 'service_id' => '', 'search_input_id' => '', 'search_button_id' => '', 'enable_on_front' => 1, 'search_mode' => '', 'search_page_id' => 0, 'rewrite_search_forms' => 1, 'redirect_s' => 1, ); } /** * Load and normalise plugin options. * * Existing 1.1.0 installs that already filled a search input ID keep overlay * mode so their search popup does not change on upgrade. * * @return array */ public static function get_plugin_options() { $options = wp_parse_args( get_option( self::OPTION_NAME, array() ), self::default_options() ); $allowed_modes = array( 'none', 'inline', 'overlay' ); if ( ! in_array( $options['search_mode'], $allowed_modes, true ) ) { $options['search_mode'] = ! empty( $options['search_input_id'] ) ? 'overlay' : 'none'; } $options['search_page_id'] = absint( $options['search_page_id'] ); $options['rewrite_search_forms'] = ! empty( $options['rewrite_search_forms'] ) ? 1 : 0; $options['redirect_s'] = ! empty( $options['redirect_s'] ) ? 1 : 0; $options['enable_on_front'] = ! empty( $options['enable_on_front'] ) ? 1 : 0; return $options; } /** * Permalink of the configured Site Search page, or empty string. * * @param array|null $options Optional options array. * @return string */ public static function get_search_page_url( $options = null ) { if ( null === $options ) { $options = self::get_plugin_options(); } $page_id = absint( $options['search_page_id'] ); if ( $page_id < 1 ) { return ''; } $permalink = get_permalink( $page_id ); if ( ! $permalink || 'publish' !== get_post_status( $page_id ) ) { return ''; } return $permalink; } /** * Constructor */ private function __construct() { $this->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() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_script' ) ); add_filter( 'script_loader_tag', array( $this, 'add_data_attributes_to_script' ), 10, 3 ); add_shortcode( 'airgentic_service_hub', array( $this, 'service_hub_shortcode' ) ); add_shortcode( 'airgentic_search', array( $this, 'search_shortcode' ) ); add_filter( 'get_search_form', array( $this, 'rewrite_search_form' ), 20 ); add_filter( 'render_block', array( $this, 'rewrite_search_block' ), 10, 2 ); add_action( 'template_redirect', array( $this, 'redirect_wordpress_search' ) ); } /** * Get plugin options * * @return array */ private function get_options() { if ( empty( $this->options ) ) { $this->options = self::get_plugin_options(); } return $this->options; } /** * Whether the front-end script should load. * * @param array $options Plugin options. * @return bool */ private function is_front_end_enabled( $options ) { return ! empty( $options['enable_on_front'] ) && ! empty( $options['account_id'] ) && ! empty( $options['service_id'] ); } /** * Whether native WordPress search forms should be rewritten for inline search. * * @param array $options Plugin options. * @return bool */ private function should_rewrite_search_forms( $options ) { return $this->is_front_end_enabled( $options ) && 'inline' === $options['search_mode'] && ! empty( $options['rewrite_search_forms'] ) && '' !== self::get_search_page_url( $options ); } /** * Enqueue the Airgentic script */ public function enqueue_script() { $options = $this->get_options(); if ( ! $this->is_front_end_enabled( $options ) ) { return; } 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 ) { unset( $src ); if ( self::SCRIPT_HANDLE !== $handle ) { return $tag; } $options = $this->get_options(); $data_attrs = 'data-account-id="' . esc_attr( $options['account_id'] ) . '" ' . 'data-service-id="' . esc_attr( $options['service_id'] ) . '"'; $mode = $options['search_mode']; if ( 'overlay' === $mode ) { 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'] ) . '"'; } } elseif ( 'inline' === $mode ) { if ( ! empty( $options['rewrite_search_forms'] ) ) { $data_attrs .= ' data-search-autocomplete="true"'; } if ( ! empty( $options['search_input_id'] ) ) { $data_attrs .= ' data-autocomplete-input-id="' . esc_attr( $options['search_input_id'] ) . '"'; } if ( ! empty( $options['search_button_id'] ) ) { $data_attrs .= ' data-autocomplete-button-id="' . esc_attr( $options['search_button_id'] ) . '"'; } } $tag = str_replace( 'id="airgentic-widget-js"', 'id="airgentic-script"', $tag ); $tag = str_replace( "id='airgentic-widget-js'", "id='airgentic-script'", $tag ); $tag = str_replace( '