PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.13
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.13
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / WPAsync / FluentFormAsyncRequest.php

FluentFormAsyncRequest.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.13, at app/Services/WPAsync/FluentFormAsyncRequest.php

231 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\WPAsync;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\App\Modules\Form\FormDataParser;
7 use FluentForm\App\Modules\Form\FormFieldsParser;
8 use FluentForm\Framework\Foundation\Application;
9
10 class FluentFormAsyncRequest
11 {
12 /**
13 * $prefix The prefix for the identifier
14 * @var string
15 */
16 protected $table = 'ff_scheduled_actions';
17
18 /**
19 * $action The action for the identifier
20 * @var string
21 */
22 protected $action = 'fluentform_background_process';
23
24 /**
25 * $actions Actions to be fired when an async request is sent
26 * @var array
27 */
28 protected $actions = array();
29
30 /**
31 * $app Instance of Application/Framework
32 * @var \FluentForm\Framework\Foundation\Application
33 */
34 protected $app = null;
35
36 static $formCache = [];
37 static $entryCache = [];
38 static $submissionCache = [];
39
40 /**
41 * Construct the Object
42 * @param \FluentForm\Framework\Foundation\Application $app
43 */
44 public function __construct(Application $app)
45 {
46 $this->app = $app;
47 }
48
49 public function queue($feed)
50 {
51 return wpFluent()->table($this->table)->insertGetId($feed);
52 }
53
54 public function queueFeeds($feeds)
55 {
56 return wpFluent()->table($this->table)
57 ->insert($feeds);
58 }
59
60 public function dispatchAjax($data = [])
61 {
62 /* This hook is deprecated and will be removed soon */
63 $sslVerify = apply_filters('fluentform_https_local_ssl_verify', false);
64
65 $args = array(
66 'timeout' => 0.1,
67 'blocking' => false,
68 'body' => $data,
69 'cookies' => $_COOKIE,
70 'sslverify' => apply_filters('fluentform/https_local_ssl_verify', $sslVerify),
71 );
72
73 $queryArgs = array(
74 'action' => $this->action,
75 'nonce' => wp_create_nonce($this->action),
76 );
77
78 $url = add_query_arg($queryArgs, Helper::getAjaxUrl());
79 wp_remote_post(esc_url_raw($url), $args);
80 }
81
82 public function handleBackgroundCall()
83 {
84 $nonce = sanitize_text_field(wpFluentForm('request')->get('nonce', ''));
85 if (!wp_verify_nonce($nonce, $this->action)) {
86 die('invalid');
87 }
88
89 $originId = wpFluentForm('request')->get('origin_id', false);
90
91 $this->processActions($originId);
92 echo 'success';
93 die();
94 }
95
96 public function processActions($originId = false)
97 {
98 $actionFeedQuery = wpFluent()->table($this->table)
99 ->where('status', 'pending');
100 if($originId) {
101 $actionFeedQuery = $actionFeedQuery->where('origin_id', $originId);
102 }
103
104 $actionFeeds = $actionFeedQuery->get();
105
106 if(count($actionFeeds) === 0) {
107 return;
108 }
109
110 $formCache = [];
111 $submissionCache = [];
112 $entryCache = [];
113
114 foreach ($actionFeeds as $actionFeed) {
115 $action = $actionFeed->action;
116 $feed = Helper::safeUnserialize($actionFeed->data);
117 $feed['scheduled_action_id'] = $actionFeed->id;
118 if(isset($submissionCache[$actionFeed->origin_id])) {
119 $submission = $submissionCache[$actionFeed->origin_id];
120 } else {
121 $submission = wpFluent()->table('fluentform_submissions')->find($actionFeed->origin_id);
122 $submissionCache[$submission->id] = $submission;
123 }
124 if(isset($formCache[$submission->form_id])) {
125 $form = $formCache[$submission->form_id];
126 } else {
127 $form = wpFluent()->table('fluentform_forms')->find($submission->form_id);
128 $formCache[$form->id] = $form;
129 }
130
131 if(isset($entryCache[$submission->id])) {
132 $entry = $entryCache[$submission->id];
133 } else {
134 $entry = $this->getEntry($submission, $form);
135 $entryCache[$submission->id] = $entry;
136 }
137 $formData = json_decode($submission->response, true);
138
139 wpFluent()->table($this->table)
140 ->where('id', $actionFeed->id)
141 ->update([
142 'status' => 'processing',
143 'retry_count' => $actionFeed->retry_count + 1,
144 'updated_at' => current_time('mysql')
145 ]);
146
147 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Dynamic hook name for async request
148 do_action($action, $feed, $formData, $entry, $form);
149 }
150
151 if($originId && !empty($form) && !empty($submission)) {
152 /* This hook is deprecated and will be removed soon */
153 do_action('fluentform_global_notify_completed', $submission->id, $form);
154
155 do_action('fluentform/global_notify_completed', $submission->id, $form);
156 }
157 }
158
159 private function getEntry($submission, $form)
160 {
161 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
162 return FormDataParser::parseFormEntry($submission, $form, $formInputs);
163 }
164
165 public function process($queue)
166 {
167 if (is_numeric($queue)) {
168 $queue = wpFluent()->table($this->table)->where('status', 'pending')->find($queue);
169 }
170
171 if (!$queue || empty($queue->action)) {
172 return;
173 }
174
175 $action = $queue->action;
176 $feed = Helper::safeUnserialize($queue->data);
177 $feed['scheduled_action_id'] = $queue->id;
178
179 if (isset(static::$submissionCache[$queue->origin_id])) {
180 $submission = static::$submissionCache[$queue->origin_id];
181 } else {
182 $submission = wpFluent()->table('fluentform_submissions')->find($queue->origin_id);
183
184 static::$submissionCache[$submission->id] = $submission;
185 }
186
187 if (isset(static::$formCache[$submission->form_id])) {
188 $form = static::$formCache[$submission->form_id];
189 } else {
190 $form = wpFluent()->table('fluentform_forms')->find($submission->form_id);
191
192 static::$formCache[$form->id] = $form;
193 }
194
195 if (isset(static::$entryCache[$submission->id])) {
196 $entry = static::$entryCache[$submission->id];
197 } else {
198 $entry = $this->getEntry($submission, $form);
199
200 static::$entryCache[$submission->id] = $entry;
201 }
202
203 $formData = json_decode($submission->response, true);
204
205 wpFluent()->table($this->table)
206 ->where('id', $queue->id)
207 ->update([
208 'status' => 'processing',
209 'retry_count' => $queue->retry_count + 1,
210 'updated_at' => current_time('mysql')
211 ]);
212
213 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Dynamic hook name for async request
214 do_action($action, $feed, $formData, $entry, $form);
215
216 $this->maybeFinished($submission->id, $form);
217 }
218
219 public function maybeFinished($originId, $form)
220 {
221 $pendingFeeds = wpFluent()->table($this->table)->where([
222 'status' => 'pending',
223 'origin_id' => $originId
224 ])->get();
225
226 if (count($pendingFeeds) === 0) {
227 do_action('fluentform/global_notify_completed', $originId, $form);
228 }
229 }
230 }
231