PluginProbe
The Innovative Form Builder – IvyForms / 0.6.1-backup
The Innovative Form Builder – IvyForms v0.6.1-backup
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.6.1-backup, at backend/src/Services/API/IvyFormsAPI.php

447 lines 14.5 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\Services\Confirmation\ConfirmationService;
13 use IvyForms\Services\Entry\EntryService;
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\Notification\NotificationService;
22 use IvyForms\Services\Settings\SettingsService;
23 use IvyForms\Services\Translations\BackendStrings;
24 use IvyForms\Vendor\DI\ContainerBuilder;
25 use WP_Error;
26 use IvyForms\Services\API\IvyFormsAPIHelpers;
27
28 /**
29 * Class IvyFormsAPI
30 *
31 * @package IvyForms\Services\API
32 *
33 * This class provides a singleton instance to interact with the IvyForms plugin.
34 * It includes methods to manage forms and fields, including creating, updating,
35 * deleting, and retrieving forms and fields.
36 */
37 class IvyFormsAPI
38 {
39 use IvyFormsAPIHelpers;
40
41 private static ?self $instance = null;
42 private FormService $formService;
43 private FieldService $fieldService;
44 private FieldOptionsService $fieldOptionsService;
45 private NotificationService $notificationService;
46 private ConfirmationService $confirmationService;
47 private EntryService $entryService;
48 private SettingsService $settingsService;
49 private function __construct(
50 FormService $formService,
51 FieldService $fieldService,
52 FieldOptionsService $fieldOptionsService,
53 NotificationService $notificationService,
54 ConfirmationService $confirmationService,
55 EntryService $entryService,
56 SettingsService $settingsService
57 ) {
58 $this->formService = $formService;
59 $this->fieldService = $fieldService;
60 $this->fieldOptionsService = $fieldOptionsService;
61 $this->notificationService = $notificationService;
62 $this->confirmationService = $confirmationService;
63 $this->entryService = $entryService;
64 $this->settingsService = $settingsService;
65 }
66
67 /**
68 * Check if IvyForms plugin is installed and activated
69 *
70 * @return bool
71 */
72 public static function isPluginActive(): bool
73 {
74 if (!function_exists('is_plugin_active')) {
75 require_once ABSPATH . 'wp-admin/includes/plugin.php';
76 }
77 return is_plugin_active('ivyforms/ivyforms.php');
78 }
79
80 /**
81 * Get instance with error handling
82 *
83 * @return self|WP_Error Returns an instance of IvyFormsAPI or WP_Error on failure
84 */
85 public static function getInstance()
86 {
87 if (self::$instance === null) {
88 return self::handleErrors(function () {
89 if (!self::isPluginActive()) {
90 throw new Exception(
91 BackendStrings::getExceptionStrings()['ivyforms_not_installed']
92 );
93 }
94 if (!file_exists(IVYFORMS_PATH . '/backend/vendor/autoload.php')) {
95 throw new Exception(
96 BackendStrings::getExceptionStrings()['autoload_not_found']
97 );
98 }
99 if (!file_exists(IVYFORMS_PATH . '/backend/src/Config/container.php')) {
100 throw new Exception(
101 BackendStrings::getExceptionStrings()['container_not_found']
102 );
103 }
104
105 $builder = new ContainerBuilder();
106 $builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/repository.php');
107 $builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/services.php');
108
109 /**
110 * Allow to hook into container before build
111 * @since 0.1.0
112 *
113 * Arguments:
114 * - ContainerBuilder $builder The container builder instance
115 */
116 do_action('ivyforms/boot/extend_container_builder', $builder);
117
118 $container = $builder->build();
119
120 $formService = $container->get(FormService::class);
121 $fieldService = $container->get(FieldService::class);
122 $fieldOptionsService = $container->get(FieldOptionsService::class);
123 $notificationService = $container->get(NotificationService::class);
124 $confirmationService = $container->get(ConfirmationService::class);
125 $entryService = $container->get(EntryService::class);
126 $settingsService = $container->get(SettingsService::class);
127
128 self::$instance = new self(
129 $formService,
130 $fieldService,
131 $fieldOptionsService,
132 $notificationService,
133 $confirmationService,
134 $entryService,
135 $settingsService
136 );
137 return self::$instance;
138 });
139 }
140 return self::$instance;
141 }
142
143 /**
144 * Get a single form by ID
145 *
146 * @param int $formId
147 * @return object|WP_Error
148 */
149 public static function getForm(int $formId)
150 {
151 return self::handleErrors(function () use ($formId) {
152 if ($formId <= 0) {
153 throw new InvalidArgumentException(
154 BackendStrings::getExceptionStrings()['invalid_form_id']
155 );
156 }
157 return self::getInstance()->formService->getFormById($formId);
158 });
159 }
160
161 /**
162 * Get all forms
163 *
164 * @return mixed[] | WP_Error
165 */
166 public static function getForms()
167 {
168 return self::handleErrors(function () {
169 return self::getInstance()->formService->getAllForms();
170 });
171 }
172
173 /**
174 * Create a new form
175 *
176 * @param mixed[] $formData
177 * @return int|WP_Error
178 */
179 public static function createForm(array $formData)
180 {
181 return self::handleErrors(function () use ($formData) {
182 if (empty($formData)) {
183 throw new InvalidArgumentException(
184 BackendStrings::getExceptionStrings()['invalid_form_data']
185 );
186 }
187 $formData = FormFactory::create($formData);
188 return self::getInstance()->formService->createForm($formData);
189 });
190 }
191
192 /**
193 * Update an existing form
194 *
195 * @param int $formId
196 * @param mixed[] $formData
197 * @return bool|WP_Error
198 */
199 public static function updateForm(int $formId, array $formData)
200 {
201 return self::handleErrors(function () use ($formId, $formData) {
202 if ($formId <= 0) {
203 throw new InvalidArgumentException(
204 BackendStrings::getExceptionStrings()['invalid_form_id']
205 );
206 }
207 if (empty($formData)) {
208 throw new InvalidArgumentException(
209 BackendStrings::getExceptionStrings()['invalid_form_data']
210 );
211 }
212 $formData = FormFactory::create($formData);
213 return self::getInstance()->formService->updateForm($formId, $formData);
214 });
215 }
216
217 /**
218 * Delete a form
219 *
220 * @param int $formId
221 * @return bool | WP_Error
222 */
223 public static function deleteForm(int $formId)
224 {
225 return self::handleErrors(function () use ($formId) {
226 if ($formId <= 0) {
227 throw new InvalidArgumentException(
228 BackendStrings::getExceptionStrings()['invalid_form_id']
229 );
230 }
231 self::getInstance()->formService->deleteForm($formId);
232 self::getInstance()->fieldService->deleteFieldsByFormId($formId);
233 return true;
234 });
235 }
236
237 /**
238 * Check if a form exists
239 *
240 * @param int $formId
241 * @return bool | WP_Error
242 */
243 public static function formExists(int $formId)
244 {
245 return self::handleErrors(function () use ($formId) {
246 if ($formId <= 0) {
247 throw new InvalidArgumentException(
248 BackendStrings::getExceptionStrings()['invalid_form_id']
249 );
250 }
251 return self::getInstance()->formService->formExists($formId);
252 });
253 }
254
255 /**
256 * Get all fields for a form
257 *
258 * @param int $formId
259 * @return mixed[] | WP_Error
260 */
261 public static function getFields(int $formId)
262 {
263 return self::handleErrors(function () use ($formId) {
264 if ($formId <= 0) {
265 throw new InvalidArgumentException(
266 BackendStrings::getExceptionStrings()['invalid_form_id']
267 );
268 }
269 return self::getInstance()->fieldService->getAllFields($formId);
270 });
271 }
272
273 /**
274 * Delete fields by form ID
275 *
276 * @param int $formId
277 * @return bool | WP_Error
278 */
279 public static function deleteFields(int $formId)
280 {
281 return self::handleErrors(function () use ($formId) {
282 if ($formId <= 0) {
283 throw new InvalidArgumentException(
284 BackendStrings::getExceptionStrings()['invalid_form_id']
285 );
286 }
287 self::getInstance()->fieldService->deleteFieldsByFormId($formId);
288 return true;
289 });
290 }
291 /**
292 * Get all fields for a form
293 *
294 * @param int $formId
295 * @return mixed[] | WP_Error
296 */
297 public static function getConfirmations(int $formId)
298 {
299 return self::handleErrors(function () use ($formId) {
300 if ($formId <= 0) {
301 throw new InvalidArgumentException(
302 BackendStrings::getExceptionStrings()['invalid_form_id']
303 );
304 }
305 return self::getInstance()->confirmationService->getConfirmationsById($formId);
306 });
307 }
308
309 /**
310 * Get all notifications for a form
311 *
312 * @param int $formId
313 * @return mixed[] | WP_Error
314 */
315 public static function getNotifications(int $formId)
316 {
317 return self::handleErrors(function () use ($formId) {
318 if ($formId <= 0) {
319 throw new InvalidArgumentException(
320 BackendStrings::getExceptionStrings()['invalid_form_id']
321 );
322 }
323 return self::getInstance()->notificationService->getNotificationsByFormId($formId);
324 });
325 }
326
327
328 /**
329 * Get all field options for a field
330 *
331 * @param int $fieldId
332 * @return mixed[] | WP_Error
333 */
334 public static function getFieldOptions(int $fieldId)
335 {
336 return self::handleErrors(function () use ($fieldId) {
337 if ($fieldId <= 0) {
338 throw new InvalidArgumentException(
339 BackendStrings::getExceptionStrings()['invalid_field_id']
340 );
341 }
342 $instance = self::getInstance();
343 if ($instance instanceof WP_Error) {
344 return $instance;
345 }
346 return $instance->fieldOptionsService->getByFieldId($fieldId);
347 });
348 }
349
350 /**
351 * Get entries for a specific form, with optional filters
352 * @param int $formId
353 * @param array<string, mixed> $params
354 * @return array<int, mixed>|WP_Error
355 */
356 public static function getFormEntries(int $formId, array $params = [])
357 {
358 return self::handleErrors(function () use ($formId, $params) {
359 if ($formId <= 0) {
360 throw new InvalidArgumentException(
361 BackendStrings::getExceptionStrings()['invalid_form_id']
362 );
363 }
364 // Merge formId into filters
365 $params['filters']['formId'] = $formId;
366 $result = self::getInstance()->entryService->getEntryManager()->searchEntries($params);
367 return $result['data'] ?? [];
368 });
369 }
370
371 /**
372 * Get entry fields for given entries
373 *
374 * @param array<int, mixed> $entries
375 * @return array<int, mixed>|WP_Error
376 */
377 public static function getEntryFields(array $entries)
378 {
379 return self::handleErrors(function () use ($entries) {
380 if (empty($entries)) {
381 throw new InvalidArgumentException(
382 BackendStrings::getExceptionStrings()['invalid_form_id']
383 );
384 }
385 return self::getInstance()->entryService->getEntryFieldManager()->getEntryFields($entries);
386 });
387 }
388
389 /**
390 * Check if an integration is enabled
391 *
392 * @param string $integration
393 * @return bool
394 */
395 public static function isIntegrationEnabled(string $integration): bool
396 {
397 $result = self::handleErrors(function () use ($integration) {
398 $instance = self::getInstance();
399 if ($instance instanceof WP_Error) {
400 return false;
401 }
402 return self::checkIntegrationEnabled($instance, $integration);
403 });
404 return $result === true;
405 }
406
407 /**
408 * Get forms with an integration enabled
409 *
410 * @param string $integration
411 * @return array<int, mixed>|WP_Error Array of forms with an integration enabled
412 */
413 public static function getFormsWithIntegrationEnabled(string $integration)
414 {
415 return self::handleErrors(function () use ($integration) {
416 $allForms = self::getInstance()->formService->getAllForms();
417 return self::filterFormsWithIntegration($allForms, $integration);
418 });
419 }
420
421
422 /**
423 * Check if a specific form has an integration enabled
424 *
425 * @param int $formId
426 * @param string $integration
427 * @return bool|WP_Error
428 */
429 public static function isIntegrationEnabledForForm(int $formId, string $integration)
430 {
431 return self::handleErrors(function () use ($integration, $formId) {
432 if ($formId <= 0) {
433 throw new InvalidArgumentException(
434 BackendStrings::getExceptionStrings()['invalid_form_id']
435 );
436 }
437 $form = self::getInstance()->formService->getFormById($formId);
438 if (method_exists($form, 'getIntegrationSettings')) {
439 $integrationSettings = $form->getIntegrationSettings();
440 return $integrationSettings->isIntegrationEnabled($integration)
441 || empty($integrationSettings->toArray());
442 }
443 return false;
444 });
445 }
446 }
447