| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Integrations\Slack; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Form\FormDataParser; |
| 6 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 7 |
use FluentForm\App\Services\Integrations\LogResponseTrait; |
| 8 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 9 |
|
| 10 |
class Slack |
| 11 |
{ |
| 12 |
use LogResponseTrait; |
| 13 |
|
| 14 |
/** |
| 15 |
* The slack integration settings of the form. |
| 16 |
* |
| 17 |
* @var array $settings |
| 18 |
*/ |
| 19 |
protected $settings = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Handle slack notifier. |
| 23 |
* |
| 24 |
* @param $submissionId |
| 25 |
* @param $formData |
| 26 |
* @param $form |
| 27 |
*/ |
| 28 |
public function handle($feed, $formData, $form, $entry) |
| 29 |
{ |
| 30 |
$settings = $feed['processedValues']; |
| 31 |
|
| 32 |
$inputs = FormFieldsParser::getEntryInputs($form); |
| 33 |
|
| 34 |
$labels = FormFieldsParser::getAdminLabels($form, $inputs); |
| 35 |
|
| 36 |
$formData = FormDataParser::parseData((object)$formData, $inputs, $form->id); |
| 37 |
|
| 38 |
$slackTitle = ArrayHelper::get($settings, 'textTitle'); |
| 39 |
|
| 40 |
if($slackTitle === '') { |
| 41 |
$title = "New submission on " . $form->title; |
| 42 |
}else { |
| 43 |
$title = $slackTitle; |
| 44 |
} |
| 45 |
|
| 46 |
$fields = []; |
| 47 |
|
| 48 |
foreach ($formData as $attribute => $value) { |
| 49 |
$value = str_replace('&', '&', $value); |
| 50 |
$value = str_replace('<', '<', $value); |
| 51 |
$value = str_replace('>', ">", $value); |
| 52 |
|
| 53 |
$fields[] = [ |
| 54 |
'title' => $labels[$attribute], |
| 55 |
'value' => $value, |
| 56 |
'short' => false |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
$slackHook = ArrayHelper::get($settings, 'webhook'); |
| 61 |
|
| 62 |
$titleLink = admin_url('admin.php?page=fluent_forms&form_id=' |
| 63 |
. $form->id |
| 64 |
. '&route=entries#/entries/' |
| 65 |
. $entry->id |
| 66 |
); |
| 67 |
|
| 68 |
$body = [ |
| 69 |
'payload' => json_encode([ |
| 70 |
'attachments' => [ |
| 71 |
[ |
| 72 |
'color' => '#0078ff', |
| 73 |
'fallback' => $title, |
| 74 |
'title' => $title, |
| 75 |
'title_link' => $titleLink, |
| 76 |
'fields' => $fields, |
| 77 |
'footer' => 'fluentform', |
| 78 |
'ts' => round(microtime(true) * 1000) |
| 79 |
] |
| 80 |
] |
| 81 |
]) |
| 82 |
]; |
| 83 |
|
| 84 |
$result = wp_remote_post($slackHook, [ |
| 85 |
'method' => 'POST', |
| 86 |
'timeout' => 30, |
| 87 |
'redirection' => 5, |
| 88 |
'httpversion' => '1.0', |
| 89 |
'headers' => [], |
| 90 |
'body' => $body, |
| 91 |
'cookies' => [] |
| 92 |
]); |
| 93 |
|
| 94 |
if (is_wp_error($result)) { |
| 95 |
$status = 'failed'; |
| 96 |
$message = $result->get_error_message(); |
| 97 |
} else { |
| 98 |
$message = $result['response']; |
| 99 |
$status = $result['response']['code'] == 200 ? 'success' : 'failed'; |
| 100 |
} |
| 101 |
|
| 102 |
if ($status == 'failed') { |
| 103 |
do_action('ff_integration_action_result', $feed, 'failed', $message); |
| 104 |
} else { |
| 105 |
do_action('ff_integration_action_result', $feed, 'success', 'Submission notification has been successfully delivered to slack channel'); |
| 106 |
} |
| 107 |
|
| 108 |
return array( |
| 109 |
'status' => $status, |
| 110 |
'message' => $message |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
|