namespace, '/threads', array( array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_threads' ), 'permission_callback' => array( $this, 'require_login' ), 'args' => array( 'page' => array( 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', ), ), ), array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_thread' ), 'permission_callback' => array( $this, 'require_login' ), 'args' => array( 'recipients' => array( 'required' => true, 'type' => 'array', ), 'subject' => array( 'type' => 'string', 'default' => 'New Message', 'sanitize_callback' => 'sanitize_text_field', ), 'content' => array( 'required' => true, 'type' => 'string', ), ), ), ) ); register_rest_route( $this->namespace, '/threads/(?P[\d]+)/messages', array( array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_messages' ), 'permission_callback' => array( $this, 'is_thread_participant' ), ), array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_message' ), 'permission_callback' => array( $this, 'is_thread_participant' ), 'args' => array( 'content' => array( 'required' => true, 'type' => 'string', ), ), ), ) ); register_rest_route( $this->namespace, '/threads/(?P[\d]+)/read', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'mark_read' ), 'permission_callback' => array( $this, 'is_thread_participant' ), ) ); register_rest_route( $this->namespace, '/unread-count', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'unread_count' ), 'permission_callback' => array( $this, 'require_login' ), ) ); } public function require_login( \WP_REST_Request $request ): true|\WP_Error { if ( is_user_logged_in() ) { return true; } return new \WP_Error( 'arshid6social_login_required', __( 'You must be logged in to perform this action.', '6arshid-social-community' ), array( 'status' => 401 ) ); } /** * Object-level check: the current user must be a participant of the * thread to read, post to, or mark it read. */ public function is_thread_participant( \WP_REST_Request $request ): bool { if ( ! is_user_logged_in() ) { return false; } global $wpdb; return (bool) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT id FROM {$wpdb->prefix}arshid6social_messages_recipients WHERE thread_id = %d AND user_id = %d AND is_deleted = 0", absint( $request['id'] ), get_current_user_id() ) ); } public function get_threads( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'messages' ); return rest_ensure_response( $component->get_threads( get_current_user_id(), $request['page'] ) ); } public function create_thread( $request ): \WP_REST_Response|\WP_Error { $component = ARSHID6SOCIAL()->component( 'messages' ); $recipients = array_map( 'absint', (array) $request->get_param( 'recipients' ) ); $thread_id = $component->start_thread( $recipients, $request->get_param( 'subject' ), wp_kses_post( $request->get_param( 'content' ) ) ); if ( ! $thread_id ) { return new \WP_Error( 'arshid6social_message_error', __( 'Could not create thread.', '6arshid-social-community' ), array( 'status' => 500 ) ); } return rest_ensure_response( array( 'thread_id' => $thread_id ) ); } public function get_messages( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'messages' ); return rest_ensure_response( $component->get_thread_messages( (int) $request['id'], get_current_user_id() ) ); } public function send_message( $request ): \WP_REST_Response|\WP_Error { $component = ARSHID6SOCIAL()->component( 'messages' ); $message_id = $component->add_message_to_thread( (int) $request['id'], get_current_user_id(), wp_kses_post( $request->get_param( 'content' ) ) ); if ( ! $message_id ) { return new \WP_Error( 'arshid6social_message_error', __( 'Could not send message.', '6arshid-social-community' ), array( 'status' => 500 ) ); } return rest_ensure_response( array( 'message_id' => $message_id ) ); } public function mark_read( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'messages' ); $component->mark_thread_read( (int) $request['id'], get_current_user_id() ); return rest_ensure_response( array( 'read' => true ) ); } public function unread_count( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'messages' ); return rest_ensure_response( array( 'count' => $component->get_unread_count( get_current_user_id() ) ) ); } }