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

The Innovative Form Builder – IvyForms, version 0.1.2. 121 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Controllers/Form/FormSubmissionController.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.1.2/raw/backend/src/Controllers/Form/FormSubmissionController.php
- Modified: 2025-06-12T10:29: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.1.2/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\Sanitizer\Sanitizer;
use IvyForms\Controllers\Controller;
use IvyForms\Services\Field\FieldService;
use IvyForms\Services\Form\FormService;
use IvyForms\Services\Mailer\MailerService;
use IvyForms\Services\Notification\NotificationService;
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;

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

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

        // Validate request data
        if (empty($data->get_params())) {
            throw new InvalidArgumentException('Invalid request data.');
        }

        // Get form ID from submission
        $formId = Sanitizer::sanitizeId($data->get_param('formId'));

        // Verify the submission nonce
        Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId);

        if ($formId <= 0) {
            throw new InvalidArgumentException('Invalid form ID.');
        }

        // Get form details
        $form = $this->formService->getFormById($formId);

        // Get form fields
        $formFields = $this->fieldService->getAllFields($form->getId());

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

        // Get active notifications for this form
        $activeNotifications = $this->notificationService->getActiveNotificationsByFormId($form->getId());

        // 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) {
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('Failed to send email notification for form ID: ' . $formId);
                }
                return new WP_REST_Response([
                    'message' => 'OK',
                    'data'    => [
                        'success' => false
                    ],
                ], 200);
            }
        }

        return new WP_REST_Response([
            'message' => 'OK',
            'data'    => [
                'success' => true
            ],
        ], 200);
    }
}

```
