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

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