| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Routes\Form; |
| 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; |
| 13 |
use IvyForms\Controllers\Form\AddFormController; |
| 14 |
use IvyForms\Controllers\Form\DeleteFormController; |
| 15 |
use IvyForms\Controllers\Form\FormSubmissionController; |
| 16 |
use IvyForms\Controllers\Form\DuplicateFormController; |
| 17 |
use IvyForms\Controllers\Form\GetFormController; |
| 18 |
use IvyForms\Controllers\Form\GetFormsController; |
| 19 |
use IvyForms\Controllers\Form\SearchFormsController; |
| 20 |
use IvyForms\Controllers\Form\UpdateFormController; |
| 21 |
use IvyForms\Controllers\Form\DeleteFormsController; |
| 22 |
use IvyForms\Controllers\Form\UpdateFormStarredController; |
| 23 |
use IvyForms\Controllers\Form\UpdateFormStatusController; |
| 24 |
use WP_REST_Server; |
| 25 |
|
| 26 |
class Form |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @throws DependencyException |
| 30 |
* @throws NotFoundException |
| 31 |
*/ |
| 32 |
public static function registerRoutes(Container $container, string $routeNamespace): void |
| 33 |
{ |
| 34 |
register_rest_route( |
| 35 |
$routeNamespace, |
| 36 |
'/form/(?P<id>\d+)', |
| 37 |
[ |
| 38 |
'methods' => WP_REST_Server::READABLE, |
| 39 |
'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'); |
| 43 |
} |
| 44 |
] |
| 45 |
); |
| 46 |
|
| 47 |
register_rest_route( |
| 48 |
$routeNamespace, |
| 49 |
'/form', |
| 50 |
[ |
| 51 |
'methods' => WP_REST_Server::READABLE, |
| 52 |
'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'); |
| 56 |
} |
| 57 |
] |
| 58 |
); |
| 59 |
|
| 60 |
register_rest_route( |
| 61 |
$routeNamespace, |
| 62 |
'/forms/search', |
| 63 |
[ |
| 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'); |
| 76 |
} |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
register_rest_route( |
| 81 |
$routeNamespace, |
| 82 |
'/form/add/', |
| 83 |
[ |
| 84 |
'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'); |
| 89 |
} |
| 90 |
] |
| 91 |
); |
| 92 |
|
| 93 |
register_rest_route( |
| 94 |
$routeNamespace, |
| 95 |
'/form/update/(?P<id>\d+)', |
| 96 |
[ |
| 97 |
'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'); |
| 102 |
} |
| 103 |
] |
| 104 |
); |
| 105 |
|
| 106 |
register_rest_route( |
| 107 |
$routeNamespace, |
| 108 |
'/form/update/starred/(?P<id>\d+)', |
| 109 |
[ |
| 110 |
'methods' => WP_REST_Server::CREATABLE, |
| 111 |
'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'); |
| 115 |
} |
| 116 |
] |
| 117 |
); |
| 118 |
|
| 119 |
register_rest_route( |
| 120 |
$routeNamespace, |
| 121 |
'/form/update/status/(?P<id>\d+)', |
| 122 |
[ |
| 123 |
'methods' => WP_REST_Server::CREATABLE, |
| 124 |
'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'); |
| 128 |
} |
| 129 |
] |
| 130 |
); |
| 131 |
|
| 132 |
|
| 133 |
register_rest_route( |
| 134 |
$routeNamespace, |
| 135 |
'/form/delete/(?P<id>\d+)', |
| 136 |
[ |
| 137 |
'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'); |
| 142 |
} |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
register_rest_route( |
| 147 |
$routeNamespace, |
| 148 |
'/forms/delete', |
| 149 |
[ |
| 150 |
'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'); |
| 155 |
} |
| 156 |
] |
| 157 |
); |
| 158 |
|
| 159 |
register_rest_route( |
| 160 |
$routeNamespace, |
| 161 |
'/form/submission/', |
| 162 |
[ |
| 163 |
'methods' => WP_REST_Server::CREATABLE, |
| 164 |
'callback' => $container->get(FormSubmissionController::class), |
| 165 |
// TODO - Add IvyForms capability - Public use |
| 166 |
'permission_callback' => '__return_true' |
| 167 |
] |
| 168 |
); |
| 169 |
|
| 170 |
register_rest_route( |
| 171 |
$routeNamespace, |
| 172 |
'/form/duplicate/(?P<id>\d+)', |
| 173 |
[ |
| 174 |
'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'); |
| 179 |
} |
| 180 |
] |
| 181 |
); |
| 182 |
} |
| 183 |
} |
| 184 |
|