PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / trunk
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment vtrunk
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / Controllers / Submissions.php

Submissions.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment trunk, at Inc/Core/Controllers/Submissions.php

279 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 3.1.13 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2026 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\Controllers;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use \FireBox\Core\Helpers\Form\Form;
20
21 class Submissions extends BaseController
22 {
23 /**
24 * The form settings name
25 *
26 * @var string
27 */
28 const settings_name = 'firebox_submission';
29
30 /**
31 * Render Submissions page.
32 *
33 * @return void
34 */
35 public function render()
36 {
37 switch ($this->getTask()) {
38 case '':
39 default:
40 $this->list();
41 break;
42
43 case 'edit':
44 $this->edit();
45 break;
46 }
47 }
48
49 /**
50 * Show submissions list view.
51 *
52 * @return void
53 */
54 public function list()
55 {
56 $forms = \FireBox\Core\Helpers\Form\Form::getForms();
57 $forms = array_map(function($form) {
58 return [
59 'id' => $form['id'],
60 'name' => $form['name']
61 ];
62 }, $forms);
63 usort($forms, function($a, $b) {
64 return strcmp(strtolower($a['name']), strtolower($b['name']));
65 });
66
67 firebox()->renderer->admin->render('pages/submissions/list', [
68 'forms' => $forms
69 ]);
70 }
71
72 /**
73 * Show single submission edit view.
74 *
75 * @return void
76 */
77 public function edit()
78 {
79 check_admin_referer('edit-firebox-submission');
80
81 $id = isset($_GET['id']) ? sanitize_key($_GET['id']) : false;
82 if (!$id)
83 {
84 $this->list();
85 return;
86 }
87
88 if (!$submission = \FireBox\Core\Helpers\Form\Submission::get($id))
89 {
90 $this->list();
91 return;
92 }
93
94 $html = firebox()->renderer->admin->render('pages/submissions/edit', [
95 'submission' => $submission
96 ], true);
97
98 $form = new \FPFramework\Base\Form($html, [
99 'section_name' => self::settings_name,
100 'button_label' => firebox()->_('FB_UPDATE_SUBMISSION')
101 ]);
102
103 echo $form->render(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
104 }
105
106 /**
107 * Returns current task.
108 *
109 * @return mixed
110 */
111 public function getTask()
112 {
113 $task = isset($_GET['task']) ? sanitize_key($_GET['task']) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
114 $allowed_tasks = ['', 'edit'];
115
116 return in_array($task, $allowed_tasks) ? $task : false;
117 }
118
119 /**
120 * Callback used to handle the processing of settings.
121 *
122 * Save the submission data.
123 *
124 * @param array $input
125 *
126 * @return void
127 */
128 public function processSubmissionEdit($input)
129 {
130 // Validate nonce
131 if (!check_admin_referer('fpf_form_nonce_firebox_submission', 'fpf_form_nonce_firebox_submission'))
132 {
133 return;
134 }
135
136 // We use $_POST and $input to retrieve the submission details
137 $data = wp_unslash($_POST);
138
139 if (!isset($data['task']) || !isset($data['form_id']) || !isset($data['fb_form']) || !isset($data['submission_id']) || !isset($data[self::settings_name]))
140 {
141 return;
142 }
143
144 $allowed_tasks = [
145 'edit_submission'
146 ];
147
148 if (!in_array($data['task'], $allowed_tasks))
149 {
150 return;
151 }
152
153 // Sanitize
154 $submission_id = absint($data['submission_id']);
155 $form_id = sanitize_key($data['form_id']);
156
157 // Ensure a submission record with given Submission ID and Form ID exist
158 $payload = [
159 'where' => [
160 'id' => ' = ' . $submission_id,
161 'form_id' => ' = "' . $form_id . '"'
162 ]
163 ];
164 if (!firebox()->tables->submission->getResults($payload, true, true))
165 {
166 return;
167 }
168
169 // Get Form
170 if (!$form = Form::getFormByID($form_id))
171 {
172 return;
173 }
174
175 $controller_settings = $data[self::settings_name];
176 $form_fields = $form['fields'];
177 $fields_values = is_array($data['fb_form']) ? $data['fb_form'] : [];
178 $submission_state = isset($controller_settings['submission_state']) ? $controller_settings['submission_state'] : 'published';
179
180 // Make the form fields not required
181 foreach ($form_fields as $key => $field)
182 {
183 $field->setOptionValue('required', false);
184 }
185
186 $valid = Form::validate($form_fields, $fields_values);
187
188 if (isset($valid['error']))
189 {
190 $error = firebox()->_('FB_VALIDATION_ERRORS') . ':<br /><br />';
191 foreach ($valid['error'] as $item)
192 {
193 $error .= '<div><strong>' . $item['label'] . ':</strong> ' . $item['validation_message'] . '</div>';
194 }
195
196 \FPFramework\Libs\AdminNotice::displayError($error);
197 return;
198 }
199
200 // Update submission
201 $replace = [
202 'modified_at' => gmdate('Y-m-d H:i:s'),
203 'state' => $submission_state === 'published' ? 1 : 0
204 ];
205 $where = [
206 'id' => $submission_id,
207 'form_id' => $form_id
208 ];
209 firebox()->tables->submission->update($replace, $where);
210
211 // Update submission meta for each field
212 foreach ((is_array($valid) ? $valid : []) as $key => $field)
213 {
214 $field_name = $field->getOptionValue('name');
215 $meta_value = isset($fields_values[$field_name]) ? $fields_values[$field_name] : '';
216
217 if (is_array($meta_value))
218 {
219 $meta_value = wp_json_encode($meta_value);
220 }
221
222 $replace = [
223 'meta_value' => $meta_value,
224 'modified_at' => gmdate('Y-m-d H:i:s')
225 ];
226 $where = [
227 'submission_id' => $submission_id,
228 'meta_key' => $field->getOptionValue('id')
229 ];
230
231 $updated = firebox()->tables->submissionmeta->update($replace, $where);
232
233 // The submission wasn't updated, this means this field doesn't have a previous value, so add a new record.
234 if (!$updated)
235 {
236 firebox()->tables->submissionmeta->insert(array_merge($where, $replace, [
237 'meta_type' => '',
238 'modified_at' => null
239 ]));
240 }
241 }
242
243 \FPFramework\Libs\AdminNotice::displaySuccess(firebox()->_('FB_SUBMISSION_UPDATED'));
244 }
245
246 /**
247 * Load required media files
248 *
249 * @return void
250 */
251 public function addMedia()
252 {
253 $task = $this->getTask();
254
255 // Viewing all submissions page
256 if ($task === '')
257 {
258 wp_register_script(
259 'fb-submissions',
260 FBOX_MEDIA_ADMIN_URL . 'js/submissions.js',
261 [],
262 FBOX_VERSION,
263 true
264 );
265 wp_enqueue_script('fb-submissions');
266 }
267 // Viewing edit submission page
268 else if ($task === 'edit')
269 {
270 wp_register_style(
271 'fb-submission-edit',
272 FBOX_MEDIA_ADMIN_URL . 'css/submissions/edit.css',
273 [],
274 FBOX_VERSION
275 );
276 wp_enqueue_style('fb-submission-edit');
277 }
278 }
279 }