# ivyforms/0.3.1/backend/src/Controllers/Form/FormSubmissionController.php

The Innovative Form Builder – IvyForms, version 0.3.1. 154 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.3.1/code/backend/src/Controllers/Form/FormSubmissionController.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.3.1/raw/backend/src/Controllers/Form/FormSubmissionController.php
- Modified: 2025-09-04T07:37:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.3.1/code/backend/src/Controllers/Form/FormSubmissionController.php#L10-L20`.

```php
<?php

/**
 * @copyright © TMS-Plugins. All rights reserved.
 * @licence   See COPYING.md for license details.
 */

namespace IvyForms\Controllers\Form;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\ForbiddenException;
use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Common\Exceptions\ValidationException;
use IvyForms\Common\Sanitizer\Sanitizer;
use IvyForms\Controllers\Controller;
use IvyForms\Factory\Entry\EntryFactory;
use IvyForms\Factory\EntryField\EntryFieldFactory;
use IvyForms\Services\Entry\EntryService;
use IvyForms\Services\Field\FieldService;
use IvyForms\Services\Form\FormService;
use IvyForms\Services\Mailer\MailerService;
use IvyForms\Services\Notification\NotificationService;
use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface;
use IvyForms\Services\Security\SecurityService;
use IvyForms\Services\Translations\BackendStrings;
use WP_REST_Request;
use WP_REST_Response;

/**
 * Class FormSubmissionController
 *
 * @package IvyForms\Controllers\Form
 */
class FormSubmissionController extends Controller
{
    private FormService $formService;
    private FieldService $fieldService;
    private NotificationService $notificationService;
    private MailerService $mailerService;
    private EntryService $entryService;
    private SecurityService $securityService;

    public function __construct(
        FormService $formService,
        FieldService $fieldService,
        NotificationService $notificationService,
        MailerService $mailerService,
        EntryService $entryService,
        SecurityService $securityService
    ) {
        $this->formService = $formService;
        $this->fieldService = $fieldService;
        $this->notificationService = $notificationService;
        $this->mailerService = $mailerService;
        $this->entryService = $entryService;
        $this->securityService = $securityService;
    }

    /**
     * @param WP_REST_Request $data
     *
     * @return WP_REST_Response
     *
     * @throws InvalidArgumentException|NotFoundException|ForbiddenException
     * @throws ValidationException
     */
    public function handle(WP_REST_Request $data): WP_REST_Response
    {
        Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));

        if (empty($data->get_params())) {
            throw new InvalidArgumentException(
                BackendStrings::getExceptionStrings()['invalid_request_data']
            );
        }

        $formId = Sanitizer::sanitizeId($data->get_param('formId'));
        Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId);

        if ($formId <= 0) {
            throw new InvalidArgumentException(
                BackendStrings::getExceptionStrings()['invalid_form_id']
            );
        }

        $form = $this->formService->getFormById($formId);
        $formFields = $this->fieldService->getAllFields($form->getId());

        // Validate CAPTCHA if present using SecurityService (automatically determines provider and validates)
        $this->securityService->validateFormSubmission($data->get_params(), $formFields);

        // Sanitize form submission data based on field types
        $submissionData = Sanitizer::sanitizeFormSubmissionData($data->get_params(), $formFields);

        $entry = [
            'stored' => false
        ];
        if ($form->isStoreEntries()) {
            $entryData = $this->entryService->buildEntryData($formId);
            $entryObj = EntryFactory::create($entryData);
            $entryId = $this->entryService->createEntry($entryObj);
            $this->entryService->addEntryFields($formFields, $entryId, $submissionData);
            $entry = [
                'stored' => true,
                'id' => $entryId,
            ];
        }

        $notificationsResult = $this->processNotifications($formId, $submissionData);

        return new WP_REST_Response([
            'message' => BackendStrings::getCommonStrings()['ok'],
            'data'    => [
                'success' => $notificationsResult,
                'entry' => $entry,
            ],
        ], 200);
    }

    /**
     * Process notifications for the form.
     *
     * @param int $formId
     * @param array<mixed> $submissionData
     * @return bool
     * @throws NotFoundException
     */
    private function processNotifications(int $formId, array $submissionData): bool
    {
        $activeNotifications = $this->notificationService->getActiveNotificationsByFormId($formId);

        // Process each active notification
        foreach ($activeNotifications as $notification) {
            // Skip notifications with empty receivers
            if (empty($notification->getReceiver())) {
                continue;
            }

            // Send email using our mailer service
            $sent = $this->mailerService->sendEmail($notification, $submissionData);

            if (!$sent) {
                error_log('Failed to send email notification for form ID: ' . $formId);
            }
        }
        return true;
    }
}

```
