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