| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Notifications; |
| 4 |
|
| 5 |
defined('ABSPATH') or die; |
| 6 |
|
| 7 |
use FluentForm\App\Helpers\Helper; |
| 8 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 9 |
use FluentForm\App\Services\FormBuilder\ShortCodeParser; |
| 10 |
use FluentForm\Framework\Foundation\Application; |
| 11 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 12 |
|
| 13 |
class EmailNotificationActions |
| 14 |
{ |
| 15 |
protected $app = null; |
| 16 |
|
| 17 |
public function __construct(Application $app) |
| 18 |
{ |
| 19 |
$this->app = $app; |
| 20 |
} |
| 21 |
|
| 22 |
public function register() |
| 23 |
{ |
| 24 |
add_filter('fluentform/notifying_async_email_notifications', '__return_false', 9); |
| 25 |
add_filter('fluentform/notifying_async_notifications', '__return_false', 9); |
| 26 |
|
| 27 |
add_filter('fluentform/global_notification_active_types', function ($types) { |
| 28 |
$types['notifications'] = 'email_notifications'; |
| 29 |
return $types; |
| 30 |
}); |
| 31 |
|
| 32 |
add_action('fluentform/integration_notify_notifications', [$this, 'notify'], 10, 4); |
| 33 |
|
| 34 |
add_action('fluentform/notify_on_form_submit', [$this, 'notifyOnSubmitPaymentForm'], 10, 3); |
| 35 |
} |
| 36 |
|
| 37 |
public function notifyOnSubmitPaymentForm($submissionId, $submissionData, $form) |
| 38 |
{ |
| 39 |
$emailFeeds = wpFluent()->table('fluentform_form_meta') |
| 40 |
->where('form_id', $form->id) |
| 41 |
->where('meta_key', 'notifications') |
| 42 |
->get(); |
| 43 |
|
| 44 |
if (count($emailFeeds) === 0) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$formData = $this->getFormData($submissionId); |
| 49 |
$notificationManager = new \FluentForm\App\Services\Integrations\GlobalNotificationManager(wpFluentForm()); |
| 50 |
|
| 51 |
$activeEmailFeeds = $notificationManager->getEnabledFeeds($emailFeeds, $formData, $submissionId); |
| 52 |
if (! $activeEmailFeeds) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$onSubmitEmailFeeds = array_filter($activeEmailFeeds, function ($feed) { |
| 57 |
return 'payment_form_submit' == ArrayHelper::get($feed, 'settings.feed_trigger_event'); |
| 58 |
}); |
| 59 |
|
| 60 |
if (! $onSubmitEmailFeeds || 'yes' === Helper::getSubmissionMeta($submissionId, '_ff_on_submit_email_sent')) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$entry = $this->getEntry($submissionId); |
| 65 |
|
| 66 |
foreach ($onSubmitEmailFeeds as $feed) { |
| 67 |
$processedValues = $feed['settings']; |
| 68 |
unset($processedValues['conditionals']); |
| 69 |
|
| 70 |
$processedValues = ShortCodeParser::parse( |
| 71 |
$processedValues, |
| 72 |
$submissionId, |
| 73 |
$formData, |
| 74 |
$form, |
| 75 |
false, |
| 76 |
$feed['meta_key'] |
| 77 |
); |
| 78 |
$feed['processedValues'] = $processedValues; |
| 79 |
|
| 80 |
$this->notify($feed, $formData, $entry, $form); |
| 81 |
} |
| 82 |
|
| 83 |
Helper::setSubmissionMeta($submissionId, '_ff_on_submit_email_sent', 'yes', $form->id); |
| 84 |
} |
| 85 |
|
| 86 |
public function notify($feed, $formData, $entry, $form) |
| 87 |
{ |
| 88 |
// If this is a payment form and the feed is configured to run on payment_success, |
| 89 |
// then do not send while the submission's payment status is still pending. |
| 90 |
if (isset($form->has_payment) && $form->has_payment) { |
| 91 |
if (FormFieldsParser::hasElement($form, 'payment_method')) { |
| 92 |
$isTriggerOnPaymentSuccess = ArrayHelper::get($feed, 'processedValues.feed_trigger_event') === 'payment_success'; |
| 93 |
$isPaymentPending = isset($entry->payment_status) && $entry->payment_status === 'pending'; |
| 94 |
if ($isTriggerOnPaymentSuccess && $isPaymentPending) { |
| 95 |
return; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$notifier = $this->app->make( |
| 101 |
'FluentForm\App\Services\FormBuilder\Notifications\EmailNotification' |
| 102 |
); |
| 103 |
|
| 104 |
$emailData = $feed['processedValues']; |
| 105 |
$emailAttachments = $this->getAttachments($emailData, $formData, $entry, $form); |
| 106 |
if ($emailAttachments) { |
| 107 |
$emailData['attachments'] = $emailAttachments; |
| 108 |
} |
| 109 |
|
| 110 |
$notifier->notify($emailData, $formData, $form, $entry->id); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param $emailData |
| 115 |
* @param $formData |
| 116 |
* @param $entry |
| 117 |
* @param $form |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
private function getAttachments($emailData, $formData, $entry, $form) |
| 122 |
{ |
| 123 |
$emailAttachments = []; |
| 124 |
|
| 125 |
$uploadDir = wp_upload_dir(); |
| 126 |
|
| 127 |
if (!empty($emailData['attachments']) && is_array($emailData['attachments'])) { |
| 128 |
$attachments = []; |
| 129 |
foreach ($emailData['attachments'] as $name) { |
| 130 |
$fileUrls = ArrayHelper::get($formData, $name); |
| 131 |
if ($fileUrls && is_array($fileUrls)) { |
| 132 |
foreach ($fileUrls as $url) { |
| 133 |
$filePath = $this->resolveUploadAttachmentPath($url, $uploadDir); |
| 134 |
if ($filePath) { |
| 135 |
$attachments[] = $filePath; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
$emailAttachments = $attachments; |
| 141 |
} |
| 142 |
$mediaAttachments = ArrayHelper::get($emailData, 'media_attachments'); |
| 143 |
if (!empty($mediaAttachments) && is_array($mediaAttachments)) { |
| 144 |
$attachments = []; |
| 145 |
foreach ($mediaAttachments as $file) { |
| 146 |
$fileUrl = ArrayHelper::get($file, 'url'); |
| 147 |
$filePath = $this->resolveUploadAttachmentPath($fileUrl, $uploadDir); |
| 148 |
if ($filePath) { |
| 149 |
$attachments[] = $filePath; |
| 150 |
} |
| 151 |
} |
| 152 |
$emailAttachments = array_merge($emailAttachments, $attachments); |
| 153 |
} |
| 154 |
|
| 155 |
$emailAttachments = apply_filters_deprecated( |
| 156 |
'fluentform_email_attachments', |
| 157 |
[ |
| 158 |
$emailAttachments, |
| 159 |
$emailData, |
| 160 |
$formData, |
| 161 |
$entry, |
| 162 |
$form |
| 163 |
], |
| 164 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 165 |
'fluentform/email_attachments', |
| 166 |
'Use fluentform/email_attachments instead of fluentform_email_attachments.' |
| 167 |
); |
| 168 |
// let others to apply attachments |
| 169 |
$emailAttachments = apply_filters( |
| 170 |
'fluentform/email_attachments', |
| 171 |
$emailAttachments, |
| 172 |
$emailData, |
| 173 |
$formData, |
| 174 |
$entry, |
| 175 |
$form |
| 176 |
); |
| 177 |
return $emailAttachments; |
| 178 |
} |
| 179 |
|
| 180 |
private function resolveUploadAttachmentPath($fileUrl, $uploadDir) |
| 181 |
{ |
| 182 |
if (!$fileUrl || empty($uploadDir['baseurl']) || empty($uploadDir['basedir'])) { |
| 183 |
return null; |
| 184 |
} |
| 185 |
|
| 186 |
$normalizedUrl = strtok($fileUrl, '?#'); |
| 187 |
if (strpos($normalizedUrl, $uploadDir['baseurl']) !== 0) { |
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
$relativePath = rawurldecode(str_replace($uploadDir['baseurl'], '', $normalizedUrl)); |
| 192 |
$relativePath = ltrim(wp_normalize_path($relativePath), '/'); |
| 193 |
|
| 194 |
if (!$relativePath || strpos($relativePath, '../') !== false) { |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
$uploadsBaseDir = realpath($uploadDir['basedir']); |
| 199 |
if (!$uploadsBaseDir) { |
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
$uploadsBaseDir = trailingslashit(wp_normalize_path($uploadsBaseDir)); |
| 204 |
$resolvedPath = realpath($uploadsBaseDir . $relativePath); |
| 205 |
|
| 206 |
if (!$resolvedPath) { |
| 207 |
return null; |
| 208 |
} |
| 209 |
|
| 210 |
$resolvedPath = wp_normalize_path($resolvedPath); |
| 211 |
if (strpos($resolvedPath, $uploadsBaseDir) !== 0 || !is_file($resolvedPath)) { |
| 212 |
return null; |
| 213 |
} |
| 214 |
|
| 215 |
return $resolvedPath; |
| 216 |
} |
| 217 |
|
| 218 |
public function getFormData($submissionId) |
| 219 |
{ |
| 220 |
$submission = wpFluent()->table('fluentform_submissions') |
| 221 |
->where('id', $submissionId) |
| 222 |
->first(); |
| 223 |
|
| 224 |
if (! $submission) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
return json_decode($submission->response, true); |
| 229 |
} |
| 230 |
|
| 231 |
public function getEntry($submissionId) |
| 232 |
{ |
| 233 |
return wpFluent()->table('fluentform_submissions') |
| 234 |
->where('id', $submissionId) |
| 235 |
->first(); |
| 236 |
} |
| 237 |
} |
| 238 |
|