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