| 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 |
do_action('fluentform_global_notify_completed', $insertId, $form); |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$feedMetaKeys = array_keys($feedKeys); |
| 33 |
|
| 34 |
|
| 35 |
$feeds = wpFluent()->table('fluentform_form_meta') |
| 36 |
->where('form_id', $form->id) |
| 37 |
->whereIn('meta_key', $feedMetaKeys) |
| 38 |
->orderBy('id', 'ASC') |
| 39 |
->get(); |
| 40 |
|
| 41 |
if (!$feeds) { |
| 42 |
do_action('fluentform_global_notify_completed', $insertId, $form); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
// Now we have to filter the feeds which are enabled |
| 47 |
$enabledFeeds = $this->getEnabledFeeds($feeds, $formData, $insertId); |
| 48 |
|
| 49 |
if (!$enabledFeeds) { |
| 50 |
do_action('fluentform_global_notify_completed', $insertId, $form); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$entry = false; |
| 55 |
$asyncFeeds = []; |
| 56 |
|
| 57 |
foreach ($enabledFeeds as $feed) { |
| 58 |
// We will decide if this feed will run on async or sync |
| 59 |
$integrationKey = ArrayHelper::get($feedKeys, $feed['meta_key']); |
| 60 |
|
| 61 |
$action = 'fluentform_integration_notify_' . $feed['meta_key']; |
| 62 |
|
| 63 |
if (!$entry) { |
| 64 |
$entry = $this->getEntry($insertId, $form); |
| 65 |
} |
| 66 |
// skip emails which will be sent on payment form submit otherwise email is sent after payment success |
| 67 |
if (!!$form->has_payment && ($feed['meta_key'] == 'notifications')) { |
| 68 |
if ((ArrayHelper::get($feed, 'settings.feed_trigger_event') == 'payment_form_submit')) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// It's sync |
| 74 |
$processedValues = $feed['settings']; |
| 75 |
unset($processedValues['conditionals']); |
| 76 |
$processedValues = ShortCodeParser::parse($processedValues, $insertId, $formData, $form, false, $feed['meta_key']); |
| 77 |
$feed['processedValues'] = $processedValues; |
| 78 |
|
| 79 |
if (apply_filters('fluentform_notifying_async_' . $integrationKey, true, $form->id)) { |
| 80 |
// It's async |
| 81 |
$asyncFeeds[] = [ |
| 82 |
'action' => $action, |
| 83 |
'form_id' => $form->id, |
| 84 |
'origin_id' => $insertId, |
| 85 |
'feed_id' => $feed['id'], |
| 86 |
'type' => 'submission_action', |
| 87 |
'status' => 'pending', |
| 88 |
'data' => maybe_serialize($feed), |
| 89 |
'created_at' => current_time('mysql'), |
| 90 |
'updated_at' => current_time('mysql') |
| 91 |
]; |
| 92 |
} else { |
| 93 |
do_action($action, $feed, $formData, $entry, $form); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (!$asyncFeeds) { |
| 98 |
do_action('fluentform_global_notify_completed', $insertId, $form); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// Now we will push this async feeds |
| 103 |
$handler = $this->app['fluentFormAsyncRequest']; |
| 104 |
$handler->queueFeeds( $asyncFeeds); |
| 105 |
|
| 106 |
$handler->dispatchAjax(['origin_id' => $insertId]); |
| 107 |
} |
| 108 |
|
| 109 |
public function checkCondition($parsedValue, $formData, $insertId) |
| 110 |
{ |
| 111 |
$conditionSettings = ArrayHelper::get($parsedValue, 'conditionals'); |
| 112 |
if ( |
| 113 |
!$conditionSettings || |
| 114 |
!ArrayHelper::isTrue($conditionSettings, 'status') || |
| 115 |
!count(ArrayHelper::get($conditionSettings, 'conditions')) |
| 116 |
) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
return ConditionAssesor::evaluate($parsedValue, $formData); |
| 121 |
} |
| 122 |
|
| 123 |
public function getEntry($id, $form) |
| 124 |
{ |
| 125 |
$submission = wpFluent()->table('fluentform_submissions')->find($id); |
| 126 |
$formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']); |
| 127 |
return FormDataParser::parseFormEntry($submission, $form, $formInputs); |
| 128 |
} |
| 129 |
|
| 130 |
public function cleanUpPassword($entryId, $form) |
| 131 |
{ |
| 132 |
// Let's get the password fields |
| 133 |
$inputs = FormFieldsParser::getInputsByElementTypes($form, ['input_password']); |
| 134 |
|
| 135 |
if(!$inputs) { |
| 136 |
return; |
| 137 |
} |
| 138 |
$passwordKeys = array_keys($inputs); |
| 139 |
|
| 140 |
// Let's delete from entry details |
| 141 |
wpFluent()->table('fluentform_entry_details') |
| 142 |
->where('form_id', $form->id) |
| 143 |
->whereIn('field_name', $passwordKeys) |
| 144 |
->where('submission_id', $entryId) |
| 145 |
->delete(); |
| 146 |
|
| 147 |
// Let's alter from main submission data |
| 148 |
$submission = wpFluent()->table('fluentform_submissions') |
| 149 |
->where('id', $entryId) |
| 150 |
->first(); |
| 151 |
|
| 152 |
if (!$submission) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
$responseInputs = \json_decode($submission->response, true); |
| 157 |
|
| 158 |
$replaced = false; |
| 159 |
foreach ($passwordKeys as $passwordKey) { |
| 160 |
if (!empty($responseInputs[$passwordKey])) { |
| 161 |
$responseInputs[$passwordKey] = str_repeat("*", 6).' '. __('(truncated)', 'fluentform'); |
| 162 |
$replaced = true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if ($replaced) { |
| 167 |
wpFluent()->table('fluentform_submissions') |
| 168 |
->where('id', $entryId) |
| 169 |
->update([ |
| 170 |
'response' => \json_encode($responseInputs) |
| 171 |
]); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @param $feeds |
| 177 |
* @param $formData |
| 178 |
* @param $insertId |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public function getEnabledFeeds($feeds, $formData, $insertId) |
| 182 |
{ |
| 183 |
$enabledFeeds = []; |
| 184 |
foreach ($feeds as $feed) { |
| 185 |
$parsedValue = json_decode($feed->value, true); |
| 186 |
if ($parsedValue && ArrayHelper::isTrue($parsedValue, 'enabled')) { |
| 187 |
// Now check if conditions matched or not |
| 188 |
$isConditionMatched = $this->checkCondition($parsedValue, $formData, $insertId); |
| 189 |
if ($isConditionMatched) { |
| 190 |
$item = [ |
| 191 |
'id' => $feed->id, |
| 192 |
'meta_key' => $feed->meta_key, |
| 193 |
'settings' => $parsedValue |
| 194 |
]; |
| 195 |
if ($feed->meta_key == 'user_registration_feeds') { |
| 196 |
array_unshift($enabledFeeds, $item); |
| 197 |
} else { |
| 198 |
$enabledFeeds[] = $item; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
return $enabledFeeds; |
| 204 |
} |
| 205 |
} |
| 206 |
|