permission_logged_in(); } /** * Requires a logged-in user. * * @return true|WP_Error */ public function permission_logged_in() { if ( is_user_logged_in() ) { return $this->permission_active_account(); } return new WP_Error( 'spacebay_rest_not_logged_in', __( 'Please log in to continue.', 'spacebay' ), array( 'status' => 401 ) ); } /** * Blocks members who wait for approval or are suspended. * * @return true|WP_Error */ protected function permission_active_account() { $user_id = get_current_user_id(); if ( ! \SpaceBay\Spaces\SpaceAccess::is_restricted( $user_id ) ) { return true; } if ( is_multisite() && ! is_user_member_of_blog( $user_id ) ) { return new WP_Error( 'spacebay_not_site_member', __( 'Your account is not a member of this site.', 'spacebay' ), array( 'status' => 403 ) ); } $profile = \SpaceBay\Members\Profiles::get( $user_id ); if ( $profile && 'blocked' === $profile['status'] ) { return new WP_Error( 'spacebay_account_blocked', __( 'Your account has been suspended by a moderator.', 'spacebay' ), array( 'status' => 403 ) ); } return new WP_Error( 'spacebay_account_pending', __( 'Your account is waiting for approval by a moderator.', 'spacebay' ), array( 'status' => 403 ) ); } /** * Requires the manage capability. * * @return true|WP_Error */ public function permission_manage() { if ( current_user_can( Capabilities::MANAGE ) ) { return true; } return $this->forbidden(); } /** * Requires the moderate capability. * * @return true|WP_Error */ public function permission_moderate() { if ( current_user_can( Capabilities::MODERATE ) || current_user_can( Capabilities::MANAGE ) ) { return true; } return $this->forbidden(); } /** * Returns a space the current user can see, or a 404 error. * * Spaces the user may not see answer "not found" so secret spaces stay hidden. * * @param int $id Space ID. * @return array|WP_Error */ protected function visible_space( $id ) { $space = \SpaceBay\Spaces\SpaceRepository::find( $id ); if ( ! $space || 'space' !== $space['type'] || ! \SpaceBay\Spaces\SpaceAccess::can_see( $space, get_current_user_id() ) ) { return new WP_Error( 'spacebay_space_not_found', __( 'Space not found.', 'spacebay' ), array( 'status' => 404 ) ); } return $space; } /** * Builds a list response with WordPress pagination headers. * * @param array $items Items. * @param int $total Total items. * @param int $per_page Items per page. * @return \WP_REST_Response */ protected function paged_response( array $items, $total, $per_page ) { $response = rest_ensure_response( $items ); $response->header( 'X-WP-Total', (string) (int) $total ); $response->header( 'X-WP-TotalPages', (string) (int) ceil( $total / max( 1, $per_page ) ) ); return $response; } /** * Common arguments for paged lists. * * @param int $max_per_page Largest allowed page size. * @return array */ protected function paging_args( $max_per_page = 50 ) { return array( 'page' => array( 'type' => 'integer', 'default' => 1, 'minimum' => 1, ), 'per_page' => array( 'type' => 'integer', 'default' => 20, 'minimum' => 1, 'maximum' => $max_per_page, ), 'search' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), ); } /** * Returns a standard "forbidden" error. * * @return WP_Error */ protected function forbidden() { return new WP_Error( 'spacebay_rest_forbidden', __( 'Sorry, you are not allowed to do that.', 'spacebay' ), array( 'status' => is_user_logged_in() ? 403 : 401 ) ); } }