namespace, '/' . $this->rest_base . '/(?P[\d]+)/status', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_status' ), 'permission_callback' => array( $this, 'require_login' ), ) ); // POST /arshid6social/v1/friends/{user_id}/request register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\d]+)/request', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_request' ), 'permission_callback' => array( $this, 'can_target_user' ), ) ); // POST /arshid6social/v1/friends/{user_id}/accept register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\d]+)/accept', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'accept_request' ), 'permission_callback' => array( $this, 'can_accept_request' ), ) ); // DELETE /arshid6social/v1/friends/{user_id} register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', array( 'methods' => \WP_REST_Server::DELETABLE, 'callback' => array( $this, 'remove' ), 'permission_callback' => array( $this, 'can_target_user' ), ) ); // POST/DELETE /arshid6social/v1/friends/{user_id}/follow register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\d]+)/follow', array( array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'follow' ), 'permission_callback' => array( $this, 'can_follow_user' ), ), array( 'methods' => \WP_REST_Server::DELETABLE, 'callback' => array( $this, 'unfollow' ), 'permission_callback' => array( $this, 'can_follow_user' ), ), ) ); // POST/DELETE /arshid6social/v1/friends/{user_id}/block register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\d]+)/block', array( array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'block' ), 'permission_callback' => array( $this, 'can_target_user' ), ), array( 'methods' => \WP_REST_Server::DELETABLE, 'callback' => array( $this, 'unblock' ), 'permission_callback' => array( $this, 'can_target_user' ), ), ) ); // GET /arshid6social/v1/friends/suggestions register_rest_route( $this->namespace, '/' . $this->rest_base . '/suggestions', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'suggestions' ), 'permission_callback' => array( $this, 'require_login' ), ) ); } /** * Requires an authenticated REST user. */ public function require_login( \WP_REST_Request $request ): bool|\WP_Error { if ( is_user_logged_in() ) { return true; } return new \WP_Error( 'rest_forbidden', __( 'Authentication required.', '6arshid-social-community' ), array( 'status' => 401 ) ); } /** * Object-level check for routes acting on another member: the user must be * logged in, the target must be a real user, and it cannot be themselves. */ public function can_target_user( \WP_REST_Request $request ): bool|\WP_Error { if ( ! is_user_logged_in() ) { return false; } $target = absint( $request['id'] ); if ( $target === get_current_user_id() ) { return new \WP_Error( 'arshid6social_self_target', __( 'You cannot perform this action on yourself.', '6arshid-social-community' ), array( 'status' => 400 ) ); } if ( ! get_userdata( $target ) ) { return new \WP_Error( 'arshid6social_not_found', __( 'User not found.', '6arshid-social-community' ), array( 'status' => 404 ) ); } return true; } /** * Accepting a friend request is only allowed for a real inbound pending * request from the route target to the current user. */ public function can_accept_request( \WP_REST_Request $request ): bool|\WP_Error { $base = $this->can_target_user( $request ); if ( true !== $base ) { return $base; } $component = ARSHID6SOCIAL()->component( 'friends' ); $current = get_current_user_id(); $requester = absint( $request['id'] ); if ( ! $component ) { return new \WP_Error( 'arshid6social_disabled', __( 'Friends component is not active.', '6arshid-social-community' ), array( 'status' => 503 ) ); } if ( $component->is_blocked( $current, $requester ) ) { return new \WP_Error( 'arshid6social_forbidden', __( 'Permission denied.', '6arshid-social-community' ), array( 'status' => 403 ) ); } if ( 'pending_received' !== $component->get_friendship_status( $current, $requester ) ) { return new \WP_Error( 'arshid6social_no_pending_request', __( 'No pending friend request from this user.', '6arshid-social-community' ), array( 'status' => 409 ) ); } return true; } public function can_follow_user( \WP_REST_Request $request ): bool|\WP_Error { if ( ! is_user_logged_in() ) { return false; } $target = absint( $request['id'] ); if ( $target === get_current_user_id() ) { return new \WP_Error( 'arshid6social_self_follow', __( 'You cannot follow yourself.', '6arshid-social-community' ), array( 'status' => 400 ) ); } if ( ! get_userdata( $target ) ) { return true; // Target user not found; let the callback return a 404. } return true; } public function get_status( $request ): \WP_REST_Response|\WP_Error { $component = ARSHID6SOCIAL()->component( 'friends' ); $target = absint( $request['id'] ); $current = get_current_user_id(); // Object-level access check: this endpoint always returns the current user's own // relationship with the target (current user is always party 1, so no IDOR risk). // We additionally deny when the target has explicitly blocked the requester and // they are not friends — a blocked user must not probe the other user's data. // Note: member profiles in this plugin are public by default; no profile-visibility // meta exists in the data model, so friendship is the meaningful access gate. if ( $current !== $target && $component ) { $friendship_status = $component->get_friendship_status( $current, $target ); if ( 'friends' !== $friendship_status && $component->is_blocked( $target, $current ) ) { return new \WP_Error( 'rest_forbidden', __( 'You do not have permission to view this.', '6arshid-social-community' ), array( 'status' => 403 ) ); } } $status = $component ? $component->get_friendship_status( $current, $target ) : 'not_friends'; $following = $component ? $component->is_following( $current, $target ) : false; $blocked = $component ? $component->is_blocked( $current, $target ) : false; return rest_ensure_response( compact( 'status', 'following', 'blocked' ) ); } public function send_request( $request ): \WP_REST_Response|\WP_Error { $component = ARSHID6SOCIAL()->component( 'friends' ); $result = $component->send_request( get_current_user_id(), (int) $request['id'] ); if ( true !== $result ) { return new \WP_Error( 'arshid6social_friend_error', $result, array( 'status' => 400 ) ); } return rest_ensure_response( array( 'status' => 'pending_sent' ) ); } public function accept_request( $request ): \WP_REST_Response|\WP_Error { $component = ARSHID6SOCIAL()->component( 'friends' ); if ( ! $component || ! $component->accept_request( get_current_user_id(), (int) $request['id'] ) ) { return new \WP_Error( 'arshid6social_no_pending_request', __( 'No pending friend request from this user.', '6arshid-social-community' ), array( 'status' => 409 ) ); } return rest_ensure_response( array( 'status' => 'friends' ) ); } public function remove( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'friends' ); $component->remove_friend( get_current_user_id(), (int) $request['id'] ); return rest_ensure_response( array( 'status' => 'not_friends' ) ); } public function follow( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'friends' ); $component->follow( get_current_user_id(), (int) $request['id'] ); return rest_ensure_response( array( 'following' => true ) ); } public function unfollow( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'friends' ); $component->unfollow( get_current_user_id(), (int) $request['id'] ); return rest_ensure_response( array( 'following' => false ) ); } public function block( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'friends' ); $component->block( get_current_user_id(), (int) $request['id'] ); return rest_ensure_response( array( 'blocked' => true ) ); } public function unblock( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'friends' ); $component->unblock( get_current_user_id(), (int) $request['id'] ); return rest_ensure_response( array( 'blocked' => false ) ); } public function suggestions( $request ): \WP_REST_Response { $component = ARSHID6SOCIAL()->component( 'friends' ); $members_comp = ARSHID6SOCIAL()->component( 'members' ); $suggestions = $component->get_suggestions( get_current_user_id() ); $data = array(); foreach ( $suggestions as $uid ) { $user = get_userdata( $uid ); if ( $user && $members_comp ) { $data[] = $members_comp->format_member( $user, get_current_user_id() ); } } return rest_ensure_response( $data ); } }