| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Hooks\Handlers; |
| 4 |
|
| 5 |
|
| 6 |
use FluentForm\App\Helpers\Helper; |
| 7 |
use FluentForm\App\Services\FormBuilder\ShortCodeParser; |
| 8 |
use FluentForm\Framework\Foundation\Application; |
| 9 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 10 |
Use FluentForm\App\Services\Integrations\GlobalNotificationService; |
| 11 |
|
| 12 |
class GlobalNotificationHandler |
| 13 |
{ |
| 14 |
|
| 15 |
/** |
| 16 |
* @param \FluentForm\Framework\Foundation\Application $app |
| 17 |
*/ |
| 18 |
protected $app; |
| 19 |
/** |
| 20 |
* @var GlobalNotificationService |
| 21 |
*/ |
| 22 |
private $globalNotificationService; |
| 23 |
|
| 24 |
public function __construct(Application $app) |
| 25 |
{ |
| 26 |
$this->app = $app; |
| 27 |
$this->globalNotificationService = new GlobalNotificationService(); |
| 28 |
} |
| 29 |
|
| 30 |
public function globalNotify($insertId, $formData, $form) |
| 31 |
{ |
| 32 |
// Let's find the feeds that are available for this form |
| 33 |
$feeds = apply_filters_deprecated( |
| 34 |
'fluentform_global_notification_active_types', |
| 35 |
[ |
| 36 |
[], |
| 37 |
$form->id |
| 38 |
], |
| 39 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 40 |
'fluentform/global_notification_active_types', |
| 41 |
'Use fluentform/global_notification_active_types instead of fluentform_global_notification_active_types.' |
| 42 |
); |
| 43 |
|
| 44 |
$feedKeys = apply_filters('fluentform/global_notification_active_types', $feeds, $form->id); |
| 45 |
|
| 46 |
if (! $feedKeys) { |
| 47 |
do_action_deprecated( |
| 48 |
'fluentform_global_notify_completed', |
| 49 |
[ |
| 50 |
$insertId, |
| 51 |
$form |
| 52 |
], |
| 53 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 54 |
'fluentform/global_notify_completed', |
| 55 |
'Use fluentform/global_notify_completed instead of fluentform_global_notify_completed.' |
| 56 |
); |
| 57 |
do_action('fluentform/global_notify_completed', $insertId, $form); |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$feedMetaKeys = array_keys($feedKeys); |
| 62 |
$feeds = $this->globalNotificationService->getNotificationFeeds($form, $feedMetaKeys); |
| 63 |
|
| 64 |
if (count($feeds) === 0) { |
| 65 |
do_action_deprecated( |
| 66 |
'fluentform_global_notify_completed', |
| 67 |
[ |
| 68 |
$insertId, |
| 69 |
$form |
| 70 |
], |
| 71 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 72 |
'fluentform/global_notify_completed', |
| 73 |
'Use fluentform/global_notify_completed instead of fluentform_global_notify_completed.' |
| 74 |
); |
| 75 |
do_action('fluentform/global_notify_completed', $insertId, $form); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Now we have to filter the feeds which are enabled |
| 80 |
$enabledFeeds = $this->globalNotificationService->getEnabledFeeds($feeds, $formData, $insertId); |
| 81 |
|
| 82 |
if (!$enabledFeeds) { |
| 83 |
do_action_deprecated( |
| 84 |
'fluentform_global_notify_completed', |
| 85 |
[ |
| 86 |
$insertId, |
| 87 |
$form |
| 88 |
], |
| 89 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 90 |
'fluentform/global_notify_completed', |
| 91 |
'Use fluentform/global_notify_completed instead of fluentform_global_notify_completed.' |
| 92 |
); |
| 93 |
do_action('fluentform/global_notify_completed', $insertId, $form); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$entry = false; |
| 98 |
$asyncFeeds = []; |
| 99 |
|
| 100 |
$scheduler = $this->app['fluentFormAsyncRequest']; |
| 101 |
|
| 102 |
foreach ($enabledFeeds as $feed) { |
| 103 |
$feed = apply_filters('fluentform/integration_feed_before_parse', $feed, $insertId, $formData, $form); |
| 104 |
// We will decide if this feed will run on async or sync |
| 105 |
$integrationKey = ArrayHelper::get($feedKeys, $feed['meta_key']); |
| 106 |
|
| 107 |
$oldAction = 'fluentform_integration_notify_' . $feed['meta_key']; |
| 108 |
$newAction = 'fluentform/integration_notify_' . $feed['meta_key']; |
| 109 |
|
| 110 |
if (! $entry) { |
| 111 |
$entry = $this->globalNotificationService->getEntry($insertId, $form); |
| 112 |
} |
| 113 |
// skip emails which will be sent on payment form submit otherwise email is sent after payment success |
| 114 |
if (!! $form->has_payment && ('notifications' == $feed['meta_key'])) { |
| 115 |
if (('payment_form_submit' == ArrayHelper::get($feed, 'settings.feed_trigger_event'))) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// It's sync |
| 121 |
$processedValues = $feed['settings']; |
| 122 |
unset($processedValues['conditionals']); |
| 123 |
$processedValues = ShortCodeParser::parse($processedValues, $insertId, $formData, $form, false, $feed['meta_key']); |
| 124 |
$feed['processedValues'] = $processedValues; |
| 125 |
|
| 126 |
$isAsync = apply_filters_deprecated( |
| 127 |
'fluentform_notifying_async_' . $integrationKey, |
| 128 |
[ |
| 129 |
true, |
| 130 |
$form->id |
| 131 |
], |
| 132 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 133 |
'fluentform/notifying_async_' . $integrationKey, |
| 134 |
'Use fluentform/notifying_async_' . $integrationKey . ' instead of fluentform_notifying_async_' . $integrationKey |
| 135 |
); |
| 136 |
|
| 137 |
$scheduleAction = [ |
| 138 |
'action' => $newAction, |
| 139 |
'form_id' => $form->id, |
| 140 |
'origin_id' => $insertId, |
| 141 |
'feed_id' => $feed['id'], |
| 142 |
'type' => 'submission_action', |
| 143 |
'status' => 'pending', |
| 144 |
'data' => maybe_serialize($feed), |
| 145 |
'created_at' => current_time('mysql'), |
| 146 |
'updated_at' => current_time('mysql'), |
| 147 |
]; |
| 148 |
|
| 149 |
if (apply_filters('fluentform/notifying_async_' . $integrationKey, $isAsync, $form->id)) { |
| 150 |
// It's async |
| 151 |
$asyncFeeds[] = $scheduleAction; |
| 152 |
|
| 153 |
$queueId = $scheduler->queue($scheduleAction); |
| 154 |
|
| 155 |
as_enqueue_async_action('fluentform/schedule_feed', ['queueId' => $queueId], 'fluentform'); |
| 156 |
} else { |
| 157 |
$isSyncFeedLogsEnable = apply_filters("fluentform/notifying_sync_{$integrationKey}_api_logs", false, $form->id); |
| 158 |
if ($isSyncFeedLogsEnable) { |
| 159 |
$scheduleAction['status'] = 'processing'; |
| 160 |
$feed['scheduled_action_id'] = wpFluent()->table('ff_scheduled_actions')->insertGetId($scheduleAction); |
| 161 |
} |
| 162 |
|
| 163 |
do_action_deprecated( |
| 164 |
$oldAction, |
| 165 |
[ |
| 166 |
$feed, |
| 167 |
$formData, |
| 168 |
$entry, |
| 169 |
$form |
| 170 |
], |
| 171 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 172 |
$newAction, |
| 173 |
'Use ' . $newAction . ' instead of ' . $oldAction |
| 174 |
); |
| 175 |
|
| 176 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Dynamic hook name constructed from deprecated hook |
| 177 |
do_action($newAction, $feed, $formData, $entry, $form); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (! $asyncFeeds) { |
| 182 |
do_action_deprecated( |
| 183 |
'fluentform_global_notify_completed', |
| 184 |
[ |
| 185 |
$insertId, |
| 186 |
$form |
| 187 |
], |
| 188 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 189 |
'fluentform/global_notify_completed', |
| 190 |
'Use fluentform/global_notify_completed instead of fluentform_global_notify_completed.' |
| 191 |
); |
| 192 |
do_action('fluentform/global_notify_completed', $insertId, $form); |
| 193 |
return; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|