PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.40
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.40
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.40, at app/Services/Integrations/GlobalNotificationManager.php

191 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 if (!$entry) {
75 $entry = $this->getEntry($insertId, $form);
76 }
77
78 // It's sync
79 $processedValues = $feed['settings'];
80 unset($processedValues['conditionals']);
81 $processedValues = ShortCodeParser::parse($processedValues, $insertId, $formData);
82 $feed['processedValues'] = $processedValues;
83
84 if (apply_filters('fluentform_notifying_async_' . $integrationKey, true, $form->id)) {
85 // It's async
86 $asyncFeeds[] = [
87 'action' => $action,
88 'form_id' => $form->id,
89 'origin_id' => $insertId,
90 'feed_id' => $feed['id'],
91 'type' => 'submission_action',
92 'status' => 'pending',
93 'data' => maybe_serialize($feed),
94 'created_at' => current_time('mysql'),
95 'updated_at' => current_time('mysql')
96 ];
97 } else {
98 do_action($action, $feed, $formData, $entry, $form);
99 }
100 }
101
102 if (!$asyncFeeds) {
103 do_action('fluentform_global_notify_completed', $insertId, $form);
104 return;
105 }
106
107 /*
108 * Maybe Migrate on run time
109 * We will remove this code at the end of 2020
110 * @todo: Remove when appropriate
111 */
112 if(get_option('fluentform_scheduled_actions_migrated') != 'yes') {
113 ScheduledActions::migrate();
114 $hookName = 'fluentform_do_scheduled_tasks';
115 if (!wp_next_scheduled($hookName)) {
116 wp_schedule_event(time(), 'ff_every_five_minutes', $hookName);
117 }
118 }
119
120 // Now we will push this async feeds
121 $handler = $this->app['fluentFormAsyncRequest'];
122 $handler->queueFeeds( $asyncFeeds);
123
124 $handler->dispatchAjax(['origin_id' => $insertId]);
125 }
126
127 private function checkCondition($parsedValue, $formData, $insertId)
128 {
129 $conditionSettings = ArrayHelper::get($parsedValue, 'conditionals');
130 if (
131 !$conditionSettings ||
132 !ArrayHelper::isTrue($conditionSettings, 'status') ||
133 !count(ArrayHelper::get($conditionSettings, 'conditions'))
134 ) {
135 return true;
136 }
137
138 return ConditionAssesor::evaluate($parsedValue, $formData);
139 }
140
141 private function getEntry($id, $form)
142 {
143 $submission = wpFluent()->table('fluentform_submissions')->find($id);
144 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
145 return FormDataParser::parseFormEntry($submission, $form, $formInputs);
146 }
147
148 public function cleanUpPassword($entryId, $form)
149 {
150 // Let's get the password fields
151 $inputs = FormFieldsParser::getInputsByElementTypes($form, ['input_password']);
152 if(!$inputs) {
153 return;
154 }
155 $passwordKeys = array_keys($inputs);
156 // Let's delete from entry details
157 wpFluent()->table('fluentform_entry_details')
158 ->where('form_id', $form->id)
159 ->whereIn('field_name', $passwordKeys)
160 ->where('submission_id', $entryId)
161 ->delete();
162
163 // Let's alter from main submission data
164 $submission = wpFluent()->table('fluentform_submissions')
165 ->where('id', $entryId)
166 ->first();
167 if(!$submission) {
168 return;
169 }
170
171 $responseInputs = \json_decode($submission->response, true);
172
173 $replaced = false;
174 foreach ($passwordKeys as $passwordKey) {
175 if(!empty($responseInputs[$passwordKey])) {
176 $originalPassword = $responseInputs[$passwordKey];
177 $responseInputs[$passwordKey] = str_repeat("*", strlen($originalPassword)).' '. __('(truncated)', 'fluentform');
178 $replaced = true;
179 }
180 }
181
182 if($replaced) {
183 wpFluent()->table('fluentform_submissions')
184 ->where('id', $entryId)
185 ->update([
186 'response' => \json_encode($responseInputs)
187 ]);
188 }
189 }
190 }
191