WP_REST_Server::READABLE, 'callback' => array( $this, 'get_config' ), 'permission_callback' => array( 'MYBOT_REST_Controller', 'public_permission' ), ) ); register_rest_route( MYBOT_REST_NAMESPACE, '/history', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_history' ), 'permission_callback' => array( 'MYBOT_REST_Controller', 'admin_permission' ), 'args' => array( 'session_id' => array( 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), 'limit' => array( 'required' => false, 'type' => 'integer', 'default' => 50, 'sanitize_callback' => 'absint', ), ), ) ); } /** * Public configuration for the widget. * * No API key is ever returned. * * @return WP_REST_Response */ public function get_config() { $plugin = mybot(); $settings = $plugin->settings; $default = $plugin->languages->default_code(); return new WP_REST_Response( array( 'success' => true, 'botName' => $plugin->languages->string( $default, 'bot_name' ), 'welcome' => $plugin->languages->string( $default, 'welcome' ), 'placeholder' => $plugin->languages->string( $default, 'placeholder' ), 'color' => (string) $settings->get( 'primary_color' ), 'position' => (string) $settings->get( 'position' ), 'ragEnabled' => $plugin->settings->rag_available(), 'maxLength' => 2000, // Chatbot languages. 'languages' => $plugin->languages->enabled(), 'languageOptions' => $plugin->languages->switch_options(), 'defaultLanguage' => $default, 'showSwitch' => $plugin->languages->show_switch(), 'strings' => $plugin->languages->widget_strings(), 'nonce' => wp_create_nonce( 'wp_rest' ), ), 200 ); } /** * Conversation history, administrators only. * * @param WP_REST_Request $request The request. * @return WP_REST_Response */ public function get_history( $request ) { $session_id = mybot()->security->clean_session_id( (string) $request->get_param( 'session_id' ) ); $limit = (int) $request->get_param( 'limit' ); if ( '' === $session_id ) { return new WP_REST_Response( array( 'success' => true, 'sessions' => mybot()->logger->recent_sessions( $limit ), 'stats' => mybot()->logger->stats(), ), 200 ); } $messages = mybot()->logger->get_history( $session_id, $limit ); return new WP_REST_Response( array( 'success' => true, 'session_id' => $session_id, 'count' => count( $messages ), 'messages' => $messages, ), 200 ); } }