| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.12 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Form; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use \FireBox\Core\Helpers\Form\Form; |
| 20 |
use \FireBox\Core\Helpers\BoxHelper; |
| 21 |
|
| 22 |
class Ajax |
| 23 |
{ |
| 24 |
public function __construct() |
| 25 |
{ |
| 26 |
$this->setupAjax(); |
| 27 |
|
| 28 |
new Actions\Ajax(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Setup ajax requests |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function setupAjax() |
| 37 |
{ |
| 38 |
add_action('wp_ajax_fb_form_submission_status_change', [$this, 'fb_form_submission_status_change']); |
| 39 |
|
| 40 |
add_action('wp_ajax_fb_form_submit', [$this, 'fb_form_submit']); |
| 41 |
add_action('wp_ajax_nopriv_fb_form_submit', [$this, 'fb_form_submit']); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Update submission status. |
| 46 |
* |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function fb_form_submission_status_change() |
| 50 |
{ |
| 51 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 52 |
|
| 53 |
// verify nonce |
| 54 |
if (!$verify = wp_verify_nonce($nonce, 'fb_form_submission_action')) |
| 55 |
{ |
| 56 |
echo json_encode([ |
| 57 |
'error' => true, |
| 58 |
'message' => 'Cannot verify request.' |
| 59 |
]); |
| 60 |
wp_die(); |
| 61 |
} |
| 62 |
|
| 63 |
$submission_id = isset($_POST['submission_id']) ? sanitize_key($_POST['submission_id']) : ''; |
| 64 |
$new_state = isset($_POST['new_state']) ? sanitize_key($_POST['new_state']) : ''; |
| 65 |
|
| 66 |
$new_state = $new_state === 'publish' ? 1 : 0; |
| 67 |
|
| 68 |
if (!\FireBox\Core\Helpers\Form\Submission::updateState($submission_id, $new_state)) |
| 69 |
{ |
| 70 |
echo json_encode([ |
| 71 |
'error' => false, |
| 72 |
'message' => 'Submission state couldn\'t be updated.' |
| 73 |
]); |
| 74 |
wp_die(); |
| 75 |
} |
| 76 |
|
| 77 |
echo json_encode([ |
| 78 |
'error' => false, |
| 79 |
'message' => 'Submission state updated successfully.' |
| 80 |
]); |
| 81 |
wp_die(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Form submit. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function fb_form_submit() |
| 90 |
{ |
| 91 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 92 |
|
| 93 |
// verify nonce |
| 94 |
if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce')) |
| 95 |
{ |
| 96 |
echo json_encode([ |
| 97 |
'error' => true, |
| 98 |
'message' => 'Cannot verify request.' |
| 99 |
]); |
| 100 |
wp_die(); |
| 101 |
} |
| 102 |
|
| 103 |
$form_data = isset($_POST['form_data']) ? sanitize_text_field($_POST['form_data']) : ''; |
| 104 |
$form_data = $form_data ? json_decode(html_entity_decode(stripslashes($form_data)), true) : ''; |
| 105 |
|
| 106 |
if (!$form_data) |
| 107 |
{ |
| 108 |
echo json_encode([ |
| 109 |
'error' => true, |
| 110 |
'message' => 'Cannot submit form.' |
| 111 |
]); |
| 112 |
wp_die(); |
| 113 |
} |
| 114 |
|
| 115 |
$form_id = isset($form_data['form_id']) ? $form_data['form_id'] : false; |
| 116 |
if (!$form_id) |
| 117 |
{ |
| 118 |
echo json_encode([ |
| 119 |
'error' => true, |
| 120 |
'message' => 'Missing Form ID.' |
| 121 |
]); |
| 122 |
wp_die(); |
| 123 |
} |
| 124 |
|
| 125 |
$values = isset($form_data['fields']) ? $form_data['fields'] : false; |
| 126 |
if (!$form_id) |
| 127 |
{ |
| 128 |
echo json_encode([ |
| 129 |
'error' => true, |
| 130 |
'message' => 'Missing submission data.' |
| 131 |
]); |
| 132 |
wp_die(); |
| 133 |
} |
| 134 |
|
| 135 |
if (!$form = Form::isValid($form_id)) |
| 136 |
{ |
| 137 |
echo json_encode([ |
| 138 |
'error' => true, |
| 139 |
'message' => 'This form does not exist.' |
| 140 |
]); |
| 141 |
wp_die(); |
| 142 |
} |
| 143 |
|
| 144 |
$form_block = $form['block']; |
| 145 |
$form_fields = $form['fields']; |
| 146 |
|
| 147 |
$validated_fields = Form::validate($form_fields, $values); |
| 148 |
|
| 149 |
if (isset($validated_fields['error'])) |
| 150 |
{ |
| 151 |
$payload = [ |
| 152 |
'error' => true, |
| 153 |
'message' => isset($validated_fields['message']) ? $validated_fields['message'] : 'Form is invalid.' |
| 154 |
]; |
| 155 |
|
| 156 |
if (is_array($validated_fields['error'])) |
| 157 |
{ |
| 158 |
$payload['validation'] = $validated_fields['error']; |
| 159 |
} |
| 160 |
echo json_encode($payload); |
| 161 |
wp_die(); |
| 162 |
} |
| 163 |
|
| 164 |
$submission = []; |
| 165 |
|
| 166 |
/** |
| 167 |
* Also set the popup log id in field values. |
| 168 |
* |
| 169 |
* This is useful for Analytics purposes regarding the form metrics (Submissions, Conversion Rate). |
| 170 |
*/ |
| 171 |
$box_log_id = isset($_POST['box_log_id']) ? sanitize_key($_POST['box_log_id']) : false; |
| 172 |
if ($box_log_id) |
| 173 |
{ |
| 174 |
$values[] = [ |
| 175 |
'id' => 'box_log_id', |
| 176 |
'value' => $box_log_id |
| 177 |
]; |
| 178 |
} |
| 179 |
|
| 180 |
// Determine whether to store the submission and store it |
| 181 |
$storeSubmissions = isset($form_block['attrs']['storeSubmissions']) ? $form_block['attrs']['storeSubmissions'] : true; |
| 182 |
if (!$submission = Form::storeSubmission($form_id, $form_block, $validated_fields, $values, $storeSubmissions)) |
| 183 |
{ |
| 184 |
echo json_encode([ |
| 185 |
'error' => true, |
| 186 |
'message' => 'Could not save submission. Please try again.' |
| 187 |
]); |
| 188 |
wp_die(); |
| 189 |
} |
| 190 |
|
| 191 |
// Replace Smart Tags |
| 192 |
Form::replaceSmartTags($form_block['attrs'], $values, $submission); |
| 193 |
|
| 194 |
// Determine whether to run actions and run any |
| 195 |
if (isset($form_block['attrs']['actions']) && is_array($form_block['attrs']['actions']) && count($form_block['attrs']['actions'])) |
| 196 |
{ |
| 197 |
$actions = new \FireBox\Core\Form\Actions\Actions($form_block, $values, $submission); |
| 198 |
if (!$actions->run()) |
| 199 |
{ |
| 200 |
echo json_encode([ |
| 201 |
'error' => true, |
| 202 |
'message' => $actions->getErrorMessage() |
| 203 |
]); |
| 204 |
wp_die(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
$action = Form::getSubmissionAction($form_block['attrs']); |
| 209 |
|
| 210 |
echo json_encode(array_merge([ |
| 211 |
'error' => false |
| 212 |
], $action)); |
| 213 |
wp_die(); |
| 214 |
} |
| 215 |
} |