| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Routes\Template; |
| 4 |
|
| 5 |
use IvyForms\Vendor\DI\Container; |
| 6 |
use IvyForms\Vendor\DI\DependencyException; |
| 7 |
use IvyForms\Vendor\DI\NotFoundException; |
| 8 |
use IvyForms\Controllers\Form\AddFormController; |
| 9 |
use IvyForms\Controllers\Template\GetTemplatesController; |
| 10 |
use IvyForms\Controllers\Template\GetTemplateController; |
| 11 |
use IvyForms\Services\Permissions\RoutePermissionService; |
| 12 |
use WP_REST_Server; |
| 13 |
|
| 14 |
class Template |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @throws DependencyException |
| 18 |
* @throws NotFoundException |
| 19 |
*/ |
| 20 |
public static function registerRoutes(Container $container, string $routeNamespace): void |
| 21 |
{ |
| 22 |
$routePermissionService = $container->get(RoutePermissionService::class); |
| 23 |
// Get all templates |
| 24 |
register_rest_route( |
| 25 |
$routeNamespace, |
| 26 |
'/templates', |
| 27 |
[ |
| 28 |
'methods' => WP_REST_Server::READABLE, |
| 29 |
'callback' => $container->get(GetTemplatesController::class), |
| 30 |
'permission_callback' => static function () use ($routePermissionService) { |
| 31 |
return $routePermissionService->canViewForms(); |
| 32 |
} |
| 33 |
] |
| 34 |
); |
| 35 |
|
| 36 |
// Get specific template by ID |
| 37 |
register_rest_route( |
| 38 |
$routeNamespace, |
| 39 |
'/template/(?P<id>[a-zA-Z0-9_-]+)', |
| 40 |
[ |
| 41 |
'methods' => WP_REST_Server::READABLE, |
| 42 |
'callback' => $container->get(GetTemplateController::class), |
| 43 |
'permission_callback' => static function () use ($routePermissionService) { |
| 44 |
return $routePermissionService->canViewForms(); |
| 45 |
} |
| 46 |
] |
| 47 |
); |
| 48 |
|
| 49 |
// Create form from template |
| 50 |
register_rest_route( |
| 51 |
$routeNamespace, |
| 52 |
'/template/create', |
| 53 |
[ |
| 54 |
'methods' => WP_REST_Server::CREATABLE, |
| 55 |
'callback' => $container->get(AddFormController::class), |
| 56 |
'permission_callback' => static function () use ($routePermissionService) { |
| 57 |
return $routePermissionService->canEditForms(); |
| 58 |
} |
| 59 |
] |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|