Settings'; array_unshift( $links, $settings_link ); return $links; } // --------------------------------------------------------------------------- // Inject widget script in the footer (public pages) // --------------------------------------------------------------------------- add_action( 'wp_enqueue_scripts', 'lobbychat_enqueue_widget' ); function lobbychat_enqueue_widget() { $enabled = get_option( 'lobbychat_widget_enabled', '0' ); $widget_id = get_option( 'lobbychat_widget_id', '' ); if ( $enabled === '1' && ! empty( $widget_id ) ) { $script_url = LOBBYCHAT_API_BASE . '/embed/' . sanitize_text_field( $widget_id ) . '.js'; wp_enqueue_script( 'lobbychat-widget', $script_url, [], null, true ); } } // --------------------------------------------------------------------------- // Enqueue admin CSS and JS (settings page only) // --------------------------------------------------------------------------- add_action( 'admin_enqueue_scripts', 'lobbychat_enqueue_admin_assets' ); function lobbychat_enqueue_admin_assets( $hook ) { if ( $hook !== 'toplevel_page_lobbychat' ) { return; } // Review builds may keep the plugin version; changed CSS still needs a new cache key. $style_version = LOBBYCHAT_VERSION . '-' . substr( hash_file( 'sha256', __DIR__ . '/assets/admin.css' ), 0, 12 ); wp_enqueue_style( 'lobbychat-admin', plugin_dir_url( __FILE__ ) . 'assets/admin.css', [], $style_version ); wp_enqueue_script( 'lobbychat-admin', plugin_dir_url( __FILE__ ) . 'assets/admin.js', [], LOBBYCHAT_VERSION, true ); wp_localize_script( 'lobbychat-admin', 'lobbychatAdminData', [ 'nonce' => wp_create_nonce( 'lobbychat_nonce' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'hasWidgetId' => trim( get_option( 'lobbychat_widget_id', '' ) ) !== '', ] ); } // --------------------------------------------------------------------------- // AJAX handlers // --------------------------------------------------------------------------- add_action( 'wp_ajax_lobbychat_save_widget_id', 'lobbychat_ajax_save_widget_id' ); add_action( 'wp_ajax_lobbychat_toggle_widget', 'lobbychat_ajax_toggle_widget' ); function lobbychat_ajax_save_widget_id() { check_ajax_referer( 'lobbychat_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_die(); $widget_id = sanitize_text_field( wp_unslash( $_POST['widget_id'] ?? '' ) ); update_option( 'lobbychat_widget_id', $widget_id ); wp_send_json_success( [ 'widget_id' => $widget_id ] ); } function lobbychat_ajax_toggle_widget() { check_ajax_referer( 'lobbychat_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_die(); $enabled = ( isset( $_POST['enabled'] ) && $_POST['enabled'] === '1' ) ? '1' : '0'; update_option( 'lobbychat_widget_enabled', $enabled ); // Sync with Lobbychat dashboard (fire-and-forget, non-blocking) $widget_id = get_option( 'lobbychat_widget_id', '' ); if ( ! empty( $widget_id ) ) { wp_remote_post( LOBBYCHAT_API_BASE . '/api/public/chatbots/' . rawurlencode( $widget_id ) . '/set-active', [ 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => wp_json_encode( [ 'enabled' => $enabled === '1' ] ), 'timeout' => 10, 'blocking' => false, ] ); } wp_send_json_success( [ 'enabled' => $enabled ] ); } // --------------------------------------------------------------------------- // Settings page // --------------------------------------------------------------------------- function lobbychat_settings_page() { if ( ! current_user_can( 'manage_options' ) ) return; $widget_id = get_option( 'lobbychat_widget_id', '' ); $enabled = get_option( 'lobbychat_widget_enabled', '0' ) === '1'; $has_widget_id = trim( $widget_id ) !== ''; $setup_request = get_option( 'lobbychat_setup_request', false ); ?>