| @@ -1,0 +1,297 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Routes\Form; | |
| 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\Form\BulkUpdateFormController; | |
| 10 | +use IvyForms\Controllers\Form\DeleteFormController; | |
| 11 | +use IvyForms\Controllers\Form\FormSubmissionController; | |
| 12 | +use IvyForms\Controllers\Form\DuplicateFormController; | |
| 13 | +use IvyForms\Controllers\Form\GetFormController; | |
| 14 | +use IvyForms\Controllers\Form\GetFormsController; | |
| 15 | +use IvyForms\Controllers\Form\SearchFormsController; | |
| 16 | +use IvyForms\Controllers\Form\UpdateFormController; | |
| 17 | +use IvyForms\Controllers\Form\DeleteFormsController; | |
| 18 | +use IvyForms\Controllers\Form\UpdateFormStarredController; | |
| 19 | +use IvyForms\Controllers\Form\UpdateFormStatusController; | |
| 20 | +use IvyForms\Controllers\Form\ValidateBeforeSubmitController; | |
| 21 | +use IvyForms\Controllers\Form\ExportFormController; | |
| 22 | +use IvyForms\Controllers\Form\ImportFormController; | |
| 23 | +use IvyForms\Infrastructure\WP\REST\RestRouteParamReader; | |
| 24 | +use IvyForms\Services\Permissions\AdminFormListPermissionService; | |
| 25 | +use IvyForms\Services\Permissions\RoutePermissionService; | |
| 26 | +use WP_REST_Server; | |
| 27 | + | |
| 28 | +class Form | |
| 29 | +{ | |
| 30 | + /** | |
| 31 | + * @throws DependencyException | |
| 32 | + * @throws NotFoundException | |
| 33 | + */ | |
| 34 | + public static function registerRoutes(Container $container, string $routeNamespace): void | |
| 35 | + { | |
| 36 | + self::registerFormCrudRoutes($container, $routeNamespace); | |
| 37 | + self::registerFormManagementRoutes($container, $routeNamespace); | |
| 38 | + self::registerFormSubmissionRoutes($container, $routeNamespace); | |
| 39 | + self::registerFormImportExportRoutes($container, $routeNamespace); | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Register CRUD routes for forms (Create, Read, Update, Delete) | |
| 44 | + * | |
| 45 | + * @throws DependencyException | |
| 46 | + * @throws NotFoundException | |
| 47 | + */ | |
| 48 | + private static function registerFormCrudRoutes(Container $container, string $routeNamespace): void | |
| 49 | + { | |
| 50 | + $routePermissionService = $container->get(RoutePermissionService::class); | |
| 51 | + $adminFormListPermissionService = $container->get(AdminFormListPermissionService::class); | |
| 52 | + // Get single form | |
| 53 | + register_rest_route( | |
| 54 | + $routeNamespace, | |
| 55 | + '/form/(?P<id>\d+)', | |
| 56 | + [ | |
| 57 | + 'methods' => WP_REST_Server::READABLE, | |
| 58 | + 'callback' => $container->get(GetFormController::class), | |
| 59 | + 'permission_callback' => static function ($request) use ( | |
| 60 | + $adminFormListPermissionService, | |
| 61 | + $routePermissionService | |
| 62 | + ) { | |
| 63 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 64 | + if ($formId === null) { | |
| 65 | + return false; | |
| 66 | + } | |
| 67 | + | |
| 68 | + // Entry-only delegates need the form structure to render entry details, | |
| 69 | + // so allow either forms-list access or entries access for this form. | |
| 70 | + return $adminFormListPermissionService->canAccessFormInAdminLists($formId) | |
| 71 | + || $routePermissionService->canViewEntries($formId); | |
| 72 | + } | |
| 73 | + ] | |
| 74 | + ); | |
| 75 | + | |
| 76 | + // Get all forms | |
| 77 | + register_rest_route( | |
| 78 | + $routeNamespace, | |
| 79 | + '/form', | |
| 80 | + [ | |
| 81 | + 'methods' => WP_REST_Server::READABLE, | |
| 82 | + 'callback' => $container->get(GetFormsController::class), | |
| 83 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 84 | + return $routePermissionService->canViewFormsInLists(); | |
| 85 | + } | |
| 86 | + ] | |
| 87 | + ); | |
| 88 | + | |
| 89 | + // Add new form | |
| 90 | + register_rest_route( | |
| 91 | + $routeNamespace, | |
| 92 | + '/form/add/', | |
| 93 | + [ | |
| 94 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 95 | + 'callback' => $container->get(AddFormController::class), | |
| 96 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 97 | + return $routePermissionService->canEditForms(); | |
| 98 | + } | |
| 99 | + ] | |
| 100 | + ); | |
| 101 | + | |
| 102 | + // Update form | |
| 103 | + register_rest_route( | |
| 104 | + $routeNamespace, | |
| 105 | + '/form/update/(?P<id>\d+)', | |
| 106 | + [ | |
| 107 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 108 | + 'callback' => $container->get(UpdateFormController::class), | |
| 109 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 110 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 111 | + return $routePermissionService->canEditForms($formId); | |
| 112 | + } | |
| 113 | + ] | |
| 114 | + ); | |
| 115 | + | |
| 116 | + // Delete single form | |
| 117 | + register_rest_route( | |
| 118 | + $routeNamespace, | |
| 119 | + '/form/delete/(?P<id>\d+)', | |
| 120 | + [ | |
| 121 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 122 | + 'callback' => $container->get(DeleteFormController::class), | |
| 123 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 124 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 125 | + return $routePermissionService->canDeleteForms($formId); | |
| 126 | + } | |
| 127 | + ] | |
| 128 | + ); | |
| 129 | + | |
| 130 | + // Delete multiple forms | |
| 131 | + register_rest_route( | |
| 132 | + $routeNamespace, | |
| 133 | + '/forms/delete', | |
| 134 | + [ | |
| 135 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 136 | + 'callback' => $container->get(DeleteFormsController::class), | |
| 137 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 138 | + return $routePermissionService->canDeleteForms(); | |
| 139 | + } | |
| 140 | + ] | |
| 141 | + ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Register form management routes (search, status updates, etc.) | |
| 146 | + * | |
| 147 | + * @throws DependencyException | |
| 148 | + * @throws NotFoundException | |
| 149 | + */ | |
| 150 | + private static function registerFormManagementRoutes(Container $container, string $routeNamespace): void | |
| 151 | + { | |
| 152 | + $routePermissionService = $container->get(RoutePermissionService::class); | |
| 153 | + // Search forms | |
| 154 | + register_rest_route( | |
| 155 | + $routeNamespace, | |
| 156 | + '/forms/search', | |
| 157 | + [ | |
| 158 | + 'methods' => WP_REST_Server::READABLE, | |
| 159 | + 'callback' => $container->get(SearchFormsController::class), | |
| 160 | + 'args' => [ | |
| 161 | + 'page' => ['type' => 'integer'], | |
| 162 | + 'perPage' => ['type' => 'integer'], | |
| 163 | + 'search' => ['type' => 'string'], | |
| 164 | + 'orderBy' => ['type' => 'string'], | |
| 165 | + 'order' => ['type' => 'string'], | |
| 166 | + 'shouldGetCount' => ['type' => 'boolean'], | |
| 167 | + ], | |
| 168 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 169 | + return $routePermissionService->canViewFormsInLists(); | |
| 170 | + } | |
| 171 | + ] | |
| 172 | + ); | |
| 173 | + | |
| 174 | + // Update form starred status | |
| 175 | + register_rest_route( | |
| 176 | + $routeNamespace, | |
| 177 | + '/form/update/starred/(?P<id>\d+)', | |
| 178 | + [ | |
| 179 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 180 | + 'callback' => $container->get(UpdateFormStarredController::class), | |
| 181 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 182 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 183 | + return $routePermissionService->canEditForms($formId); | |
| 184 | + } | |
| 185 | + ] | |
| 186 | + ); | |
| 187 | + | |
| 188 | + // Update form status | |
| 189 | + register_rest_route( | |
| 190 | + $routeNamespace, | |
| 191 | + '/form/update/status/(?P<id>\d+)', | |
| 192 | + [ | |
| 193 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 194 | + 'callback' => $container->get(UpdateFormStatusController::class), | |
| 195 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 196 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 197 | + return $routePermissionService->canEditForms($formId); | |
| 198 | + } | |
| 199 | + ] | |
| 200 | + ); | |
| 201 | + | |
| 202 | + // Duplicate form | |
| 203 | + register_rest_route( | |
| 204 | + $routeNamespace, | |
| 205 | + '/form/duplicate/(?P<id>\d+)', | |
| 206 | + [ | |
| 207 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 208 | + 'callback' => $container->get(DuplicateFormController::class), | |
| 209 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 210 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 211 | + return $routePermissionService->canEditForms($formId); | |
| 212 | + } | |
| 213 | + ] | |
| 214 | + ); | |
| 215 | + | |
| 216 | + // Bulk update forms | |
| 217 | + register_rest_route( | |
| 218 | + $routeNamespace, | |
| 219 | + '/forms/batch-actions', | |
| 220 | + [ | |
| 221 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 222 | + 'callback' => [$container->get(BulkUpdateFormController::class), 'handle'], | |
| 223 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 224 | + return $routePermissionService->canEditForms(); | |
| 225 | + } | |
| 226 | + ] | |
| 227 | + ); | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * Register form submission routes (public-facing) | |
| 232 | + * | |
| 233 | + * @throws DependencyException | |
| 234 | + * @throws NotFoundException | |
| 235 | + */ | |
| 236 | + private static function registerFormSubmissionRoutes(Container $container, string $routeNamespace): void | |
| 237 | + { | |
| 238 | + // Form submission (public route) | |
| 239 | + register_rest_route( | |
| 240 | + $routeNamespace, | |
| 241 | + '/form/submission/', | |
| 242 | + [ | |
| 243 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 244 | + 'callback' => $container->get(FormSubmissionController::class), | |
| 245 | + // TODO - Add IvyForms capability - Public use | |
| 246 | + 'permission_callback' => '__return_true' | |
| 247 | + ] | |
| 248 | + ); | |
| 249 | + | |
| 250 | + register_rest_route( | |
| 251 | + $routeNamespace, | |
| 252 | + '/form/validate_before_submit/', | |
| 253 | + [ | |
| 254 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 255 | + 'callback' => $container->get(ValidateBeforeSubmitController::class), | |
| 256 | + 'permission_callback' => '__return_true', | |
| 257 | + ] | |
| 258 | + ); | |
| 259 | + } | |
| 260 | + | |
| 261 | + /** | |
| 262 | + * Register form import/export routes | |
| 263 | + * | |
| 264 | + * @throws DependencyException | |
| 265 | + * @throws NotFoundException | |
| 266 | + */ | |
| 267 | + private static function registerFormImportExportRoutes(Container $container, string $routeNamespace): void | |
| 268 | + { | |
| 269 | + $routePermissionService = $container->get(RoutePermissionService::class); | |
| 270 | + | |
| 271 | + // Export forms | |
| 272 | + register_rest_route( | |
| 273 | + $routeNamespace, | |
| 274 | + '/forms/export', | |
| 275 | + [ | |
| 276 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 277 | + 'callback' => $container->get(ExportFormController::class), | |
| 278 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 279 | + return $routePermissionService->canExportForms(); | |
| 280 | + } | |
| 281 | + ] | |
| 282 | + ); | |
| 283 | + | |
| 284 | + // Import forms | |
| 285 | + register_rest_route( | |
| 286 | + $routeNamespace, | |
| 287 | + '/forms/import', | |
| 288 | + [ | |
| 289 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 290 | + 'callback' => $container->get(ImportFormController::class), | |
| 291 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 292 | + return $routePermissionService->canEditForms(); | |
| 293 | + } | |
| 294 | + ] | |
| 295 | + ); | |
| 296 | + } | |
| 297 | +} | |