PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.1.14
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.1.14
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 2.1.14, at Inc/Core/Controllers/Submissions.php

274 lines 5.9 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 2.1.14 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2024 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();
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 // Ensure a submission record with given Submission ID and Form ID exist
154 $payload = [
155 'where' => [
156 'id' => ' = ' . esc_sql($data['submission_id']),
157 'form_id' => ' = "' . esc_sql($data['form_id']) . '"'
158 ]
159 ];
160 if (!firebox()->tables->submission->getResults($payload, true, true))
161 {
162 return;
163 }
164
165 // Get Form
166 if (!$form = Form::getFormByID($data['form_id']))
167 {
168 return;
169 }
170
171 $controller_settings = $data[self::settings_name];
172 $form_fields = $form['fields'];
173 $fields_values = $data['fb_form'];
174 $submission_state = isset($controller_settings['submission_state']) ? $controller_settings['submission_state'] : 'published';
175
176 // Make the form fields not required
177 foreach ($form_fields as $key => $field)
178 {
179 $field->setOptionValue('required', false);
180 }
181
182 $valid = Form::validate($form_fields, $fields_values);
183
184 if (isset($valid['error']))
185 {
186 $error = firebox()->_('FB_VALIDATION_ERRORS') . ':<br /><br />';
187 foreach ($valid['error'] as $item)
188 {
189 $error .= '<div><strong>' . $item['label'] . ':</strong> ' . $item['validation_message'] . '</div>';
190 }
191
192 \FPFramework\Libs\AdminNotice::displayError($error);
193 return;
194 }
195
196 // Update submission
197 $replace = [
198 'modified_at' => gmdate('Y-m-d H:i:s'),
199 'state' => $submission_state === 'published' ? 1 : 0
200 ];
201 $where = [
202 'id' => $data['submission_id'],
203 'form_id' => $data['form_id']
204 ];
205 firebox()->tables->submission->update($replace, $where);
206
207 // Update submission meta for each field
208 foreach ($valid as $key => $field)
209 {
210 $meta_value = $fields_values[$field->getOptionValue('name')];
211
212 if (is_array($meta_value))
213 {
214 $meta_value = json_encode($meta_value);
215 }
216
217 $replace = [
218 'meta_value' => $meta_value,
219 'modified_at' => gmdate('Y-m-d H:i:s')
220 ];
221 $where = [
222 'submission_id' => $data['submission_id'],
223 'meta_key' => $field->getOptionValue('id')
224 ];
225
226 $updated = firebox()->tables->submissionmeta->update($replace, $where);
227
228 // The submission wasn't updated, this means this field doesn't have a previous value, so add a new record.
229 if (!$updated)
230 {
231 firebox()->tables->submissionmeta->insert(array_merge($where, $replace, [
232 'meta_type' => '',
233 'modified_at' => null
234 ]));
235 }
236 }
237
238 \FPFramework\Libs\AdminNotice::displaySuccess(firebox()->_('FB_SUBMISSION_UPDATED'));
239 }
240
241 /**
242 * Load required media files
243 *
244 * @return void
245 */
246 public function addMedia()
247 {
248 $task = $this->getTask();
249
250 // Viewing all submissions page
251 if ($task === '')
252 {
253 wp_register_script(
254 'fb-submissions',
255 FBOX_MEDIA_ADMIN_URL . 'js/submissions.js',
256 [],
257 FBOX_VERSION,
258 true
259 );
260 wp_enqueue_script('fb-submissions');
261 }
262 // Viewing edit submission page
263 else if ($task === 'edit')
264 {
265 wp_register_style(
266 'fb-submission-edit',
267 FBOX_MEDIA_ADMIN_URL . 'css/submissions/edit.css',
268 [],
269 FBOX_VERSION
270 );
271 wp_enqueue_style('fb-submission-edit');
272 }
273 }
274 }