| @@ -1,17 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Routes\Form; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | -use DI\Container; | |
| 11 | -use DI\DependencyException; | |
| 12 | -use DI\NotFoundException; | |
| 5 | +use IvyForms\Vendor\DI\Container; | |
| 6 | +use IvyForms\Vendor\DI\DependencyException; | |
| 7 | +use IvyForms\Vendor\DI\NotFoundException; | |
| 13 | 8 | use IvyForms\Controllers\Form\AddFormController; |
| 9 | +use IvyForms\Controllers\Form\BulkUpdateFormController; | |
| 14 | 10 | use IvyForms\Controllers\Form\DeleteFormController; |
| 15 | 11 | use IvyForms\Controllers\Form\FormSubmissionController; |
| 16 | 12 | use IvyForms\Controllers\Form\DuplicateFormController; |
| 17 | 13 | use IvyForms\Controllers\Form\GetFormController; |
| @@ -20,8 +16,14 @@ | ||
| 20 | 16 | use IvyForms\Controllers\Form\UpdateFormController; |
| 21 | 17 | use IvyForms\Controllers\Form\DeleteFormsController; |
| 22 | 18 | use IvyForms\Controllers\Form\UpdateFormStarredController; |
| 23 | 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; | |
| 24 | 26 | use WP_REST_Server; |
| 25 | 27 | |
| 26 | 28 | class Form |
| 27 | 29 | { |
| @@ -30,8 +32,25 @@ | ||
| 30 | 32 | * @throws NotFoundException |
| 31 | 33 | */ |
| 32 | 34 | public static function registerRoutes(Container $container, string $routeNamespace): void |
| 33 | 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 | |
| 34 | 53 | register_rest_route( |
| 35 | 54 | $routeNamespace, |
| 36 | 55 | '/form/(?P<id>\d+)', |
| 37 | 56 | [ |
| @@ -36,15 +55,26 @@ | ||
| 36 | 55 | '/form/(?P<id>\d+)', |
| 37 | 56 | [ |
| 38 | 57 | 'methods' => WP_REST_Server::READABLE, |
| 39 | 58 | 'callback' => $container->get(GetFormController::class), |
| 40 | - // TODO - Add IvyForms capability - Admin only for now | |
| 41 | - 'permission_callback' => function () { | |
| 42 | - return current_user_can('manage_options'); | |
| 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); | |
| 43 | 72 | } |
| 44 | 73 | ] |
| 45 | 74 | ); |
| 46 | 75 | |
| 76 | + // Get all forms | |
| 47 | 77 | register_rest_route( |
| 48 | 78 | $routeNamespace, |
| 49 | 79 | '/form', |
| 50 | 80 | [ |
| @@ -49,74 +79,114 @@ | ||
| 49 | 79 | '/form', |
| 50 | 80 | [ |
| 51 | 81 | 'methods' => WP_REST_Server::READABLE, |
| 52 | 82 | 'callback' => $container->get(GetFormsController::class), |
| 53 | - // TODO - Add IvyForms capability - Admin only for now | |
| 54 | - 'permission_callback' => function () { | |
| 55 | - return current_user_can('manage_options'); | |
| 83 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 84 | + return $routePermissionService->canViewFormsInLists(); | |
| 56 | 85 | } |
| 57 | 86 | ] |
| 58 | 87 | ); |
| 59 | 88 | |
| 89 | + // Add new form | |
| 60 | 90 | register_rest_route( |
| 61 | 91 | $routeNamespace, |
| 62 | - '/forms/search', | |
| 92 | + '/form/add/', | |
| 63 | 93 | [ |
| 64 | - 'methods' => WP_REST_Server::READABLE, | |
| 65 | - 'callback' => $container->get(SearchFormsController::class), | |
| 66 | - 'args' => [ | |
| 67 | - 'page' => ['type' => 'integer'], | |
| 68 | - 'perPage' => ['type' => 'integer'], | |
| 69 | - 'search' => ['type' => 'string'], | |
| 70 | - 'orderBy' => ['type' => 'string'], | |
| 71 | - 'order' => ['type' => 'string'], | |
| 72 | - ], | |
| 73 | - // TODO - Add IvyForms capability - Admin only for now | |
| 74 | - 'permission_callback' => function () { | |
| 75 | - return current_user_can('manage_options'); | |
| 94 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 95 | + 'callback' => $container->get(AddFormController::class), | |
| 96 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 97 | + return $routePermissionService->canEditForms(); | |
| 76 | 98 | } |
| 77 | - ] | |
| 99 | + ] | |
| 78 | 100 | ); |
| 79 | 101 | |
| 102 | + // Update form | |
| 80 | 103 | register_rest_route( |
| 81 | 104 | $routeNamespace, |
| 82 | - '/form/add/', | |
| 105 | + '/form/update/(?P<id>\d+)', | |
| 83 | 106 | [ |
| 84 | 107 | 'methods' => WP_REST_Server::CREATABLE, |
| 85 | - 'callback' => $container->get(AddFormController::class), | |
| 86 | - // TODO - Add IvyForms capability - Admin only for now | |
| 87 | - 'permission_callback' => function () { | |
| 88 | - return current_user_can('manage_options'); | |
| 108 | + 'callback' => $container->get(UpdateFormController::class), | |
| 109 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 110 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 111 | + return $routePermissionService->canEditForms($formId); | |
| 89 | 112 | } |
| 90 | 113 | ] |
| 91 | 114 | ); |
| 92 | 115 | |
| 116 | + // Delete single form | |
| 93 | 117 | register_rest_route( |
| 94 | 118 | $routeNamespace, |
| 95 | - '/form/update/(?P<id>\d+)', | |
| 119 | + '/form/delete/(?P<id>\d+)', | |
| 96 | 120 | [ |
| 97 | 121 | 'methods' => WP_REST_Server::CREATABLE, |
| 98 | - 'callback' => $container->get(UpdateFormController::class), | |
| 99 | - // TODO - Add IvyForms capability - Admin only for now | |
| 100 | - 'permission_callback' => function () { | |
| 101 | - return current_user_can('manage_options'); | |
| 122 | + 'callback' => $container->get(DeleteFormController::class), | |
| 123 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 124 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 125 | + return $routePermissionService->canDeleteForms($formId); | |
| 102 | 126 | } |
| 103 | 127 | ] |
| 104 | 128 | ); |
| 105 | 129 | |
| 130 | + // Delete multiple forms | |
| 106 | 131 | register_rest_route( |
| 107 | 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, | |
| 108 | 177 | '/form/update/starred/(?P<id>\d+)', |
| 109 | 178 | [ |
| 110 | 179 | 'methods' => WP_REST_Server::CREATABLE, |
| 111 | 180 | 'callback' => $container->get(UpdateFormStarredController::class), |
| 112 | - // TODO - Add IvyForms capability - Admin only for now | |
| 113 | - 'permission_callback' => function () { | |
| 114 | - return current_user_can('manage_options'); | |
| 181 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 182 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 183 | + return $routePermissionService->canEditForms($formId); | |
| 115 | 184 | } |
| 116 | 185 | ] |
| 117 | 186 | ); |
| 118 | 187 | |
| 188 | + // Update form status | |
| 119 | 189 | register_rest_route( |
| 120 | 190 | $routeNamespace, |
| 121 | 191 | '/form/update/status/(?P<id>\d+)', |
| 122 | 192 | [ |
| @@ -121,42 +191,52 @@ | ||
| 121 | 191 | '/form/update/status/(?P<id>\d+)', |
| 122 | 192 | [ |
| 123 | 193 | 'methods' => WP_REST_Server::CREATABLE, |
| 124 | 194 | 'callback' => $container->get(UpdateFormStatusController::class), |
| 125 | - // TODO - Add IvyForms capability - Admin only for now | |
| 126 | - 'permission_callback' => function () { | |
| 127 | - return current_user_can('manage_options'); | |
| 195 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 196 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 197 | + return $routePermissionService->canEditForms($formId); | |
| 128 | 198 | } |
| 129 | 199 | ] |
| 130 | 200 | ); |
| 131 | 201 | |
| 132 | - | |
| 202 | + // Duplicate form | |
| 133 | 203 | register_rest_route( |
| 134 | 204 | $routeNamespace, |
| 135 | - '/form/delete/(?P<id>\d+)', | |
| 205 | + '/form/duplicate/(?P<id>\d+)', | |
| 136 | 206 | [ |
| 137 | 207 | 'methods' => WP_REST_Server::CREATABLE, |
| 138 | - 'callback' => $container->get(DeleteFormController::class), | |
| 139 | - // TODO - Add IvyForms capability - Admin only for now | |
| 140 | - 'permission_callback' => function () { | |
| 141 | - return current_user_can('manage_options'); | |
| 208 | + 'callback' => $container->get(DuplicateFormController::class), | |
| 209 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 210 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 211 | + return $routePermissionService->canEditForms($formId); | |
| 142 | 212 | } |
| 143 | - ] | |
| 213 | + ] | |
| 144 | 214 | ); |
| 145 | 215 | |
| 216 | + // Bulk update forms | |
| 146 | 217 | register_rest_route( |
| 147 | 218 | $routeNamespace, |
| 148 | - '/forms/delete', | |
| 219 | + '/forms/batch-actions', | |
| 149 | 220 | [ |
| 150 | 221 | 'methods' => WP_REST_Server::CREATABLE, |
| 151 | - 'callback' => $container->get(DeleteFormsController::class), | |
| 152 | - // TODO - Add IvyForms capability - Admin only for now | |
| 153 | - 'permission_callback' => function () { | |
| 154 | - return current_user_can('manage_options'); | |
| 222 | + 'callback' => [$container->get(BulkUpdateFormController::class), 'handle'], | |
| 223 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 224 | + return $routePermissionService->canEditForms(); | |
| 155 | 225 | } |
| 156 | 226 | ] |
| 157 | 227 | ); |
| 228 | + } | |
| 158 | 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) | |
| 159 | 239 | register_rest_route( |
| 160 | 240 | $routeNamespace, |
| 161 | 241 | '/form/submission/', |
| 162 | 242 | [ |
| @@ -168,15 +248,49 @@ | ||
| 168 | 248 | ); |
| 169 | 249 | |
| 170 | 250 | register_rest_route( |
| 171 | 251 | $routeNamespace, |
| 172 | - '/form/duplicate/(?P<id>\d+)', | |
| 252 | + '/form/validate_before_submit/', | |
| 173 | 253 | [ |
| 174 | 254 | 'methods' => WP_REST_Server::CREATABLE, |
| 175 | - 'callback' => $container->get(DuplicateFormController::class), | |
| 176 | - // TODO - Add IvyForms capability - Admin only for now | |
| 177 | - 'permission_callback' => function () { | |
| 178 | - return current_user_can('manage_options'); | |
| 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(); | |
| 179 | 293 | } |
| 180 | 294 | ] |
| 181 | 295 | ); |
| 182 | 296 | } |