PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V-3.3.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV-3.3.0
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Admin / AdminAjax.php

AdminAjax.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder V-3.3.0, at includes/Admin/AdminAjax.php

2,578 lines 84.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Admin;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 use BitCode\BitForm\Admin\Form\FrontEndScriptGenerator;
10 use BitCode\BitForm\Admin\Form\Helpers;
11 use BitCode\BitForm\Admin\Form\Template\TemplateProvider;
12 use BitCode\BitForm\Core\Database\FormEntryLogModel;
13 use BitCode\BitForm\Core\Database\FormEntryModel;
14 use BitCode\BitForm\Core\Database\FormModel;
15 use BitCode\BitForm\Core\Form\FormHandler;
16 use BitCode\BitForm\Core\Integration\IntegrationHandler;
17 use BitCode\BitForm\Core\Integration\Integrations;
18 use BitCode\BitForm\Core\Util\IpTool;
19 use BitCode\BitForm\Core\Util\MailConfig;
20 use BitCode\BitForm\Core\Util\MetaBoxService;
21 use BitCode\BitForm\Frontend\Form\FrontendFormManager;
22 use BitCode\BitForm\GlobalHelper;
23 use WP_Error;
24
25 class AdminAjax
26 {
27 public function register()
28 {
29 add_action('wp_ajax_bitforms_integrations', [$this, 'integrations']);
30 add_action('wp_ajax_bitforms_save_connected_integration_apps', [$this, 'saveConnectedIntegrationApps']);
31 add_action('wp_ajax_bitforms_get_connected_integration_apps', [$this, 'getConnectedIntegrationApps']);
32 add_action('wp_ajax_bitforms_delete_connected_app', [$this, 'deleteConnectedApp']);
33 add_action('wp_ajax_bitforms_update_connected_app', [$this, 'updateConnectedApp']);
34 add_action('wp_ajax_bitforms_integration_last_runs', [$this, 'integrationLastRuns']);
35 add_action('wp_ajax_bitforms_update_form', [$this, 'updateForm']);
36 add_action('wp_ajax_bitforms_templates', [$this, 'templates']);
37 add_action('wp_ajax_bitforms_create_new_form', [$this, 'createNewForm']);
38 add_action('wp_ajax_bitforms_save_css', [$this, 'saveCss']);
39 add_action('wp_ajax_bitforms_get_template', [$this, 'getTemplate']);
40 add_action('wp_ajax_bitforms_change_status', [$this, 'changeFormStatus']);
41 add_action('wp_ajax_bitforms_bulk_status_change', [$this, 'changeBulkFormStatus']);
42 add_action('wp_ajax_bitforms_get_a_form', [$this, 'getAForm']);
43 add_action('wp_ajax_bitforms_bulk_delete_form', [$this, 'deleteBlukForm']);
44 add_action('wp_ajax_bitforms_bulk_delete_form_entries', [$this, 'deleteBlukFormEntries']);
45 add_action('wp_ajax_bitforms_delete_aform', [$this, 'deleteAForm']);
46 add_action('wp_ajax_bitforms_duplicate_aform', [$this, 'duplicateAForm']);
47 add_action('wp_ajax_bitforms_export_aform', [$this, 'exportAForm']);
48 add_action('wp_ajax_bitforms_import_aform', [$this, 'importAForm']);
49 add_action('wp_ajax_bitforms_get_form_entries', [$this, 'getFormEntry']);
50 add_action('wp_ajax_bitforms_get_entries_for_report', [$this, 'getEntriesForReport']);
51 add_action('wp_ajax_bitforms_duplicate_form_entries', [$this, 'duplicateFormEntry']);
52 add_action('wp_ajax_bitforms_edit_form_entry', [$this, 'editFormEntry']);
53 add_action('wp_ajax_bitforms_update_form_entry', [$this, 'updateFormEntry']);
54 add_action('wp_ajax_bitforms_get_all_form', [$this, 'getAllForms']);
55 add_action('wp_ajax_bitforms_get_all_wp_pages', [$this, 'getAllWPPages']);
56 add_action('wp_ajax_bitforms_delete_success_messsage', [$this, 'deleteSuccessMessage']);
57 add_action('wp_ajax_bitforms_delete_integration', [$this, 'deleteAIntegration']);
58 add_action('wp_ajax_bitforms_update_integration_status', [$this, 'updateIntegrationStatus']);
59 add_action('wp_ajax_bitforms_delete_workflow', [$this, 'deleteAWorkflow']);
60 add_action('wp_ajax_bitforms_delete_mailtemplate', [$this, 'deleteAMailTemplate']);
61 add_action('wp_ajax_bitforms_duplicate_mailtemplate', [$this, 'duplicateAMailTemplate']);
62 add_action('wp_ajax_bitforms_save_allForm_report_prefs', [$this, 'setAllFormsReport']);
63 add_action('wp_ajax_bitforms_save_grecaptcha', [$this, 'savegReCaptcha']);
64 add_action('wp_ajax_bitforms_form_log_history', [$this, 'getLogHistory']);
65 add_action('wp_ajax_bitforms_filter_export_data', [$this, 'filterExportEntry']);
66 add_action('wp_ajax_bitforms_api_key', [$this, 'saveApiKey']);
67 add_action('wp_ajax_bitforms_form_helpers_state', [$this, 'builerHelperState']);
68 add_action('wp_ajax_bitforms_icn_save_setting', [$this, 'iconUpload']);
69 add_action('wp_ajax_bitforms_get_download_icn', [$this, 'getDownlodedIcons']);
70 add_action('wp_ajax_bitforms_icon_remove', [$this, 'iconRemove']);
71 add_action('wp_ajax_bitforms_add_custom_code', [$this, 'addCustomCode']);
72 add_action('wp_ajax_bitforms_get_custom_code', [$this, 'getCustomCode']);
73 add_action('wp_ajax_bitforms_entry_status_update', [$this, 'updateEntryStatus']);
74 add_action('wp_ajax_bitforms_get_generel_settings', [$this, 'getGenerelSettings']);
75 add_action('wp_ajax_bitforms_save_generel_settings', [$this, 'saveGenerelSettings']);
76 add_action('wp_ajax_bitforms_get_form_entry_count', [$this, 'getFormEntryLabelAndCount']);
77 add_action('wp_ajax_bitforms_save_global_messages', [$this, 'saveGlobalMessages']);
78
79 // form migrate code
80 add_action('wp_ajax_bitforms_get_migrated_form_contents', [$this, 'migrateFormContents']);
81 add_action('wp_ajax_bitforms_migrate_to_v2_complete', [$this, 'migrationComplete']);
82 // add_action('wp_ajax_bitforms_migrate_back_to_v1', [$this, 'migrationBackToV1']);
83
84 // PRO TO FREE (SMTP)
85 add_action('wp_ajax_bitforms_get_mail_config', [$this, 'getEmailConfig']);
86 add_action('wp_ajax_bitforms_mail_config', [$this, 'saveEmailConfig']);
87 add_action('wp_ajax_bitforms_test_email', [$this, 'testEmail']);
88
89 // PODS INTEGRATION
90 add_action('wp_ajax_bitforms_get_pod_field', [$this, 'getPodsField']);
91 add_action('wp_ajax_bitforms_get_pod_type', [$this, 'getPodsType']);
92
93 // ACF INTEGRATION
94 add_action('wp_ajax_bitforms_get_acf_group_fields', [$this, 'getAcfGroupFields']);
95 add_action('wp_ajax_bitforms_get_custom_field', [$this, 'getCustomField']);
96
97 // common (get post type) for integration
98 add_action('wp_ajax_bitforms_get_post_type', [$this, 'postTypeByUser']);
99
100 // WP users and roles (lazy-loaded, not inlined on page load)
101 add_action('wp_ajax_bitforms_get_wp_users', [$this, 'getWPUsers']);
102 add_action('wp_ajax_bitforms_get_user_roles', [$this, 'getUserRoles']);
103
104 // transient invalidation when users change
105 add_action('user_register', [$this, 'clearUsersTransient']);
106 add_action('deleted_user', [$this, 'clearUsersTransient']);
107 add_action('profile_update', [$this, 'clearUsersTransient']);
108
109 // Meta Box INTEGRATION
110 add_action('wp_ajax_bitforms_get_metabox_fields', [$this, 'getMetaBoxFields']);
111
112 // CHANGELOG VERSION OPTIONS
113 add_action('wp_ajax_bitforms_changelog_version', [$this, 'setChangelogVersion']);
114
115 // Notice Options
116 add_action('wp_ajax_bitforms_handle_notice', [$this, 'handleNotice']);
117
118 // conversational
119 add_action('wp_ajax_bitforms_save_conversational_css', [$this, 'saveConversationalCSS']);
120
121 // get form html markup
122 add_action('wp_ajax_bitforms_get_form_html', [$this, 'getFormHtml']);
123 }
124
125 private function verifyAdminPermission()
126 {
127 if (!current_user_can('manage_bitform') && !current_user_can('manage_options')) {
128 wp_send_json_error(__('Insufficient permissions.', 'bit-form'), 403);
129 }
130 }
131
132 public function getFormHtml()
133 {
134 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
135 $this->verifyAdminPermission();
136 $formId = isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '';
137
138 $FrontendFormManager = FrontendFormManager::getInstance($formId);
139 if (!$FrontendFormManager->isExist()) {
140 wp_send_json_error(__('Form is not exists.', 'bit-form'), 404);
141 }
142 $formContent = $FrontendFormManager->getFormContentWithValue();
143 if (!is_object($formContent) || !isset($formContent->fields, $formContent->layout)) {
144 wp_send_json_error(__('Form content is unavailable.', 'bit-form'), 404);
145 }
146 $fields = $formContent->fields;
147 $layout = $formContent->layout;
148 $file = count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false;
149 $html = $FrontendFormManager->formView($fields, $file);
150
151 if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')) {
152 $cssPath = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . "bitform-{$formId}-formid" . '.css';
153
154 if (file_exists($cssPath)) {
155 $getCss = file_get_contents($cssPath);
156 } else {
157 $getCss = '';
158 }
159 }
160
161 $data = [
162 'html' => $html,
163 'css' => $getCss,
164 ];
165
166 wp_send_json_success(
167 $data,
168 200
169 );
170 } else {
171 wp_send_json_error(
172 __(
173 'Token expired',
174 'bit-form'
175 ),
176 401
177 );
178 }
179 }
180
181 /**
182 * Undocumented function
183 *
184 * @return void
185 */
186 public function integrations()
187 {
188 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
189 $this->verifyAdminPermission();
190 $testIntegration = Integrations::getInstance();
191 $allIntegrations = $testIntegration->getAllintegrations();
192 if ($allIntegrations) {
193 wp_send_json_success($allIntegrations, 200);
194 } else {
195 wp_send_json_error(
196 __('No Integration Found', 'bit-form'),
197 404
198 );
199 }
200 } else {
201 wp_send_json_error(
202 __(
203 'Token expired',
204 'bit-form'
205 ),
206 401
207 );
208 }
209 }
210
211 /**
212 * Undocumented function
213 *
214 * @return void
215 */
216 public function saveConnectedIntegrationApps()
217 {
218 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
219 $this->verifyAdminPermission();
220 // $inputJSON = wp_unslash($_POST['data']);
221 // $input = json_decode($inputJSON);
222 GlobalHelper::requirePostMethod();
223 try {
224 $input = GlobalHelper::formatRequestData();
225 } catch (\InvalidArgumentException $e) {
226 wp_send_json_error($e->getMessage(), 400);
227 }
228 // wp_send_json_success($input, 200);
229 $integrations = Integrations::getInstance();
230 $status = $integrations->saveConnectedIntegrationApp($input);
231
232 // if (isset($input->customCodes)) {
233 // FrontEndScriptGenerator::customCodeFile($formId, $input->customCodes);
234 // }
235 if (is_wp_error($status)) {
236 wp_send_json_error($status->get_error_message(), 411);
237 } else {
238 wp_send_json_success($status, 200);
239 }
240 } else {
241 wp_send_json_error(
242 __(
243 'Token expired',
244 'bit-form'
245 ),
246 401
247 );
248 }
249 }
250
251 /**
252 * Undocumented function
253 *
254 * @return void
255 */
256 public function getConnectedIntegrationApps()
257 {
258 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
259 $this->verifyAdminPermission();
260 GlobalHelper::requirePostMethod();
261 try {
262 $input = GlobalHelper::formatRequestData();
263 } catch (\InvalidArgumentException $e) {
264 wp_send_json_error($e->getMessage(), 400);
265 }
266
267 $integrationType = isset($input->integrationType) ? $input->integrationType : null;
268 $testIntegration = Integrations::getInstance();
269 $allIntegrations = $testIntegration->getConnectedIntegrationApp($integrationType);
270 if ($allIntegrations) {
271 wp_send_json_success($allIntegrations, 200);
272 } else {
273 wp_send_json_error(
274 __('No Connected App Found', 'bit-form'),
275 404
276 );
277 }
278 } else {
279 wp_send_json_error(
280 __(
281 'Token expired',
282 'bit-form'
283 ),
284 401
285 );
286 }
287 }
288
289 public function deleteConnectedApp()
290 {
291 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
292 $this->verifyAdminPermission();
293 // $inputJSON = wp_unslash($_POST['data']);
294 // $input = json_decode($inputJSON);
295 GlobalHelper::requirePostMethod();
296 try {
297 $input = GlobalHelper::formatRequestData();
298 } catch (\InvalidArgumentException $e) {
299 wp_send_json_error($e->getMessage(), 400);
300 }
301 // Nonce verified at handler entry (wp_verify_nonce 'bitforms_save', line 273).
302 $requestedAppId = isset($_REQUEST['appId'])
303 ? sanitize_text_field(wp_unslash($_REQUEST['appId']))
304 : '';
305 $appId = $requestedAppId
306 ? $requestedAppId
307 : sanitize_text_field(wp_unslash((string) ($input->appId ?? '')));
308 $integrationHandler = Integrations::getInstance();
309 $status = $integrationHandler->deleteConnectedApp($appId);
310 if (is_wp_error($status)) {
311 wp_send_json_error($status->get_error_message(), 411);
312 } else {
313 wp_send_json_success($status, 200);
314 }
315 } else {
316 wp_send_json_error(
317 __(
318 'Token expired',
319 'bit-form'
320 ),
321 401
322 );
323 }
324 }
325
326 /**
327 * Renames a connected account (category connected_integration_apps, form_id 0).
328 * Only integration_name is writable; updateIntegration would overwrite the rest.
329 *
330 * @return void
331 */
332 public function updateConnectedApp()
333 {
334 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
335 $this->verifyAdminPermission();
336 GlobalHelper::requirePostMethod();
337 try {
338 $input = GlobalHelper::formatRequestData();
339 } catch (\InvalidArgumentException $e) {
340 wp_send_json_error($e->getMessage(), 400);
341 }
342 $appId = !empty($input->appId) ? sanitize_text_field((string) $input->appId) : '';
343 $name = isset($input->name) ? sanitize_text_field((string) $input->name) : '';
344 if (empty($appId) || '' === trim($name)) {
345 wp_send_json_error(__('Invalid request', 'bit-form'), 400);
346 }
347 $integrations = Integrations::getInstance();
348 $status = $integrations->renameConnectedApp($appId, $name);
349 if (is_wp_error($status)) {
350 wp_send_json_error($status->get_error_message(), 411);
351 } else {
352 wp_send_json_success($status, 200);
353 }
354 } else {
355 wp_send_json_error(
356 __(
357 'Token expired',
358 'bit-form'
359 ),
360 401
361 );
362 }
363 }
364
365 /**
366 * Last execution per integration of a form, for the integrations list Last Run column.
367 *
368 * @return void
369 */
370 public function integrationLastRuns()
371 {
372 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
373 $this->verifyAdminPermission();
374 GlobalHelper::requirePostMethod();
375 try {
376 $input = GlobalHelper::formatRequestData();
377 } catch (\InvalidArgumentException $e) {
378 wp_send_json_error($e->getMessage(), 400);
379 }
380 $formID = !empty($input->formID) ? absint($input->formID) : 0;
381 if (empty($formID)) {
382 wp_send_json_error(__('Invalid request', 'bit-form'), 400);
383 }
384 $logModel = new FormEntryLogModel();
385 $rows = $logModel->getIntegrationLastRuns($formID);
386 if (is_wp_error($rows)) {
387 // result_empty means nothing has run yet, which is a success. Any other
388 // error is a failed query — answering [] would paint every integration
389 // as never executed. Message stays generic: no DB detail to the browser.
390 if ('result_empty' !== $rows->get_error_code()) {
391 wp_send_json_error(__('Could not load integration run history', 'bit-form'), 500);
392 }
393 $rows = [];
394 }
395 wp_send_json_success($rows, 200);
396 } else {
397 wp_send_json_error(
398 __(
399 'Token expired',
400 'bit-form'
401 ),
402 401
403 );
404 }
405 }
406
407 public function templates()
408 {
409 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
410 $this->verifyAdminPermission();
411 $templateProvider = new TemplateProvider();
412 $status = $templateProvider->getAllTemplates();
413 if (is_wp_error($status)) {
414 wp_send_json_error($status->get_error_message(), 411);
415 } else {
416 wp_send_json_success($status, 200);
417 }
418 } else {
419 wp_send_json_error(
420 __(
421 'Token expired',
422 'bit-form'
423 ),
424 401
425 );
426 }
427 }
428
429 public function builerHelperState()
430 {
431 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
432 $this->verifyAdminPermission();
433 // $inputJSON = wp_unslash($_POST['data']);
434 // $input = json_decode($inputJSON);
435
436 GlobalHelper::requirePostMethod();
437 try {
438 $input = GlobalHelper::formatRequestData();
439 } catch (\InvalidArgumentException $e) {
440 wp_send_json_error($e->getMessage(), 400);
441 }
442
443 $formID = $input->formID;
444 $formHandler = FormHandler::getInstance();
445 $results = $formHandler->admin->builerHelperState($formID);
446 if (is_wp_error($results)) {
447 wp_send_json_error($results, 411);
448 } else {
449 wp_send_json_success($results, 200);
450 }
451 } else {
452 wp_send_json_error(
453 __(
454 'Token expired',
455 'bit-form'
456 ),
457 401
458 );
459 }
460 }
461
462 public function getTemplate()
463 {
464 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
465 $this->verifyAdminPermission();
466 // $inputJSON = wp_unslash($_POST['data']);
467 // $input = json_decode($inputJSON);
468 GlobalHelper::requirePostMethod();
469 try {
470 $input = GlobalHelper::formatRequestData();
471 } catch (\InvalidArgumentException $e) {
472 wp_send_json_error($e->getMessage(), 400);
473 }
474 $formHandler = FormHandler::getInstance();
475 $sanitized_request = [
476 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
477 ];
478 $status = $formHandler->admin->getTemplate($sanitized_request, $input);
479 if (is_wp_error($status)) {
480 wp_send_json_error($status->get_error_message(), 411);
481 } else {
482 wp_send_json_success($status, 200);
483 }
484 } else {
485 wp_send_json_error(
486 __(
487 'Token expired',
488 'bit-form'
489 ),
490 401
491 );
492 }
493 }
494
495 public function getAllForms()
496 {
497 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
498 $this->verifyAdminPermission();
499 $formHandler = FormHandler::getInstance();
500 $all_forms = $formHandler->admin->getAllForm();
501 if (is_wp_error($all_forms)) {
502 wp_send_json_error($all_forms->get_error_message(), 411);
503 } else {
504 wp_send_json_success($all_forms, 200);
505 }
506 } else {
507 wp_send_json_error(
508 __(
509 'Token expired',
510 'bit-form'
511 ),
512 401
513 );
514 }
515 }
516
517 public function migrateFormContents()
518 {
519 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
520 $this->verifyAdminPermission();
521 $all_forms = get_transient('bitforms_v1_form_contents');
522 wp_send_json_success($all_forms, 200);
523 } else {
524 wp_send_json_error(
525 __(
526 'Token expired',
527 'bit-form'
528 ),
529 401
530 );
531 }
532 }
533
534 public function setChangelogVersion()
535 {
536 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
537 $this->verifyAdminPermission();
538 // $inputJSON = wp_unslash($_POST['data']);
539 // $input = json_decode($inputJSON);
540 GlobalHelper::requirePostMethod();
541 try {
542 $input = GlobalHelper::formatRequestData();
543 } catch (\InvalidArgumentException $e) {
544 wp_send_json_error($e->getMessage(), 400);
545 }
546
547 $version = isset($input->version) ? $input->version : '';
548 update_option('bitforms_changelog_version', $version);
549 wp_send_json_success($version, 200);
550 } else {
551 wp_send_json_error(
552 __(
553 'Token expired',
554 'bit-form'
555 ),
556 401
557 );
558 }
559 }
560
561 public function handleNotice()
562 {
563 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
564 $this->verifyAdminPermission();
565 GlobalHelper::requirePostMethod();
566 try {
567 // $inputJSON = wp_unslash($_POST['data']);
568 // $input = json_decode($inputJSON);
569 $input = GlobalHelper::formatRequestData();
570 $optionName = isset($input->optionName) ? $input->optionName : '';
571 $optionValue = isset($input->optionValue) ? $input->optionValue : '';
572 $allowedNoticeKeys = ['bitforms_hide_cashback', 'bitforms_hide_announcement'];
573 if (!in_array($optionName, $allowedNoticeKeys, true)) {
574 wp_send_json_error(__('Invalid option', 'bit-form'), 400);
575 }
576 update_option($optionName, (bool) $optionValue);
577 wp_send_json_success([$optionName, (bool) $optionValue], 200);
578 } catch (\Exception |\InvalidArgumentException $e) {
579 wp_send_json_error($e->getMessage(), 400);
580 }
581 } else {
582 wp_send_json_error(
583 __(
584 'Token expired',
585 'bit-form'
586 ),
587 401
588 );
589 }
590 }
591
592 private function formatFormContentForUpdate($formContents)
593 {
594 $updatedFormContents = (object) [];
595 $updatedPaths = [
596 'id' => 'id',
597 'form_name' => 'form_name',
598 'layout' => 'form_content->layout',
599 'fields' => 'form_content->fields',
600 'additional' => 'additional',
601 'workFlows' => 'workFlows',
602 'formStyle' => null,
603 'style' => null,
604 'staticStyles' => null,
605 'themeColors' => null,
606 'themeVars' => null,
607 'breakpointSize' => null,
608 'customCodes' => null,
609 'builderSettings' => null,
610 'layoutChanged' => null,
611 'rowHeight' => null,
612 'formSettings' => 'formSettings',
613 ];
614 foreach ($updatedPaths as $key => $value) {
615 if (!is_null($value)) {
616 $value = Helpers::getDataFromNestedPath($formContents, $value);
617 }
618 $updatedFormContents->$key = $value;
619 }
620
621 foreach ($formContents['reports'] as $report) {
622 if ('1' === $report['isDefault']) {
623 $updatedFormContents->currentReport = $report;
624 $updatedFormContents->report_id = $report['id'];
625 }
626 }
627
628 $updatedFormContents = json_decode(wp_json_encode($updatedFormContents));
629
630 $updatedFormContents->formSettings->theme = 'default';
631
632 return $updatedFormContents;
633 }
634
635 public function getEmailConfig()
636 {
637 \ignore_user_abort();
638
639 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
640 $this->verifyAdminPermission();
641 $ipTool = new IpTool();
642 $user_details = $ipTool->getUserDetail();
643 $integrationHandler = new IntegrationHandler(0, $user_details);
644 $user_details = $ipTool->getUserDetail();
645 $formIntegrations = $integrationHandler->getAllIntegration('mail', 'smtp');
646 if (is_wp_error($formIntegrations)) {
647 wp_send_json_error($formIntegrations->get_error_message(), 411);
648 }
649 if (isset($formIntegrations[0]->integration_details) && is_string($formIntegrations[0]->integration_details)) {
650 $formIntegrations[0]->integration_details = wp_unslash($formIntegrations[0]->integration_details);
651 }
652 wp_send_json_success($formIntegrations, 200);
653 } else {
654 wp_send_json_error(
655 __(
656 'Token expired',
657 'bit-form'
658 ),
659 401
660 );
661 }
662 }
663
664 public function saveEmailConfig()
665 {
666 \ignore_user_abort();
667 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
668 $this->verifyAdminPermission();
669 GlobalHelper::requirePostMethod();
670 $ipTool = new IpTool();
671 $user_details = $ipTool->getUserDetail();
672 $integrationHandler = new IntegrationHandler(0, $user_details);
673 try {
674 $formattedData = GlobalHelper::formatRequestData();
675 } catch (\InvalidArgumentException $e) {
676 wp_send_json_error($e->getMessage(), 400);
677 }
678 $status = isset($formattedData->status) ? $formattedData->status : null;
679 $integrationDetails = json_encode($formattedData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
680 $user_details = $ipTool->getUserDetail();
681 $integrationName = 'smtp';
682 $integrationType = 'smtp';
683 $formIntegrations = $integrationHandler->getAllIntegration('mail', 'smtp');
684 if (isset($formIntegrations->errors['result_empty'])) {
685 $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'mail', $status);
686 } else {
687 $integrationHandler->updateIntegration($formIntegrations[0]->id, $integrationName, $integrationType, $integrationDetails, 'mail', $status);
688 }
689 wp_send_json_success($formIntegrations, 200);
690 } else {
691 wp_send_json_error(
692 __(
693 'Token expired',
694 'bit-form'
695 ),
696 401
697 );
698 }
699 }
700
701 public function testEmail()
702 {
703 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
704 $this->verifyAdminPermission();
705 GlobalHelper::requirePostMethod();
706
707 try {
708 $formattedData = GlobalHelper::formatRequestData();
709 } catch (\InvalidArgumentException $e) {
710 wp_send_json_error($e->getMessage(), 400);
711 }
712
713 $to = isset($formattedData->to) ? sanitize_email($formattedData->to) : '';
714 $subject = isset($formattedData->subject) ? sanitize_text_field($formattedData->subject) : '';
715 $message = isset($formattedData->message) ? sanitize_textarea_field($formattedData->message) : '';
716
717 if (!empty($to) && !empty($subject) && !empty($message)) {
718 try {
719 (new MailConfig())->sendMail();
720 add_action('wp_mail_failed', function ($error) {
721 $data = [];
722 $data['errors'] = $error->errors['wp_mail_failed'];
723 wp_send_json_error($data, 400);
724 });
725 $result = wp_mail($to, $subject, $message);
726 wp_send_json_success($result, 200);
727 } catch (\Exception $e) {
728 wp_send_json_error($e->getMessage(), 400);
729 }
730 } else {
731 wp_send_json_error(
732 __(
733 'Some of the test fields are empty or an invalid email supplied',
734 'bit-form'
735 ),
736 401
737 );
738 }
739 } else {
740 wp_send_json_error(
741 __(
742 'Token expired',
743 'bit-form'
744 ),
745 401
746 );
747 }
748 }
749
750 public function migrationComplete()
751 {
752 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
753 $this->verifyAdminPermission();
754 delete_transient('bitforms_v1_form_contents');
755 delete_option('bitforms_migrating_to_v2');
756 update_option('bitforms_migrated_to_v2', true);
757 wp_send_json_success(__('Migration Complete', 'bit-form'), 200);
758 } else {
759 wp_send_json_error(
760 __(
761 'Token expired',
762 'bit-form'
763 ),
764 401
765 );
766 }
767 }
768
769 public function importDataStore()
770 {
771 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
772 $this->verifyAdminPermission();
773 // $inputJSON = wp_unslash($_POST['data']);
774 // $input = json_decode($inputJSON);
775 GlobalHelper::requirePostMethod();
776 try {
777 $input = GlobalHelper::formatRequestData();
778 } catch (\InvalidArgumentException $e) {
779 wp_send_json_error($e->getMessage(), 400);
780 }
781 echo wp_json_encode($input);
782 die;
783 } else {
784 wp_send_json_error(
785 __(
786 'Token expired',
787 'bit-form'
788 ),
789 401
790 );
791 }
792 }
793
794 public function getFormEntryLabelAndCount()
795 {
796 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
797 $this->verifyAdminPermission();
798 // $inputJSON = wp_unslash($_POST['data']);
799 // $input = json_decode($inputJSON);
800 GlobalHelper::requirePostMethod();
801 try {
802 $input = GlobalHelper::formatRequestData();
803 } catch (\InvalidArgumentException $e) {
804 wp_send_json_error($e->getMessage(), 400);
805 }
806
807 $formHandler = FormHandler::getInstance();
808 $status = $formHandler->admin->getFormEntryLabelAndCount($_REQUEST, $input);
809 if (is_wp_error($status)) {
810 wp_send_json_error($status->get_error_message(), 411);
811 } else {
812 wp_send_json_success($status, 200);
813 }
814 } else {
815 wp_send_json_error(
816 __(
817 'Token expired',
818 'bit-form'
819 ),
820 401
821 );
822 }
823 }
824
825 public function getFormEntry()
826 {
827 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
828 $this->verifyAdminPermission();
829 GlobalHelper::requirePostMethod();
830 try {
831 $input = GlobalHelper::formatRequestData();
832 } catch (\InvalidArgumentException $e) {
833 wp_send_json_error($e->getMessage(), 400);
834 }
835 $formHandler = FormHandler::getInstance();
836 $sanitized_request = [
837 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
838 ];
839 $status = $formHandler->admin->getFormEntry($sanitized_request, $input);
840 if (is_wp_error($status)) {
841 wp_send_json_error($status->get_error_message(), 411);
842 } else {
843 wp_send_json_success($status, 200);
844 }
845 } else {
846 wp_send_json_error(__('Token expired', 'bit-form'), 401);
847 }
848 }
849
850 public function getEntriesForReport()
851 {
852 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
853 $this->verifyAdminPermission();
854 GlobalHelper::requirePostMethod();
855 try {
856 $input = GlobalHelper::formatRequestData();
857 } catch (\InvalidArgumentException $e) {
858 wp_send_json_error($e->getMessage(), 400);
859 }
860 $formHandler = FormHandler::getInstance();
861 $sanitized_request = [
862 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
863 ];
864 $status = $formHandler->admin->getEntriesForReport($sanitized_request, $input);
865 if (is_wp_error($status)) {
866 wp_send_json_error($status->get_error_message(), 411);
867 } else {
868 wp_send_json_success($status, 200);
869 }
870 } else {
871 wp_send_json_error(__('Token expired', 'bit-form'), 401);
872 }
873 }
874
875 public function filterExportEntry()
876 {
877 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
878 $this->verifyAdminPermission();
879 GlobalHelper::requirePostMethod();
880 try {
881 $input = GlobalHelper::formatRequestData();
882 } catch (\InvalidArgumentException $e) {
883 wp_send_json_error($e->getMessage(), 400);
884 }
885 $formHandler = FormHandler::getInstance();
886 $status = $formHandler->admin->getExportEntry($input->data);
887 if (is_wp_error($status)) {
888 wp_send_json_error($status->get_error_message(), 411);
889 } else {
890 wp_send_json_success($status, 200);
891 }
892 } else {
893 wp_send_json_error(__('Token expired', 'bit-form'), 401);
894 }
895 }
896
897 /**
898 * Undocumented function
899 *
900 * @return void
901 */
902 public function updateForm()
903 {
904 \ignore_user_abort();
905 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
906 $this->verifyAdminPermission();
907 GlobalHelper::requirePostMethod();
908 try {
909 $input = GlobalHelper::formatRequestData();
910 } catch (\InvalidArgumentException $e) {
911 wp_send_json_error($e->getMessage(), 400);
912 }
913 $formHandler = FormHandler::getInstance();
914 $status = $formHandler->admin->updateForm([], $input);
915 if (isset($input->customCodes)) {
916 FrontEndScriptGenerator::customCodeFile($input->id, $input->customCodes);
917 }
918 if (is_wp_error($status)) {
919 wp_send_json_error($status->get_error_message(), 411);
920 } else {
921 wp_send_json_success($status, 200);
922 }
923 } else {
924 wp_send_json_error(
925 __(
926 'Token expired',
927 'bit-form'
928 ),
929 401
930 );
931 }
932 }
933
934 public function saveCss()
935 {
936 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
937 $this->verifyAdminPermission();
938 GlobalHelper::requirePostMethod();
939 try {
940 $input = GlobalHelper::formatRequestData();
941 } catch (\InvalidArgumentException $e) {
942 wp_send_json_error($e->getMessage(), 400);
943 }
944
945 $formId = isset($input->form_id) ? sanitize_text_field($input->form_id) : '';
946 if (!filter_var($formId, FILTER_VALIDATE_INT)) {
947 wp_send_json_error(__('Invalid form id', 'bit-form'), 400);
948 }
949 if (isset($input->atomicCssText)) {
950 $status = FrontEndScriptGenerator::saveCssFile($formId, $input->atomicCssText);
951 }
952 if (isset($input->atomicCssWithFormIdText)) {
953 $status = FrontEndScriptGenerator::saveCssFile("{$formId}-formid", $input->atomicCssWithFormIdText);
954 }
955 if (isset($input->atomicClassMap) || isset($input->atomicClassMap)) {
956 $formModel = new FormModel();
957 $atomicClsMap = [
958 'atomic_class_map' => isset($input->atomicClassMap) ? $input->atomicClassMap : (object) [],
959 'atomic_class_map_with_form_id' => isset($input->atomicClassMapWithFormId) ? $input->atomicClassMapWithFormId : (object) [],
960 ];
961 $updateData['atomic_class_map'] = wp_json_encode($atomicClsMap);
962 $formModel->update(
963 $updateData,
964 [
965 'id' => $formId,
966 ]
967 );
968 }
969 if (is_wp_error($status)) {
970 wp_send_json_error($status->get_error_message(), 411);
971 } else {
972 wp_send_json_success($status, 200);
973 }
974 } else {
975 wp_send_json_error(
976 __(
977 'Token expired',
978 'bit-form'
979 ),
980 401
981 );
982 }
983 }
984
985 public function createNewForm()
986 {
987 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
988 $this->verifyAdminPermission();
989 GlobalHelper::requirePostMethod();
990 try {
991 $input = GlobalHelper::formatRequestData();
992 } catch (\InvalidArgumentException $e) {
993 wp_send_json_error($e->getMessage(), 400);
994 }
995 $formHandler = FormHandler::getInstance();
996 $sanitized_request = [
997 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
998 ];
999 $status = $formHandler->admin->createNewForm($sanitized_request, $input);
1000 $formId = sanitize_text_field($input->form_id);
1001 if (isset($input->customCodes)) {
1002 FrontEndScriptGenerator::customCodeFile($formId, $input->customCodes);
1003 }
1004 if (is_wp_error($status)) {
1005 wp_send_json_error($status->get_error_message(), 411);
1006 } else {
1007 wp_send_json_success($status, 200);
1008 }
1009 } else {
1010 wp_send_json_error(
1011 __(
1012 'Token expired',
1013 'bit-form'
1014 ),
1015 401
1016 );
1017 }
1018 }
1019
1020 public function updateEntryStatus()
1021 {
1022 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1023 $this->verifyAdminPermission();
1024 GlobalHelper::requirePostMethod();
1025 try {
1026 $input = GlobalHelper::formatRequestData();
1027 } catch (\InvalidArgumentException $e) {
1028 wp_send_json_error($e->getMessage(), 400);
1029 }
1030 $formId = $input->formId;
1031 $entryId = $input->entryId;
1032 $formEntryModel = new FormEntryModel();
1033 $updatedTime = current_time('mysql');
1034 $status = $formEntryModel->update(
1035 [
1036 'status' => 0,
1037 'updated_at' => $updatedTime,
1038 ],
1039 [
1040 'form_id' => $formId,
1041 'id' => $entryId,
1042 ]
1043 );
1044 if (is_wp_error($status)) {
1045 wp_send_json_error($status->get_error_message(), 411);
1046 } else {
1047 wp_send_json_success(['update_at_time' => $updatedTime], 200);
1048 }
1049 } else {
1050 wp_send_json_error(
1051 __(
1052 'Token expired',
1053 'bit-form'
1054 ),
1055 401
1056 );
1057 }
1058 }
1059
1060 public function changeFormStatus()
1061 {
1062 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1063 $this->verifyAdminPermission();
1064 // $inputJSON = wp_unslash($_POST['data']);
1065 // $input = json_decode($inputJSON);
1066
1067 GlobalHelper::requirePostMethod();
1068 try {
1069 $input = GlobalHelper::formatRequestData();
1070 } catch (\InvalidArgumentException $e) {
1071 wp_send_json_error($e->getMessage(), 400);
1072 }
1073
1074 $formHandler = FormHandler::getInstance();
1075 $sanitized_request = [
1076 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1077 ];
1078 $status = $formHandler->admin->changeFormStatus($sanitized_request, $input);
1079 if (is_wp_error($status)) {
1080 wp_send_json_error($status->get_error_message(), 411);
1081 } else {
1082 wp_send_json_success($status, 200);
1083 }
1084 } else {
1085 wp_send_json_error(
1086 __(
1087 'Token expired',
1088 'bit-form'
1089 ),
1090 401
1091 );
1092 }
1093 }
1094
1095 public function changeBulkFormStatus()
1096 {
1097 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1098 $this->verifyAdminPermission();
1099 GlobalHelper::requirePostMethod();
1100 try {
1101 $input = GlobalHelper::formatRequestData();
1102 } catch (\InvalidArgumentException $e) {
1103 wp_send_json_error($e->getMessage(), 400);
1104 }
1105 $formHandler = FormHandler::getInstance();
1106 $sanitized_request = [
1107 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1108 ];
1109 $status = $formHandler->admin->changeBulkFormStatus($sanitized_request, $input);
1110 if (is_wp_error($status)) {
1111 wp_send_json_error($status->get_error_message(), 411);
1112 } else {
1113 wp_send_json_success($status, 200);
1114 }
1115 } else {
1116 wp_send_json_error(
1117 __(
1118 'Token expired',
1119 'bit-form'
1120 ),
1121 401
1122 );
1123 }
1124 }
1125
1126 public function getAForm()
1127 {
1128 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1129 $this->verifyAdminPermission();
1130 GlobalHelper::requirePostMethod();
1131 try {
1132 $input = GlobalHelper::formatRequestData();
1133 } catch (\InvalidArgumentException $e) {
1134 wp_send_json_error($e->getMessage(), 400);
1135 }
1136 $formHandler = FormHandler::getInstance();
1137 $sanitized_request = [
1138 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1139 ];
1140 $status = $formHandler->admin->getAForm($sanitized_request, $input);
1141 if (is_wp_error($status)) {
1142 wp_send_json_error($status->get_error_message(), 411);
1143 } else {
1144 wp_send_json_success($status, 200);
1145 }
1146 } else {
1147 wp_send_json_error(
1148 __(
1149 'Token expired',
1150 'bit-form'
1151 ),
1152 401
1153 );
1154 }
1155 }
1156
1157 public function duplicateAForm()
1158 {
1159 \ignore_user_abort();
1160 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1161 $this->verifyAdminPermission();
1162 GlobalHelper::requirePostMethod();
1163 try {
1164 $input = GlobalHelper::formatRequestData();
1165 } catch (\InvalidArgumentException $e) {
1166 wp_send_json_error($e->getMessage(), 400);
1167 }
1168 $formHandler = FormHandler::getInstance();
1169 $sanitized_request = [
1170 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1171 ];
1172 $status = $formHandler->admin->duplicateAForm($sanitized_request, $input);
1173 if (is_wp_error($status)) {
1174 wp_send_json_error($status->get_error_message(), 411);
1175 } else {
1176 wp_send_json_success($status, 200);
1177 }
1178 } else {
1179 wp_send_json_error(
1180 __(
1181 'Token expired',
1182 'bit-form'
1183 ),
1184 401
1185 );
1186 }
1187 }
1188
1189 public function importAForm()
1190 {
1191 \ignore_user_abort();
1192 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1193 $this->verifyAdminPermission();
1194 GlobalHelper::requirePostMethod();
1195 try {
1196 $input = GlobalHelper::formatRequestData();
1197 } catch (\InvalidArgumentException $e) {
1198 wp_send_json_error($e->getMessage(), 400);
1199 }
1200 $formHandler = FormHandler::getInstance();
1201 $status = $formHandler->admin->importAForm($input);
1202 if (is_wp_error($status)) {
1203 wp_send_json_error($status->get_error_message(), 411);
1204 } else {
1205 wp_send_json_success($status, 200);
1206 }
1207 } else {
1208 wp_send_json_error(
1209 __(
1210 'Token expired',
1211 'bit-form'
1212 ),
1213 401
1214 );
1215 }
1216 }
1217
1218 // public function exportAForm() {
1219 // if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1220 // $formHandler = FormHandler::getInstance();
1221 // $status = $formHandler->admin->exportAForm($_REQUEST);
1222 // if (is_wp_error($status)) {
1223 // wp_send_json_error($status->get_error_message(), 411);
1224 // }
1225 // } else {
1226 // wp_send_json_error(
1227 // __(
1228 // 'Token expired',
1229 // 'bit-form'
1230 // ),
1231 // 401
1232 // );
1233 // }
1234 // }
1235 public function exportAForm()
1236 {
1237 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1238 $this->verifyAdminPermission();
1239 GlobalHelper::requirePostMethod();
1240 try {
1241 $input = GlobalHelper::formatRequestData();
1242 } catch (\InvalidArgumentException $e) {
1243 wp_send_json_error($e->getMessage(), 400);
1244 }
1245 $formHandler = FormHandler::getInstance();
1246 $sanitized_request = [
1247 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1248 ];
1249 $status = $formHandler->admin->exportAForm($sanitized_request, $input);
1250 if (is_wp_error($status)) {
1251 wp_send_json_error($status->get_error_message(), 411);
1252 } else {
1253 wp_send_json_success($status);
1254 }
1255 } else {
1256 wp_send_json_error(
1257 __(
1258 'Token expired',
1259 'bit-form'
1260 ),
1261 401
1262 );
1263 }
1264 }
1265
1266 public function deleteAForm()
1267 {
1268 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1269 $this->verifyAdminPermission();
1270 GlobalHelper::requirePostMethod();
1271 try {
1272 $input = GlobalHelper::formatRequestData();
1273 } catch (\InvalidArgumentException $e) {
1274 wp_send_json_error($e->getMessage(), 400);
1275 }
1276 $formHandler = FormHandler::getInstance();
1277 $sanitized_request = [
1278 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1279 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1280 ];
1281 $status = $formHandler->admin->deleteAForm($sanitized_request, $input);
1282 if (is_wp_error($status)) {
1283 wp_send_json_error($status->get_error_message(), 411);
1284 } else {
1285 wp_send_json_success($status, 200);
1286 }
1287 } else {
1288 wp_send_json_error(
1289 __(
1290 'Token expired',
1291 'bit-form'
1292 ),
1293 401
1294 );
1295 }
1296 }
1297
1298 public function deleteBlukForm()
1299 {
1300 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1301 $this->verifyAdminPermission();
1302 GlobalHelper::requirePostMethod();
1303 try {
1304 $input = GlobalHelper::formatRequestData();
1305 } catch (\InvalidArgumentException $e) {
1306 wp_send_json_error($e->getMessage(), 400);
1307 }
1308 $formHandler = FormHandler::getInstance();
1309 $sanitized_request = [
1310 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1311 'entries' => isset($_REQUEST['entries']) && is_array($_REQUEST['entries'])
1312 ? array_map('absint', $_REQUEST['entries'])
1313 : [],
1314 ];
1315 $status = $formHandler->admin->deleteBlukForm($sanitized_request, $input);
1316 if (is_wp_error($status)) {
1317 wp_send_json_error($status->get_error_message(), 411);
1318 } else {
1319 wp_send_json_success($status, 200);
1320 }
1321 } else {
1322 wp_send_json_error(
1323 __(
1324 'Token expired',
1325 'bit-form'
1326 ),
1327 401
1328 );
1329 }
1330 }
1331
1332 public function deleteBlukFormEntries()
1333 {
1334 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1335 $this->verifyAdminPermission();
1336 GlobalHelper::requirePostMethod();
1337 try {
1338 $input = GlobalHelper::formatRequestData();
1339 } catch (\InvalidArgumentException $e) {
1340 wp_send_json_error($e->getMessage(), 400);
1341 }
1342 $formHandler = FormHandler::getInstance();
1343 $sanitized_request = [
1344 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1345 'entries' => isset($_REQUEST['entries']) && is_array($_REQUEST['entries'])
1346 ? array_map('absint', $_REQUEST['entries'])
1347 : [],
1348 ];
1349 $status = $formHandler->admin->deleteBlukFormEntries($sanitized_request, $input);
1350 if (is_wp_error($status)) {
1351 wp_send_json_error($status->get_error_message(), 411);
1352 } else {
1353 wp_send_json_success($status, 200);
1354 }
1355 } else {
1356 wp_send_json_error(
1357 __(
1358 'Token expired',
1359 'bit-form'
1360 ),
1361 401
1362 );
1363 }
1364 }
1365
1366 public function duplicateFormEntry()
1367 {
1368 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1369 $this->verifyAdminPermission();
1370 GlobalHelper::requirePostMethod();
1371 try {
1372 $input = GlobalHelper::formatRequestData();
1373 } catch (\InvalidArgumentException $e) {
1374 wp_send_json_error($e->getMessage(), 400);
1375 }
1376 $formHandler = FormHandler::getInstance();
1377 $sanitized_request = [
1378 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1379 'entries' => isset($_REQUEST['entries']) && is_array($_REQUEST['entries'])
1380 ? array_map('absint', $_REQUEST['entries'])
1381 : [],
1382 ];
1383 $status = $formHandler->admin->duplicateFormEntry($sanitized_request, $input);
1384 if (is_wp_error($status)) {
1385 wp_send_json_error($status->get_error_message(), 411);
1386 } else {
1387 wp_send_json_success($status, 200);
1388 }
1389 } else {
1390 wp_send_json_error(
1391 __(
1392 'Token expired',
1393 'bit-form'
1394 ),
1395 401
1396 );
1397 }
1398 }
1399
1400 public function editFormEntry()
1401 {
1402 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1403 $this->verifyAdminPermission();
1404 // $inputJSON = wp_unslash($_POST['data']);
1405 // $input = json_decode($inputJSON);
1406 GlobalHelper::requirePostMethod();
1407 try {
1408 $input = GlobalHelper::formatRequestData();
1409 } catch (\InvalidArgumentException $e) {
1410 wp_send_json_error($e->getMessage(), 400);
1411 }
1412 $formHandler = FormHandler::getInstance();
1413 $sanitized_request = [
1414 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1415 'entryID' => isset($_REQUEST['entryID']) ? absint($_REQUEST['entryID']) : 0,
1416 ];
1417 $status = $formHandler->admin->editFormEntry($sanitized_request, $input);
1418 if (is_wp_error($status)) {
1419 wp_send_json_error($status->get_error_message(), 411);
1420 } else {
1421 wp_send_json_success($status, 200);
1422 }
1423 } else {
1424 wp_send_json_error(
1425 __(
1426 'Token expired',
1427 'bit-form'
1428 ),
1429 401
1430 );
1431 }
1432 }
1433
1434 public function getLogHistory()
1435 {
1436 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1437 $this->verifyAdminPermission();
1438 // $inputJSON = wp_unslash($_POST['data']);
1439 // $input = json_decode($inputJSON);
1440
1441 GlobalHelper::requirePostMethod();
1442 try {
1443 $input = GlobalHelper::formatRequestData();
1444 } catch (\InvalidArgumentException $e) {
1445 wp_send_json_error($e->getMessage(), 400);
1446 }
1447
1448 $formHandler = FormHandler::getInstance();
1449 $sanitized_request = [
1450 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1451 'entryID' => isset($_REQUEST['entryID']) ? absint($_REQUEST['entryID']) : 0,
1452 ];
1453 $status = $formHandler->admin->getLogHistory($sanitized_request, $input);
1454 if (is_wp_error($status)) {
1455 wp_send_json_error($status->get_error_message(), 411);
1456 } else {
1457 wp_send_json_success($status, 200);
1458 }
1459 } else {
1460 wp_send_json_error(
1461 __(
1462 'Token expired',
1463 'bit-form'
1464 ),
1465 401
1466 );
1467 }
1468 }
1469
1470 public function updateFormEntry()
1471 {
1472 \ignore_user_abort();
1473 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1474 $this->verifyAdminPermission();
1475 $formHandler = FormHandler::getInstance();
1476 $sanitized_request = [
1477 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1478 'entryID' => isset($_REQUEST['entryID']) ? absint($_REQUEST['entryID']) : 0,
1479 ];
1480 $status = $formHandler->admin->updateFormEntry($sanitized_request, wp_unslash($_POST));
1481
1482 if (is_wp_error($status)) {
1483 wp_send_json_error($status->get_error_message(), 411);
1484 } else {
1485 $status = IntegrationHandler::maybeSetCronForIntegration($status, 'update');
1486 wp_send_json_success($status, 200);
1487 }
1488 } else {
1489 wp_send_json_error(
1490 __(
1491 'Token expired',
1492 'bit-form'
1493 ),
1494 401
1495 );
1496 }
1497 }
1498
1499 public function getAllWPPages()
1500 {
1501 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1502 $this->verifyAdminPermission();
1503 // $inputJSON = wp_unslash($_POST['data']);
1504 // $input = json_decode($inputJSON);
1505
1506 GlobalHelper::requirePostMethod();
1507 try {
1508 $input = GlobalHelper::formatRequestData();
1509 } catch (\InvalidArgumentException $e) {
1510 wp_send_json_error($e->getMessage(), 400);
1511 }
1512
1513 $formHandler = FormHandler::getInstance();
1514 $sanitized_request = [
1515 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1516 ];
1517 $status = $formHandler->admin->getAllWPPages($sanitized_request, $input);
1518 if (is_wp_error($status)) {
1519 wp_send_json_error($status->get_error_message(), 411);
1520 } else {
1521 wp_send_json_success($status, 200);
1522 }
1523 } else {
1524 wp_send_json_error(
1525 __(
1526 'Token expired',
1527 'bit-form'
1528 ),
1529 401
1530 );
1531 }
1532 }
1533
1534 public function deleteSuccessMessage()
1535 {
1536 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1537 $this->verifyAdminPermission();
1538 // $inputJSON = wp_unslash($_POST['data']);
1539 // $input = json_decode($inputJSON);
1540 GlobalHelper::requirePostMethod();
1541 try {
1542 $input = GlobalHelper::formatRequestData();
1543 } catch (\InvalidArgumentException $e) {
1544 wp_send_json_error($e->getMessage(), 400);
1545 }
1546 $formHandler = FormHandler::getInstance();
1547 $sanitized_request = [
1548 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1549 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1550 ];
1551 $status = $formHandler->admin->deleteSuccessMessage($sanitized_request, $input);
1552 if (is_wp_error($status)) {
1553 wp_send_json_error($status->get_error_message(), 411);
1554 } else {
1555 wp_send_json_success($status, 200);
1556 }
1557 } else {
1558 wp_send_json_error(
1559 __(
1560 'Token expired',
1561 'bit-form'
1562 ),
1563 401
1564 );
1565 }
1566 }
1567
1568 public function deleteAIntegration()
1569 {
1570 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1571 $this->verifyAdminPermission();
1572 // $inputJSON = wp_unslash($_POST['data']);
1573 // $input = json_decode($inputJSON);
1574
1575 GlobalHelper::requirePostMethod();
1576 try {
1577 $input = GlobalHelper::formatRequestData();
1578 } catch (\InvalidArgumentException $e) {
1579 wp_send_json_error($e->getMessage(), 400);
1580 }
1581
1582 $formHandler = FormHandler::getInstance();
1583 $sanitized_request = [
1584 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1585 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1586 ];
1587 $status = $formHandler->admin->deleteAIntegration($sanitized_request, $input);
1588 if (is_wp_error($status)) {
1589 wp_send_json_error($status->get_error_message(), 411);
1590 } else {
1591 wp_send_json_success($status, 200);
1592 }
1593 } else {
1594 wp_send_json_error(
1595 __(
1596 'Token expired',
1597 'bit-form'
1598 ),
1599 401
1600 );
1601 }
1602 }
1603
1604 public function updateIntegrationStatus()
1605 {
1606 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1607 $this->verifyAdminPermission();
1608 GlobalHelper::requirePostMethod();
1609 try {
1610 $input = GlobalHelper::formatRequestData();
1611 } catch (\InvalidArgumentException $e) {
1612 wp_send_json_error($e->getMessage(), 400);
1613 }
1614 $formID = isset($input->formID) ? sanitize_text_field(wp_unslash($input->formID)) : '';
1615 $integrationID = isset($input->id) ? absint($input->id) : 0;
1616 $status = isset($input->status) ? absint($input->status) : 0;
1617 if (empty($formID) || empty($integrationID)) {
1618 wp_send_json_error(__('Invalid request', 'bit-form'), 400);
1619 }
1620 $integrationHandler = new IntegrationHandler($formID);
1621 $result = $integrationHandler->updateIntegrationStatus($integrationID, $status);
1622 if (is_wp_error($result) && 'result_empty' !== $result->get_error_code()) {
1623 wp_send_json_error($result->get_error_message(), 411);
1624 } else {
1625 wp_send_json_success(['id' => $integrationID, 'status' => $status], 200);
1626 }
1627 } else {
1628 wp_send_json_error(__('Token expired', 'bit-form'), 401);
1629 }
1630 }
1631
1632 public function deleteAWorkflow()
1633 {
1634 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1635 $this->verifyAdminPermission();
1636 // $inputJSON = wp_unslash($_POST['data']);
1637 // $input = json_decode($inputJSON);
1638 GlobalHelper::requirePostMethod();
1639
1640 try {
1641 $input = GlobalHelper::formatRequestData();
1642 } catch (\InvalidArgumentException $e) {
1643 wp_send_json_error($e->getMessage(), 400);
1644 }
1645
1646 $formHandler = FormHandler::getInstance();
1647 $sanitized_request = [
1648 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1649 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1650 ];
1651 $status = $formHandler->admin->deleteAWorkflow($sanitized_request, $input);
1652 if (is_wp_error($status)) {
1653 wp_send_json_error($status->get_error_message(), 411);
1654 } else {
1655 wp_send_json_success($status, 200);
1656 }
1657 } else {
1658 wp_send_json_error(
1659 __(
1660 'Token expired',
1661 'bit-form'
1662 ),
1663 401
1664 );
1665 }
1666 }
1667
1668 public function deleteAMailTemplate()
1669 {
1670 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1671 $this->verifyAdminPermission();
1672 // $inputJSON = wp_unslash($_POST['data']);
1673 // $input = json_decode($inputJSON);
1674 GlobalHelper::requirePostMethod();
1675
1676 try {
1677 $input = GlobalHelper::formatRequestData();
1678 } catch (\InvalidArgumentException $e) {
1679 wp_send_json_error($e->getMessage(), 400);
1680 }
1681
1682 $formHandler = FormHandler::getInstance();
1683 $sanitized_request = [
1684 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1685 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1686 ];
1687 $status = $formHandler->admin->deleteAMailTemplate($sanitized_request, $input);
1688 if (is_wp_error($status)) {
1689 wp_send_json_error($status->get_error_message(), 411);
1690 } else {
1691 wp_send_json_success($status, 200);
1692 }
1693 } else {
1694 wp_send_json_error(
1695 __(
1696 'Token expired',
1697 'bit-form'
1698 ),
1699 401
1700 );
1701 }
1702 }
1703
1704 public function duplicateAMailTemplate()
1705 {
1706 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1707 $this->verifyAdminPermission();
1708 // $inputJSON = wp_unslash($_POST['data']);
1709 // $input = json_decode($inputJSON);
1710
1711 GlobalHelper::requirePostMethod();
1712 try {
1713 $input = GlobalHelper::formatRequestData();
1714 } catch (\InvalidArgumentException $e) {
1715 wp_send_json_error($e->getMessage(), 400);
1716 }
1717 $formHandler = FormHandler::getInstance();
1718 $sanitized_request = [
1719 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1720 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1721 ];
1722 $status = $formHandler->admin->duplicateAMailTemplate($sanitized_request, $input);
1723 if (is_wp_error($status)) {
1724 wp_send_json_error($status->get_error_message(), 411);
1725 } else {
1726 wp_send_json_success($status, 200);
1727 }
1728 } else {
1729 wp_send_json_error(
1730 __(
1731 'Token expired',
1732 'bit-form'
1733 ),
1734 401
1735 );
1736 }
1737 }
1738
1739 public function setAllFormsReport()
1740 {
1741 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1742 $this->verifyAdminPermission();
1743 // $inputJSON = wp_unslash($_POST['data']);
1744 // $input = json_decode($inputJSON);
1745
1746 GlobalHelper::requirePostMethod();
1747
1748 try {
1749 $input = GlobalHelper::formatRequestData();
1750 } catch (\InvalidArgumentException $e) {
1751 wp_send_json_error($e->getMessage(), 400);
1752 }
1753
1754 $formHandler = FormHandler::getInstance();
1755 $sanitized_request = [
1756 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1757 ];
1758 $status = $formHandler->admin->setAllFormsReport($sanitized_request, $input);
1759 if (is_wp_error($status)) {
1760 wp_send_json_error($status->get_error_message(), 411);
1761 } else {
1762 wp_send_json_success($status, 200);
1763 }
1764 } else {
1765 wp_send_json_error(
1766 __(
1767 'Token expired',
1768 'bit-form'
1769 ),
1770 401
1771 );
1772 }
1773 }
1774
1775 public function savegReCaptcha()
1776 {
1777 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1778 $this->verifyAdminPermission();
1779 GlobalHelper::requirePostMethod();
1780
1781 try {
1782 $input = GlobalHelper::formatRequestData();
1783 } catch (\InvalidArgumentException $e) {
1784 wp_send_json_error($e->getMessage(), 400);
1785 }
1786
1787 $formHandler = FormHandler::getInstance();
1788 $sanitized_request = [
1789 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1790 ];
1791 $status = $formHandler->admin->savegReCaptcha($sanitized_request, $input);
1792 if (is_wp_error($status)) {
1793 wp_send_json_error($status->get_error_message(), 411);
1794 } else {
1795 wp_send_json_success($status, 200);
1796 }
1797 } else {
1798 wp_send_json_error(
1799 __(
1800 'Token expired',
1801 'bit-form'
1802 ),
1803 401
1804 );
1805 }
1806 }
1807
1808 public function saveApiKey()
1809 {
1810 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1811 $this->verifyAdminPermission();
1812 // $inputJSON = wp_unslash($_POST['data']);
1813 // $input = json_decode($inputJSON);
1814
1815 GlobalHelper::requirePostMethod();
1816 try {
1817 $input = GlobalHelper::formatRequestData();
1818 } catch (\InvalidArgumentException $e) {
1819 wp_send_json_error($e->getMessage(), 400);
1820 }
1821
1822 if (empty($input->api_key)) {
1823 $api_key = get_option('bitform_secret_api_key');
1824 } elseif (!empty($input->api_key)) {
1825 update_option('bitform_secret_api_key', sanitize_text_field($input->api_key));
1826 $api_key = $input->api_key;
1827 }
1828 if (!$api_key) {
1829 // The generated key authenticates the whole bitform/v1 data API. It was
1830 // previously a constant (sha1 of a fixed string), so every install that
1831 // never set one explicitly shared the same secret. Existing stored keys
1832 // are left untouched — only first-time generation changes.
1833 $api_key = wp_generate_password(64, false, false);
1834 update_option('bitform_secret_api_key', $api_key);
1835 }
1836 wp_send_json_success($api_key, 200);
1837 } else {
1838 wp_send_json_error(
1839 __(
1840 'Token expired',
1841 'bit-form'
1842 ),
1843 401
1844 );
1845 }
1846 }
1847
1848 private function checkExtensionWithURL($urlStr)
1849 {
1850 // Extension is mandatory (no optional group): the URL must end in a known image extension,
1851 // otherwise any http(s) URL would pass and be fetched.
1852 $pattern = '/^https?:\/\/.*\.(svg|png|jpg|jpeg|gif)$/i';
1853
1854 return preg_match($pattern, $urlStr);
1855 }
1856
1857 public function iconUpload()
1858 {
1859 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1860 $this->verifyAdminPermission();
1861 // $inputJSON = wp_unslash($_POST['data']);
1862 // $input = json_decode($inputJSON);
1863
1864 GlobalHelper::requirePostMethod();
1865 try {
1866 $input = GlobalHelper::formatRequestData();
1867 } catch (\InvalidArgumentException $e) {
1868 wp_send_json_error($e->getMessage(), 400);
1869 }
1870
1871 $sanitize_url = sanitize_url($input->src);
1872
1873 if (!$this->checkExtensionWithURL($sanitize_url)) {
1874 return new WP_Error(
1875 'type_error',
1876 __('Invalid file type', 'bit-form')
1877 );
1878 }
1879
1880 $uploadDirInfo = wp_upload_dir();
1881 $wpUploadbaseDir = $uploadDirInfo['basedir'];
1882 $icnDir = $wpUploadbaseDir . DIRECTORY_SEPARATOR . 'bitforms' . DIRECTORY_SEPARATOR . 'icons';
1883
1884 if (!is_dir($icnDir)) {
1885 wp_mkdir_p($icnDir);
1886 }
1887
1888 $response = wp_safe_remote_get($sanitize_url);
1889 if (is_wp_error($response) || 200 !== (int) wp_remote_retrieve_response_code($response)) {
1890 wp_send_json_error(__('Unable to fetch icon', 'bit-form'), 400);
1891 }
1892 $imageUrlData = wp_remote_retrieve_body($response);
1893
1894 $filename = sanitize_file_name($input->id . '-' . basename($sanitize_url));
1895
1896 $validation = wp_check_filetype($filename);
1897 $type = $validation['type'];
1898 $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
1899 $is_svg = 'svg' === $ext; // Check if the file is an SVG
1900 if ($type && 0 === strpos($type, 'image/') || $is_svg) {
1901 if ($is_svg) {
1902 $imageUrlData = (new \BitCode\BitForm\enshrined\svgSanitize\Sanitizer())->sanitize($imageUrlData);
1903 if (false === $imageUrlData) {
1904 wp_send_json_error(__('Invalid file type', 'bit-form'), 400);
1905 }
1906 }
1907 $uploaded = file_put_contents($icnDir . '/' . $filename, $imageUrlData);
1908
1909 if ($uploaded) {
1910 $uploadedFile = BITFORMS_UPLOAD_BASE_URL . '/' . 'icons' . '/' . $filename;
1911 wp_send_json_success($uploadedFile, 200);
1912 }
1913 } else {
1914 wp_send_json_error(
1915 __(
1916 'Invalid file type',
1917 'bit-form'
1918 ),
1919 401
1920 );
1921 }
1922 } else {
1923 wp_send_json_error(
1924 __(
1925 'Token expired',
1926 'bit-form'
1927 ),
1928 401
1929 );
1930 }
1931 }
1932
1933 private function getFiles()
1934 {
1935 $uploadDirInfo = wp_upload_dir();
1936 $wpUploadbaseDir = $uploadDirInfo['basedir'];
1937 $icnDir = $wpUploadbaseDir . DIRECTORY_SEPARATOR . 'bitforms' . DIRECTORY_SEPARATOR . 'icons';
1938 $files = [];
1939 if (file_exists($icnDir)) {
1940 $openDir = opendir($icnDir . DIRECTORY_SEPARATOR);
1941
1942 while (false !== ($fileName = readdir($openDir))) {
1943 $ext = substr($fileName, strrpos($fileName, '.') + 1);
1944 if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'svg'])) {
1945 $files[] = $fileName;
1946 }
1947 }
1948
1949 closedir($openDir);
1950 }
1951 return $files;
1952 }
1953
1954 public function getDownlodedIcons()
1955 {
1956 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1957 $this->verifyAdminPermission();
1958 $files = $this->getFiles();
1959 wp_send_json_success($files, 200);
1960 } else {
1961 wp_send_json_error(
1962 __(
1963 'Token expired',
1964 'bit-form'
1965 ),
1966 401
1967 );
1968 }
1969 }
1970
1971 public function iconRemove()
1972 {
1973 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1974 $this->verifyAdminPermission();
1975 // $inputJSON = wp_unslash($_POST['data']);
1976 // $input = json_decode($inputJSON);
1977
1978 GlobalHelper::requirePostMethod();
1979
1980 try {
1981 $input = GlobalHelper::formatRequestData();
1982 } catch (\InvalidArgumentException $e) {
1983 wp_send_json_error($e->getMessage(), 400);
1984 }
1985
1986 $uploadDirInfo = wp_upload_dir();
1987
1988 $wpUploadbaseDir = $uploadDirInfo['basedir'];
1989 $icnDir = $wpUploadbaseDir . DIRECTORY_SEPARATOR . 'bitforms' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR;
1990 $sanitizeFileName = sanitize_file_name($input->file);
1991 $filePath = $icnDir . $sanitizeFileName;
1992 if (file_exists($filePath)) {
1993 wp_delete_file($filePath);
1994 wp_send_json_success($this->getFiles(), 200);
1995 }
1996 } else {
1997 wp_send_json_error(
1998 __(
1999 'Token expired',
2000 'bit-form'
2001 ),
2002 401
2003 );
2004 }
2005 }
2006
2007 public function addCustomCode()
2008 {
2009 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2010 $this->verifyAdminPermission();
2011 // $inputJSON = wp_unslash($_POST['data']);
2012 // $input = json_decode($inputJSON);
2013
2014 GlobalHelper::requirePostMethod();
2015
2016 try {
2017 $input = GlobalHelper::formatRequestData();
2018 } catch (\InvalidArgumentException $e) {
2019 wp_send_json_error($e->getMessage(), 400);
2020 }
2021
2022 $formId = sanitize_text_field($input->form_id);
2023 if (filter_var($formId, FILTER_VALIDATE_INT)) {
2024 FrontEndScriptGenerator::customCodeFile($formId, $input->customCodes);
2025 $status = ['message' => 'File Update Successfully..'];
2026 if (is_wp_error($status)) {
2027 wp_send_json_error($status->get_error_message(), 411);
2028 } else {
2029 wp_send_json_success($status, 200);
2030 }
2031 } else {
2032 wp_send_json_error(
2033 __(
2034 'Invalid form id',
2035 'bit-form'
2036 ),
2037 401
2038 );
2039 }
2040 } else {
2041 wp_send_json_error(
2042 __(
2043 'Token expired',
2044 'bit-form'
2045 ),
2046 401
2047 );
2048 }
2049 }
2050
2051 public function getCustomCode()
2052 {
2053 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2054 $this->verifyAdminPermission();
2055 // $inputJSON = wp_unslash($_POST['data']);
2056 // $input = json_decode($inputJSON);
2057
2058 GlobalHelper::requirePostMethod();
2059
2060 try {
2061 $input = GlobalHelper::formatRequestData();
2062 } catch (\InvalidArgumentException $e) {
2063 wp_send_json_error($e->getMessage(), 400);
2064 }
2065
2066 $formId = sanitize_text_field($input->form_id);
2067
2068 if (filter_var($formId, FILTER_VALIDATE_INT)) {
2069 $status = FrontEndScriptGenerator::getCustomCodes($formId);
2070 if (is_wp_error($status)) {
2071 wp_send_json_error($status->get_error_message(), 411);
2072 } else {
2073 wp_send_json_success($status, 200);
2074 }
2075 } else {
2076 wp_send_json_error(
2077 __(
2078 'Invalid form id',
2079 'bit-form'
2080 ),
2081 401
2082 );
2083 }
2084 } else {
2085 wp_send_json_error(
2086 __(
2087 'Token expired',
2088 'bit-form'
2089 ),
2090 401
2091 );
2092 }
2093 }
2094
2095 public function getGenerelSettings()
2096 {
2097 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2098 $this->verifyAdminPermission();
2099 $data = get_option('bitform_app_config', (object) ['cache_plugin' => true, 'delete_table' => 0]);
2100
2101 if (is_wp_error($data)) {
2102 wp_send_json_error($data->get_error_message(), 411);
2103 } else {
2104 if (empty($data)) {
2105 $data = (object)[];
2106 }
2107 wp_send_json_success($data, 200);
2108 }
2109 } else {
2110 wp_send_json_error(
2111 __(
2112 'Token expired',
2113 'bit-form'
2114 ),
2115 401
2116 );
2117 }
2118 }
2119
2120 public function saveGenerelSettings()
2121 {
2122 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2123 $this->verifyAdminPermission();
2124 GlobalHelper::requirePostMethod();
2125
2126 try {
2127 $inputData = GlobalHelper::formatRequestData();
2128 } catch (\InvalidArgumentException $e) {
2129 wp_send_json_error($e->getMessage(), 400);
2130 return;
2131 }
2132
2133 $status = update_option('bitform_app_config', $inputData->config);
2134 if (is_wp_error($status)) {
2135 wp_send_json_error($status->get_error_message(), 411);
2136 } else {
2137 if (Helpers::property_exists_nested($inputData, 'config->cache_plugin', true)) {
2138 $formHandler = FormHandler::getInstance();
2139 $formHandler->admin->updateGeneratedScriptPageIds();
2140 }
2141 wp_send_json_success(__('Save successfully done', 'bit-form'));
2142 }
2143 } else {
2144 wp_send_json_error(
2145 __(
2146 'Token expired',
2147 'bit-form'
2148 ),
2149 401
2150 );
2151 }
2152 }
2153
2154 public function saveGlobalMessages()
2155 {
2156 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2157 $this->verifyAdminPermission();
2158 // $inputJSON = wp_unslash($_POST['data']);
2159 // $inputData = json_decode($inputJSON);
2160
2161 GlobalHelper::requirePostMethod();
2162
2163 try {
2164 $inputData = GlobalHelper::formatRequestData();
2165 } catch (\InvalidArgumentException $e) {
2166 wp_send_json_error($e->getMessage(), 400);
2167 return;
2168 }
2169
2170 $appSettings = get_option('bitform_app_settings', (object) []);
2171
2172 $appSettings->globalMessages = $inputData;
2173 $status = update_option('bitform_app_settings', $appSettings);
2174 // delete_option('bitform_app_settings');
2175 if (is_wp_error($status)) {
2176 wp_send_json_error($status->get_error_message(), 411);
2177 } else {
2178 $formHandler = FormHandler::getInstance();
2179 $formHandler->admin->replaceAllFormsErrorMessagesByGlobalMessages();
2180 wp_send_json_success(__('Save successfully done', 'bit-form'));
2181 }
2182 } else {
2183 wp_send_json_error(
2184 __(
2185 'Token expired',
2186 'bit-form'
2187 ),
2188 401
2189 );
2190 }
2191 }
2192
2193 public function getPodsField()
2194 {
2195 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2196 $this->verifyAdminPermission();
2197 // $inputJSON = wp_unslash($_POST['data']);
2198 // $input = json_decode($inputJSON);
2199 GlobalHelper::requirePostMethod();
2200 try {
2201 $input = GlobalHelper::formatRequestData();
2202 } catch (\InvalidArgumentException $e) {
2203 wp_send_json_error($e->getMessage(), 400);
2204 }
2205 $podsAdminExists = is_plugin_active('pods/init.php');
2206
2207 $podField = [];
2208 if ($podsAdminExists) {
2209 $pods = pods($input->pod_type);
2210 $i = 0;
2211 foreach ($pods->fields as $field) {
2212 $i++;
2213 $podField[$i]['key'] = $field['name'];
2214 $podField[$i]['name'] = $field['label'];
2215 $podField[$i]['is-repeatable'] = $field['repeatable'] ?? 0;
2216 $podField[$i]['required'] = 1 === $field['options']['required'] ? true : false;
2217 }
2218 }
2219
2220 if (is_wp_error($podField)) {
2221 wp_send_json_error($podField, 411);
2222 } else {
2223 wp_send_json_success($podField, 200);
2224 }
2225 } else {
2226 wp_send_json_error(
2227 __(
2228 'Token expired',
2229 'bit-form'
2230 ),
2231 401
2232 );
2233 }
2234 }
2235
2236 public function getPodsType()
2237 {
2238 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2239 $this->verifyAdminPermission();
2240 $users = get_users(['fields' => ['ID', 'display_name']]);
2241 $pods = [];
2242 $podsAdminExists = is_plugin_active('pods/init.php');
2243 if ($podsAdminExists) {
2244 $allPods = pods_api()->load_pods();
2245 foreach ($allPods as $key => $pod) {
2246 $pods[$key]['name'] = $pod['name'];
2247 $pods[$key]['label'] = $pod['label'];
2248 }
2249 }
2250 $data = ['users' => $users, 'post_types' => $pods];
2251 wp_send_json_success($data, 200);
2252 } else {
2253 wp_send_json_error(
2254 __(
2255 'Token expired',
2256 'bit-form'
2257 ),
2258 401
2259 );
2260 }
2261 }
2262
2263 public function postTypeByUser()
2264 {
2265 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2266 $this->verifyAdminPermission();
2267 $users = get_users(
2268 [
2269 'fields' => ['ID', 'display_name', 'user_login', 'user_email', 'user_nicename'],
2270 ]
2271 );
2272
2273 $postTypes = $this->getPostTypes();
2274
2275 $data = ['post_types' => $postTypes, 'users' => $users];
2276 wp_send_json_success($data, 200);
2277 } else {
2278 wp_send_json_error(
2279 __(
2280 'Token expired',
2281 'bit-form'
2282 ),
2283 401
2284 );
2285 }
2286 }
2287
2288 public function getWPUsers()
2289 {
2290 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2291 $this->verifyAdminPermission();
2292
2293 $users = get_transient('bitforms_admin_users_list');
2294 if (false === $users) {
2295 $rawUsers = get_users(['fields' => ['ID', 'user_nicename', 'user_email', 'display_name']]);
2296 $userMail = [];
2297 $userNames = [];
2298 foreach ($rawUsers as $key => $value) {
2299 $userMail[$key] = [
2300 'label' => !empty($value->display_name) ? $value->display_name : '',
2301 'value' => !empty($value->user_email) ? $value->user_email : '',
2302 'id' => $value->ID,
2303 ];
2304 $userNames[$value->ID] = [
2305 'name' => $value->display_name,
2306 'url' => admin_url('user-edit.php?user_id=' . absint($value->ID)),
2307 ];
2308 }
2309 $users = ['userMail' => $userMail, 'user' => $userNames];
2310 set_transient('bitforms_admin_users_list', $users, 10 * MINUTE_IN_SECONDS);
2311 }
2312
2313 wp_send_json_success($users, 200);
2314 } else {
2315 wp_send_json_error(__('Token expired', 'bit-form'), 401);
2316 }
2317 }
2318
2319 public function getUserRoles()
2320 {
2321 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2322 $this->verifyAdminPermission();
2323 wp_send_json_success(['userRoles' => get_editable_roles()], 200);
2324 } else {
2325 wp_send_json_error(__('Token expired', 'bit-form'), 401);
2326 }
2327 }
2328
2329 public function clearUsersTransient(): void
2330 {
2331 delete_transient('bitforms_admin_users_list');
2332 }
2333
2334 private function getPostTypes()
2335 {
2336 $all_cpt = get_post_types([
2337 'public' => true,
2338 'exclude_from_search' => false,
2339 '_builtin' => false,
2340 'capability_type' => 'post',
2341
2342 ], 'objects');
2343 $cpt = [];
2344
2345 foreach ($all_cpt as $key => $post_type) {
2346 $cpt[$key]['name'] = $post_type->name;
2347 $cpt[$key]['label'] = $post_type->label;
2348 }
2349 $wp_post_types = get_post_types([
2350 'public' => true,
2351 '_builtin' => true,
2352 ]);
2353
2354 $wp_all_post_types = [];
2355
2356 foreach ($wp_post_types as $key => $post_type) {
2357 if ('attachment' !== $post_type) {
2358 $wp_all_post_types[$key]['name'] = $post_type;
2359 $wp_all_post_types[$key]['label'] = ucwords($post_type);
2360 }
2361 }
2362 return array_merge($wp_all_post_types, $cpt);
2363 }
2364
2365 public function getAcfGroupFields()
2366 {
2367 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2368 $this->verifyAdminPermission();
2369 $acfFields = [];
2370 $types = ['select', 'checkbox', 'radio'];
2371
2372 $field_groups = get_posts(['post_type' => 'acf-field-group']);
2373
2374 if ($field_groups) {
2375 $groups = acf_get_field_groups();
2376 foreach ($groups as $group) {
2377 foreach (acf_get_fields($group['key']) as $acfField) {
2378 if (in_array($acfField['type'], $types)) {
2379 array_push($acfFields, [
2380 'key' => $acfField['key'],
2381 'name' => $acfField['label'],
2382 'choices' => $acfField['choices'],
2383 'group_title' => $group['title'],
2384 'location' => $group['location'],
2385 ]);
2386 }
2387 }
2388 }
2389 }
2390
2391 wp_send_json_success($acfFields, 200);
2392 } else {
2393 wp_send_json_error(
2394 __(
2395 'Token expired',
2396 'bit-form'
2397 ),
2398 401
2399 );
2400 }
2401 }
2402
2403 public function getCustomField()
2404 {
2405 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2406 $this->verifyAdminPermission();
2407 // $inputJSON = wp_unslash($_POST['data']);
2408 // $input = json_decode($inputJSON);
2409
2410 GlobalHelper::requirePostMethod();
2411
2412 try {
2413 $input = GlobalHelper::formatRequestData();
2414 } catch (\InvalidArgumentException $e) {
2415 wp_send_json_error($e->getMessage(), 400);
2416 }
2417 $acfFields = [];
2418 $acfFiles = [];
2419
2420 $allowedFields = [
2421 'repeater',
2422 'text',
2423 'textarea',
2424 'password',
2425 'wysiwyg',
2426 'number',
2427 'radio',
2428 'color_picker',
2429 'oembed',
2430 'email',
2431 'url',
2432 'date_picker',
2433 'true_false',
2434 'date_time_picker',
2435 'time_picker',
2436 'message',
2437 'checkbox',
2438 'select',
2439 'post_object',
2440 'user',
2441 'file',
2442 'image',
2443 'gallery'
2444 ];
2445
2446 $field_groups = get_posts(['post_type' => 'acf-field-group']);
2447
2448 if ($field_groups) {
2449 $groups = acf_get_field_groups(['post_type' => $input->post_type]);
2450
2451 foreach ($groups as $group) {
2452 foreach (acf_get_fields($group['key']) as $acfField) {
2453 if (in_array($acfField['type'], $allowedFields)) {
2454 if ('repeater' === $acfField['type']) {
2455 foreach ($acfField['sub_fields'] as $subField) {
2456 if (in_array($subField['type'], $allowedFields)) {
2457 array_push($acfFields, [
2458 'key' => $acfField['key'] . '.' . $subField['key'],
2459 'name' => $acfField['label'] . '-' . $subField['label'],
2460 'required' => $subField['required'],
2461 ]);
2462 }
2463 }
2464 } elseif (in_array($acfField['type'], ['file', 'image', 'gallery'])) {
2465 array_push($acfFiles, [
2466 'key' => $acfField['key'],
2467 'name' => $acfField['label'],
2468 'required' => $acfField['required'],
2469 ]);
2470 } else {
2471 array_push($acfFields, [
2472 'key' => $acfField['key'],
2473 'name' => $acfField['label'],
2474 'required' => $acfField['required'],
2475 ]);
2476 }
2477 }
2478 }
2479 }
2480 }
2481
2482 wp_send_json_success(['acfFields' => $acfFields, 'acfFile' => $acfFiles], 200);
2483 } else {
2484 wp_send_json_error(
2485 __(
2486 'Token expired',
2487 'bit-form'
2488 ),
2489 401
2490 );
2491 }
2492 }
2493
2494 public function getMetaBoxFields()
2495 {
2496 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2497 $this->verifyAdminPermission();
2498 if (!function_exists('rwmb_meta')) {
2499 wp_send_json_error(__('Meta Box must be activated!', 'bit-form'));
2500 }
2501
2502 GlobalHelper::requirePostMethod();
2503
2504 try {
2505 $input = GlobalHelper::formatRequestData();
2506 } catch (\InvalidArgumentException $e) {
2507 wp_send_json_error($e->getMessage(), 400);
2508 }
2509
2510 $metaBoxFields = rwmb_get_object_fields($input->post_type);
2511
2512 $metaBoxFields = MetaBoxService::getMetaBoxFields($input->post_type);
2513
2514 wp_send_json_success(
2515 [
2516 'metaboxFields' => array_values($metaBoxFields['text_fields']),
2517 'metaboxFile' => array_values($metaBoxFields['file_fields']),
2518 ],
2519 200
2520 );
2521 } else {
2522 wp_send_json_error(
2523 __(
2524 'Token expired',
2525 'bit-form'
2526 ),
2527 401
2528 );
2529 }
2530 }
2531
2532 public function saveConversationalCSS()
2533 {
2534 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2535 $this->verifyAdminPermission();
2536 // $inputJSON = wp_unslash($_POST['data']);
2537 // $requestsParams = json_decode($inputJSON);
2538 GlobalHelper::requirePostMethod();
2539 try {
2540 $requestsParams = GlobalHelper::formatRequestData();
2541 } catch (\InvalidArgumentException $e) {
2542 wp_send_json_error($e->getMessage(), 400);
2543 }
2544 $formId = sanitize_text_field($requestsParams->formID);
2545 $css = $requestsParams->css;
2546
2547 if (filter_var($formId, FILTER_VALIDATE_INT)) {
2548 $path = 'form-styles';
2549 $fileName = "bitform-conversational-$formId.css";
2550 FrontEndScriptGenerator::customCodeFileSaveOrDelete($css, $path, $fileName);
2551 wp_send_json_success(
2552 __(
2553 'Conversational CSS Saved Successfully!',
2554 'bit-form'
2555 ),
2556 200
2557 );
2558 } else {
2559 wp_send_json_error(
2560 __(
2561 'Invalid form id',
2562 'bit-form'
2563 ),
2564 401
2565 );
2566 }
2567 } else {
2568 wp_send_json_error(
2569 __(
2570 'Token expired',
2571 'bit-form'
2572 ),
2573 401
2574 );
2575 }
2576 }
2577 }
2578