PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.21
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 / Integrations / GlobalNotificationManager.php

GlobalNotificationManager.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.21, at app/Services/Integrations/GlobalNotificationManager.php

192 lines 6.4 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\Integrations;
4
5 use FluentForm\App\Databases\Migrations\ScheduledActions;
6 use FluentForm\App\Modules\Form\FormDataParser;
7 use FluentForm\App\Modules\Form\FormFieldsParser;
8 use FluentForm\App\Services\ConditionAssesor;
9 use FluentForm\App\Services\FormBuilder\ShortCodeParser;
10 use FluentForm\Framework\Foundation\Application;
11 use FluentForm\Framework\Helpers\ArrayHelper;
12
13 class GlobalNotificationManager
14 {
15 private $app;
16
17 public function __construct(Application $app)
18 {
19 $this->app = $app;
20 }
21
22 public function globalNotify($insertId, $formData, $form)
23 {
24 // Let's find the feeds that are available for this form
25 $feedKeys = apply_filters('fluentform_global_notification_active_types', [], $form->id);
26
27 if (!$feedKeys) {
28 return;
29 }
30
31 $feedMetaKeys = array_keys($feedKeys);
32
33 $feeds = wpFluent()->table('fluentform_form_meta')
34 ->where('form_id', $form->id)
35 ->whereIn('meta_key', $feedMetaKeys)
36 ->orderBy('id', 'ASC')
37 ->get();
38 if (!$feeds) {
39 return;
40 }
41
42 // Now we have to filter the feeds which are enabled
43 $enabledFeeds = [];
44 foreach ($feeds as $feed) {
45 $parsedValue = json_decode($feed->value, true);
46 if ($parsedValue && ArrayHelper::isTrue($parsedValue, 'enabled')) {
47 // Now check if conditions matched or not
48 $isConditionMatched = $this->checkCondition($parsedValue, $formData, $insertId);
49
50 if ($isConditionMatched) {
51 $enabledFeeds[] = [
52 'id' => $feed->id,
53 'meta_key' => $feed->meta_key,
54 'settings' => $parsedValue
55 ];
56 }
57 }
58 }
59
60 if(!$enabledFeeds) {
61 do_action('fluentform_global_notify_completed', $insertId, $form);
62 return;
63 }
64
65 $entry = false;
66 $asyncFeeds = [];
67
68 foreach ($enabledFeeds as $feed) {
69 // We will decide if this feed will run on async or sync
70 $integrationKey = ArrayHelper::get($feedKeys, $feed['meta_key']);
71
72 $action = 'fluentform_integration_notify_' . $feed['meta_key'];
73
74
75 if (!$entry) {
76 $entry = $this->getEntry($insertId, $form);
77 }
78
79 // It's sync
80 $processedValues = $feed['settings'];
81 unset($processedValues['conditionals']);
82 $processedValues = ShortCodeParser::parse($processedValues, $insertId, $formData);
83 $feed['processedValues'] = $processedValues;
84
85 if (apply_filters('fluentform_notifying_async_' . $integrationKey, true, $form->id)) {
86 // It's async
87 $asyncFeeds[] = [
88 'action' => $action,
89 'form_id' => $form->id,
90 'origin_id' => $insertId,
91 'feed_id' => $feed['id'],
92 'type' => 'submission_action',
93 'status' => 'pending',
94 'data' => maybe_serialize($feed),
95 'created_at' => current_time('mysql'),
96 'updated_at' => current_time('mysql')
97 ];
98 } else {
99 do_action($action, $feed, $formData, $entry, $form);
100 }
101 }
102
103 if (!$asyncFeeds) {
104 do_action('fluentform_global_notify_completed', $insertId, $form);
105 return;
106 }
107
108 /*
109 * Maybe Migrate on run time
110 * We will remove this code at the end of 2020
111 * @todo: Remove when appropriate
112 */
113 if(get_option('fluentform_scheduled_actions_migrated') != 'yes') {
114 ScheduledActions::migrate();
115 $hookName = 'fluentform_do_scheduled_tasks';
116 if (!wp_next_scheduled($hookName)) {
117 wp_schedule_event(time(), 'ff_every_five_minutes', $hookName);
118 }
119 }
120
121 // Now we will push this async feeds
122 $handler = $this->app['fluentFormAsyncRequest'];
123 $handler->queueFeeds( $asyncFeeds);
124
125 $handler->dispatchAjax(['origin_id' => $insertId]);
126 }
127
128 private function checkCondition($parsedValue, $formData, $insertId)
129 {
130 $conditionSettings = ArrayHelper::get($parsedValue, 'conditionals');
131 if (
132 !$conditionSettings ||
133 !ArrayHelper::isTrue($conditionSettings, 'status') ||
134 !count(ArrayHelper::get($conditionSettings, 'conditions'))
135 ) {
136 return true;
137 }
138
139 return ConditionAssesor::evaluate($parsedValue, $formData);
140 }
141
142 private function getEntry($id, $form)
143 {
144 $submission = wpFluent()->table('fluentform_submissions')->find($id);
145 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
146 return FormDataParser::parseFormEntry($submission, $form, $formInputs);
147 }
148
149 public function cleanUpPassword($entryId, $form)
150 {
151 // Let's get the password fields
152 $inputs = FormFieldsParser::getInputsByElementTypes($form, ['input_password']);
153 if(!$inputs) {
154 return;
155 }
156 $passwordKeys = array_keys($inputs);
157 // Let's delete from entry details
158 wpFluent()->table('fluentform_entry_details')
159 ->where('form_id', $form->id)
160 ->whereIn('field_name', $passwordKeys)
161 ->where('submission_id', $entryId)
162 ->delete();
163
164 // Let's alter from main submission data
165 $submission = wpFluent()->table('fluentform_submissions')
166 ->where('id', $entryId)
167 ->first();
168 if(!$submission) {
169 return;
170 }
171
172 $responseInputs = \json_decode($submission->response, true);
173
174 $replaced = false;
175 foreach ($passwordKeys as $passwordKey) {
176 if(!empty($responseInputs[$passwordKey])) {
177 $originalPassword = $responseInputs[$passwordKey];
178 $responseInputs[$passwordKey] = str_repeat("*", strlen($originalPassword)).' '. __('(truncated)', 'fluentform');
179 $replaced = true;
180 }
181 }
182
183 if($replaced) {
184 wpFluent()->table('fluentform_submissions')
185 ->where('id', $entryId)
186 ->update([
187 'response' => \json_encode($responseInputs)
188 ]);
189 }
190 }
191 }
192