PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.0.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.0.2
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 V3.0.2, at includes/Admin/AdminAjax.php

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