| @@ -1,0 +1,74 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Routes\Integrations; | |
| 4 | + | |
| 5 | +use IvyForms\Services\Permissions\IvyFormsAccess; | |
| 6 | +use IvyForms\Vendor\DI\Container; | |
| 7 | +use IvyForms\Vendor\DI\DependencyException; | |
| 8 | +use IvyForms\Vendor\DI\NotFoundException; | |
| 9 | +use IvyForms\Controllers\Integrations\InstallPluginController; | |
| 10 | +use IvyForms\Controllers\Integration\GetAllIntegrationsController; | |
| 11 | +use IvyForms\Controllers\Integration\GetIntegrationController; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Class Integrations | |
| 15 | + * | |
| 16 | + * Registers REST API routes for integrations | |
| 17 | + * | |
| 18 | + * @package IvyForms\Routes\Integrations | |
| 19 | + */ | |
| 20 | +class Integrations | |
| 21 | +{ | |
| 22 | + /** | |
| 23 | + * Register integrations routes | |
| 24 | + * | |
| 25 | + * @param Container $container | |
| 26 | + * @param string $routeNamespace | |
| 27 | + * | |
| 28 | + * @throws DependencyException | |
| 29 | + * @throws NotFoundException | |
| 30 | + */ | |
| 31 | + public static function registerRoutes(Container $container, string $routeNamespace): void | |
| 32 | + { | |
| 33 | + // Get all integrations | |
| 34 | + register_rest_route( | |
| 35 | + $routeNamespace, | |
| 36 | + '/integrations', | |
| 37 | + [ | |
| 38 | + 'methods' => 'GET', | |
| 39 | + 'callback' => $container->get(GetAllIntegrationsController::class), | |
| 40 | + 'permission_callback' => function () { | |
| 41 | + return IvyFormsAccess::canAccessPluginAdminRoutes(); | |
| 42 | + }, | |
| 43 | + ] | |
| 44 | + ); | |
| 45 | + | |
| 46 | + // Get single integration | |
| 47 | + register_rest_route( | |
| 48 | + $routeNamespace, | |
| 49 | + '/integrations/(?P<slug>[a-zA-Z0-9_-]+)', | |
| 50 | + [ | |
| 51 | + 'methods' => 'GET', | |
| 52 | + 'callback' => $container->get(GetIntegrationController::class), | |
| 53 | + 'permission_callback' => function () { | |
| 54 | + return IvyFormsAccess::canAccessPluginAdminRoutes(); | |
| 55 | + }, | |
| 56 | + ] | |
| 57 | + ); | |
| 58 | + | |
| 59 | + // Install plugin endpoint | |
| 60 | + register_rest_route( | |
| 61 | + $routeNamespace, | |
| 62 | + '/integrations/install-plugin', | |
| 63 | + [ | |
| 64 | + 'methods' => 'POST', | |
| 65 | + 'callback' => [$container->get(InstallPluginController::class), 'install'], | |
| 66 | + 'permission_callback' => function () { | |
| 67 | + return IvyFormsAccess::canAccessPluginAdminRoutes() | |
| 68 | + && current_user_can('install_plugins') | |
| 69 | + && current_user_can('activate_plugins'); | |
| 70 | + }, | |
| 71 | + ] | |
| 72 | + ); | |
| 73 | + } | |
| 74 | +} | |