'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => ''
));
register_setting('ailiveagents_settings', 'ailiveagents_exclude_pages', array(
'type' => 'string',
'sanitize_callback' => 'sanitize_textarea_field',
'default' => ''
));
}
public function render_admin_page() {
if (!current_user_can('manage_options')) {
return;
}
// Check nonce for security
if (isset($_POST['submit']) && check_admin_referer('ailiveagents_settings_save', 'ailiveagents_settings_nonce')) {
// Settings are saved by WordPress settings API
}
// Save settings notification
if (isset($_GET['settings-updated'])) {
add_settings_error('ailiveagents_messages', 'ailiveagents_message', esc_html__('Settings saved successfully', 'ai-live-agents'), 'updated');
}
settings_errors('ailiveagents_messages');
?>
is_page_excluded()) {
return;
}
// Register and enqueue the external script properly
wp_register_script(
'ailiveagents-widget',
'https://agent.ailiveagents.com/ai-chat.es.js',
array(),
AILIVEAGENTS_VERSION,
array(
'in_footer' => true,
'strategy' => 'defer'
)
);
wp_enqueue_script('ailiveagents-widget');
// Add module type attribute using proper WordPress filter
add_filter('script_loader_tag', array($this, 'add_module_type'), 10, 3);
}
public function add_module_type($tag, $handle, $src) {
// Only modify our specific script handle
if ('ailiveagents-widget' !== $handle && 'ailiveagents-widget-shortcode' !== $handle) {
return $tag;
}
// Add type="module" attribute to the script tag
$tag = str_replace(' src=', ' type="module" src=', $tag);
return $tag;
}
public function enqueue_admin_styles($hook_suffix) {
// Only load on our admin page
if ('settings_page_ai-live-agents' !== $hook_suffix) {
return;
}
// Register a dummy style handle to attach inline CSS
wp_register_style('ailiveagents-admin', false, array(), AILIVEAGENTS_VERSION);
wp_enqueue_style('ailiveagents-admin');
// Add inline CSS using proper WordPress function
$admin_css = '
.wrap h1 {
margin-bottom: 10px;
}
.form-table th {
padding: 20px 10px 20px 0;
}
.form-table td {
padding: 15px 10px;
}
';
wp_add_inline_style('ailiveagents-admin', $admin_css);
}
public function render_widget() {
$bot_id = get_option('ailiveagents_bot_id');
// Don't display if Bot ID is missing
if (empty($bot_id)) {
return;
}
// Check if current page should be excluded
if ($this->is_page_excluded()) {
return;
}
// Output the widget element
$this->output_widget_element($bot_id);
}
private function is_page_excluded() {
$exclude_pages = get_option('ailiveagents_exclude_pages', '');
if (empty($exclude_pages)) {
return false;
}
// Safely get and sanitize the REQUEST_URI
if (!isset($_SERVER['REQUEST_URI'])) {
return false;
}
$current_url = sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI']));
$excluded_urls = array_filter(array_map('trim', explode("\n", $exclude_pages)));
foreach ($excluded_urls as $excluded_url) {
if (strpos($current_url, $excluded_url) !== false) {
return true;
}
}
return false;
}
private function output_widget_element($bot_id) {
// Output only the custom element, script is already enqueued
echo '';
echo '';
echo '';
}
public function shortcode($atts) {
$atts = shortcode_atts(array(
'bot_id' => get_option('ailiveagents_bot_id')
), $atts);
$bot_id = sanitize_text_field($atts['bot_id']);
if (empty($bot_id)) {
if (current_user_can('manage_options')) {
return '' .
esc_html__('AI Live Agents: Please configure your Bot ID in the settings.', 'ai-live-agents') .
'
';
}
return '';
}
// Register and enqueue script for shortcode usage
wp_register_script(
'ailiveagents-widget-shortcode',
'https://agent.ailiveagents.com/ai-chat.es.js',
array(),
AILIVEAGENTS_VERSION,
array(
'in_footer' => true,
'strategy' => 'defer'
)
);
wp_enqueue_script('ailiveagents-widget-shortcode');
// The add_module_type filter already handles both script handles
// Return only the HTML element
return '';
}
public function add_module_type_shortcode($tag, $handle, $src) {
// This method is no longer needed - removed
return $tag;
}
public function add_settings_link($links) {
$settings_link = '' .
esc_html__('Settings', 'ai-live-agents') . '';
array_unshift($links, $settings_link);
return $links;
}
}
// Initialize the plugin
function ailiveagents_init() {
return AILIVEAGENTS_Widget::get_instance();
}
// Load the plugin after WordPress is fully loaded
add_action('plugins_loaded', 'ailiveagents_init');
// Activation hook
register_activation_hook(__FILE__, 'ailiveagents_activate');
function ailiveagents_activate() {
// Set default options
add_option('ailiveagents_bot_id', '');
add_option('ailiveagents_exclude_pages', '');
}
// Deactivation hook
register_deactivation_hook(__FILE__, 'ailiveagents_deactivate');
function ailiveagents_deactivate() {
// Cleanup if needed
}
// Uninstall hook
register_uninstall_hook(__FILE__, 'ailiveagents_uninstall');
function ailiveagents_uninstall() {
// Remove all options
delete_option('ailiveagents_bot_id');
delete_option('ailiveagents_exclude_pages');
}