'application/json' ); if ( '' !== $access ) { $headers['Authorization'] = 'Bearer ' . $access; } $response = wp_remote_post( FLWCB_API_URL . $path, array( 'timeout' => 15, 'headers' => $headers, 'body' => wp_json_encode( (object) $data ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); return array( 'status' => (int) wp_remote_retrieve_response_code( $response ), 'body' => is_array( $body ) ? $body : array(), ); } /** * GETs from the chatbot builder's API as the signed-in account. * * @return array|WP_Error { status: int, body: array }, or an error when the service can't be reached. */ function flwcb_api_get( $path, $access ) { $response = wp_remote_get( FLWCB_API_URL . $path, array( 'timeout' => 15, 'headers' => array( 'Authorization' => 'Bearer ' . $access ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); return array( 'status' => (int) wp_remote_retrieve_response_code( $response ), 'body' => is_array( $body ) ? $body : array(), ); } /** * The signed-in account's chatbots, each as { id, name, domain }. * * @return array|WP_Error|null The list; null when signed out; an error when the service can't be reached. */ function flwcb_chatbots() { $tokens = flwcb_tokens(); if ( ! is_array( $tokens ) ) { return $tokens; } $result = flwcb_api_get( '/chatbot/chatbots/', $tokens['access'] ); if ( is_wp_error( $result ) ) { return $result; } if ( 200 !== $result['status'] ) { return new WP_Error( 'flwcb_api', flwcb_api_error( $result['body'] ) ); } $bots = array(); foreach ( $result['body'] as $bot ) { if ( ! is_array( $bot ) || empty( $bot['id'] ) ) { continue; } $domain = flwcb_sanitize_domain( isset( $bot['website']['url'] ) ? $bot['website']['url'] : '' ); if ( '' === $domain ) { continue; } $bots[] = array( 'id' => sanitize_text_field( (string) $bot['id'] ), 'name' => sanitize_text_field( isset( $bot['name'] ) ? (string) $bot['name'] : $domain ), 'domain' => $domain, ); } return $bots; } // The service's message for a failed request: { detail } or { field: [message] } function flwcb_api_error( $body ) { if ( ! empty( $body['detail'] ) && is_string( $body['detail'] ) ) { return $body['detail']; } foreach ( $body as $messages ) { $message = is_array( $messages ) ? reset( $messages ) : $messages; if ( is_string( $message ) && '' !== $message ) { return $message; } } return __( 'Something went wrong. Please try again.', '41-labs-website-chatbot-builder' ); } /** * Signs the current WordPress user in to the chatbot builder and keeps the * refresh token, so the builder opens already signed in. * * @return true|string True, or a message saying what went wrong. */ function flwcb_sign_in( $email, $password ) { $result = flwcb_api_post( '/auth/jwt/create/', array( 'email' => $email, 'password' => $password, ) ); if ( is_wp_error( $result ) ) { return __( "Couldn't reach the chatbot builder. Please try again.", '41-labs-website-chatbot-builder' ); } if ( 200 !== $result['status'] || empty( $result['body']['refresh'] ) ) { return flwcb_api_error( $result['body'] ); } update_user_meta( get_current_user_id(), 'flwcb_refresh_token', sanitize_text_field( $result['body']['refresh'] ) ); update_user_meta( get_current_user_id(), 'flwcb_account_email', sanitize_email( $email ) ); return true; } /** * A fresh sign-in for the builder from the kept refresh token. * * @return array|WP_Error|null { access, refresh }; null when signed out or the * sign-in has expired; an error when the service can't be reached. */ function flwcb_tokens() { $refresh = (string) get_user_meta( get_current_user_id(), 'flwcb_refresh_token', true ); if ( '' === $refresh ) { return null; } $result = flwcb_api_post( '/auth/jwt/refresh/', array( 'refresh' => $refresh ) ); if ( is_wp_error( $result ) ) { return $result; } if ( 200 !== $result['status'] || empty( $result['body']['access'] ) ) { delete_user_meta( get_current_user_id(), 'flwcb_refresh_token' ); return null; } return array( 'access' => $result['body']['access'], 'refresh' => $refresh, ); } // ─── Settings ──────────────────────────────────────────────────────────────── add_action( 'admin_init', function () { register_setting( 'flwcb_settings', 'flwcb_enabled', array( 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ) ); register_setting( 'flwcb_settings', 'flwcb_domain', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'flwcb_sanitize_domain_setting', ) ); } ); add_action( 'admin_menu', function () { $hook = add_menu_page( __( '41 Labs Website Chatbot Builder', '41-labs-website-chatbot-builder' ), __( 'Website Chatbot', '41-labs-website-chatbot-builder' ), 'manage_options', FLWCB_PAGE, 'flwcb_render_page', 'dashicons-format-chat', 80 ); add_submenu_page( FLWCB_PAGE, __( '41 Labs Website Chatbot Builder', '41-labs-website-chatbot-builder' ), __( 'Chatbot', '41-labs-website-chatbot-builder' ), 'manage_options', FLWCB_PAGE, 'flwcb_render_page' ); add_submenu_page( FLWCB_PAGE, __( 'Website Chatbot settings', '41-labs-website-chatbot-builder' ), __( 'Settings', '41-labs-website-chatbot-builder' ), 'manage_options', FLWCB_SETTINGS_PAGE, 'flwcb_render_settings_page' ); add_action( 'admin_enqueue_scripts', function ( $current ) use ( $hook ) { if ( $current !== $hook ) { return; } wp_enqueue_style( 'flwcb-admin', plugins_url( 'admin.css', __FILE__ ), array(), FLWCB_VERSION ); } ); } ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) { $url = admin_url( 'admin.php?page=' . FLWCB_SETTINGS_PAGE ); array_unshift( $links, '' . esc_html__( 'Settings', '41-labs-website-chatbot-builder' ) . '' ); return $links; } ); // ─── Sign in / create account / sign out ───────────────────────────────────── // Back to the main page, with a message for the form when something went wrong function flwcb_back_to_page( $mode = 'signin', $error = '', $email = '' ) { if ( '' !== $error ) { set_transient( 'flwcb_auth_' . get_current_user_id(), array( 'error' => $error, 'email' => $email ), 5 * MINUTE_IN_SECONDS ); } $args = array( 'page' => FLWCB_PAGE ); if ( 'signup' === $mode ) { $args['mode'] = 'signup'; } wp_safe_redirect( add_query_arg( $args, admin_url( 'admin.php' ) ) ); exit; } add_action( 'admin_post_flwcb_auth', function () { check_admin_referer( 'flwcb_auth' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '41-labs-website-chatbot-builder' ) ); } $mode = isset( $_POST['mode'] ) && 'signup' === $_POST['mode'] ? 'signup' : 'signin'; $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; // Passwords go to the service exactly as typed; they aren't stored or shown here // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $password = isset( $_POST['password'] ) ? (string) wp_unslash( $_POST['password'] ) : ''; if ( '' === $email || '' === $password ) { flwcb_back_to_page( $mode, __( 'Enter your email and password.', '41-labs-website-chatbot-builder' ), $email ); } if ( 'signup' === $mode ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $password_again = isset( $_POST['password_again'] ) ? (string) wp_unslash( $_POST['password_again'] ) : ''; $result = flwcb_api_post( '/auth/users/', array( 'first_name' => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '', 'last_name' => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '', 'email' => $email, 'password' => $password, 're_password' => $password_again, ) ); if ( is_wp_error( $result ) ) { flwcb_back_to_page( $mode, __( "Couldn't reach the chatbot builder. Please try again.", '41-labs-website-chatbot-builder' ), $email ); } if ( 201 !== $result['status'] ) { flwcb_back_to_page( $mode, flwcb_api_error( $result['body'] ), $email ); } } $signed_in = flwcb_sign_in( $email, $password ); if ( true !== $signed_in ) { flwcb_back_to_page( $mode, $signed_in, $email ); } flwcb_back_to_page(); } ); add_action( 'admin_post_flwcb_sign_out', function () { check_admin_referer( 'flwcb_sign_out' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '41-labs-website-chatbot-builder' ) ); } delete_user_meta( get_current_user_id(), 'flwcb_refresh_token' ); flwcb_back_to_page(); } ); // ─── Continue with Google, and opening the 41 Labs site signed in ──────────── // Both use one-time codes from the API that expire after 2 minutes; no // password is sent. "Continue with Google" sends you to the 41 Labs site to log // in and confirm, and it comes back here with a code that is swapped for a // sign-in. Opening the builder makes a code here and passes it to the site. // Where the 41 Labs site sends you back to after "Continue with Google" function flwcb_connect_return_url() { return admin_url( 'admin-post.php?action=flwcb_connect_return' ); } // Redirect to a page of the 41 Labs site (and only there) function flwcb_go_to_app( $url ) { add_filter( 'allowed_redirect_hosts', function ( $hosts ) { $hosts[] = wp_parse_url( FLWCB_APP_URL, PHP_URL_HOST ); return $hosts; } ); wp_safe_redirect( $url ); exit; } add_action( 'admin_post_flwcb_connect', function () { check_admin_referer( 'flwcb_connect' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '41-labs-website-chatbot-builder' ) ); } // Checked when the site sends you back, so only a trip started here can sign you in $state = wp_generate_password( 32, false ); set_transient( 'flwcb_state_' . get_current_user_id(), $state, 15 * MINUTE_IN_SECONDS ); flwcb_go_to_app( add_query_arg( array( 'return_url' => rawurlencode( flwcb_connect_return_url() ), 'state' => $state, 'provider' => 'google', ), FLWCB_APP_URL . '/connect/wordpress' ) ); } ); add_action( 'admin_post_flwcb_connect_return', function () { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '41-labs-website-chatbot-builder' ) ); } // The state made for this user when they left (see above) stands in for a nonce // phpcs:disable WordPress.Security.NonceVerification.Recommended $state = isset( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : ''; $code = isset( $_GET['code'] ) ? sanitize_text_field( wp_unslash( $_GET['code'] ) ) : ''; $cancelled = isset( $_GET['error'] ); // phpcs:enable WordPress.Security.NonceVerification.Recommended $expected = get_transient( 'flwcb_state_' . get_current_user_id() ); delete_transient( 'flwcb_state_' . get_current_user_id() ); if ( ! is_string( $expected ) || '' === $state || ! hash_equals( $expected, $state ) ) { flwcb_back_to_page( 'signin', __( 'That sign-in took too long or was started elsewhere. Please try again.', '41-labs-website-chatbot-builder' ) ); } if ( $cancelled || '' === $code ) { flwcb_back_to_page(); } $result = flwcb_api_post( '/auth/handoff/exchange/', array( 'code' => $code, 'redirect_uri' => flwcb_connect_return_url(), ) ); if ( is_wp_error( $result ) ) { flwcb_back_to_page( 'signin', __( "Couldn't reach the chatbot builder. Please try again.", '41-labs-website-chatbot-builder' ) ); } if ( 200 !== $result['status'] || empty( $result['body']['refresh'] ) ) { flwcb_back_to_page( 'signin', flwcb_api_error( $result['body'] ) ); } update_user_meta( get_current_user_id(), 'flwcb_refresh_token', sanitize_text_field( $result['body']['refresh'] ) ); update_user_meta( get_current_user_id(), 'flwcb_account_email', sanitize_email( isset( $result['body']['email'] ) ? $result['body']['email'] : '' ) ); flwcb_back_to_page(); } ); // Open the builder (or the dashboard) on the 41 Labs site, already signed in add_action( 'admin_post_flwcb_open', function () { check_admin_referer( 'flwcb_open' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '41-labs-website-chatbot-builder' ) ); } $next = isset( $_GET['to'] ) && 'dashboard' === $_GET['to'] ? '/dashboard' : '/chatbot/url?url=' . rawurlencode( home_url( '/' ) ); $tokens = flwcb_tokens(); if ( is_array( $tokens ) ) { $result = flwcb_api_post( '/auth/handoff/', array(), $tokens['access'] ); if ( ! is_wp_error( $result ) && 200 === $result['status'] && ! empty( $result['body']['code'] ) ) { flwcb_go_to_app( add_query_arg( array( 'code' => rawurlencode( $result['body']['code'] ), 'next' => rawurlencode( $next ), ), FLWCB_APP_URL . '/auth/handoff' ) ); } } // Couldn't sign in for you: open the page anyway, where you can log in flwcb_go_to_app( FLWCB_APP_URL . $next ); } ); // Back to the chatbot list, saying what changed function flwcb_back_to_list( $done ) { wp_safe_redirect( add_query_arg( array( 'page' => FLWCB_PAGE, 'flwcb_done' => $done ), admin_url( 'admin.php' ) ) ); exit; } // Show one of the account's chatbots on this site. The address comes from the // account's own list, not from the form, so only a real chatbot can be chosen. add_action( 'admin_post_flwcb_show', function () { check_admin_referer( 'flwcb_show' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '41-labs-website-chatbot-builder' ) ); } $id = isset( $_POST['chatbot'] ) ? sanitize_text_field( wp_unslash( $_POST['chatbot'] ) ) : ''; $bots = flwcb_chatbots(); if ( ! is_array( $bots ) ) { flwcb_back_to_list( 'error' ); } foreach ( $bots as $bot ) { if ( $bot['id'] === $id ) { update_option( 'flwcb_domain', $bot['domain'] ); update_option( 'flwcb_enabled', true ); flwcb_back_to_list( 'shown' ); } } flwcb_back_to_list( 'error' ); } ); add_action( 'admin_post_flwcb_hide', function () { check_admin_referer( 'flwcb_hide' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', '41-labs-website-chatbot-builder' ) ); } update_option( 'flwcb_enabled', false ); flwcb_back_to_list( 'hidden' ); } ); // ─── Pages ─────────────────────────────────────────────────────────────────── /** * Before signing in: a sign-in / create account form. Once signed in: the * account's chatbots, read from the API, to pick the one shown on this site. * Building a chatbot happens on the 41 Labs site, in a new tab. */ function flwcb_render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $bots = flwcb_chatbots(); if ( null === $bots || ( is_wp_error( $bots ) && 'flwcb_api' !== $bots->get_error_code() ) ) { flwcb_render_auth( is_wp_error( $bots ) ); } else { flwcb_render_chatbots( $bots ); } } function flwcb_render_auth( $unreachable ) { // Only picks which of the two forms to show, so no nonce is needed // phpcs:ignore WordPress.Security.NonceVerification.Recommended $signup = isset( $_GET['mode'] ) && 'signup' === $_GET['mode']; $state = get_transient( 'flwcb_auth_' . get_current_user_id() ); delete_transient( 'flwcb_auth_' . get_current_user_id() ); $error = is_array( $state ) ? $state['error'] : ''; $email = is_array( $state ) ? $state['email'] : (string) get_user_meta( get_current_user_id(), 'flwcb_account_email', true ); if ( '' === $error && $unreachable ) { $error = __( "Couldn't reach the chatbot builder. Please reload the page to try again.", '41-labs-website-chatbot-builder' ); } $switch_url = add_query_arg( array( 'page' => FLWCB_PAGE, 'mode' => $signup ? 'signin' : 'signup' ), admin_url( 'admin.php' ) ); ?>


' . esc_html( $account ) . '' ); ?>

get_error_message() ); ?>

|

FLWCB_APP_URL . '/hosted-chatbot/' . rawurlencode( flwcb_domain() ), 'label' => __( 'Open chat', '41-labs-website-chatbot-builder' ), 'close' => __( 'Close chat', '41-labs-website-chatbot-builder' ), ) ); } );