'aiblitz_chat_sanitize_settings',
'default' => [],
] );
add_settings_section(
'aiblitz_chat_main',
__( 'Configuration', 'aiblitz-chat' ),
'__return_false',
'aiblitz-chat'
);
add_settings_field(
'chatbot_id',
__( 'Chatbot ID', 'aiblitz-chat' ),
'aiblitz_chat_field_chatbot_id',
'aiblitz-chat',
'aiblitz_chat_main'
);
add_settings_field(
'show_on',
__( 'Show widget on', 'aiblitz-chat' ),
'aiblitz_chat_field_show_on',
'aiblitz-chat',
'aiblitz_chat_main'
);
add_settings_field(
'exclude_ids',
__( 'Exclude page / post IDs', 'aiblitz-chat' ),
'aiblitz_chat_field_exclude_ids',
'aiblitz-chat',
'aiblitz_chat_main'
);
}
function aiblitz_chat_sanitize_settings( $input ) {
$clean = [];
$clean['chatbot_id'] = isset( $input['chatbot_id'] ) ? sanitize_text_field( $input['chatbot_id'] ) : '';
$clean['show_on'] = isset( $input['show_on'] ) && in_array( $input['show_on'], [ 'all', 'home', 'singular' ], true )
? $input['show_on'] : 'all';
$clean['exclude_ids'] = isset( $input['exclude_ids'] ) ? sanitize_text_field( $input['exclude_ids'] ) : '';
return $clean;
}
function aiblitz_chat_get( $key, $default = '' ) {
$opts = get_option( AIBLITZ_CHAT_OPTION, [] );
return $opts[ $key ] ?? $default;
}
function aiblitz_chat_field_chatbot_id() {
$val = aiblitz_chat_get( 'chatbot_id' );
?>
AIBlitz dashboard → Chatbot → Embed.', 'aiblitz-chat' ) ),
'https://aiblitz.io/dashboard/chat'
);
?>
__( 'All pages', 'aiblitz-chat' ),
'home' => __( 'Front page only', 'aiblitz-chat' ),
'singular' => __( 'Posts & pages only (not archives / search)', 'aiblitz-chat' ),
];
foreach ( $options as $key => $label ) {
printf(
'',
esc_attr( AIBLITZ_CHAT_OPTION ),
esc_attr( $key ),
checked( $val, $key, false ),
esc_html( $label )
);
}
}
function aiblitz_chat_field_exclude_ids() {
$val = aiblitz_chat_get( 'exclude_ids' );
?>
Create a free chatbot on AIBlitz and paste the ID below.', 'aiblitz-chat' ) ),
'https://aiblitz.io/register'
);
?>
<script src=""
data-chatbot-id=""></script>
'async', 'in_footer' => true )
);
wp_script_add_data( 'aiblitz-chat-widget', 'data-chatbot-id', $chatbot_id );
}
add_filter( 'script_loader_tag', 'aiblitz_chat_add_data_attribute', 10, 3 );
function aiblitz_chat_add_data_attribute( $tag, $handle, $src ) {
if ( 'aiblitz-chat-widget' !== $handle ) {
return $tag;
}
$chatbot_id = aiblitz_chat_get( 'chatbot_id' );
return str_replace( ' src=', ' data-chatbot-id="' . esc_attr( $chatbot_id ) . '" src=', $tag );
}
function aiblitz_chat_should_show() {
// Check excluded IDs first
$excluded_raw = aiblitz_chat_get( 'exclude_ids' );
if ( $excluded_raw ) {
$excluded = array_map( 'intval', array_filter( array_map( 'trim', explode( ',', $excluded_raw ) ) ) );
if ( in_array( (int) get_the_ID(), $excluded, true ) ) {
return false;
}
}
$show_on = aiblitz_chat_get( 'show_on', 'all' );
if ( 'all' === $show_on ) {
return true;
}
if ( 'home' === $show_on ) {
return is_front_page();
}
if ( 'singular' === $show_on ) {
return is_singular();
}
return true;
}
// ---------------------------------------------------------------------------
// Shortcode [aiblitz_chat] or [aiblitz_chat id="custom-id"]
// ---------------------------------------------------------------------------
add_shortcode( 'aiblitz_chat', 'aiblitz_chat_shortcode' );
function aiblitz_chat_shortcode( $atts ) {
$atts = shortcode_atts( [ 'id' => aiblitz_chat_get( 'chatbot_id' ) ], $atts, 'aiblitz_chat' );
$chatbot_id = sanitize_text_field( $atts['id'] );
if ( ! $chatbot_id ) {
return '';
}
wp_enqueue_script(
'aiblitz-chat-widget-sc',
AIBLITZ_CHAT_WIDGET_URL,
array(),
AIBLITZ_CHAT_VERSION,
array( 'strategy' => 'async', 'in_footer' => true )
);
add_filter( 'script_loader_tag', function( $tag, $handle ) use ( $chatbot_id ) {
if ( 'aiblitz-chat-widget-sc' === $handle ) {
$tag = str_replace( ' src=', ' data-chatbot-id="' . esc_attr( $chatbot_id ) . '" src=', $tag );
}
return $tag;
}, 10, 2 );
return '';
}
// ---------------------------------------------------------------------------
// Activation / Deactivation
// ---------------------------------------------------------------------------
register_activation_hook( __FILE__, 'aiblitz_chat_activate' );
function aiblitz_chat_activate() {
if ( false === get_option( AIBLITZ_CHAT_OPTION ) ) {
add_option( AIBLITZ_CHAT_OPTION, [] );
}
}
register_deactivation_hook( __FILE__, 'aiblitz_chat_deactivate' );
function aiblitz_chat_deactivate() {
// Intentionally left blank — settings are preserved on deactivation.
}