namespace = strtolower($namespace) . '/' .strtolower( $version ) . self::FEATURE_ROUTE . '/' . strtolower( $featureVersion ); $reflect = new ReflectionClass($this); if (!$reflect->isFinal()) { wp_die('Subclasses of Rsssl_Abstract_Controller must be declared as final.'); } } /** * Abstract method to register API routes. * Must be implemented by subclasses. * * @return void */ abstract public function register_api_routes(): void; /** * Registers a REST API route. * * @param string $namespace The namespace for the route. * @param string $method The HTTP method for the route (e.g., 'POST', 'GET'). * @param string $route The route endpoint. * @param callable $callback The callback function to handle the request. * @param callable|null $permission_callback The permission callback function or true to allow all requests. * @param array $args Optional. The arguments for the route. * * @return void * @throws Exception */ protected function route( string $namespace, string $method, string $route, callable $callback, ?callable $permission_callback = null, array $args = array() ): void { if ($permission_callback === null) { $permission_callback = array($this, 'permission_check'); } register_rest_route($namespace, $route, array( 'methods' => $method, 'permission_callback' => $permission_callback, 'callback' => $callback, 'args' => $args, )); } /** * Checks if the user is logged in and has the correct nonce. * * * @return bool */ public function permission_check(WP_REST_Request $request):bool { $parameters = new Rsssl_Request_Parameters( $request ); return $this->verify_hashed_user_id( $parameters->user_id, $parameters->login_nonce ); } /** * Verifies a login nonce, gets user by the user id, and returns an error response if any steps fail. * * @throws Exception */ public function check_login_and_get_user( int $user_id, string $login_nonce ): WP_User { if ( ! Rsssl_Two_Fa_Authentication::verify_login_nonce( $user_id, $login_nonce ) ) { // We throw an error wp_die(); } /** * Get the user by the user ID. * * @var WP_User $user */ $user = get_user_by('id', $user_id); if (!$user) { throw new Exception('User not found'); } return $user; } }