| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentMail\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentMail\App\Models\Settings; |
| 6 |
use FluentMail\App\Services\NotificationHelper; |
| 7 |
use FluentMail\Includes\Request\Request; |
| 8 |
use FluentMail\Includes\Support\Arr; |
| 9 |
|
| 10 |
class SlackController extends Controller |
| 11 |
{ |
| 12 |
public function registerSite(Request $request) |
| 13 |
{ |
| 14 |
$this->verify(); |
| 15 |
|
| 16 |
$formData = $request->get('settings', []); |
| 17 |
|
| 18 |
$userEmail = sanitize_email(Arr::get($formData, 'user_email')); |
| 19 |
|
| 20 |
if (!is_email($userEmail)) { |
| 21 |
return $this->sendError([ |
| 22 |
'message' => __('Please provide a valid email address.', 'fluent-smtp') |
| 23 |
], 422); |
| 24 |
} |
| 25 |
|
| 26 |
$nonce = wp_create_nonce('fluent_smtp_slack_register_site'); |
| 27 |
|
| 28 |
$payload = [ |
| 29 |
'admin_email' => $userEmail, |
| 30 |
'smtp_url' => admin_url('options-general.php?_slack_nonce=' . $nonce . '&page=fluent-mail#/'), |
| 31 |
'site_url' => site_url(), |
| 32 |
'site_title' => fluentMailSiteTitle(), |
| 33 |
'site_lang' => get_bloginfo('language'), |
| 34 |
]; |
| 35 |
|
| 36 |
|
| 37 |
$activationData = NotificationHelper::registerSlackSite($payload); |
| 38 |
|
| 39 |
if (is_wp_error($activationData)) { |
| 40 |
return $this->sendError([ |
| 41 |
'message' => $activationData->get_error_message(), |
| 42 |
'errors' => $activationData->get_error_data(), |
| 43 |
], 422); |
| 44 |
} |
| 45 |
|
| 46 |
NotificationHelper::updateChannelSettings('slack', [ |
| 47 |
'status' => 'pending', |
| 48 |
'token' => Arr::get($activationData, 'site_token'), |
| 49 |
'redirect_url' => '' |
| 50 |
]); |
| 51 |
|
| 52 |
return $this->sendSuccess([ |
| 53 |
'message' => __('Redirecting you to Slack...', 'fluent-smtp'), |
| 54 |
'redirect_url' => Arr::get($activationData, 'redirect_url') |
| 55 |
]); |
| 56 |
} |
| 57 |
|
| 58 |
public function sendTestMessage(Request $request) |
| 59 |
{ |
| 60 |
$this->verify(); |
| 61 |
|
| 62 |
// Let's update the notification status |
| 63 |
$settings = (new Settings())->notificationSettings(); |
| 64 |
|
| 65 |
if (Arr::get($settings, 'slack.status') != 'yes') { |
| 66 |
return $this->sendError([ |
| 67 |
'message' => __('Slack notifications are not enabled.', 'fluent-smtp') |
| 68 |
], 422); |
| 69 |
} |
| 70 |
|
| 71 |
$message = __('Test message from ', 'fluent-smtp') . site_url() . '. ' . __('If you can read this, the connection is working.', 'fluent-smtp'); |
| 72 |
|
| 73 |
$result = NotificationHelper::sendSlackMessage($message, Arr::get($settings, 'slack.webhook_url')); |
| 74 |
|
| 75 |
if (is_wp_error($result)) { |
| 76 |
return $this->sendError([ |
| 77 |
'message' => $result->get_error_message(), |
| 78 |
'errors' => $result->get_error_data(), |
| 79 |
], 422); |
| 80 |
} |
| 81 |
|
| 82 |
return $this->sendSuccess([ |
| 83 |
'message' => __('Test message sent.', 'fluent-smtp') |
| 84 |
]); |
| 85 |
} |
| 86 |
|
| 87 |
public function disconnect() |
| 88 |
{ |
| 89 |
$this->verify(); |
| 90 |
|
| 91 |
NotificationHelper::updateChannelSettings('slack', [ |
| 92 |
'status' => 'no', |
| 93 |
'webhook_url' => '', |
| 94 |
'token' => '' |
| 95 |
]); |
| 96 |
|
| 97 |
return $this->sendSuccess([ |
| 98 |
'message' => __('Slack has been disconnected.', 'fluent-smtp') |
| 99 |
]); |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|