PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
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 2.10.2 All 137 releases
bit-form / includes / Admin / AdminAjax.php

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

2,489 lines 80.7 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_update_integration_status', [$this, 'updateIntegrationStatus']);
56 add_action('wp_ajax_bitforms_delete_workflow', [$this, 'deleteAWorkflow']);
57 add_action('wp_ajax_bitforms_delete_mailtemplate', [$this, 'deleteAMailTemplate']);
58 add_action('wp_ajax_bitforms_duplicate_mailtemplate', [$this, 'duplicateAMailTemplate']);
59 add_action('wp_ajax_bitforms_save_allForm_report_prefs', [$this, 'setAllFormsReport']);
60 add_action('wp_ajax_bitforms_save_grecaptcha', [$this, 'savegReCaptcha']);
61 add_action('wp_ajax_bitforms_form_log_history', [$this, 'getLogHistory']);
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 $allowedNoticeKeys = ['bitforms_hide_cashback', 'bitforms_hide_announcement'];
481 if (!in_array($optionName, $allowedNoticeKeys, true)) {
482 wp_send_json_error(__('Invalid option', 'bit-form'), 400);
483 }
484 update_option($optionName, (bool) $optionValue);
485 wp_send_json_success([$optionName, (bool) $optionValue], 200);
486 } catch (\Exception |\InvalidArgumentException $e) {
487 wp_send_json_error($e->getMessage(), 400);
488 }
489 } else {
490 wp_send_json_error(
491 __(
492 'Token expired',
493 'bit-form'
494 ),
495 401
496 );
497 }
498 }
499
500 public function dismissProUpgradeNotice()
501 {
502 check_ajax_referer('bitforms_dismiss_pro_notice', 'nonce');
503 update_user_meta(get_current_user_id(), 'bitforms_dismiss_pro_upgrade_notice', BITFORMS_REQUIRED_BITFORMPRO_VERSION);
504 wp_die();
505 }
506
507 private function formatFormContentForUpdate($formContents)
508 {
509 $updatedFormContents = (object) [];
510 $updatedPaths = [
511 'id' => 'id',
512 'form_name' => 'form_name',
513 'layout' => 'form_content->layout',
514 'fields' => 'form_content->fields',
515 'additional' => 'additional',
516 'workFlows' => 'workFlows',
517 'formStyle' => null,
518 'style' => null,
519 'staticStyles' => null,
520 'themeColors' => null,
521 'themeVars' => null,
522 'breakpointSize' => null,
523 'customCodes' => null,
524 'builderSettings' => null,
525 'layoutChanged' => null,
526 'rowHeight' => null,
527 'formSettings' => 'formSettings',
528 ];
529 foreach ($updatedPaths as $key => $value) {
530 if (!is_null($value)) {
531 $value = Helpers::getDataFromNestedPath($formContents, $value);
532 }
533 $updatedFormContents->$key = $value;
534 }
535
536 foreach ($formContents['reports'] as $report) {
537 if ('1' === $report['isDefault']) {
538 $updatedFormContents->currentReport = $report;
539 $updatedFormContents->report_id = $report['id'];
540 }
541 }
542
543 $updatedFormContents = json_decode(wp_json_encode($updatedFormContents));
544
545 $updatedFormContents->formSettings->theme = 'default';
546
547 return $updatedFormContents;
548 }
549
550 public function getEmailConfig()
551 {
552 \ignore_user_abort();
553
554 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
555 $this->verifyAdminPermission();
556 $ipTool = new IpTool();
557 $user_details = $ipTool->getUserDetail();
558 $integrationHandler = new IntegrationHandler(0, $user_details);
559 $user_details = $ipTool->getUserDetail();
560 $formIntegrations = $integrationHandler->getAllIntegration('mail', 'smtp');
561 if (is_wp_error($formIntegrations)) {
562 wp_send_json_error($formIntegrations->get_error_message(), 411);
563 }
564 if (isset($formIntegrations[0]->integration_details) && is_string($formIntegrations[0]->integration_details)) {
565 $formIntegrations[0]->integration_details = wp_unslash($formIntegrations[0]->integration_details);
566 }
567 wp_send_json_success($formIntegrations, 200);
568 } else {
569 wp_send_json_error(
570 __(
571 'Token expired',
572 'bit-form'
573 ),
574 401
575 );
576 }
577 }
578
579 public function saveEmailConfig()
580 {
581 \ignore_user_abort();
582 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
583 $this->verifyAdminPermission();
584 GlobalHelper::requirePostMethod();
585 $ipTool = new IpTool();
586 $user_details = $ipTool->getUserDetail();
587 $integrationHandler = new IntegrationHandler(0, $user_details);
588 try {
589 $formattedData = GlobalHelper::formatRequestData();
590 } catch (\InvalidArgumentException $e) {
591 wp_send_json_error($e->getMessage(), 400);
592 }
593 $status = isset($formattedData->status) ? $formattedData->status : null;
594 $integrationDetails = json_encode($formattedData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
595 $user_details = $ipTool->getUserDetail();
596 $integrationName = 'smtp';
597 $integrationType = 'smtp';
598 $formIntegrations = $integrationHandler->getAllIntegration('mail', 'smtp');
599 if (isset($formIntegrations->errors['result_empty'])) {
600 $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'mail', $status);
601 } else {
602 $integrationHandler->updateIntegration($formIntegrations[0]->id, $integrationName, $integrationType, $integrationDetails, 'mail', $status);
603 }
604 wp_send_json_success($formIntegrations, 200);
605 } else {
606 wp_send_json_error(
607 __(
608 'Token expired',
609 'bit-form'
610 ),
611 401
612 );
613 }
614 }
615
616 public function testEmail()
617 {
618 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
619 $this->verifyAdminPermission();
620 GlobalHelper::requirePostMethod();
621
622 try {
623 $formattedData = GlobalHelper::formatRequestData();
624 } catch (\InvalidArgumentException $e) {
625 wp_send_json_error($e->getMessage(), 400);
626 }
627
628 $to = isset($formattedData->to) ? sanitize_email($formattedData->to) : '';
629 $subject = isset($formattedData->subject) ? sanitize_text_field($formattedData->subject) : '';
630 $message = isset($formattedData->message) ? sanitize_textarea_field($formattedData->message) : '';
631
632 if (!empty($to) && !empty($subject) && !empty($message)) {
633 try {
634 (new MailConfig())->sendMail();
635 add_action('wp_mail_failed', function ($error) {
636 $data = [];
637 $data['errors'] = $error->errors['wp_mail_failed'];
638 wp_send_json_error($data, 400);
639 });
640 $result = wp_mail($to, $subject, $message);
641 wp_send_json_success($result, 200);
642 } catch (\Exception $e) {
643 wp_send_json_error($e->getMessage(), 400);
644 }
645 } else {
646 wp_send_json_error(
647 __(
648 'Some of the test fields are empty or an invalid email supplied',
649 'bit-form'
650 ),
651 401
652 );
653 }
654 } else {
655 wp_send_json_error(
656 __(
657 'Token expired',
658 'bit-form'
659 ),
660 401
661 );
662 }
663 }
664
665 public function migrationComplete()
666 {
667 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
668 $this->verifyAdminPermission();
669 delete_transient('bitforms_v1_form_contents');
670 delete_option('bitforms_migrating_to_v2');
671 update_option('bitforms_migrated_to_v2', true);
672 wp_send_json_success(__('Migration Complete', 'bit-form'), 200);
673 } else {
674 wp_send_json_error(
675 __(
676 'Token expired',
677 'bit-form'
678 ),
679 401
680 );
681 }
682 }
683
684 public function importDataStore()
685 {
686 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
687 $this->verifyAdminPermission();
688 // $inputJSON = wp_unslash($_POST['data']);
689 // $input = json_decode($inputJSON);
690 GlobalHelper::requirePostMethod();
691 try {
692 $input = GlobalHelper::formatRequestData();
693 } catch (\InvalidArgumentException $e) {
694 wp_send_json_error($e->getMessage(), 400);
695 }
696 echo wp_json_encode($input);
697 die;
698 } else {
699 wp_send_json_error(
700 __(
701 'Token expired',
702 'bit-form'
703 ),
704 401
705 );
706 }
707 }
708
709 public function getFormEntryLabelAndCount()
710 {
711 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
712 $this->verifyAdminPermission();
713 // $inputJSON = wp_unslash($_POST['data']);
714 // $input = json_decode($inputJSON);
715 GlobalHelper::requirePostMethod();
716 try {
717 $input = GlobalHelper::formatRequestData();
718 } catch (\InvalidArgumentException $e) {
719 wp_send_json_error($e->getMessage(), 400);
720 }
721
722 $formHandler = FormHandler::getInstance();
723 $status = $formHandler->admin->getFormEntryLabelAndCount($_REQUEST, $input);
724 if (is_wp_error($status)) {
725 wp_send_json_error($status->get_error_message(), 411);
726 } else {
727 wp_send_json_success($status, 200);
728 }
729 } else {
730 wp_send_json_error(
731 __(
732 'Token expired',
733 'bit-form'
734 ),
735 401
736 );
737 }
738 }
739
740 public function getFormEntry()
741 {
742 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
743 $this->verifyAdminPermission();
744 GlobalHelper::requirePostMethod();
745 try {
746 $input = GlobalHelper::formatRequestData();
747 } catch (\InvalidArgumentException $e) {
748 wp_send_json_error($e->getMessage(), 400);
749 }
750 $formHandler = FormHandler::getInstance();
751 $sanitized_request = [
752 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
753 ];
754 $status = $formHandler->admin->getFormEntry($sanitized_request, $input);
755 if (is_wp_error($status)) {
756 wp_send_json_error($status->get_error_message(), 411);
757 } else {
758 wp_send_json_success($status, 200);
759 }
760 } else {
761 wp_send_json_error(__('Token expired', 'bit-form'), 401);
762 }
763 }
764
765 public function getEntriesForReport()
766 {
767 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
768 $this->verifyAdminPermission();
769 GlobalHelper::requirePostMethod();
770 try {
771 $input = GlobalHelper::formatRequestData();
772 } catch (\InvalidArgumentException $e) {
773 wp_send_json_error($e->getMessage(), 400);
774 }
775 $formHandler = FormHandler::getInstance();
776 $sanitized_request = [
777 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
778 ];
779 $status = $formHandler->admin->getEntriesForReport($sanitized_request, $input);
780 if (is_wp_error($status)) {
781 wp_send_json_error($status->get_error_message(), 411);
782 } else {
783 wp_send_json_success($status, 200);
784 }
785 } else {
786 wp_send_json_error(__('Token expired', 'bit-form'), 401);
787 }
788 }
789
790 public function filterExportEntry()
791 {
792 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
793 $this->verifyAdminPermission();
794 GlobalHelper::requirePostMethod();
795 try {
796 $input = GlobalHelper::formatRequestData();
797 } catch (\InvalidArgumentException $e) {
798 wp_send_json_error($e->getMessage(), 400);
799 }
800 $formHandler = FormHandler::getInstance();
801 $status = $formHandler->admin->getExportEntry($input->data);
802 if (is_wp_error($status)) {
803 wp_send_json_error($status->get_error_message(), 411);
804 } else {
805 wp_send_json_success($status, 200);
806 }
807 } else {
808 wp_send_json_error(__('Token expired', 'bit-form'), 401);
809 }
810 }
811
812 /**
813 * Undocumented function
814 *
815 * @return void
816 */
817 public function updateForm()
818 {
819 \ignore_user_abort();
820 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
821 $this->verifyAdminPermission();
822 GlobalHelper::requirePostMethod();
823 try {
824 $input = GlobalHelper::formatRequestData();
825 } catch (\InvalidArgumentException $e) {
826 wp_send_json_error($e->getMessage(), 400);
827 }
828 $formHandler = FormHandler::getInstance();
829 $status = $formHandler->admin->updateForm([], $input);
830 if (isset($input->customCodes)) {
831 FrontEndScriptGenerator::customCodeFile($input->id, $input->customCodes);
832 }
833 if (is_wp_error($status)) {
834 wp_send_json_error($status->get_error_message(), 411);
835 } else {
836 wp_send_json_success($status, 200);
837 }
838 } else {
839 wp_send_json_error(
840 __(
841 'Token expired',
842 'bit-form'
843 ),
844 401
845 );
846 }
847 }
848
849 public function saveCss()
850 {
851 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
852 $this->verifyAdminPermission();
853 GlobalHelper::requirePostMethod();
854 try {
855 $input = GlobalHelper::formatRequestData();
856 } catch (\InvalidArgumentException $e) {
857 wp_send_json_error($e->getMessage(), 400);
858 }
859
860 $formId = isset($input->form_id) ? sanitize_text_field($input->form_id) : '';
861 if (!filter_var($formId, FILTER_VALIDATE_INT)) {
862 wp_send_json_error(__('Invalid form id', 'bit-form'), 400);
863 }
864 if (isset($input->atomicCssText)) {
865 $status = FrontEndScriptGenerator::saveCssFile($formId, $input->atomicCssText);
866 }
867 if (isset($input->atomicCssWithFormIdText)) {
868 $status = FrontEndScriptGenerator::saveCssFile("{$formId}-formid", $input->atomicCssWithFormIdText);
869 }
870 if (isset($input->atomicClassMap) || isset($input->atomicClassMap)) {
871 $formModel = new FormModel();
872 $atomicClsMap = [
873 'atomic_class_map' => isset($input->atomicClassMap) ? $input->atomicClassMap : (object) [],
874 'atomic_class_map_with_form_id' => isset($input->atomicClassMapWithFormId) ? $input->atomicClassMapWithFormId : (object) [],
875 ];
876 $updateData['atomic_class_map'] = wp_json_encode($atomicClsMap);
877 $formModel->update(
878 $updateData,
879 [
880 'id' => $formId,
881 ]
882 );
883 }
884 if (is_wp_error($status)) {
885 wp_send_json_error($status->get_error_message(), 411);
886 } else {
887 wp_send_json_success($status, 200);
888 }
889 } else {
890 wp_send_json_error(
891 __(
892 'Token expired',
893 'bit-form'
894 ),
895 401
896 );
897 }
898 }
899
900 public function createNewForm()
901 {
902 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
903 $this->verifyAdminPermission();
904 GlobalHelper::requirePostMethod();
905 try {
906 $input = GlobalHelper::formatRequestData();
907 } catch (\InvalidArgumentException $e) {
908 wp_send_json_error($e->getMessage(), 400);
909 }
910 $formHandler = FormHandler::getInstance();
911 $sanitized_request = [
912 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
913 ];
914 $status = $formHandler->admin->createNewForm($sanitized_request, $input);
915 $formId = sanitize_text_field($input->form_id);
916 if (isset($input->customCodes)) {
917 FrontEndScriptGenerator::customCodeFile($formId, $input->customCodes);
918 }
919 if (is_wp_error($status)) {
920 wp_send_json_error($status->get_error_message(), 411);
921 } else {
922 wp_send_json_success($status, 200);
923 }
924 } else {
925 wp_send_json_error(
926 __(
927 'Token expired',
928 'bit-form'
929 ),
930 401
931 );
932 }
933 }
934
935 public function updateEntryStatus()
936 {
937 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
938 $this->verifyAdminPermission();
939 GlobalHelper::requirePostMethod();
940 try {
941 $input = GlobalHelper::formatRequestData();
942 } catch (\InvalidArgumentException $e) {
943 wp_send_json_error($e->getMessage(), 400);
944 }
945 $formId = $input->formId;
946 $entryId = $input->entryId;
947 $formEntryModel = new FormEntryModel();
948 $updatedTime = current_time('mysql');
949 $status = $formEntryModel->update(
950 [
951 'status' => 0,
952 'updated_at' => $updatedTime,
953 ],
954 [
955 'form_id' => $formId,
956 'id' => $entryId,
957 ]
958 );
959 if (is_wp_error($status)) {
960 wp_send_json_error($status->get_error_message(), 411);
961 } else {
962 wp_send_json_success(['update_at_time' => $updatedTime], 200);
963 }
964 } else {
965 wp_send_json_error(
966 __(
967 'Token expired',
968 'bit-form'
969 ),
970 401
971 );
972 }
973 }
974
975 public function changeFormStatus()
976 {
977 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
978 $this->verifyAdminPermission();
979 // $inputJSON = wp_unslash($_POST['data']);
980 // $input = json_decode($inputJSON);
981
982 GlobalHelper::requirePostMethod();
983 try {
984 $input = GlobalHelper::formatRequestData();
985 } catch (\InvalidArgumentException $e) {
986 wp_send_json_error($e->getMessage(), 400);
987 }
988
989 $formHandler = FormHandler::getInstance();
990 $sanitized_request = [
991 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
992 ];
993 $status = $formHandler->admin->changeFormStatus($sanitized_request, $input);
994 if (is_wp_error($status)) {
995 wp_send_json_error($status->get_error_message(), 411);
996 } else {
997 wp_send_json_success($status, 200);
998 }
999 } else {
1000 wp_send_json_error(
1001 __(
1002 'Token expired',
1003 'bit-form'
1004 ),
1005 401
1006 );
1007 }
1008 }
1009
1010 public function changeBulkFormStatus()
1011 {
1012 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1013 $this->verifyAdminPermission();
1014 GlobalHelper::requirePostMethod();
1015 try {
1016 $input = GlobalHelper::formatRequestData();
1017 } catch (\InvalidArgumentException $e) {
1018 wp_send_json_error($e->getMessage(), 400);
1019 }
1020 $formHandler = FormHandler::getInstance();
1021 $sanitized_request = [
1022 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1023 ];
1024 $status = $formHandler->admin->changeBulkFormStatus($sanitized_request, $input);
1025 if (is_wp_error($status)) {
1026 wp_send_json_error($status->get_error_message(), 411);
1027 } else {
1028 wp_send_json_success($status, 200);
1029 }
1030 } else {
1031 wp_send_json_error(
1032 __(
1033 'Token expired',
1034 'bit-form'
1035 ),
1036 401
1037 );
1038 }
1039 }
1040
1041 public function getAForm()
1042 {
1043 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1044 $this->verifyAdminPermission();
1045 GlobalHelper::requirePostMethod();
1046 try {
1047 $input = GlobalHelper::formatRequestData();
1048 } catch (\InvalidArgumentException $e) {
1049 wp_send_json_error($e->getMessage(), 400);
1050 }
1051 $formHandler = FormHandler::getInstance();
1052 $sanitized_request = [
1053 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1054 ];
1055 $status = $formHandler->admin->getAForm($sanitized_request, $input);
1056 if (is_wp_error($status)) {
1057 wp_send_json_error($status->get_error_message(), 411);
1058 } else {
1059 wp_send_json_success($status, 200);
1060 }
1061 } else {
1062 wp_send_json_error(
1063 __(
1064 'Token expired',
1065 'bit-form'
1066 ),
1067 401
1068 );
1069 }
1070 }
1071
1072 public function duplicateAForm()
1073 {
1074 \ignore_user_abort();
1075 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1076 $this->verifyAdminPermission();
1077 GlobalHelper::requirePostMethod();
1078 try {
1079 $input = GlobalHelper::formatRequestData();
1080 } catch (\InvalidArgumentException $e) {
1081 wp_send_json_error($e->getMessage(), 400);
1082 }
1083 $formHandler = FormHandler::getInstance();
1084 $sanitized_request = [
1085 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1086 ];
1087 $status = $formHandler->admin->duplicateAForm($sanitized_request, $input);
1088 if (is_wp_error($status)) {
1089 wp_send_json_error($status->get_error_message(), 411);
1090 } else {
1091 wp_send_json_success($status, 200);
1092 }
1093 } else {
1094 wp_send_json_error(
1095 __(
1096 'Token expired',
1097 'bit-form'
1098 ),
1099 401
1100 );
1101 }
1102 }
1103
1104 public function importAForm()
1105 {
1106 \ignore_user_abort();
1107 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1108 $this->verifyAdminPermission();
1109 GlobalHelper::requirePostMethod();
1110 try {
1111 $input = GlobalHelper::formatRequestData();
1112 } catch (\InvalidArgumentException $e) {
1113 wp_send_json_error($e->getMessage(), 400);
1114 }
1115 $formHandler = FormHandler::getInstance();
1116 $status = $formHandler->admin->importAForm($input);
1117 if (is_wp_error($status)) {
1118 wp_send_json_error($status->get_error_message(), 411);
1119 } else {
1120 wp_send_json_success($status, 200);
1121 }
1122 } else {
1123 wp_send_json_error(
1124 __(
1125 'Token expired',
1126 'bit-form'
1127 ),
1128 401
1129 );
1130 }
1131 }
1132
1133 // public function exportAForm() {
1134 // if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1135 // $formHandler = FormHandler::getInstance();
1136 // $status = $formHandler->admin->exportAForm($_REQUEST);
1137 // if (is_wp_error($status)) {
1138 // wp_send_json_error($status->get_error_message(), 411);
1139 // }
1140 // } else {
1141 // wp_send_json_error(
1142 // __(
1143 // 'Token expired',
1144 // 'bit-form'
1145 // ),
1146 // 401
1147 // );
1148 // }
1149 // }
1150 public function exportAForm()
1151 {
1152 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1153 $this->verifyAdminPermission();
1154 GlobalHelper::requirePostMethod();
1155 try {
1156 $input = GlobalHelper::formatRequestData();
1157 } catch (\InvalidArgumentException $e) {
1158 wp_send_json_error($e->getMessage(), 400);
1159 }
1160 $formHandler = FormHandler::getInstance();
1161 $sanitized_request = [
1162 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1163 ];
1164 $status = $formHandler->admin->exportAForm($sanitized_request, $input);
1165 if (is_wp_error($status)) {
1166 wp_send_json_error($status->get_error_message(), 411);
1167 } else {
1168 wp_send_json_success($status);
1169 }
1170 } else {
1171 wp_send_json_error(
1172 __(
1173 'Token expired',
1174 'bit-form'
1175 ),
1176 401
1177 );
1178 }
1179 }
1180
1181 public function deleteAForm()
1182 {
1183 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1184 $this->verifyAdminPermission();
1185 GlobalHelper::requirePostMethod();
1186 try {
1187 $input = GlobalHelper::formatRequestData();
1188 } catch (\InvalidArgumentException $e) {
1189 wp_send_json_error($e->getMessage(), 400);
1190 }
1191 $formHandler = FormHandler::getInstance();
1192 $sanitized_request = [
1193 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1194 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1195 ];
1196 $status = $formHandler->admin->deleteAForm($sanitized_request, $input);
1197 if (is_wp_error($status)) {
1198 wp_send_json_error($status->get_error_message(), 411);
1199 } else {
1200 wp_send_json_success($status, 200);
1201 }
1202 } else {
1203 wp_send_json_error(
1204 __(
1205 'Token expired',
1206 'bit-form'
1207 ),
1208 401
1209 );
1210 }
1211 }
1212
1213 public function deleteBlukForm()
1214 {
1215 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1216 $this->verifyAdminPermission();
1217 GlobalHelper::requirePostMethod();
1218 try {
1219 $input = GlobalHelper::formatRequestData();
1220 } catch (\InvalidArgumentException $e) {
1221 wp_send_json_error($e->getMessage(), 400);
1222 }
1223 $formHandler = FormHandler::getInstance();
1224 $sanitized_request = [
1225 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1226 'entries' => isset($_REQUEST['entries']) && is_array($_REQUEST['entries'])
1227 ? array_map('absint', $_REQUEST['entries'])
1228 : [],
1229 ];
1230 $status = $formHandler->admin->deleteBlukForm($sanitized_request, $input);
1231 if (is_wp_error($status)) {
1232 wp_send_json_error($status->get_error_message(), 411);
1233 } else {
1234 wp_send_json_success($status, 200);
1235 }
1236 } else {
1237 wp_send_json_error(
1238 __(
1239 'Token expired',
1240 'bit-form'
1241 ),
1242 401
1243 );
1244 }
1245 }
1246
1247 public function deleteBlukFormEntries()
1248 {
1249 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1250 $this->verifyAdminPermission();
1251 GlobalHelper::requirePostMethod();
1252 try {
1253 $input = GlobalHelper::formatRequestData();
1254 } catch (\InvalidArgumentException $e) {
1255 wp_send_json_error($e->getMessage(), 400);
1256 }
1257 $formHandler = FormHandler::getInstance();
1258 $sanitized_request = [
1259 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1260 'entries' => isset($_REQUEST['entries']) && is_array($_REQUEST['entries'])
1261 ? array_map('absint', $_REQUEST['entries'])
1262 : [],
1263 ];
1264 $status = $formHandler->admin->deleteBlukFormEntries($sanitized_request, $input);
1265 if (is_wp_error($status)) {
1266 wp_send_json_error($status->get_error_message(), 411);
1267 } else {
1268 wp_send_json_success($status, 200);
1269 }
1270 } else {
1271 wp_send_json_error(
1272 __(
1273 'Token expired',
1274 'bit-form'
1275 ),
1276 401
1277 );
1278 }
1279 }
1280
1281 public function duplicateFormEntry()
1282 {
1283 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1284 $this->verifyAdminPermission();
1285 GlobalHelper::requirePostMethod();
1286 try {
1287 $input = GlobalHelper::formatRequestData();
1288 } catch (\InvalidArgumentException $e) {
1289 wp_send_json_error($e->getMessage(), 400);
1290 }
1291 $formHandler = FormHandler::getInstance();
1292 $sanitized_request = [
1293 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1294 'entries' => isset($_REQUEST['entries']) && is_array($_REQUEST['entries'])
1295 ? array_map('absint', $_REQUEST['entries'])
1296 : [],
1297 ];
1298 $status = $formHandler->admin->duplicateFormEntry($sanitized_request, $input);
1299 if (is_wp_error($status)) {
1300 wp_send_json_error($status->get_error_message(), 411);
1301 } else {
1302 wp_send_json_success($status, 200);
1303 }
1304 } else {
1305 wp_send_json_error(
1306 __(
1307 'Token expired',
1308 'bit-form'
1309 ),
1310 401
1311 );
1312 }
1313 }
1314
1315 public function editFormEntry()
1316 {
1317 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1318 $this->verifyAdminPermission();
1319 // $inputJSON = wp_unslash($_POST['data']);
1320 // $input = json_decode($inputJSON);
1321 GlobalHelper::requirePostMethod();
1322 try {
1323 $input = GlobalHelper::formatRequestData();
1324 } catch (\InvalidArgumentException $e) {
1325 wp_send_json_error($e->getMessage(), 400);
1326 }
1327 $formHandler = FormHandler::getInstance();
1328 $sanitized_request = [
1329 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1330 'entryID' => isset($_REQUEST['entryID']) ? absint($_REQUEST['entryID']) : 0,
1331 ];
1332 $status = $formHandler->admin->editFormEntry($sanitized_request, $input);
1333 if (is_wp_error($status)) {
1334 wp_send_json_error($status->get_error_message(), 411);
1335 } else {
1336 wp_send_json_success($status, 200);
1337 }
1338 } else {
1339 wp_send_json_error(
1340 __(
1341 'Token expired',
1342 'bit-form'
1343 ),
1344 401
1345 );
1346 }
1347 }
1348
1349 public function getLogHistory()
1350 {
1351 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1352 $this->verifyAdminPermission();
1353 // $inputJSON = wp_unslash($_POST['data']);
1354 // $input = json_decode($inputJSON);
1355
1356 GlobalHelper::requirePostMethod();
1357 try {
1358 $input = GlobalHelper::formatRequestData();
1359 } catch (\InvalidArgumentException $e) {
1360 wp_send_json_error($e->getMessage(), 400);
1361 }
1362
1363 $formHandler = FormHandler::getInstance();
1364 $sanitized_request = [
1365 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1366 'entryID' => isset($_REQUEST['entryID']) ? absint($_REQUEST['entryID']) : 0,
1367 ];
1368 $status = $formHandler->admin->getLogHistory($sanitized_request, $input);
1369 if (is_wp_error($status)) {
1370 wp_send_json_error($status->get_error_message(), 411);
1371 } else {
1372 wp_send_json_success($status, 200);
1373 }
1374 } else {
1375 wp_send_json_error(
1376 __(
1377 'Token expired',
1378 'bit-form'
1379 ),
1380 401
1381 );
1382 }
1383 }
1384
1385 public function updateFormEntry()
1386 {
1387 \ignore_user_abort();
1388 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1389 $this->verifyAdminPermission();
1390 $formHandler = FormHandler::getInstance();
1391 $sanitized_request = [
1392 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1393 'entryID' => isset($_REQUEST['entryID']) ? absint($_REQUEST['entryID']) : 0,
1394 ];
1395 $status = $formHandler->admin->updateFormEntry($sanitized_request, wp_unslash($_POST));
1396
1397 if (is_wp_error($status)) {
1398 wp_send_json_error($status->get_error_message(), 411);
1399 } else {
1400 $status = IntegrationHandler::maybeSetCronForIntegration($status, 'update');
1401 wp_send_json_success($status, 200);
1402 }
1403 } else {
1404 wp_send_json_error(
1405 __(
1406 'Token expired',
1407 'bit-form'
1408 ),
1409 401
1410 );
1411 }
1412 }
1413
1414 public function getAllWPPages()
1415 {
1416 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1417 $this->verifyAdminPermission();
1418 // $inputJSON = wp_unslash($_POST['data']);
1419 // $input = json_decode($inputJSON);
1420
1421 GlobalHelper::requirePostMethod();
1422 try {
1423 $input = GlobalHelper::formatRequestData();
1424 } catch (\InvalidArgumentException $e) {
1425 wp_send_json_error($e->getMessage(), 400);
1426 }
1427
1428 $formHandler = FormHandler::getInstance();
1429 $sanitized_request = [
1430 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1431 ];
1432 $status = $formHandler->admin->getAllWPPages($sanitized_request, $input);
1433 if (is_wp_error($status)) {
1434 wp_send_json_error($status->get_error_message(), 411);
1435 } else {
1436 wp_send_json_success($status, 200);
1437 }
1438 } else {
1439 wp_send_json_error(
1440 __(
1441 'Token expired',
1442 'bit-form'
1443 ),
1444 401
1445 );
1446 }
1447 }
1448
1449 public function deleteSuccessMessage()
1450 {
1451 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1452 $this->verifyAdminPermission();
1453 // $inputJSON = wp_unslash($_POST['data']);
1454 // $input = json_decode($inputJSON);
1455 GlobalHelper::requirePostMethod();
1456 try {
1457 $input = GlobalHelper::formatRequestData();
1458 } catch (\InvalidArgumentException $e) {
1459 wp_send_json_error($e->getMessage(), 400);
1460 }
1461 $formHandler = FormHandler::getInstance();
1462 $sanitized_request = [
1463 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1464 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1465 ];
1466 $status = $formHandler->admin->deleteSuccessMessage($sanitized_request, $input);
1467 if (is_wp_error($status)) {
1468 wp_send_json_error($status->get_error_message(), 411);
1469 } else {
1470 wp_send_json_success($status, 200);
1471 }
1472 } else {
1473 wp_send_json_error(
1474 __(
1475 'Token expired',
1476 'bit-form'
1477 ),
1478 401
1479 );
1480 }
1481 }
1482
1483 public function deleteAIntegration()
1484 {
1485 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1486 $this->verifyAdminPermission();
1487 // $inputJSON = wp_unslash($_POST['data']);
1488 // $input = json_decode($inputJSON);
1489
1490 GlobalHelper::requirePostMethod();
1491 try {
1492 $input = GlobalHelper::formatRequestData();
1493 } catch (\InvalidArgumentException $e) {
1494 wp_send_json_error($e->getMessage(), 400);
1495 }
1496
1497 $formHandler = FormHandler::getInstance();
1498 $sanitized_request = [
1499 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1500 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1501 ];
1502 $status = $formHandler->admin->deleteAIntegration($sanitized_request, $input);
1503 if (is_wp_error($status)) {
1504 wp_send_json_error($status->get_error_message(), 411);
1505 } else {
1506 wp_send_json_success($status, 200);
1507 }
1508 } else {
1509 wp_send_json_error(
1510 __(
1511 'Token expired',
1512 'bit-form'
1513 ),
1514 401
1515 );
1516 }
1517 }
1518
1519 public function updateIntegrationStatus()
1520 {
1521 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1522 $this->verifyAdminPermission();
1523 GlobalHelper::requirePostMethod();
1524 try {
1525 $input = GlobalHelper::formatRequestData();
1526 } catch (\InvalidArgumentException $e) {
1527 wp_send_json_error($e->getMessage(), 400);
1528 }
1529 $formID = isset($input->formID) ? sanitize_text_field(wp_unslash($input->formID)) : '';
1530 $integrationID = isset($input->id) ? absint($input->id) : 0;
1531 $status = isset($input->status) ? absint($input->status) : 0;
1532 if (empty($formID) || empty($integrationID)) {
1533 wp_send_json_error(__('Invalid request', 'bit-form'), 400);
1534 }
1535 $integrationHandler = new IntegrationHandler($formID);
1536 $result = $integrationHandler->updateIntegrationStatus($integrationID, $status);
1537 if (is_wp_error($result) && 'result_empty' !== $result->get_error_code()) {
1538 wp_send_json_error($result->get_error_message(), 411);
1539 } else {
1540 wp_send_json_success(['id' => $integrationID, 'status' => $status], 200);
1541 }
1542 } else {
1543 wp_send_json_error(__('Token expired', 'bit-form'), 401);
1544 }
1545 }
1546
1547 public function deleteAWorkflow()
1548 {
1549 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1550 $this->verifyAdminPermission();
1551 // $inputJSON = wp_unslash($_POST['data']);
1552 // $input = json_decode($inputJSON);
1553 GlobalHelper::requirePostMethod();
1554
1555 try {
1556 $input = GlobalHelper::formatRequestData();
1557 } catch (\InvalidArgumentException $e) {
1558 wp_send_json_error($e->getMessage(), 400);
1559 }
1560
1561 $formHandler = FormHandler::getInstance();
1562 $sanitized_request = [
1563 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1564 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1565 ];
1566 $status = $formHandler->admin->deleteAWorkflow($sanitized_request, $input);
1567 if (is_wp_error($status)) {
1568 wp_send_json_error($status->get_error_message(), 411);
1569 } else {
1570 wp_send_json_success($status, 200);
1571 }
1572 } else {
1573 wp_send_json_error(
1574 __(
1575 'Token expired',
1576 'bit-form'
1577 ),
1578 401
1579 );
1580 }
1581 }
1582
1583 public function deleteAMailTemplate()
1584 {
1585 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1586 $this->verifyAdminPermission();
1587 // $inputJSON = wp_unslash($_POST['data']);
1588 // $input = json_decode($inputJSON);
1589 GlobalHelper::requirePostMethod();
1590
1591 try {
1592 $input = GlobalHelper::formatRequestData();
1593 } catch (\InvalidArgumentException $e) {
1594 wp_send_json_error($e->getMessage(), 400);
1595 }
1596
1597 $formHandler = FormHandler::getInstance();
1598 $sanitized_request = [
1599 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1600 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1601 ];
1602 $status = $formHandler->admin->deleteAMailTemplate($sanitized_request, $input);
1603 if (is_wp_error($status)) {
1604 wp_send_json_error($status->get_error_message(), 411);
1605 } else {
1606 wp_send_json_success($status, 200);
1607 }
1608 } else {
1609 wp_send_json_error(
1610 __(
1611 'Token expired',
1612 'bit-form'
1613 ),
1614 401
1615 );
1616 }
1617 }
1618
1619 public function duplicateAMailTemplate()
1620 {
1621 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1622 $this->verifyAdminPermission();
1623 // $inputJSON = wp_unslash($_POST['data']);
1624 // $input = json_decode($inputJSON);
1625
1626 GlobalHelper::requirePostMethod();
1627 try {
1628 $input = GlobalHelper::formatRequestData();
1629 } catch (\InvalidArgumentException $e) {
1630 wp_send_json_error($e->getMessage(), 400);
1631 }
1632 $formHandler = FormHandler::getInstance();
1633 $sanitized_request = [
1634 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1635 'id' => isset($_REQUEST['id']) ? absint($_REQUEST['id']) : 0,
1636 ];
1637 $status = $formHandler->admin->duplicateAMailTemplate($sanitized_request, $input);
1638 if (is_wp_error($status)) {
1639 wp_send_json_error($status->get_error_message(), 411);
1640 } else {
1641 wp_send_json_success($status, 200);
1642 }
1643 } else {
1644 wp_send_json_error(
1645 __(
1646 'Token expired',
1647 'bit-form'
1648 ),
1649 401
1650 );
1651 }
1652 }
1653
1654 public function setAllFormsReport()
1655 {
1656 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1657 $this->verifyAdminPermission();
1658 // $inputJSON = wp_unslash($_POST['data']);
1659 // $input = json_decode($inputJSON);
1660
1661 GlobalHelper::requirePostMethod();
1662
1663 try {
1664 $input = GlobalHelper::formatRequestData();
1665 } catch (\InvalidArgumentException $e) {
1666 wp_send_json_error($e->getMessage(), 400);
1667 }
1668
1669 $formHandler = FormHandler::getInstance();
1670 $sanitized_request = [
1671 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1672 ];
1673 $status = $formHandler->admin->setAllFormsReport($sanitized_request, $input);
1674 if (is_wp_error($status)) {
1675 wp_send_json_error($status->get_error_message(), 411);
1676 } else {
1677 wp_send_json_success($status, 200);
1678 }
1679 } else {
1680 wp_send_json_error(
1681 __(
1682 'Token expired',
1683 'bit-form'
1684 ),
1685 401
1686 );
1687 }
1688 }
1689
1690 public function savegReCaptcha()
1691 {
1692 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1693 $this->verifyAdminPermission();
1694 GlobalHelper::requirePostMethod();
1695
1696 try {
1697 $input = GlobalHelper::formatRequestData();
1698 } catch (\InvalidArgumentException $e) {
1699 wp_send_json_error($e->getMessage(), 400);
1700 }
1701
1702 $formHandler = FormHandler::getInstance();
1703 $sanitized_request = [
1704 'formID' => isset($_REQUEST['formID']) ? sanitize_text_field(wp_unslash($_REQUEST['formID'])) : '',
1705 ];
1706 $status = $formHandler->admin->savegReCaptcha($sanitized_request, $input);
1707 if (is_wp_error($status)) {
1708 wp_send_json_error($status->get_error_message(), 411);
1709 } else {
1710 wp_send_json_success($status, 200);
1711 }
1712 } else {
1713 wp_send_json_error(
1714 __(
1715 'Token expired',
1716 'bit-form'
1717 ),
1718 401
1719 );
1720 }
1721 }
1722
1723 public function saveApiKey()
1724 {
1725 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1726 $this->verifyAdminPermission();
1727 // $inputJSON = wp_unslash($_POST['data']);
1728 // $input = json_decode($inputJSON);
1729
1730 GlobalHelper::requirePostMethod();
1731 try {
1732 $input = GlobalHelper::formatRequestData();
1733 } catch (\InvalidArgumentException $e) {
1734 wp_send_json_error($e->getMessage(), 400);
1735 }
1736
1737 if (empty($input->api_key)) {
1738 $api_key = get_option('bitform_secret_api_key');
1739 } elseif (!empty($input->api_key)) {
1740 update_option('bitform_secret_api_key', sanitize_text_field($input->api_key));
1741 $api_key = $input->api_key;
1742 }
1743 if (!$api_key) {
1744 $api_key = hash('sha1', base64_encode(12345));
1745 update_option('bitform_secret_api_key', $api_key);
1746 }
1747 wp_send_json_success($api_key, 200);
1748 } else {
1749 wp_send_json_error(
1750 __(
1751 'Token expired',
1752 'bit-form'
1753 ),
1754 401
1755 );
1756 }
1757 }
1758
1759 private function checkExtensionWithURL($urlStr)
1760 {
1761 // Extension is mandatory (no optional group): the URL must end in a known image extension,
1762 // otherwise any http(s) URL would pass and be fetched.
1763 $pattern = '/^https?:\/\/.*\.(svg|png|jpg|jpeg|gif)$/i';
1764
1765 return preg_match($pattern, $urlStr);
1766 }
1767
1768 public function iconUpload()
1769 {
1770 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1771 $this->verifyAdminPermission();
1772 // $inputJSON = wp_unslash($_POST['data']);
1773 // $input = json_decode($inputJSON);
1774
1775 GlobalHelper::requirePostMethod();
1776 try {
1777 $input = GlobalHelper::formatRequestData();
1778 } catch (\InvalidArgumentException $e) {
1779 wp_send_json_error($e->getMessage(), 400);
1780 }
1781
1782 $sanitize_url = sanitize_url($input->src);
1783
1784 if (!$this->checkExtensionWithURL($sanitize_url)) {
1785 return new WP_Error(
1786 'type_error',
1787 __('Invalid file type', 'bit-form')
1788 );
1789 }
1790
1791 $uploadDirInfo = wp_upload_dir();
1792 $wpUploadbaseDir = $uploadDirInfo['basedir'];
1793 $icnDir = $wpUploadbaseDir . DIRECTORY_SEPARATOR . 'bitforms' . DIRECTORY_SEPARATOR . 'icons';
1794
1795 if (!is_dir($icnDir)) {
1796 wp_mkdir_p($icnDir);
1797 }
1798
1799 $response = wp_safe_remote_get($sanitize_url);
1800 if (is_wp_error($response) || 200 !== (int) wp_remote_retrieve_response_code($response)) {
1801 wp_send_json_error(__('Unable to fetch icon', 'bit-form'), 400);
1802 }
1803 $imageUrlData = wp_remote_retrieve_body($response);
1804
1805 $filename = sanitize_file_name($input->id . '-' . basename($sanitize_url));
1806
1807 $validation = wp_check_filetype($filename);
1808 $type = $validation['type'];
1809 $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
1810 $is_svg = 'svg' === $ext; // Check if the file is an SVG
1811 if ($type && 0 === strpos($type, 'image/') || $is_svg) {
1812 if ($is_svg) {
1813 $imageUrlData = (new \BitCode\BitForm\enshrined\svgSanitize\Sanitizer())->sanitize($imageUrlData);
1814 if (false === $imageUrlData) {
1815 wp_send_json_error(__('Invalid file type', 'bit-form'), 400);
1816 }
1817 }
1818 $uploaded = file_put_contents($icnDir . '/' . $filename, $imageUrlData);
1819
1820 if ($uploaded) {
1821 $uploadedFile = BITFORMS_UPLOAD_BASE_URL . '/' . 'icons' . '/' . $filename;
1822 wp_send_json_success($uploadedFile, 200);
1823 }
1824 } else {
1825 wp_send_json_error(
1826 __(
1827 'Invalid file type',
1828 'bit-form'
1829 ),
1830 401
1831 );
1832 }
1833 } else {
1834 wp_send_json_error(
1835 __(
1836 'Token expired',
1837 'bit-form'
1838 ),
1839 401
1840 );
1841 }
1842 }
1843
1844 private function getFiles()
1845 {
1846 $uploadDirInfo = wp_upload_dir();
1847 $wpUploadbaseDir = $uploadDirInfo['basedir'];
1848 $icnDir = $wpUploadbaseDir . DIRECTORY_SEPARATOR . 'bitforms' . DIRECTORY_SEPARATOR . 'icons';
1849 $files = [];
1850 if (file_exists($icnDir)) {
1851 $openDir = opendir($icnDir . DIRECTORY_SEPARATOR);
1852
1853 while (false !== ($fileName = readdir($openDir))) {
1854 $ext = substr($fileName, strrpos($fileName, '.') + 1);
1855 if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'svg'])) {
1856 $files[] = $fileName;
1857 }
1858 }
1859
1860 closedir($openDir);
1861 }
1862 return $files;
1863 }
1864
1865 public function getDownlodedIcons()
1866 {
1867 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1868 $this->verifyAdminPermission();
1869 $files = $this->getFiles();
1870 wp_send_json_success($files, 200);
1871 } else {
1872 wp_send_json_error(
1873 __(
1874 'Token expired',
1875 'bit-form'
1876 ),
1877 401
1878 );
1879 }
1880 }
1881
1882 public function iconRemove()
1883 {
1884 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1885 $this->verifyAdminPermission();
1886 // $inputJSON = wp_unslash($_POST['data']);
1887 // $input = json_decode($inputJSON);
1888
1889 GlobalHelper::requirePostMethod();
1890
1891 try {
1892 $input = GlobalHelper::formatRequestData();
1893 } catch (\InvalidArgumentException $e) {
1894 wp_send_json_error($e->getMessage(), 400);
1895 }
1896
1897 $uploadDirInfo = wp_upload_dir();
1898
1899 $wpUploadbaseDir = $uploadDirInfo['basedir'];
1900 $icnDir = $wpUploadbaseDir . DIRECTORY_SEPARATOR . 'bitforms' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR;
1901 $sanitizeFileName = sanitize_file_name($input->file);
1902 $filePath = $icnDir . $sanitizeFileName;
1903 if (file_exists($filePath)) {
1904 wp_delete_file($filePath);
1905 wp_send_json_success($this->getFiles(), 200);
1906 }
1907 } else {
1908 wp_send_json_error(
1909 __(
1910 'Token expired',
1911 'bit-form'
1912 ),
1913 401
1914 );
1915 }
1916 }
1917
1918 public function addCustomCode()
1919 {
1920 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1921 $this->verifyAdminPermission();
1922 // $inputJSON = wp_unslash($_POST['data']);
1923 // $input = json_decode($inputJSON);
1924
1925 GlobalHelper::requirePostMethod();
1926
1927 try {
1928 $input = GlobalHelper::formatRequestData();
1929 } catch (\InvalidArgumentException $e) {
1930 wp_send_json_error($e->getMessage(), 400);
1931 }
1932
1933 $formId = sanitize_text_field($input->form_id);
1934 if (filter_var($formId, FILTER_VALIDATE_INT)) {
1935 FrontEndScriptGenerator::customCodeFile($formId, $input->customCodes);
1936 $status = ['message' => 'File Update Successfully..'];
1937 if (is_wp_error($status)) {
1938 wp_send_json_error($status->get_error_message(), 411);
1939 } else {
1940 wp_send_json_success($status, 200);
1941 }
1942 } else {
1943 wp_send_json_error(
1944 __(
1945 'Invalid form id',
1946 'bit-form'
1947 ),
1948 401
1949 );
1950 }
1951 } else {
1952 wp_send_json_error(
1953 __(
1954 'Token expired',
1955 'bit-form'
1956 ),
1957 401
1958 );
1959 }
1960 }
1961
1962 public function getCustomCode()
1963 {
1964 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
1965 $this->verifyAdminPermission();
1966 // $inputJSON = wp_unslash($_POST['data']);
1967 // $input = json_decode($inputJSON);
1968
1969 GlobalHelper::requirePostMethod();
1970
1971 try {
1972 $input = GlobalHelper::formatRequestData();
1973 } catch (\InvalidArgumentException $e) {
1974 wp_send_json_error($e->getMessage(), 400);
1975 }
1976
1977 $formId = sanitize_text_field($input->form_id);
1978
1979 if (filter_var($formId, FILTER_VALIDATE_INT)) {
1980 $status = FrontEndScriptGenerator::getCustomCodes($formId);
1981 if (is_wp_error($status)) {
1982 wp_send_json_error($status->get_error_message(), 411);
1983 } else {
1984 wp_send_json_success($status, 200);
1985 }
1986 } else {
1987 wp_send_json_error(
1988 __(
1989 'Invalid form id',
1990 'bit-form'
1991 ),
1992 401
1993 );
1994 }
1995 } else {
1996 wp_send_json_error(
1997 __(
1998 'Token expired',
1999 'bit-form'
2000 ),
2001 401
2002 );
2003 }
2004 }
2005
2006 public function getGenerelSettings()
2007 {
2008 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2009 $this->verifyAdminPermission();
2010 $data = get_option('bitform_app_config', (object) ['cache_plugin' => true, 'delete_table' => 0]);
2011
2012 if (is_wp_error($data)) {
2013 wp_send_json_error($data->get_error_message(), 411);
2014 } else {
2015 if (empty($data)) {
2016 $data = (object)[];
2017 }
2018 wp_send_json_success($data, 200);
2019 }
2020 } else {
2021 wp_send_json_error(
2022 __(
2023 'Token expired',
2024 'bit-form'
2025 ),
2026 401
2027 );
2028 }
2029 }
2030
2031 public function saveGenerelSettings()
2032 {
2033 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2034 $this->verifyAdminPermission();
2035 GlobalHelper::requirePostMethod();
2036
2037 try {
2038 $inputData = GlobalHelper::formatRequestData();
2039 } catch (\InvalidArgumentException $e) {
2040 wp_send_json_error($e->getMessage(), 400);
2041 return;
2042 }
2043
2044 $status = update_option('bitform_app_config', $inputData->config);
2045 if (is_wp_error($status)) {
2046 wp_send_json_error($status->get_error_message(), 411);
2047 } else {
2048 if (Helpers::property_exists_nested($inputData, 'config->cache_plugin', true)) {
2049 $formHandler = FormHandler::getInstance();
2050 $formHandler->admin->updateGeneratedScriptPageIds();
2051 }
2052 wp_send_json_success(__('Save successfully done', 'bit-form'));
2053 }
2054 } else {
2055 wp_send_json_error(
2056 __(
2057 'Token expired',
2058 'bit-form'
2059 ),
2060 401
2061 );
2062 }
2063 }
2064
2065 public function saveGlobalMessages()
2066 {
2067 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2068 $this->verifyAdminPermission();
2069 // $inputJSON = wp_unslash($_POST['data']);
2070 // $inputData = json_decode($inputJSON);
2071
2072 GlobalHelper::requirePostMethod();
2073
2074 try {
2075 $inputData = GlobalHelper::formatRequestData();
2076 } catch (\InvalidArgumentException $e) {
2077 wp_send_json_error($e->getMessage(), 400);
2078 return;
2079 }
2080
2081 $appSettings = get_option('bitform_app_settings', (object) []);
2082
2083 $appSettings->globalMessages = $inputData;
2084 $status = update_option('bitform_app_settings', $appSettings);
2085 // delete_option('bitform_app_settings');
2086 if (is_wp_error($status)) {
2087 wp_send_json_error($status->get_error_message(), 411);
2088 } else {
2089 $formHandler = FormHandler::getInstance();
2090 $formHandler->admin->replaceAllFormsErrorMessagesByGlobalMessages();
2091 wp_send_json_success(__('Save successfully done', 'bit-form'));
2092 }
2093 } else {
2094 wp_send_json_error(
2095 __(
2096 'Token expired',
2097 'bit-form'
2098 ),
2099 401
2100 );
2101 }
2102 }
2103
2104 public function getPodsField()
2105 {
2106 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2107 $this->verifyAdminPermission();
2108 // $inputJSON = wp_unslash($_POST['data']);
2109 // $input = json_decode($inputJSON);
2110 GlobalHelper::requirePostMethod();
2111 try {
2112 $input = GlobalHelper::formatRequestData();
2113 } catch (\InvalidArgumentException $e) {
2114 wp_send_json_error($e->getMessage(), 400);
2115 }
2116 $podsAdminExists = is_plugin_active('pods/init.php');
2117
2118 $podField = [];
2119 if ($podsAdminExists) {
2120 $pods = pods($input->pod_type);
2121 $i = 0;
2122 foreach ($pods->fields as $field) {
2123 $i++;
2124 $podField[$i]['key'] = $field['name'];
2125 $podField[$i]['name'] = $field['label'];
2126 $podField[$i]['is-repeatable'] = $field['repeatable'] ?? 0;
2127 $podField[$i]['required'] = 1 === $field['options']['required'] ? true : false;
2128 }
2129 }
2130
2131 if (is_wp_error($podField)) {
2132 wp_send_json_error($podField, 411);
2133 } else {
2134 wp_send_json_success($podField, 200);
2135 }
2136 } else {
2137 wp_send_json_error(
2138 __(
2139 'Token expired',
2140 'bit-form'
2141 ),
2142 401
2143 );
2144 }
2145 }
2146
2147 public function getPodsType()
2148 {
2149 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2150 $this->verifyAdminPermission();
2151 $users = get_users(['fields' => ['ID', 'display_name']]);
2152 $pods = [];
2153 $podsAdminExists = is_plugin_active('pods/init.php');
2154 if ($podsAdminExists) {
2155 $allPods = pods_api()->load_pods();
2156 foreach ($allPods as $key => $pod) {
2157 $pods[$key]['name'] = $pod['name'];
2158 $pods[$key]['label'] = $pod['label'];
2159 }
2160 }
2161 $data = ['users' => $users, 'post_types' => $pods];
2162 wp_send_json_success($data, 200);
2163 } else {
2164 wp_send_json_error(
2165 __(
2166 'Token expired',
2167 'bit-form'
2168 ),
2169 401
2170 );
2171 }
2172 }
2173
2174 public function postTypeByUser()
2175 {
2176 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2177 $this->verifyAdminPermission();
2178 $users = get_users(
2179 [
2180 'fields' => ['ID', 'display_name', 'user_login', 'user_email', 'user_nicename'],
2181 ]
2182 );
2183
2184 $postTypes = $this->getPostTypes();
2185
2186 $data = ['post_types' => $postTypes, 'users' => $users];
2187 wp_send_json_success($data, 200);
2188 } else {
2189 wp_send_json_error(
2190 __(
2191 'Token expired',
2192 'bit-form'
2193 ),
2194 401
2195 );
2196 }
2197 }
2198
2199 public function getWPUsers()
2200 {
2201 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2202 $this->verifyAdminPermission();
2203
2204 $users = get_transient('bitforms_admin_users_list');
2205 if (false === $users) {
2206 $rawUsers = get_users(['fields' => ['ID', 'user_nicename', 'user_email', 'display_name']]);
2207 $userMail = [];
2208 $userNames = [];
2209 foreach ($rawUsers as $key => $value) {
2210 $userMail[$key] = [
2211 'label' => !empty($value->display_name) ? $value->display_name : '',
2212 'value' => !empty($value->user_email) ? $value->user_email : '',
2213 'id' => $value->ID,
2214 ];
2215 $userNames[$value->ID] = [
2216 'name' => $value->display_name,
2217 'url' => admin_url('user-edit.php?user_id=' . absint($value->ID)),
2218 ];
2219 }
2220 $users = ['userMail' => $userMail, 'user' => $userNames];
2221 set_transient('bitforms_admin_users_list', $users, 10 * MINUTE_IN_SECONDS);
2222 }
2223
2224 wp_send_json_success($users, 200);
2225 } else {
2226 wp_send_json_error(__('Token expired', 'bit-form'), 401);
2227 }
2228 }
2229
2230 public function getUserRoles()
2231 {
2232 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2233 $this->verifyAdminPermission();
2234 wp_send_json_success(['userRoles' => get_editable_roles()], 200);
2235 } else {
2236 wp_send_json_error(__('Token expired', 'bit-form'), 401);
2237 }
2238 }
2239
2240 public function clearUsersTransient(): void
2241 {
2242 delete_transient('bitforms_admin_users_list');
2243 }
2244
2245 private function getPostTypes()
2246 {
2247 $all_cpt = get_post_types([
2248 'public' => true,
2249 'exclude_from_search' => false,
2250 '_builtin' => false,
2251 'capability_type' => 'post',
2252
2253 ], 'objects');
2254 $cpt = [];
2255
2256 foreach ($all_cpt as $key => $post_type) {
2257 $cpt[$key]['name'] = $post_type->name;
2258 $cpt[$key]['label'] = $post_type->label;
2259 }
2260 $wp_post_types = get_post_types([
2261 'public' => true,
2262 '_builtin' => true,
2263 ]);
2264
2265 $wp_all_post_types = [];
2266
2267 foreach ($wp_post_types as $key => $post_type) {
2268 if ('attachment' !== $post_type) {
2269 $wp_all_post_types[$key]['name'] = $post_type;
2270 $wp_all_post_types[$key]['label'] = ucwords($post_type);
2271 }
2272 }
2273 return array_merge($wp_all_post_types, $cpt);
2274 }
2275
2276 public function getAcfGroupFields()
2277 {
2278 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2279 $this->verifyAdminPermission();
2280 $acfFields = [];
2281 $types = ['select', 'checkbox', 'radio'];
2282
2283 $field_groups = get_posts(['post_type' => 'acf-field-group']);
2284
2285 if ($field_groups) {
2286 $groups = acf_get_field_groups();
2287 foreach ($groups as $group) {
2288 foreach (acf_get_fields($group['key']) as $acfField) {
2289 if (in_array($acfField['type'], $types)) {
2290 array_push($acfFields, [
2291 'key' => $acfField['key'],
2292 'name' => $acfField['label'],
2293 'choices' => $acfField['choices'],
2294 'group_title' => $group['title'],
2295 'location' => $group['location'],
2296 ]);
2297 }
2298 }
2299 }
2300 }
2301
2302 wp_send_json_success($acfFields, 200);
2303 } else {
2304 wp_send_json_error(
2305 __(
2306 'Token expired',
2307 'bit-form'
2308 ),
2309 401
2310 );
2311 }
2312 }
2313
2314 public function getCustomField()
2315 {
2316 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2317 $this->verifyAdminPermission();
2318 // $inputJSON = wp_unslash($_POST['data']);
2319 // $input = json_decode($inputJSON);
2320
2321 GlobalHelper::requirePostMethod();
2322
2323 try {
2324 $input = GlobalHelper::formatRequestData();
2325 } catch (\InvalidArgumentException $e) {
2326 wp_send_json_error($e->getMessage(), 400);
2327 }
2328 $acfFields = [];
2329 $acfFiles = [];
2330
2331 $allowedFields = [
2332 'repeater',
2333 'text',
2334 'textarea',
2335 'password',
2336 'wysiwyg',
2337 'number',
2338 'radio',
2339 'color_picker',
2340 'oembed',
2341 'email',
2342 'url',
2343 'date_picker',
2344 'true_false',
2345 'date_time_picker',
2346 'time_picker',
2347 'message',
2348 'checkbox',
2349 'select',
2350 'post_object',
2351 'user',
2352 'file',
2353 'image',
2354 'gallery'
2355 ];
2356
2357 $field_groups = get_posts(['post_type' => 'acf-field-group']);
2358
2359 if ($field_groups) {
2360 $groups = acf_get_field_groups(['post_type' => $input->post_type]);
2361
2362 foreach ($groups as $group) {
2363 foreach (acf_get_fields($group['key']) as $acfField) {
2364 if (in_array($acfField['type'], $allowedFields)) {
2365 if ('repeater' === $acfField['type']) {
2366 foreach ($acfField['sub_fields'] as $subField) {
2367 if (in_array($subField['type'], $allowedFields)) {
2368 array_push($acfFields, [
2369 'key' => $acfField['key'] . '.' . $subField['key'],
2370 'name' => $acfField['label'] . '-' . $subField['label'],
2371 'required' => $subField['required'],
2372 ]);
2373 }
2374 }
2375 } elseif (in_array($acfField['type'], ['file', 'image', 'gallery'])) {
2376 array_push($acfFiles, [
2377 'key' => $acfField['key'],
2378 'name' => $acfField['label'],
2379 'required' => $acfField['required'],
2380 ]);
2381 } else {
2382 array_push($acfFields, [
2383 'key' => $acfField['key'],
2384 'name' => $acfField['label'],
2385 'required' => $acfField['required'],
2386 ]);
2387 }
2388 }
2389 }
2390 }
2391 }
2392
2393 wp_send_json_success(['acfFields' => $acfFields, 'acfFile' => $acfFiles], 200);
2394 } else {
2395 wp_send_json_error(
2396 __(
2397 'Token expired',
2398 'bit-form'
2399 ),
2400 401
2401 );
2402 }
2403 }
2404
2405 public function getMetaBoxFields()
2406 {
2407 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2408 $this->verifyAdminPermission();
2409 if (!function_exists('rwmb_meta')) {
2410 wp_send_json_error(__('Meta Box must be activated!', 'bit-form'));
2411 }
2412
2413 GlobalHelper::requirePostMethod();
2414
2415 try {
2416 $input = GlobalHelper::formatRequestData();
2417 } catch (\InvalidArgumentException $e) {
2418 wp_send_json_error($e->getMessage(), 400);
2419 }
2420
2421 $metaBoxFields = rwmb_get_object_fields($input->post_type);
2422
2423 $metaBoxFields = MetaBoxService::getMetaBoxFields($input->post_type);
2424
2425 wp_send_json_success(
2426 [
2427 'metaboxFields' => array_values($metaBoxFields['text_fields']),
2428 'metaboxFile' => array_values($metaBoxFields['file_fields']),
2429 ],
2430 200
2431 );
2432 } else {
2433 wp_send_json_error(
2434 __(
2435 'Token expired',
2436 'bit-form'
2437 ),
2438 401
2439 );
2440 }
2441 }
2442
2443 public function saveConversationalCSS()
2444 {
2445 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
2446 $this->verifyAdminPermission();
2447 // $inputJSON = wp_unslash($_POST['data']);
2448 // $requestsParams = json_decode($inputJSON);
2449 GlobalHelper::requirePostMethod();
2450 try {
2451 $requestsParams = GlobalHelper::formatRequestData();
2452 } catch (\InvalidArgumentException $e) {
2453 wp_send_json_error($e->getMessage(), 400);
2454 }
2455 $formId = sanitize_text_field($requestsParams->formID);
2456 $css = $requestsParams->css;
2457
2458 if (filter_var($formId, FILTER_VALIDATE_INT)) {
2459 $path = 'form-styles';
2460 $fileName = "bitform-conversational-$formId.css";
2461 FrontEndScriptGenerator::customCodeFileSaveOrDelete($css, $path, $fileName);
2462 wp_send_json_success(
2463 __(
2464 'Conversational CSS Saved Successfully!',
2465 'bit-form'
2466 ),
2467 200
2468 );
2469 } else {
2470 wp_send_json_error(
2471 __(
2472 'Invalid form id',
2473 'bit-form'
2474 ),
2475 401
2476 );
2477 }
2478 } else {
2479 wp_send_json_error(
2480 __(
2481 'Token expired',
2482 'bit-form'
2483 ),
2484 401
2485 );
2486 }
2487 }
2488 }
2489