PluginProbe
The Innovative Form Builder – IvyForms / 0.4
The Innovative Form Builder – IvyForms v0.4
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / API / IvyFormsAPI.php

IvyFormsAPI.php in The Innovative Form Builder – IvyForms 0.4, at backend/src/Services/API/IvyFormsAPI.php

317 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\API;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use Exception;
11 use IvyForms\Factory\Form\FormFactory;
12 use IvyForms\Repository\FieldOptions\FieldOptionsRepositoryInterface;
13 use IvyForms\Services\Confirmation\ConfirmationService;
14 use IvyForms\Services\FieldOptions\FieldOptionsService;
15 use IvyForms\Services\Form\FormService;
16 use IvyForms\Services\Field\FieldService;
17 use IvyForms\Common\Exceptions\NotFoundException;
18 use IvyForms\Common\Exceptions\InvalidArgumentException;
19 use IvyForms\Common\Exceptions\QueryExecutionException;
20 use IvyForms\Common\Exceptions\ValidationException;
21 use IvyForms\Services\Translations\BackendStrings;
22 use WP_Error;
23
24 /**
25 * Class IvyFormsAPI
26 *
27 * @package IvyForms\Services\API
28 *
29 * This class provides a singleton instance to interact with the IvyForms plugin.
30 * It includes methods to manage forms and fields, including creating, updating,
31 * deleting, and retrieving forms and fields.
32 */
33 class IvyFormsAPI
34 {
35 private static ?self $instance = null;
36 private FormService $formService;
37 private FieldService $fieldService;
38 private ConfirmationService $confirmationService;
39 private FieldOptionsService $fieldOptionsService;
40
41 private function __construct(
42 FormService $formService,
43 FieldService $fieldService,
44 ConfirmationService $confirmationService,
45 FieldOptionsService $fieldOptionsService
46 ) {
47 $this->formService = $formService;
48 $this->fieldService = $fieldService;
49 $this->confirmationService = $confirmationService;
50 $this->fieldOptionsService = $fieldOptionsService;
51 }
52
53 /**
54 * Protected method to handle errors uniformly across all API methods
55 *
56 * @param callable $callback The function to execute
57 * @return mixed The result of the callback or WP_Error on failure
58 */
59 protected static function handleErrors(callable $callback)
60 {
61 try {
62 return $callback();
63 } catch (InvalidArgumentException $e) {
64 return new WP_Error('invalid_argument', $e->getMessage(), ['status' => 400]);
65 } catch (NotFoundException $e) {
66 return new WP_Error('not_found', $e->getMessage(), ['status' => 404]);
67 } catch (QueryExecutionException $e) {
68 return new WP_Error('query_execution_error', $e->getMessage(), ['status' => 500]);
69 } catch (ValidationException $e) {
70 return new WP_Error('validation_error', $e->getMessage(), ['status' => 422]);
71 } catch (Exception $e) {
72 return new WP_Error('unknown_error', $e->getMessage(), ['status' => 500]);
73 }
74 }
75
76 /**
77 * Check if IvyForms plugin is installed and activated
78 *
79 * @return bool
80 */
81 public static function isPluginActive(): bool
82 {
83 if (!function_exists('is_plugin_active')) {
84 require_once ABSPATH . 'wp-admin/includes/plugin.php';
85 }
86 return is_plugin_active('ivyforms/ivyforms.php');
87 }
88
89 /**
90 * Get instance with error handling
91 *
92 * @return self|WP_Error Returns an instance of IvyFormsAPI or WP_Error on failure
93 */
94 public static function getInstance()
95 {
96 if (self::$instance === null) {
97 return self::handleErrors(function () {
98 if (!self::isPluginActive()) {
99 throw new Exception(
100 BackendStrings::getExceptionStrings()['ivyforms_not_installed']
101 );
102 }
103 if (!file_exists(IVYFORMS_PATH . '/backend/vendor/autoload.php')) {
104 throw new Exception(
105 BackendStrings::getExceptionStrings()['autoload_not_found']
106 );
107 }
108 if (!file_exists(IVYFORMS_PATH . '/backend/src/Config/container.php')) {
109 throw new Exception(
110 BackendStrings::getExceptionStrings()['container_not_found']
111 );
112 }
113
114 require_once IVYFORMS_PATH . '/backend/vendor/autoload.php';
115 $container = require IVYFORMS_PATH . '/backend/src/Config/container.php';
116
117 $formService = $container->get(FormService::class);
118 $fieldService = $container->get(FieldService::class);
119 $confirmationService = $container->get(ConfirmationService::class);
120 $fieldOptionsService = $container->get(FieldOptionsService::class);
121
122 self::$instance = new self($formService, $fieldService, $confirmationService, $fieldOptionsService);
123 return self::$instance;
124 });
125 }
126 return self::$instance;
127 }
128
129 /**
130 * Get a single form by ID
131 *
132 * @param int $formId
133 * @return object|WP_Error
134 */
135 public static function getForm(int $formId)
136 {
137 return self::handleErrors(function () use ($formId) {
138 if ($formId <= 0) {
139 throw new InvalidArgumentException(
140 BackendStrings::getExceptionStrings()['invalid_form_id']
141 );
142 }
143 return self::getInstance()->formService->getFormById($formId);
144 });
145 }
146
147 /**
148 * Get all forms
149 *
150 * @return mixed[] | WP_Error
151 */
152 public static function getForms()
153 {
154 return self::handleErrors(function () {
155 return self::getInstance()->formService->getAllForms();
156 });
157 }
158
159 /**
160 * Create a new form
161 *
162 * @param mixed[] $formData
163 * @return int|WP_Error
164 */
165 public static function createForm(array $formData)
166 {
167 return self::handleErrors(function () use ($formData) {
168 if (empty($formData)) {
169 throw new InvalidArgumentException(
170 BackendStrings::getExceptionStrings()['invalid_form_data']
171 );
172 }
173 $formData = FormFactory::create($formData);
174 return self::getInstance()->formService->createForm($formData);
175 });
176 }
177
178 /**
179 * Update an existing form
180 *
181 * @param int $formId
182 * @param mixed[] $formData
183 * @return bool|WP_Error
184 */
185 public static function updateForm(int $formId, array $formData)
186 {
187 return self::handleErrors(function () use ($formId, $formData) {
188 if ($formId <= 0) {
189 throw new InvalidArgumentException(
190 BackendStrings::getExceptionStrings()['invalid_form_id']
191 );
192 }
193 if (empty($formData)) {
194 throw new InvalidArgumentException(
195 BackendStrings::getExceptionStrings()['invalid_form_data']
196 );
197 }
198 $formData = FormFactory::create($formData);
199 return self::getInstance()->formService->updateForm($formId, $formData);
200 });
201 }
202
203 /**
204 * Delete a form
205 *
206 * @param int $formId
207 * @return bool | WP_Error
208 */
209 public static function deleteForm(int $formId)
210 {
211 return self::handleErrors(function () use ($formId) {
212 if ($formId <= 0) {
213 throw new InvalidArgumentException(
214 BackendStrings::getExceptionStrings()['invalid_form_id']
215 );
216 }
217 self::getInstance()->formService->deleteForm($formId);
218 self::getInstance()->fieldService->deleteFieldsByFormId($formId);
219 return true;
220 });
221 }
222
223 /**
224 * Check if a form exists
225 *
226 * @param int $formId
227 * @return bool | WP_Error
228 */
229 public static function formExists(int $formId)
230 {
231 return self::handleErrors(function () use ($formId) {
232 if ($formId <= 0) {
233 throw new InvalidArgumentException(
234 BackendStrings::getExceptionStrings()['invalid_form_id']
235 );
236 }
237 return self::getInstance()->formService->formExists($formId);
238 });
239 }
240
241 /**
242 * Get all fields for a form
243 *
244 * @param int $formId
245 * @return mixed[] | WP_Error
246 */
247 public static function getFields(int $formId)
248 {
249 return self::handleErrors(function () use ($formId) {
250 if ($formId <= 0) {
251 throw new InvalidArgumentException(
252 BackendStrings::getExceptionStrings()['invalid_form_id']
253 );
254 }
255 return self::getInstance()->fieldService->getAllFields($formId);
256 });
257 }
258
259 /**
260 * Delete fields by form ID
261 *
262 * @param int $formId
263 * @return bool | WP_Error
264 */
265 public static function deleteFields(int $formId)
266 {
267 return self::handleErrors(function () use ($formId) {
268 if ($formId <= 0) {
269 throw new InvalidArgumentException(
270 BackendStrings::getExceptionStrings()['invalid_form_id']
271 );
272 }
273 self::getInstance()->fieldService->deleteFieldsByFormId($formId);
274 return true;
275 });
276 }
277 /**
278 * Get all fields for a form
279 *
280 * @param int $formId
281 * @return mixed[] | WP_Error
282 */
283 public static function getConfirmations(int $formId)
284 {
285 return self::handleErrors(function () use ($formId) {
286 if ($formId <= 0) {
287 throw new InvalidArgumentException(
288 BackendStrings::getExceptionStrings()['invalid_form_id']
289 );
290 }
291 return self::getInstance()->confirmationService->getConfirmationsById($formId);
292 });
293 }
294
295 /**
296 * Get all field options for a field
297 *
298 * @param int $fieldId
299 * @return mixed[] | WP_Error
300 */
301 public static function getFieldOptions(int $fieldId)
302 {
303 return self::handleErrors(function () use ($fieldId) {
304 if ($fieldId <= 0) {
305 throw new InvalidArgumentException(
306 BackendStrings::getExceptionStrings()['invalid_field_id']
307 );
308 }
309 $instance = self::getInstance();
310 if ($instance instanceof WP_Error) {
311 return $instance;
312 }
313 return $instance->fieldOptionsService->getByFieldId($fieldId);
314 });
315 }
316 }
317