admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'recomai_nonce' ), 'dashboard_url' => admin_url( 'admin.php?page=aimbrill-chatbot-for-recomai' ), 'site_url' => esc_url_raw( home_url( '/' ) ), 'blog_name' => sanitize_text_field( get_bloginfo( 'name' ) ), 'verify_otp_url'=> esc_url_raw( $this->get_frontend_base_url() . '/auth/verifyOTP' ), 'onboarding_url'=> esc_url_raw( $this->get_frontend_base_url() . '/dashboard' ), 'logo_url' => esc_url_raw( RECOMAI_PLUGIN_URL . 'assets/images/logo-dark.png' ), ) ); } private function get_settings() { $defaults = array( 'api_token' => '', 'user_email' => '', 'user_name' => '', 'is_authenticated' => false, 'onboarding_completed' => false, 'onboarding_url' => '', 'chatbot_enabled' => true, 'load_sitewide' => true, 'chatbot_embed_token' => '', 'last_sync_error' => '', 'last_synced_at' => 0, ); $settings = get_option( 'recomai_settings', array() ); if ( ! is_array( $settings ) ) { $settings = array(); } return wp_parse_args( $settings, $defaults ); } public function display_admin_page() { $settings = $this->get_settings(); $is_logged_in = ! empty( $settings['is_authenticated'] ) && ! empty( $settings['api_token'] ); $onboarding_completed = ! empty( $settings['onboarding_completed'] ); ?>

https://recomai.one
+917600855571
hello@recomai.one


is_woocommerce_connected( $settings ) ) : ?>

'Permission denied.' ), 403 ); } $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $password = isset( $_POST['password'] ) ? wp_unslash( $_POST['password'] ) : ''; if ( empty( $email ) || empty( $password ) ) { wp_send_json_error( array( 'message' => __( 'Email and password are required.', 'aimbrill-chatbot-for-recomai' ) ), 400 ); } $api = new RecomAI_API(); $response = $api->sign_in( $email, $password ); $token = ''; if ( ! empty( $response['data']['data']['token'] ) ) { $token = sanitize_text_field( $response['data']['data']['token'] ); } elseif ( ! empty( $response['data']['token'] ) ) { $token = sanitize_text_field( $response['data']['token'] ); } if ( ! $response['success'] || empty( $token ) ) { wp_send_json_error( array( 'message' => $response['message'] ), 400 ); } $settings = $this->get_settings(); $settings['api_token'] = $token; $settings['user_email'] = $email; if ( isset( $response['data']['data']['name'] ) ) { $settings['user_name'] = sanitize_text_field( $response['data']['data']['name'] ); } elseif ( isset( $response['data']['user']['name'] ) ) { $settings['user_name'] = sanitize_text_field( $response['data']['user']['name'] ); } else { $settings['user_name'] = ''; } $settings['is_authenticated'] = true; $settings['onboarding_completed'] = false; $settings['chatbot_embed_token'] = ''; $settings['last_sync_error'] = ''; $settings['onboarding_url'] = admin_url( 'admin.php?page=aimbrill-chatbot-for-recomai&step=onboarding' ); update_option( 'recomai_settings', $settings ); wp_send_json_success( array( 'message' => __( 'Login successful.', 'aimbrill-chatbot-for-recomai' ), 'redirect_url' => $settings['onboarding_url'], ) ); } public function handle_register() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $password = isset( $_POST['password'] ) ? wp_unslash( $_POST['password'] ) : ''; $phone = ''; if ( isset( $_POST['phone'] ) ) { $phone = sanitize_text_field( wp_unslash( $_POST['phone'] ) ); } elseif ( isset( $_POST['mobile'] ) ) { $phone = sanitize_text_field( wp_unslash( $_POST['mobile'] ) ); } $store_url = isset( $_POST['storeUrl'] ) ? esc_url_raw( wp_unslash( $_POST['storeUrl'] ) ) : ''; if ( empty( $store_url ) && isset( $_POST['webUrl'] ) ) { $store_url = esc_url_raw( wp_unslash( $_POST['webUrl'] ) ); } $store_name = isset( $_POST['storeName'] ) ? sanitize_text_field( wp_unslash( $_POST['storeName'] ) ) : sanitize_text_field( get_bloginfo( 'name' ) ); if ( empty( $name ) || empty( $email ) || empty( $password ) || empty( $store_url ) ) { wp_send_json_error( array( 'message' => 'All required fields must be filled.' ), 400 ); } $phone_trim = trim( $phone ); if ( strlen( $phone_trim ) < 10 ) { wp_send_json_error( array( 'message' => 'Phone number must be at least 10 characters.' ), 400 ); } $api = new RecomAI_API(); $response = $api->sign_up( $name, $email, $password, $phone_trim, $store_url, $store_name ); if ( ! $response['success'] ) { wp_send_json_error( array( 'message' => $response['message'] ), 400 ); } wp_send_json_success( array( 'message' => 'Registration successful. Enter OTP to verify your account.', 'email' => $email, ) ); } public function handle_verify_otp() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $otp = isset( $_POST['otp'] ) ? sanitize_text_field( wp_unslash( $_POST['otp'] ) ) : ''; if ( empty( $email ) || empty( $otp ) ) { wp_send_json_error( array( 'message' => 'Email and OTP are required.' ), 400 ); } $api = new RecomAI_API(); $response = $api->verify_email_otp( $email, $otp ); $token = ''; if ( ! empty( $response['data']['data']['token'] ) ) { $token = sanitize_text_field( $response['data']['data']['token'] ); } elseif ( ! empty( $response['data']['token'] ) ) { $token = sanitize_text_field( $response['data']['token'] ); } if ( ! $response['success'] || empty( $token ) ) { wp_send_json_error( array( 'message' => $response['message'] ), 400 ); } $settings = $this->get_settings(); $settings['api_token'] = $token; $settings['user_email'] = $email; $settings['is_authenticated'] = true; $settings['onboarding_completed'] = false; $settings['chatbot_embed_token'] = ''; $settings['last_sync_error'] = ''; update_option( 'recomai_settings', $settings ); wp_send_json_success( array( 'message' => 'OTP verified successfully.', 'redirect_url' => esc_url_raw( add_query_arg( array( 'authToken' => $token, ), $this->get_frontend_base_url() . '/dashboard' ) ), ) ); } public function handle_resend_otp() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; if ( empty( $email ) ) { wp_send_json_error( array( 'message' => 'Email is required.' ), 400 ); } $api = new RecomAI_API(); $response = $api->resend_otp( $email ); if ( ! $response['success'] ) { wp_send_json_error( array( 'message' => $response['message'] ), 400 ); } wp_send_json_success( array( 'message' => 'OTP resent successfully.' ) ); } public function handle_forgot_password() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; if ( empty( $email ) ) { wp_send_json_error( array( 'message' => 'Email is required.' ), 400 ); } $api = new RecomAI_API(); $response = $api->resend_otp( $email ); // auth/sendOtp endpoint is used for both resend and forgot password start if ( ! $response['success'] ) { wp_send_json_error( array( 'message' => $response['message'] ), 400 ); } wp_send_json_success( array( 'message' => 'Reset OTP sent to your email.', 'email' => $email ) ); } public function handle_reset_password() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $password = isset( $_POST['password'] ) ? wp_unslash( $_POST['password'] ) : ''; $confirm_password = isset( $_POST['confirm_password'] ) ? wp_unslash( $_POST['confirm_password'] ) : ''; if ( empty( $email ) || empty( $password ) || empty( $confirm_password ) ) { wp_send_json_error( array( 'message' => 'All fields are required.' ), 400 ); } if ( $password !== $confirm_password ) { wp_send_json_error( array( 'message' => 'Passwords do not match.' ), 400 ); } $api = new RecomAI_API(); $response = $api->reset_password( $email, $password, $confirm_password ); if ( ! $response['success'] ) { wp_send_json_error( array( 'message' => $response['message'] ), 400 ); } wp_send_json_success( array( 'message' => 'Password reset successfully. Please sign in now.' ) ); } public function complete_onboarding() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $settings = $this->get_settings(); if ( empty( $settings['api_token'] ) || empty( $settings['user_email'] ) ) { wp_send_json_error( array( 'message' => 'Please log in first.' ), 400 ); } $api = new RecomAI_API(); $config = $api->fetch_chatbot_config( $settings['user_email'], $settings['api_token'] ); if ( ! $config['success'] || empty( $config['normalized']['chatbot_embed_token'] ) ) { $details = $config['message']; if ( $config['success'] && empty( $config['normalized']['chatbot_embed_token'] ) ) { $details = 'No chatbot found. Please create at least one chatbot in the RecomAI dashboard before completing setup.'; } else if ( ! empty( $config['code'] ) ) { $details .= ' (HTTP ' . (int) $config['code'] . ')'; } $settings['last_sync_error'] = $details; update_option( 'recomai_settings', $settings ); wp_send_json_error( array( 'message' => $details, 'redirect_url' => $this->get_saas_manage_url( $settings['api_token'] ), ) ); } $settings['chatbot_embed_token'] = $config['normalized']['chatbot_embed_token']; $settings['onboarding_url'] = $config['normalized']['onboarding_url']; $settings['onboarding_completed'] = true; $settings['last_sync_error'] = ''; $settings['last_synced_at'] = time(); update_option( 'recomai_settings', $settings ); wp_send_json_success( array( 'message' => 'Onboarding completed successfully.', 'redirect_url' => admin_url( 'admin.php?page=aimbrill-chatbot-for-recomai' ), 'frontend_onboarding_url' => esc_url_raw( $this->get_frontend_base_url() . '/onboarding' ), ) ); } public function update_settings() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $settings = $this->get_settings(); $settings['chatbot_enabled'] = ! empty( $_POST['chatbot_enabled'] ); $settings['load_sitewide'] = ! empty( $_POST['load_sitewide'] ); update_option( 'recomai_settings', $settings ); wp_send_json_success( array( 'message' => 'Settings saved.' ) ); } public function handle_logout() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } $settings = $this->get_settings(); $settings['api_token'] = ''; $settings['user_email'] = ''; $settings['user_name'] = ''; $settings['is_authenticated'] = false; $settings['onboarding_completed'] = false; $settings['onboarding_url'] = ''; $settings['chatbot_embed_token'] = ''; $settings['last_sync_error'] = ''; update_option( 'recomai_settings', $settings ); wp_send_json_success( array( 'message' => 'Logged out successfully.' ) ); } private function is_woocommerce_connected( $settings ) { return ! empty( $settings['wc_consumer_key'] ) && ! empty( $settings['wc_consumer_secret'] ); } public function get_wc_auth_url() { check_ajax_referer( 'recomai_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 ); } if ( ! class_exists( 'RecomAI_WooCommerce' ) ) { require_once RECOMAI_PLUGIN_DIR . 'includes/class-woocommerce.php'; } $wc = new RecomAI_WooCommerce(); $auth_url = $wc->get_auth_url(); if ( is_wp_error( $auth_url ) ) { wp_send_json_error( array( 'message' => $auth_url->get_error_message() ) ); } wp_send_json_success( array( 'auth_url' => $auth_url ) ); } private function get_saas_manage_url( $token ) { return add_query_arg( array( 'authToken' => $token, ), $this->get_frontend_base_url() . '/dashboard' ); } }