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

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