| @@ -1,17 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Services\API; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | 5 | use Exception; |
| 11 | 6 | use IvyForms\Factory\Form\FormFactory; |
| 12 | -use IvyForms\Repository\FieldOptions\FieldOptionsRepositoryInterface; | |
| 13 | 7 | use IvyForms\Services\Confirmation\ConfirmationService; |
| 8 | +use IvyForms\Services\Entry\EntryService; | |
| 14 | 9 | use IvyForms\Services\FieldOptions\FieldOptionsService; |
| 15 | 10 | use IvyForms\Services\Form\FormService; |
| 16 | 11 | use IvyForms\Services\Field\FieldService; |
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| @@ -17,10 +12,14 @@ | ||
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 18 | 13 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 19 | 14 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 20 | 15 | use IvyForms\Common\Exceptions\ValidationException; |
| 16 | +use IvyForms\Services\Notification\NotificationService; | |
| 17 | +use IvyForms\Services\Settings\SettingsService; | |
| 21 | 18 | use IvyForms\Services\Translations\BackendStrings; |
| 19 | +use IvyForms\Vendor\DI\ContainerBuilder; | |
| 22 | 20 | use WP_Error; |
| 21 | +use IvyForms\Services\API\IvyFormsAPIHelpers; | |
| 23 | 22 | |
| 24 | 23 | /** |
| 25 | 24 | * Class IvyFormsAPI |
| 26 | 25 | * |
| @@ -31,60 +30,63 @@ | ||
| 31 | 30 | * deleting, and retrieving forms and fields. |
| 32 | 31 | */ |
| 33 | 32 | class IvyFormsAPI |
| 34 | 33 | { |
| 34 | + use IvyFormsAPIHelpers; | |
| 35 | + | |
| 35 | 36 | private static ?self $instance = null; |
| 36 | 37 | private FormService $formService; |
| 37 | 38 | private FieldService $fieldService; |
| 39 | + private FieldOptionsService $fieldOptionsService; | |
| 40 | + private NotificationService $notificationService; | |
| 38 | 41 | private ConfirmationService $confirmationService; |
| 39 | - private FieldOptionsService $fieldOptionsService; | |
| 40 | - | |
| 42 | + private EntryService $entryService; | |
| 43 | + private SettingsService $settingsService; | |
| 41 | 44 | private function __construct( |
| 42 | 45 | FormService $formService, |
| 43 | 46 | FieldService $fieldService, |
| 47 | + FieldOptionsService $fieldOptionsService, | |
| 48 | + NotificationService $notificationService, | |
| 44 | 49 | ConfirmationService $confirmationService, |
| 45 | - FieldOptionsService $fieldOptionsService | |
| 50 | + EntryService $entryService, | |
| 51 | + SettingsService $settingsService | |
| 46 | 52 | ) { |
| 47 | 53 | $this->formService = $formService; |
| 48 | 54 | $this->fieldService = $fieldService; |
| 55 | + $this->fieldOptionsService = $fieldOptionsService; | |
| 56 | + $this->notificationService = $notificationService; | |
| 49 | 57 | $this->confirmationService = $confirmationService; |
| 50 | - $this->fieldOptionsService = $fieldOptionsService; | |
| 58 | + $this->entryService = $entryService; | |
| 59 | + $this->settingsService = $settingsService; | |
| 51 | 60 | } |
| 52 | 61 | |
| 53 | 62 | /** |
| 54 | - * Protected method to handle errors uniformly across all API methods | |
| 63 | + * Check if IvyForms plugin is installed and activated | |
| 55 | 64 | * |
| 56 | - * @param callable $callback The function to execute | |
| 57 | - * @return mixed The result of the callback or WP_Error on failure | |
| 65 | + * @return bool | |
| 58 | 66 | */ |
| 59 | - protected static function handleErrors(callable $callback) | |
| 67 | + public static function isPluginActive(): bool | |
| 60 | 68 | { |
| 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]); | |
| 69 | + if (!function_exists('is_plugin_active')) { | |
| 70 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 73 | 71 | } |
| 72 | + return is_plugin_active('ivyforms/ivyforms.php'); | |
| 74 | 73 | } |
| 75 | 74 | |
| 76 | 75 | /** |
| 77 | - * Check if IvyForms plugin is installed and activated | |
| 76 | + * Check if Pro plugin is installed and activated | |
| 78 | 77 | * |
| 79 | 78 | * @return bool |
| 80 | 79 | */ |
| 81 | - public static function isPluginActive(): bool | |
| 80 | + public static function isProPluginActive(): bool | |
| 82 | 81 | { |
| 83 | - if (!function_exists('is_plugin_active')) { | |
| 84 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 82 | + // Check if Pro plugin constant is defined | |
| 83 | + if (defined('IVYFORMS_PRO_VERSION')) { | |
| 84 | + return true; | |
| 85 | 85 | } |
| 86 | - return is_plugin_active('ivyforms/ivyforms.php'); | |
| 86 | + | |
| 87 | + // Alternative check: see if Pro plugin class exists | |
| 88 | + return class_exists('IvyFormsPro\\Plugin\\Plugin', false); | |
| 87 | 89 | } |
| 88 | 90 | |
| 89 | 91 | /** |
| 90 | 92 | * Get instance with error handling |
| @@ -110,17 +112,40 @@ | ||
| 110 | 112 | BackendStrings::getExceptionStrings()['container_not_found'] |
| 111 | 113 | ); |
| 112 | 114 | } |
| 113 | 115 | |
| 114 | - require_once IVYFORMS_PATH . '/backend/vendor/autoload.php'; | |
| 115 | - $container = require IVYFORMS_PATH . '/backend/src/Config/container.php'; | |
| 116 | + $builder = new ContainerBuilder(); | |
| 117 | + $builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/repository.php'); | |
| 118 | + $builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/services.php'); | |
| 116 | 119 | |
| 120 | + /** | |
| 121 | + * Allow to hook into container before build | |
| 122 | + * @since 0.1.0 | |
| 123 | + * | |
| 124 | + * Arguments: | |
| 125 | + * - ContainerBuilder $builder The container builder instance | |
| 126 | + */ | |
| 127 | + do_action('ivyforms/boot/extend_container_builder', $builder); | |
| 128 | + | |
| 129 | + $container = $builder->build(); | |
| 130 | + | |
| 117 | 131 | $formService = $container->get(FormService::class); |
| 118 | 132 | $fieldService = $container->get(FieldService::class); |
| 133 | + $fieldOptionsService = $container->get(FieldOptionsService::class); | |
| 134 | + $notificationService = $container->get(NotificationService::class); | |
| 119 | 135 | $confirmationService = $container->get(ConfirmationService::class); |
| 120 | - $fieldOptionsService = $container->get(FieldOptionsService::class); | |
| 136 | + $entryService = $container->get(EntryService::class); | |
| 137 | + $settingsService = $container->get(SettingsService::class); | |
| 121 | 138 | |
| 122 | - self::$instance = new self($formService, $fieldService, $confirmationService, $fieldOptionsService); | |
| 139 | + self::$instance = new self( | |
| 140 | + $formService, | |
| 141 | + $fieldService, | |
| 142 | + $fieldOptionsService, | |
| 143 | + $notificationService, | |
| 144 | + $confirmationService, | |
| 145 | + $entryService, | |
| 146 | + $settingsService | |
| 147 | + ); | |
| 123 | 148 | return self::$instance; |
| 124 | 149 | }); |
| 125 | 150 | } |
| 126 | 151 | return self::$instance; |
| @@ -292,8 +317,25 @@ | ||
| 292 | 317 | }); |
| 293 | 318 | } |
| 294 | 319 | |
| 295 | 320 | /** |
| 321 | + * Get all notifications for a form | |
| 322 | + * | |
| 323 | + * @param int $formId | |
| 324 | + * @return mixed[] | WP_Error | |
| 325 | + */ | |
| 326 | + public static function getNotifications(int $formId) | |
| 327 | + { | |
| 328 | + return self::handleErrors(function () use ($formId) { | |
| 329 | + if ($formId <= 0) { | |
| 330 | + throw new InvalidArgumentException( | |
| 331 | + BackendStrings::getExceptionStrings()['invalid_form_id'] | |
| 332 | + ); | |
| 333 | + } | |
| 334 | + return self::getInstance()->notificationService->getNotificationsByFormId($formId); | |
| 335 | + }); | |
| 336 | + } | |
| 337 | + /** | |
| 296 | 338 | * Get all field options for a field |
| 297 | 339 | * |
| 298 | 340 | * @param int $fieldId |
| 299 | 341 | * @return mixed[] | WP_Error |
| @@ -310,7 +352,102 @@ | ||
| 310 | 352 | if ($instance instanceof WP_Error) { |
| 311 | 353 | return $instance; |
| 312 | 354 | } |
| 313 | 355 | return $instance->fieldOptionsService->getByFieldId($fieldId); |
| 356 | + }); | |
| 357 | + } | |
| 358 | + | |
| 359 | + /** | |
| 360 | + * Get entries for a specific form, with optional filters | |
| 361 | + * @param int $formId | |
| 362 | + * @param array<string, mixed> $params | |
| 363 | + * @return array<int, mixed>|WP_Error | |
| 364 | + */ | |
| 365 | + public static function getFormEntries(int $formId, array $params = []) | |
| 366 | + { | |
| 367 | + return self::handleErrors(function () use ($formId, $params) { | |
| 368 | + if ($formId <= 0) { | |
| 369 | + throw new InvalidArgumentException( | |
| 370 | + BackendStrings::getExceptionStrings()['invalid_form_id'] | |
| 371 | + ); | |
| 372 | + } | |
| 373 | + // Merge formId into filters | |
| 374 | + $params['filters']['formId'] = $formId; | |
| 375 | + $result = self::getInstance()->entryService->getEntryManager()->searchEntries($params); | |
| 376 | + return $result['data'] ?? []; | |
| 377 | + }); | |
| 378 | + } | |
| 379 | + | |
| 380 | + /** | |
| 381 | + * Get entry fields for given entries | |
| 382 | + * | |
| 383 | + * @param array<int, mixed> $entries | |
| 384 | + * @return array<int, mixed>|WP_Error | |
| 385 | + */ | |
| 386 | + public static function getEntryFields(array $entries) | |
| 387 | + { | |
| 388 | + return self::handleErrors(function () use ($entries) { | |
| 389 | + if (empty($entries)) { | |
| 390 | + throw new InvalidArgumentException( | |
| 391 | + BackendStrings::getExceptionStrings()['invalid_form_id'] | |
| 392 | + ); | |
| 393 | + } | |
| 394 | + return self::getInstance()->entryService->getEntryFieldManager()->getEntryFields($entries); | |
| 395 | + }); | |
| 396 | + } | |
| 397 | + | |
| 398 | + /** | |
| 399 | + * Check if an integration is enabled | |
| 400 | + * | |
| 401 | + * @param string $integration | |
| 402 | + * @return bool | |
| 403 | + */ | |
| 404 | + public static function isIntegrationEnabled(string $integration): bool | |
| 405 | + { | |
| 406 | + $result = self::handleErrors(function () use ($integration) { | |
| 407 | + $instance = self::getInstance(); | |
| 408 | + if ($instance instanceof WP_Error) { | |
| 409 | + return false; | |
| 410 | + } | |
| 411 | + return self::checkIntegrationEnabled($instance, $integration); | |
| 412 | + }); | |
| 413 | + return $result === true; | |
| 414 | + } | |
| 415 | + | |
| 416 | + /** | |
| 417 | + * Get forms with an integration enabled | |
| 418 | + * | |
| 419 | + * @param string $integration | |
| 420 | + * @return array<int, mixed>|WP_Error Array of forms with an integration enabled | |
| 421 | + */ | |
| 422 | + public static function getFormsWithIntegrationEnabled(string $integration) | |
| 423 | + { | |
| 424 | + return self::handleErrors(function () use ($integration) { | |
| 425 | + $allForms = self::getInstance()->formService->getAllForms(); | |
| 426 | + return self::filterFormsWithIntegration($allForms, $integration); | |
| 427 | + }); | |
| 428 | + } | |
| 429 | + /** | |
| 430 | + * Check if a specific form has an integration enabled | |
| 431 | + * | |
| 432 | + * @param int $formId | |
| 433 | + * @param string $integration | |
| 434 | + * @return bool|WP_Error | |
| 435 | + */ | |
| 436 | + public static function isIntegrationEnabledForForm(int $formId, string $integration) | |
| 437 | + { | |
| 438 | + return self::handleErrors(function () use ($integration, $formId) { | |
| 439 | + if ($formId <= 0) { | |
| 440 | + throw new InvalidArgumentException( | |
| 441 | + BackendStrings::getExceptionStrings()['invalid_form_id'] | |
| 442 | + ); | |
| 443 | + } | |
| 444 | + $form = self::getInstance()->formService->getFormById($formId); | |
| 445 | + if (method_exists($form, 'getIntegrationSettings')) { | |
| 446 | + $integrationSettings = $form->getIntegrationSettings(); | |
| 447 | + return $integrationSettings->isIntegrationEnabled($integration) | |
| 448 | + || empty($integrationSettings->toArray()); | |
| 449 | + } | |
| 450 | + return false; | |
| 314 | 451 | }); |
| 315 | 452 | } |
| 316 | 453 | } |