| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die; |
| 4 |
|
| 5 |
/** |
| 6 |
* Add all ajax hooks |
| 7 |
*/ |
| 8 |
|
| 9 |
use FluentForm\App\Modules\Acl\Acl; |
| 10 |
|
| 11 |
/** |
| 12 |
* App instance |
| 13 |
* |
| 14 |
* @var $app \FluentForm\Framework\Foundation\Application |
| 15 |
*/ |
| 16 |
|
| 17 |
$app->addAction('wp_ajax_nopriv_fluentform_submit', function () use ($app) { |
| 18 |
(new \FluentForm\App\Modules\SubmissionHandler\SubmissionHandler($app))->submit(); |
| 19 |
}); |
| 20 |
|
| 21 |
$app->addAction('wp_ajax_fluentform_submit', function () use ($app) { |
| 22 |
// (new \FluentForm\App\Modules\Form\FormHandler($app))->onSubmit(); |
| 23 |
(new \FluentForm\App\Modules\SubmissionHandler\SubmissionHandler($app))->submit(); |
| 24 |
}); |
| 25 |
|
| 26 |
/* |
| 27 |
* We are using this ajax call for updating form fields |
| 28 |
* REST API seems not working for some servers with Mod Security Enabled |
| 29 |
*/ |
| 30 |
$app->addAction('wp_ajax_fluentform-form-update', function () use ($app) { |
| 31 |
$formId = Acl::verifyFormId($app->request->get('form_id')); |
| 32 |
Acl::verify('fluentform_forms_manager', $formId); |
| 33 |
try { |
| 34 |
$data = $app->request->all(); |
| 35 |
$data['form_id'] = $formId; |
| 36 |
$isValidJson = (!empty($data['formFields'])) && json_decode($data['formFields'], true); |
| 37 |
|
| 38 |
if(!$isValidJson) { |
| 39 |
wp_send_json([ |
| 40 |
'message' => 'Looks like the provided JSON is invalid. Please try again or contact support', |
| 41 |
'reason' => 'formFields JSON validation failed' |
| 42 |
], 422); |
| 43 |
} |
| 44 |
|
| 45 |
$formService = new \FluentForm\App\Services\Form\FormService(); |
| 46 |
$form = $formService->update($data); |
| 47 |
wp_send_json([ |
| 48 |
'message' => __('The form is successfully updated.', 'fluentform') |
| 49 |
], 200); |
| 50 |
} catch (\Exception $exception) { |
| 51 |
wp_send_json([ |
| 52 |
'message' => $exception->getMessage(), |
| 53 |
], 422); |
| 54 |
} |
| 55 |
}); |
| 56 |
|
| 57 |
/* |
| 58 |
* This ajax endpoint is used to update form general settings |
| 59 |
* Mod-Security also block this request |
| 60 |
*/ |
| 61 |
$app->addAction('wp_ajax_fluentform-save-settings-general-formSettings', function () use ($app) { |
| 62 |
$formId = Acl::verifyFormId($app->request->get('form_id')); |
| 63 |
Acl::verify('fluentform_forms_manager', $formId); |
| 64 |
try { |
| 65 |
$settingsService = new \FluentForm\App\Services\Settings\SettingsService(); |
| 66 |
$attributes = $app->request->all(); |
| 67 |
$attributes['form_id'] = $formId; |
| 68 |
|
| 69 |
$settingsService->saveGeneral($attributes); |
| 70 |
wp_send_json([ |
| 71 |
'message' => __('Settings has been saved.', 'fluentform'), |
| 72 |
]); |
| 73 |
} catch (\FluentForm\Framework\Validator\ValidationException $exception) { |
| 74 |
wp_send_json($exception->errors(), 422); |
| 75 |
} |
| 76 |
}); |
| 77 |
|
| 78 |
/* |
| 79 |
* This ajax endpoint is used to update form email notifications settings |
| 80 |
* Mod-Security also block this request |
| 81 |
*/ |
| 82 |
$app->addAction('wp_ajax_fluentform-save-form-email-notification', function () use ($app) { |
| 83 |
$formId = Acl::verifyFormId($app->request->get('form_id')); |
| 84 |
Acl::verify('fluentform_forms_manager', $formId); |
| 85 |
try { |
| 86 |
$settingsService = new \FluentForm\App\Services\Settings\SettingsService(); |
| 87 |
$attributes = $app->request->all(); |
| 88 |
$attributes['form_id'] = $formId; |
| 89 |
|
| 90 |
[$settingsId, $settings] = $settingsService->store($attributes); |
| 91 |
|
| 92 |
wp_send_json([ |
| 93 |
'message' => __('Settings has been saved.', 'fluentform'), |
| 94 |
'id' => $settingsId, |
| 95 |
'settings' => $settings, |
| 96 |
]); |
| 97 |
} catch (\FluentForm\Framework\Validator\ValidationException $exception) { |
| 98 |
wp_send_json($exception->errors(), 422); |
| 99 |
} |
| 100 |
}); |
| 101 |
|
| 102 |
|
| 103 |
// Legacy AJAX handlers removed — these routes are handled by the REST API. |
| 104 |
// Kept: fluentform-form-find-shortcode-locations (still in active use) |
| 105 |
$app->addAdminAjaxAction('fluentform-form-find-shortcode-locations', function () use ($app) { |
| 106 |
$formId = Acl::verifyFormId($app->request->get('form_id')); |
| 107 |
Acl::verify('fluentform_forms_manager', $formId); |
| 108 |
|
| 109 |
(new \FluentForm\App\Modules\Form\Form($app))->findFormLocations(); |
| 110 |
}); |
| 111 |
|
| 112 |
// Legacy AJAX handlers removed — these routes are now handled by the REST API. |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
$resolveSubmissionFormId = function ($submissionId) { |
| 117 |
if (!$submissionId) { |
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
$submission = \FluentForm\App\Models\Submission::select('form_id')->find($submissionId); |
| 122 |
|
| 123 |
return $submission ? $submission->form_id : null; |
| 124 |
}; |
| 125 |
|
| 126 |
$app->addAction('wp_ajax_fluentform-form-entries-export', function () use ($app) { |
| 127 |
$formId = Acl::verifyFormId($app->request->get('form_id')); |
| 128 |
|
| 129 |
Acl::verify('fluentform_entries_viewer', $formId); |
| 130 |
(new \FluentForm\App\Modules\Transfer\Transfer())->exportEntries(); |
| 131 |
}); |
| 132 |
|
| 133 |
$app->addAction('wp_ajax_fluentform-update-entry-user', function () use ($app, $resolveSubmissionFormId) { |
| 134 |
$submissionId = absint($app->request->get('submission_id')); |
| 135 |
$formId = $resolveSubmissionFormId($submissionId); |
| 136 |
|
| 137 |
Acl::verify('fluentform_manage_entries', $formId); |
| 138 |
$userId = absint($app->request->get('user_id')); |
| 139 |
try { |
| 140 |
$result = (new \FluentForm\App\Services\Submission\SubmissionService())->updateSubmissionUser($userId, $submissionId); |
| 141 |
wp_send_json_success($result); |
| 142 |
} catch (\Exception $e) { |
| 143 |
wp_send_json_error(['message' => $e->getMessage()], 423); |
| 144 |
} |
| 145 |
}); |
| 146 |
|
| 147 |
$app->addAction('wp_ajax_fluentform-get-users', function () use ($app) { |
| 148 |
Acl::verify('fluentform_entries_viewer'); |
| 149 |
$search = sanitize_text_field($app->request->get('search')); |
| 150 |
$users = get_users([ |
| 151 |
'search' => "*{$search}*", |
| 152 |
'number' => 50, |
| 153 |
]); |
| 154 |
$formattedUsers = []; |
| 155 |
foreach ($users as $user) { |
| 156 |
$formattedUsers[] = [ |
| 157 |
'ID' => $user->ID, |
| 158 |
'label' => $user->display_name . ' - ' . $user->user_email, |
| 159 |
]; |
| 160 |
} |
| 161 |
wp_send_json_success(['users' => $formattedUsers]); |
| 162 |
}); |
| 163 |
|
| 164 |
|
| 165 |
// Legacy log AJAX handlers removed — these routes are now handled by the REST API. |
| 166 |
|
| 167 |
$app->addAction('wp_ajax_fluentform-change-entry-status', function () use ($app, $resolveSubmissionFormId) { |
| 168 |
$entryId = absint($app->request->get('entry_id')); |
| 169 |
$formId = $resolveSubmissionFormId($entryId); |
| 170 |
|
| 171 |
Acl::verify('fluentform_manage_entries', $formId); |
| 172 |
|
| 173 |
$attributes = [ |
| 174 |
'entry_id' => $entryId, |
| 175 |
'status' => sanitize_text_field($app->request->get('status')), |
| 176 |
]; |
| 177 |
$newStatus = (new \FluentForm\App\Services\Submission\SubmissionService())->updateStatus($attributes); |
| 178 |
wp_send_json_success([ |
| 179 |
// translators: %s is the submission status name |
| 180 |
'message' => sprintf(__('Item has been marked as %s', 'fluentform'), $newStatus), |
| 181 |
'status' => $newStatus, |
| 182 |
], 200); |
| 183 |
}); |
| 184 |
|
| 185 |
|
| 186 |
$app->addAction('wp_ajax_fluentform_notice_action_track_yes', function () { |
| 187 |
Acl::verify('fluentform_settings_manager'); |
| 188 |
(new FluentForm\App\Modules\Track\TrackModule())->sendInitialInfo(); |
| 189 |
}); |
| 190 |
|
| 191 |
$app->addAction('wp_ajax_fluentform_install_fluentsmtp', function () { |
| 192 |
Acl::verify('fluentform_settings_manager'); |
| 193 |
(new FluentForm\App\Modules\Track\SetupModule())->installPlugin('fluent-smtp'); |
| 194 |
}); |
| 195 |
|
| 196 |
// Export forms |
| 197 |
$app->addAction('wp_ajax_fluentform-export-forms', function () use ($app) { |
| 198 |
Acl::verify('fluentform_settings_manager', $app->request->get('forms')); |
| 199 |
(new \FluentForm\App\Modules\Transfer\Transfer())->exportForms(); |
| 200 |
}); |
| 201 |
|
| 202 |
// Import forms |
| 203 |
$app->addAction('wp_ajax_fluentform-import-forms', function () use ($app) { |
| 204 |
Acl::verify('fluentform_settings_manager'); |
| 205 |
(new \FluentForm\App\Modules\Transfer\Transfer())->importForms(); |
| 206 |
}); |
| 207 |
|
| 208 |
/* |
| 209 |
* Background Process Receiver |
| 210 |
*/ |
| 211 |
|
| 212 |
// $this refers to the Application instance (included via Application::requireCommonFiles → includes.php) |
| 213 |
$app->addAction('wp_ajax_fluentform_background_process', function () { |
| 214 |
$this->app['fluentFormAsyncRequest']->handleBackgroundCall(); |
| 215 |
}); |
| 216 |
|
| 217 |
$app->addAction('wp_ajax_nopriv_fluentform_background_process', function () { |
| 218 |
$this->app['fluentFormAsyncRequest']->handleBackgroundCall(); |
| 219 |
}); |
| 220 |
|
| 221 |
/* |
| 222 |
* Background Report Data Migration |
| 223 |
*/ |
| 224 |
$app->addAction('wp_ajax_fluentform_report_data_migrate', function () { |
| 225 |
if (!wp_verify_nonce(sanitize_text_field(wpFluentForm('request')->get('nonce')), 'fluentform_report_data_migrate')) { |
| 226 |
die('invalid'); |
| 227 |
} |
| 228 |
$formId = Acl::normalizeFormId(wpFluentForm('request')->get('form_id')); |
| 229 |
if ($formId && Acl::hasPermission('fluentform_entries_viewer', $formId)) { |
| 230 |
\FluentForm\App\Services\Report\ReportHelper::runMigrationBatch($formId); |
| 231 |
} |
| 232 |
die('done'); |
| 233 |
}); |
| 234 |
|
| 235 |
/* |
| 236 |
* For REST API Nonce Renewal |
| 237 |
*/ |
| 238 |
$app->addAction('wp_ajax_fluentform_renew_rest_nonce', function () { |
| 239 |
if (!Acl::getCurrentUserPermissions()) { |
| 240 |
wp_send_json([ |
| 241 |
'error' => 'You do not have permission to do this', |
| 242 |
], 403); |
| 243 |
} |
| 244 |
|
| 245 |
wp_send_json([ |
| 246 |
'nonce' => wp_create_nonce('wp_rest'), |
| 247 |
], 200); |
| 248 |
}); |
| 249 |
/* |
| 250 |
* For selectGroup Component Grouped Options |
| 251 |
* Use this filter to pass data to component |
| 252 |
*/ |
| 253 |
|
| 254 |
add_action('wp_ajax_fluentform_select_group_ajax_data', function () { |
| 255 |
Acl::verify('fluentform_dashboard_access'); |
| 256 |
$requestData = wpFluentForm('request')->all(); |
| 257 |
$ajaxList = apply_filters('fluentform/select_group_component_ajax_options', [], $requestData); |
| 258 |
wp_send_json_success($ajaxList); |
| 259 |
}); |
| 260 |
|