namespace, '/leaderboard', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'leaderboard' ), 'permission_callback' => array( $this, 'permission_leaderboard' ), 'args' => array( 'period' => array( 'type' => 'string', 'enum' => array( 'week', 'month', 'all' ), 'default' => 'week', ), 'limit' => array( 'type' => 'integer', 'default' => 50, 'minimum' => 1, 'maximum' => 100, ), ), ), ) ); register_rest_route( $this->namespace, '/me/points', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'history' ), 'permission_callback' => array( $this, 'permission_points' ), 'args' => $this->paging_args( 50 ), ), ) ); } /** * Logged-in members while the leaderboard is on. * * @return true|WP_Error */ public function permission_leaderboard() { if ( ! Gamification::leaderboard_enabled() ) { return $this->off(); } return $this->permission_logged_in(); } /** * Logged-in members while points are on. * * @return true|WP_Error */ public function permission_points() { if ( ! Gamification::enabled() ) { return $this->off(); } return $this->permission_logged_in(); } /** * Returns the leaderboard. * * @param WP_REST_Request $request Request. * @return \WP_REST_Response */ public function leaderboard( $request ) { return rest_ensure_response( Gamification::leaderboard( $request['period'], get_current_user_id(), (int) $request['limit'] ) ); } /** * Returns the viewer's point changes. * * @param WP_REST_Request $request Request. * @return \WP_REST_Response */ public function history( $request ) { $result = Points::history( get_current_user_id(), $request['page'], $request['per_page'] ); return $this->paged_response( $result['items'], $result['total'], $request['per_page'] ); } /** * Error used while points are switched off. * * @return WP_Error */ private function off() { return new WP_Error( 'spacebay_points_off', __( 'Points and badges are switched off in this community.', 'spacebay' ), array( 'status' => 404 ) ); } }