| @@ -1,0 +1,186 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Routes\Notification; | |
| 4 | + | |
| 5 | +use IvyForms\Vendor\DI\Container; | |
| 6 | +use IvyForms\Vendor\DI\DependencyException; | |
| 7 | +use IvyForms\Vendor\DI\NotFoundException; | |
| 8 | +use IvyForms\Controllers\Notification\AddNotificationController; | |
| 9 | +use IvyForms\Controllers\Notification\DeleteNotificationController; | |
| 10 | +use IvyForms\Controllers\Notification\DeleteNotificationsController; | |
| 11 | +use IvyForms\Controllers\Notification\DuplicateNotificationController; | |
| 12 | +use IvyForms\Controllers\Notification\GetNotificationController; | |
| 13 | +use IvyForms\Controllers\Notification\GetNotificationsController; | |
| 14 | +use IvyForms\Controllers\Notification\SearchNotificationsController; | |
| 15 | +use IvyForms\Controllers\Notification\UpdateNotificationController; | |
| 16 | +use IvyForms\Infrastructure\WP\REST\RestRouteParamReader; | |
| 17 | +use IvyForms\Services\Permissions\PermissionResourceResolver; | |
| 18 | +use IvyForms\Services\Permissions\RoutePermissionService; | |
| 19 | +use WP_REST_Server; | |
| 20 | + | |
| 21 | +class Notification | |
| 22 | +{ | |
| 23 | + /** | |
| 24 | + * @throws DependencyException | |
| 25 | + * @throws NotFoundException | |
| 26 | + */ | |
| 27 | + public static function registerRoutes(Container $container, string $routeNamespace): void | |
| 28 | + { | |
| 29 | + self::registerNotificationReadRoutes($container, $routeNamespace); | |
| 30 | + self::registerNotificationWriteRoutes($container, $routeNamespace); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @throws DependencyException | |
| 35 | + * @throws NotFoundException | |
| 36 | + */ | |
| 37 | + private static function registerNotificationReadRoutes(Container $container, string $routeNamespace): void | |
| 38 | + { | |
| 39 | + $routePermissionService = $container->get(RoutePermissionService::class); | |
| 40 | + $permissionResourceResolver = $container->get(PermissionResourceResolver::class); | |
| 41 | + | |
| 42 | + register_rest_route( | |
| 43 | + $routeNamespace, | |
| 44 | + '/notification/(?P<id>\d+)', | |
| 45 | + [ | |
| 46 | + 'methods' => WP_REST_Server::READABLE, | |
| 47 | + 'callback' => $container->get(GetNotificationController::class), | |
| 48 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 49 | + $permissionResourceResolver, | |
| 50 | + $routePermissionService | |
| 51 | + ), | |
| 52 | + ] | |
| 53 | + ); | |
| 54 | + | |
| 55 | + register_rest_route( | |
| 56 | + $routeNamespace, | |
| 57 | + '/notifications/(?P<id>\d+)', | |
| 58 | + [ | |
| 59 | + 'methods' => WP_REST_Server::READABLE, | |
| 60 | + 'callback' => $container->get(GetNotificationsController::class), | |
| 61 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 62 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 63 | + return $routePermissionService->canEditForms($formId); | |
| 64 | + }, | |
| 65 | + ] | |
| 66 | + ); | |
| 67 | + | |
| 68 | + register_rest_route( | |
| 69 | + $routeNamespace, | |
| 70 | + '/notifications/search', | |
| 71 | + [ | |
| 72 | + 'methods' => WP_REST_Server::READABLE, | |
| 73 | + 'callback' => $container->get(SearchNotificationsController::class), | |
| 74 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 75 | + return $routePermissionService->canEditForms(); | |
| 76 | + }, | |
| 77 | + ] | |
| 78 | + ); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * @throws DependencyException | |
| 83 | + * @throws NotFoundException | |
| 84 | + */ | |
| 85 | + private static function registerNotificationWriteRoutes(Container $container, string $routeNamespace): void | |
| 86 | + { | |
| 87 | + $routePermissionService = $container->get(RoutePermissionService::class); | |
| 88 | + $permissionResourceResolver = $container->get(PermissionResourceResolver::class); | |
| 89 | + | |
| 90 | + register_rest_route( | |
| 91 | + $routeNamespace, | |
| 92 | + '/notification/add/', | |
| 93 | + [ | |
| 94 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 95 | + 'callback' => $container->get(AddNotificationController::class), | |
| 96 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 97 | + $formId = RestRouteParamReader::getPositiveIntParam($request, 'formId'); | |
| 98 | + return $routePermissionService->canEditForms($formId); | |
| 99 | + }, | |
| 100 | + ] | |
| 101 | + ); | |
| 102 | + | |
| 103 | + register_rest_route( | |
| 104 | + $routeNamespace, | |
| 105 | + '/notification/update/(?P<id>\d+)', | |
| 106 | + [ | |
| 107 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 108 | + 'callback' => $container->get(UpdateNotificationController::class), | |
| 109 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 110 | + $permissionResourceResolver, | |
| 111 | + $routePermissionService | |
| 112 | + ), | |
| 113 | + ] | |
| 114 | + ); | |
| 115 | + | |
| 116 | + register_rest_route( | |
| 117 | + $routeNamespace, | |
| 118 | + '/notification/delete/(?P<id>\d+)', | |
| 119 | + [ | |
| 120 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 121 | + 'callback' => $container->get(DeleteNotificationController::class), | |
| 122 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 123 | + $permissionResourceResolver, | |
| 124 | + $routePermissionService | |
| 125 | + ), | |
| 126 | + ] | |
| 127 | + ); | |
| 128 | + | |
| 129 | + register_rest_route( | |
| 130 | + $routeNamespace, | |
| 131 | + '/notification/duplicate/(?P<id>\d+)', | |
| 132 | + [ | |
| 133 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 134 | + 'callback' => $container->get(DuplicateNotificationController::class), | |
| 135 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 136 | + $permissionResourceResolver, | |
| 137 | + $routePermissionService | |
| 138 | + ), | |
| 139 | + ] | |
| 140 | + ); | |
| 141 | + | |
| 142 | + register_rest_route( | |
| 143 | + $routeNamespace, | |
| 144 | + '/notifications/delete', | |
| 145 | + [ | |
| 146 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 147 | + 'callback' => $container->get(DeleteNotificationsController::class), | |
| 148 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 149 | + return $routePermissionService->canEditForms(); | |
| 150 | + }, | |
| 151 | + 'args' => [ | |
| 152 | + 'ids' => [ | |
| 153 | + 'required' => true, | |
| 154 | + 'type' => 'array', | |
| 155 | + 'items' => ['type' => 'integer'], | |
| 156 | + ], | |
| 157 | + ], | |
| 158 | + ] | |
| 159 | + ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * @return callable | |
| 164 | + */ | |
| 165 | + private static function permissionForNotificationForm( | |
| 166 | + PermissionResourceResolver $permissionResourceResolver, | |
| 167 | + RoutePermissionService $routePermissionService | |
| 168 | + ): callable { | |
| 169 | + return static function ($request) use ( | |
| 170 | + $permissionResourceResolver, | |
| 171 | + $routePermissionService | |
| 172 | + ): bool { | |
| 173 | + $notificationId = RestRouteParamReader::getPositiveIntParam($request); | |
| 174 | + if ($notificationId === null) { | |
| 175 | + return false; | |
| 176 | + } | |
| 177 | + | |
| 178 | + $formId = $permissionResourceResolver->formIdFromNotificationId($notificationId); | |
| 179 | + if ($formId === null) { | |
| 180 | + return false; | |
| 181 | + } | |
| 182 | + | |
| 183 | + return $routePermissionService->canEditForms($formId); | |
| 184 | + }; | |
| 185 | + } | |
| 186 | +} | |