api = new Airabot_API(); add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'admin_post_airabot_connect', array( $this, 'handle_connect' ) ); add_action( 'admin_post_airabot_logout', array( $this, 'handle_logout' ) ); add_action( 'admin_post_airabot_save', array( $this, 'handle_save' ) ); // The return leg from airabot.in is a GET back to the settings screen. add_action( 'admin_init', array( $this, 'maybe_complete_connect' ) ); } /** * Adds the top-level "Airabot" menu. */ public function register_menu() { add_menu_page( __( 'Airabot Chatbot', 'airabot-chatbot' ), __( 'Airabot', 'airabot-chatbot' ), self::CAPABILITY, self::PAGE_SLUG, array( $this, 'render_page' ), 'dashicons-format-chat', 58 ); } /** * Loads the admin stylesheet on this plugin's screen only. * * @param string $hook Current admin page hook suffix. */ public function enqueue_assets( $hook ) { if ( 'toplevel_page_' . self::PAGE_SLUG !== $hook ) { return; } // Sora is bundled in assets/fonts and declared via @font-face in // admin.css — deliberately not loaded from Google Fonts, which would // leak every administrator's IP to a third party on each page view. wp_enqueue_style( 'airabot-admin', AIRABOT_PLUGIN_URL . 'assets/admin.css', array(), AIRABOT_VERSION ); wp_enqueue_script( 'airabot-admin', AIRABOT_PLUGIN_URL . 'assets/admin.js', array(), AIRABOT_VERSION, true ); } /** * The URL of the settings page, optionally with a status message. * * @param array $args Extra query args. * @return string */ private function page_url( $args = array() ) { return add_query_arg( array_merge( array( 'page' => self::PAGE_SLUG ), $args ), admin_url( 'admin.php' ) ); } /** * Blocks any request from a user who may not configure the plugin. */ private function require_permission() { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You do not have permission to configure Airabot.', 'airabot-chatbot' ) ); } } /** * Starts the connection: sends the admin to airabot.in to approve this site. * * A random `state` value is stored first and checked when the browser comes * back, so a code delivered by any request this site did not initiate is * rejected rather than silently exchanged. */ public function handle_connect() { check_admin_referer( 'airabot_connect' ); $this->require_permission(); $state = wp_generate_password( 32, false ); set_transient( self::STATE_TRANSIENT, $state, 15 * MINUTE_IN_SECONDS ); // add_query_arg() does NOT url-encode values, so a site name such as "A & B #1" would // truncate the URL at the '#' and drop redirect_uri/state. Build the query explicitly: // each value encoded exactly once (RFC 3986), and the site name decoded first because // get_bloginfo() returns it HTML-escaped (& / '). $authorize_url = AIRABOT_CDN_BASE . '/authorize?' . http_build_query( array( 'site_url' => home_url(), 'site_name' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), 'redirect_uri' => $this->page_url(), 'state' => $state, ), '', '&', PHP_QUERY_RFC3986 ); // Allow only the Airabot host as an external redirect target. add_filter( 'allowed_redirect_hosts', static function ( $hosts ) { $hosts[] = wp_parse_url( AIRABOT_CDN_BASE, PHP_URL_HOST ); return $hosts; } ); wp_safe_redirect( $authorize_url ); exit; } /** * Completes the connection when airabot.in redirects back with a code. * * Hooked on admin page load rather than admin-post.php because the return * trip is a plain GET to the settings screen. */ public function maybe_complete_connect() { // This return trip comes from airabot.in, which cannot carry a WordPress nonce. // The one-time random `state` value stored by handle_connect() and compared with // hash_equals() below plays that role; the user must also have the capability. // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['page'] ) || self::PAGE_SLUG !== $_GET['page'] ) { return; } if ( ! current_user_can( self::CAPABILITY ) ) { return; } $expected = get_transient( self::STATE_TRANSIENT ); if ( isset( $_GET['airabot_error'] ) ) { delete_transient( self::STATE_TRANSIENT ); $this->redirect_with( 'error', __( 'Connection cancelled — this site was not connected.', 'airabot-chatbot' ) ); } if ( ! isset( $_GET['airabot_code'] ) ) { return; } $code = sanitize_text_field( wp_unslash( $_GET['airabot_code'] ) ); $state = isset( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : ''; delete_transient( self::STATE_TRANSIENT ); if ( empty( $expected ) || ! hash_equals( $expected, $state ) ) { $this->redirect_with( 'error', __( 'That connection link did not come from this site. Please try connecting again.', 'airabot-chatbot' ) ); } $result = $this->api->exchange_code( $code ); if ( is_wp_error( $result ) ) { $this->redirect_with( 'error', $result->get_error_message() ); } $this->redirect_with( 'success', __( 'Connected to Airabot.', 'airabot-chatbot' ) ); // phpcs:enable WordPress.Security.NonceVerification.Recommended } /** * Handles the disconnect button. */ public function handle_logout() { check_admin_referer( 'airabot_logout' ); $this->require_permission(); $this->api->disconnect(); $this->redirect_with( 'success', __( 'This site has been disconnected from Airabot.', 'airabot-chatbot' ) ); } /** * Handles the chatbot / widget-type form submission. */ public function handle_save() { check_admin_referer( 'airabot_save' ); $this->require_permission(); $bot_id = isset( $_POST['airabot_bot_id'] ) ? sanitize_text_field( wp_unslash( $_POST['airabot_bot_id'] ) ) : ''; if ( empty( $bot_id ) ) { $this->redirect_with( 'error', __( 'Please choose a chatbot before saving.', 'airabot-chatbot' ) ); } $type = isset( $_POST['airabot_widget_type'] ) ? sanitize_text_field( wp_unslash( $_POST['airabot_widget_type'] ) ) : 'bubble'; if ( ! in_array( $type, array( 'bubble', 'overlay' ), true ) ) { $type = 'bubble'; } // The bot's display name is submitted alongside its id purely so the // settings screen can name the active bot without a second API call. $bot_name = isset( $_POST['airabot_bot_name_' . $bot_id ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'airabot_bot_name_' . $bot_id ] ) ) : ''; update_option( 'airabot_bot_id', $bot_id, false ); update_option( 'airabot_bot_name', $bot_name, false ); update_option( 'airabot_widget_type', $type, false ); update_option( 'airabot_enabled', empty( $_POST['airabot_enabled'] ) ? 0 : 1, false ); $this->redirect_with( 'success', __( 'Chatbot settings saved. The widget is now live on your site.', 'airabot-chatbot' ) ); } /** * Redirects back to the settings page carrying a status message. * * Uses POST-redirect-GET so refreshing the page cannot resubmit the form. * * @param string $type 'success' or 'error'. * @param string $message Message to display. */ private function redirect_with( $type, $message ) { set_transient( 'airabot_notice', array( 'type' => $type, 'message' => $message ), 60 ); wp_safe_redirect( $this->page_url() ); exit; } /** * Prints a queued admin notice, if any. */ private function render_notice() { $notice = get_transient( 'airabot_notice' ); if ( ! $notice || ! is_array( $notice ) ) { return; } delete_transient( 'airabot_notice' ); printf( '
%s
airabot.in' ); ?>
get_error_message() ); ?>
airabot.in' ); ?>
' . esc_html( $bot_name ? $bot_name : $bot_id ) . '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); } else { esc_html_e( 'Saved, but not currently shown on your site — tick “Show the chatbot on my website” above to activate it.', 'airabot-chatbot' ); } ?>