| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
use FluentForm\App\Modules\Form\FormDataParser; |
| 7 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 8 |
|
| 9 |
class Slack |
| 10 |
{ |
| 11 |
/** |
| 12 |
* The slack integration settings of the form. |
| 13 |
* |
| 14 |
* @var array $settings |
| 15 |
*/ |
| 16 |
protected $settings = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Determine whether the slack notification should be sent or not. |
| 20 |
* |
| 21 |
* @param $formId |
| 22 |
* |
| 23 |
* @return boolean |
| 24 |
*/ |
| 25 |
public function shouldApply($formId) |
| 26 |
{ |
| 27 |
$this->settings = wpFluent()->table('fluentform_form_meta') |
| 28 |
->where('form_id', $formId) |
| 29 |
->where('meta_key', 'slack') |
| 30 |
->first(); |
| 31 |
|
| 32 |
$this->settings = $this->settings ? json_decode($this->settings->value, true) : []; |
| 33 |
|
| 34 |
return ArrayHelper::get($this->settings, 'enabled', false); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Handle slack notifier. |
| 39 |
* |
| 40 |
* @param $submissionId |
| 41 |
* @param $formData |
| 42 |
* @param $form |
| 43 |
*/ |
| 44 |
public function handle($submissionId, $formData, $form) |
| 45 |
{ |
| 46 |
if (!$this->shouldApply($form->id)) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$inputs = FormFieldsParser::getEntryInputs($form); |
| 51 |
|
| 52 |
$labels = FormFieldsParser::getAdminLabels($form, $formFields); |
| 53 |
|
| 54 |
$formData = FormDataParser::parseData((object) $formData, $inputs, $form->id); |
| 55 |
|
| 56 |
$title = __("New submission on ".$form->title, 'fluentform'); |
| 57 |
|
| 58 |
$fields = []; |
| 59 |
|
| 60 |
foreach ($formData as $attribute => $value) { |
| 61 |
$value = str_replace('&', '&', $value); |
| 62 |
$value = str_replace('<', '<', $value); |
| 63 |
$value = str_replace('>', ">", $value); |
| 64 |
|
| 65 |
$fields[] = [ |
| 66 |
'title' => $labels[$attribute], |
| 67 |
'value' => $value, |
| 68 |
'short' => false |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
$slackHook = ArrayHelper::get($this->settings, 'webhook'); |
| 73 |
|
| 74 |
$titleLink = admin_url('admin.php?page=fluent_forms&form_id=' |
| 75 |
.$form->id |
| 76 |
.'&route=entries#/entries/' |
| 77 |
.$submissionId |
| 78 |
); |
| 79 |
|
| 80 |
$body = [ |
| 81 |
'payload' => json_encode([ |
| 82 |
'attachments' => [ |
| 83 |
[ |
| 84 |
'color' => '#0078ff', |
| 85 |
'fallback' => $title, |
| 86 |
'title' => $title, |
| 87 |
'title_link' => $titleLink, |
| 88 |
'fields' => $fields, |
| 89 |
'footer' => 'fluentform', |
| 90 |
'ts' => current_time('timestamp') |
| 91 |
] |
| 92 |
] |
| 93 |
]) |
| 94 |
]; |
| 95 |
|
| 96 |
wp_remote_post($slackHook, [ |
| 97 |
'method' => 'POST', |
| 98 |
'timeout' => 30, |
| 99 |
'redirection' => 5, |
| 100 |
'httpversion' => '1.0', |
| 101 |
'blocking' => false, |
| 102 |
'headers' => [], |
| 103 |
'body' => $body, |
| 104 |
'cookies' => [] |
| 105 |
]); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Invoke slack notifier. |
| 110 |
* |
| 111 |
* @param $submissionId |
| 112 |
* @param $formData |
| 113 |
* @param $form |
| 114 |
*/ |
| 115 |
public static function notify($submissionId, $formData, $form) |
| 116 |
{ |
| 117 |
(new static)->handle($submissionId, $formData, $form); |
| 118 |
} |
| 119 |
} |