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

238 lines 5.0 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.0 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\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 firebox()->renderer->admin->render('pages/submissions/list', [
57 'forms' => \FireBox\Core\Helpers\Form\Form::getForms()
58 ]);
59 }
60
61 /**
62 * Show single submission edit view.
63 *
64 * @return void
65 */
66 public function edit()
67 {
68 $id = isset($_GET['id']) ? sanitize_key($_GET['id']) : false;
69 if (!$id)
70 {
71 $this->list();
72 return;
73 }
74
75 if (!$submission = \FireBox\Core\Helpers\Form\Submission::get($id))
76 {
77 $this->list();
78 return;
79 }
80
81 $html = firebox()->renderer->admin->render('pages/submissions/edit', [
82 'submission' => $submission
83 ], true);
84
85 $form = new \FPFramework\Base\Form($html, [
86 'section_name' => self::settings_name,
87 'button_label' => firebox()->_('FB_UPDATE_SUBMISSION')
88 ]);
89
90 echo $form->render();
91 }
92
93 /**
94 * Returns current task.
95 *
96 * @return mixed
97 */
98 public function getTask()
99 {
100 $task = isset($_GET['task']) ? sanitize_key($_GET['task']) : '';
101 $allowed_tasks = ['', 'edit'];
102
103 return in_array($task, $allowed_tasks) ? $task : false;
104 }
105
106 /**
107 * Callback used to handle the processing of settings.
108 *
109 * Save the submission data.
110 *
111 * @param array $input
112 *
113 * @return void
114 */
115 public function processSubmissionEdit($input)
116 {
117 // We use $_POST and $input to retrieve the submission details
118 $data = $_POST;
119
120 if (!isset($data['task']) || !isset($data['form_id']) || !isset($data['fb_form']) || !isset($data['submission_id']) || !isset($data[self::settings_name]))
121 {
122 return;
123 }
124
125 // Validate nonce
126 if (!check_admin_referer('fpf_form_nonce_firebox_submission', 'fpf_form_nonce_firebox_submission'))
127 {
128 return;
129 }
130
131 $allowed_tasks = [
132 'edit_submission'
133 ];
134
135 if (!in_array($data['task'], $allowed_tasks))
136 {
137 return;
138 }
139
140 // Ensure a submission record with given Submission ID and Form ID exist
141 $payload = [
142 'where' => [
143 'id' => ' = ' . esc_sql($data['submission_id']),
144 'form_id' => ' = "' . esc_sql($data['form_id']) . '"'
145 ]
146 ];
147 if (!firebox()->tables->submission->getResults($payload, true, true))
148 {
149 return;
150 }
151
152 // Get Form
153 if (!$form = Form::getFormByID($data['form_id']))
154 {
155 return;
156 }
157
158 $controller_settings = $data[self::settings_name];
159 $form_fields = $form['fields'];
160 $fields_values = $data['fb_form'];
161 $submission_state = isset($controller_settings['submission_state']) ? $controller_settings['submission_state'] : 'published';
162
163 $valid = Form::validate($form_fields, $fields_values);
164
165 if (isset($valid['error']))
166 {
167 $error = 'Validation errors:<br />';
168 foreach ($valid['error'] as $item)
169 {
170 $error .= '<p><strong>' . $item['label'] . ':</strong> ' . $item['validation_message'] . '</p>';
171 }
172
173 \FPFramework\Libs\AdminNotice::displayError($error);
174 return;
175 }
176
177 // Update submission
178 $replace = [
179 'modified_at' => gmdate('Y-m-d H:i:s'),
180 'state' => $submission_state === 'published' ? 1 : 0
181 ];
182 $where = [
183 'id' => $data['submission_id'],
184 'form_id' => $data['form_id']
185 ];
186 firebox()->tables->submission->update($replace, $where);
187
188 // Update submission meta for each field
189 foreach ($valid as $key => $field)
190 {
191 $replace = [
192 'meta_value' => $fields_values[$field->getOptionValue('name')],
193 'modified_at' => gmdate('Y-m-d H:i:s')
194 ];
195 $where = [
196 'submission_id' => $data['submission_id'],
197 'meta_key' => $field->getOptionValue('id')
198 ];
199 firebox()->tables->submissionmeta->update($replace, $where);
200 }
201
202 \FPFramework\Libs\AdminNotice::displaySuccess('Submission updated.');
203 }
204
205 /**
206 * Load required media files
207 *
208 * @return void
209 */
210 public function addMedia()
211 {
212 $task = $this->getTask();
213
214 // Viewing all submissions page
215 if ($task === '')
216 {
217 wp_register_script(
218 'fb-submissions',
219 FBOX_MEDIA_ADMIN_URL . 'js/submissions.js',
220 [],
221 FBOX_VERSION,
222 true
223 );
224 wp_enqueue_script('fb-submissions');
225 }
226 // Viewing edit submission page
227 else if ($task === 'edit')
228 {
229 wp_register_style(
230 'fb-submission-edit',
231 FBOX_MEDIA_ADMIN_URL . 'css/submissions/edit.css',
232 [],
233 FBOX_VERSION
234 );
235 wp_enqueue_style('fb-submission-edit');
236 }
237 }
238 }