diff --git a/lib/API/Route.php b/lib/API/Route.php index 1befefb..a56568a 100644 --- a/lib/API/Route.php +++ b/lib/API/Route.php @@ -35,6 +35,14 @@ class Route { */ private $callback; + /** + * Callback to get arguments. + * + * @since 0.0.0 + * @var array + */ + private $args = []; + /** * Route configuration. * @@ -43,11 +51,12 @@ class Route { * @param string $method HTTP Method. * @param array $callback Callback that responsed to endpoint. */ - public function __construct( string $endpoint, string $method, array $callback ) { + public function __construct( string $endpoint, string $method, array $callback, array $args = [] ) { $this->endpoint = $endpoint; $this->method = $method; $this->callback = $callback; + $this->args = $args; } /** @@ -79,4 +88,14 @@ public function method() : string { public function callback() : array { return $this->callback; } + + /** + * Returns the arguments. + * + * @since 0.0.0 + * @return array + */ + public function args() : array { + return $this->args; + } } diff --git a/lib/API/Routes.php b/lib/API/Routes.php index 9b633b6..80aca12 100644 --- a/lib/API/Routes.php +++ b/lib/API/Routes.php @@ -62,8 +62,9 @@ public function __construct( string $namespace, $permission = null ) { * @param string $method HTTP method. * @param object $class Object that will handle the callback. * @param string $callback Custom method to answer to the endpoint. + * @param array $args register_route arguments */ - public function add( string $endpoint, string $method, $class, $callback = null ) { + public function add( string $endpoint, string $method, $class, $callback = null, array $args = [] ) { $method = strtolower( $method ); @@ -91,7 +92,7 @@ public function add( string $endpoint, string $method, $class, $callback = null return; } - $this->routes[] = new Route( $endpoint, $method, [ $class, $callback ] ); + $this->routes[] = new Route( $endpoint, $method, [ $class, $callback ], $args ); } /** @@ -114,10 +115,13 @@ public function register() { foreach ( $this->routes as $route ) { + $args = null; + register_rest_route( $this->namespace, $route->endpoint(), [ 'methods' => $route->method(), 'callback' => $route->callback(), 'permission_callback' => $permission, + 'args' => $route->args(), ] ); } }