| @@ -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,9 +32,25 @@ | ||
| 30 | 32 | * @throws NotFoundException |
| 31 | 33 | */ |
| 32 | 34 | public static function registerRoutes(Container $container, string $routeNamespace): void |
| 33 | 35 | { |
| 34 | - // TODO - Add IvyForms capability - Admin only for now | |
| 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 | |
| 35 | 53 | register_rest_route( |
| 36 | 54 | $routeNamespace, |
| 37 | 55 | '/form/(?P<id>\d+)', |
| 38 | 56 | [ |
| @@ -37,14 +55,26 @@ | ||
| 37 | 55 | '/form/(?P<id>\d+)', |
| 38 | 56 | [ |
| 39 | 57 | 'methods' => WP_REST_Server::READABLE, |
| 40 | 58 | 'callback' => $container->get(GetFormController::class), |
| 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,16 +79,81 @@ | ||
| 49 | 79 | '/form', |
| 50 | 80 | [ |
| 51 | 81 | 'methods' => WP_REST_Server::READABLE, |
| 52 | 82 | 'callback' => $container->get(GetFormsController::class), |
| 53 | - 'permission_callback' => function () { | |
| 54 | - return current_user_can('manage_options'); | |
| 83 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 84 | + return $routePermissionService->canViewFormsInLists(); | |
| 55 | 85 | } |
| 56 | 86 | ] |
| 57 | 87 | ); |
| 58 | 88 | |
| 89 | + // Add new form | |
| 59 | 90 | register_rest_route( |
| 60 | 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, | |
| 61 | 156 | '/forms/search', |
| 62 | 157 | [ |
| 63 | 158 | 'methods' => WP_REST_Server::READABLE, |
| 64 | 159 | 'callback' => $container->get(SearchFormsController::class), |
| @@ -69,106 +164,133 @@ | ||
| 69 | 164 | 'orderBy' => ['type' => 'string'], |
| 70 | 165 | 'order' => ['type' => 'string'], |
| 71 | 166 | 'shouldGetCount' => ['type' => 'boolean'], |
| 72 | 167 | ], |
| 73 | - 'permission_callback' => function () { | |
| 74 | - return current_user_can('manage_options'); | |
| 168 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 169 | + return $routePermissionService->canViewFormsInLists(); | |
| 75 | 170 | } |
| 76 | 171 | ] |
| 77 | 172 | ); |
| 78 | 173 | |
| 174 | + // Update form starred status | |
| 79 | 175 | register_rest_route( |
| 80 | 176 | $routeNamespace, |
| 81 | - '/form/add/', | |
| 177 | + '/form/update/starred/(?P<id>\d+)', | |
| 82 | 178 | [ |
| 83 | 179 | 'methods' => WP_REST_Server::CREATABLE, |
| 84 | - 'callback' => $container->get(AddFormController::class), | |
| 85 | - 'permission_callback' => function () { | |
| 86 | - return current_user_can('manage_options'); | |
| 180 | + 'callback' => $container->get(UpdateFormStarredController::class), | |
| 181 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 182 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 183 | + return $routePermissionService->canEditForms($formId); | |
| 87 | 184 | } |
| 88 | 185 | ] |
| 89 | 186 | ); |
| 90 | 187 | |
| 188 | + // Update form status | |
| 91 | 189 | register_rest_route( |
| 92 | 190 | $routeNamespace, |
| 93 | - '/form/update/(?P<id>\d+)', | |
| 191 | + '/form/update/status/(?P<id>\d+)', | |
| 94 | 192 | [ |
| 95 | 193 | 'methods' => WP_REST_Server::CREATABLE, |
| 96 | - 'callback' => $container->get(UpdateFormController::class), | |
| 97 | - 'permission_callback' => function () { | |
| 98 | - return current_user_can('manage_options'); | |
| 194 | + 'callback' => $container->get(UpdateFormStatusController::class), | |
| 195 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 196 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 197 | + return $routePermissionService->canEditForms($formId); | |
| 99 | 198 | } |
| 100 | 199 | ] |
| 101 | 200 | ); |
| 102 | 201 | |
| 202 | + // Duplicate form | |
| 103 | 203 | register_rest_route( |
| 104 | 204 | $routeNamespace, |
| 105 | - '/form/update/starred/(?P<id>\d+)', | |
| 205 | + '/form/duplicate/(?P<id>\d+)', | |
| 106 | 206 | [ |
| 107 | 207 | 'methods' => WP_REST_Server::CREATABLE, |
| 108 | - 'callback' => $container->get(UpdateFormStarredController::class), | |
| 109 | - 'permission_callback' => function () { | |
| 110 | - 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); | |
| 111 | 212 | } |
| 112 | - ] | |
| 213 | + ] | |
| 113 | 214 | ); |
| 114 | 215 | |
| 216 | + // Bulk update forms | |
| 115 | 217 | register_rest_route( |
| 116 | 218 | $routeNamespace, |
| 117 | - '/form/update/status/(?P<id>\d+)', | |
| 219 | + '/forms/batch-actions', | |
| 118 | 220 | [ |
| 119 | 221 | 'methods' => WP_REST_Server::CREATABLE, |
| 120 | - 'callback' => $container->get(UpdateFormStatusController::class), | |
| 121 | - 'permission_callback' => function () { | |
| 122 | - return current_user_can('manage_options'); | |
| 222 | + 'callback' => [$container->get(BulkUpdateFormController::class), 'handle'], | |
| 223 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 224 | + return $routePermissionService->canEditForms(); | |
| 123 | 225 | } |
| 124 | - ] | |
| 226 | + ] | |
| 125 | 227 | ); |
| 228 | + } | |
| 126 | 229 | |
| 127 | - | |
| 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) | |
| 128 | 239 | register_rest_route( |
| 129 | 240 | $routeNamespace, |
| 130 | - '/form/delete/(?P<id>\d+)', | |
| 241 | + '/form/submission/', | |
| 131 | 242 | [ |
| 132 | 243 | 'methods' => WP_REST_Server::CREATABLE, |
| 133 | - 'callback' => $container->get(DeleteFormController::class), | |
| 134 | - 'permission_callback' => function () { | |
| 135 | - return current_user_can('manage_options'); | |
| 136 | - } | |
| 137 | - ] | |
| 244 | + 'callback' => $container->get(FormSubmissionController::class), | |
| 245 | + // TODO - Add IvyForms capability - Public use | |
| 246 | + 'permission_callback' => '__return_true' | |
| 247 | + ] | |
| 138 | 248 | ); |
| 139 | 249 | |
| 140 | 250 | register_rest_route( |
| 141 | 251 | $routeNamespace, |
| 142 | - '/forms/delete', | |
| 252 | + '/form/validate_before_submit/', | |
| 143 | 253 | [ |
| 144 | 254 | 'methods' => WP_REST_Server::CREATABLE, |
| 145 | - 'callback' => $container->get(DeleteFormsController::class), | |
| 146 | - 'permission_callback' => function () { | |
| 147 | - return current_user_can('manage_options'); | |
| 148 | - } | |
| 255 | + 'callback' => $container->get(ValidateBeforeSubmitController::class), | |
| 256 | + 'permission_callback' => '__return_true', | |
| 149 | 257 | ] |
| 150 | 258 | ); |
| 259 | + } | |
| 151 | 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 | |
| 152 | 272 | register_rest_route( |
| 153 | 273 | $routeNamespace, |
| 154 | - '/form/submission/', | |
| 274 | + '/forms/export', | |
| 155 | 275 | [ |
| 156 | 276 | 'methods' => WP_REST_Server::CREATABLE, |
| 157 | - 'callback' => $container->get(FormSubmissionController::class), | |
| 158 | - // TODO - Add IvyForms capability - Public use | |
| 159 | - 'permission_callback' => '__return_true' | |
| 277 | + 'callback' => $container->get(ExportFormController::class), | |
| 278 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 279 | + return $routePermissionService->canExportForms(); | |
| 280 | + } | |
| 160 | 281 | ] |
| 161 | 282 | ); |
| 162 | 283 | |
| 284 | + // Import forms | |
| 163 | 285 | register_rest_route( |
| 164 | 286 | $routeNamespace, |
| 165 | - '/form/duplicate/(?P<id>\d+)', | |
| 287 | + '/forms/import', | |
| 166 | 288 | [ |
| 167 | 289 | 'methods' => WP_REST_Server::CREATABLE, |
| 168 | - 'callback' => $container->get(DuplicateFormController::class), | |
| 169 | - 'permission_callback' => function () { | |
| 170 | - return current_user_can('manage_options'); | |
| 290 | + 'callback' => $container->get(ImportFormController::class), | |
| 291 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 292 | + return $routePermissionService->canEditForms(); | |
| 171 | 293 | } |
| 172 | 294 | ] |
| 173 | 295 | ); |
| 174 | 296 | } |