| 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\App\Services\Notification\Manager as NotificationManager; |
| 8 |
use FluentMail\Includes\Request\Request; |
| 9 |
use FluentMail\Includes\Support\Arr; |
| 10 |
|
| 11 |
class DiscordController extends Controller |
| 12 |
{ |
| 13 |
public function registerSite(Request $request) |
| 14 |
{ |
| 15 |
$this->verify(); |
| 16 |
|
| 17 |
$formData = $request->get('settings', []); |
| 18 |
|
| 19 |
if (empty($formData['webhook_url'])) { |
| 20 |
return $this->sendError([ |
| 21 |
'message' => __('A webhook URL is required.', 'fluent-smtp') |
| 22 |
], 422); |
| 23 |
} |
| 24 |
|
| 25 |
// validate the webhook URL |
| 26 |
$webhookUrl = Arr::get($formData, 'webhook_url'); |
| 27 |
if (!filter_var($webhookUrl, FILTER_VALIDATE_URL)) { |
| 28 |
return $this->sendError([ |
| 29 |
'message' => __('Please provide a valid webhook URL.', 'fluent-smtp') |
| 30 |
], 422); |
| 31 |
} |
| 32 |
|
| 33 |
if (empty($formData['channel_name'])) { |
| 34 |
return $this->sendError([ |
| 35 |
'message' => __('A channel name is required.', 'fluent-smtp') |
| 36 |
], 422); |
| 37 |
} |
| 38 |
|
| 39 |
NotificationHelper::updateChannelSettings('discord', [ |
| 40 |
'status' => 'yes', |
| 41 |
'channel_name' => sanitize_text_field(Arr::get($formData, 'channel_name')), |
| 42 |
'webhook_url' => sanitize_url(Arr::get($formData, 'webhook_url')), |
| 43 |
]); |
| 44 |
|
| 45 |
return $this->sendSuccess([ |
| 46 |
'message' => __('Settings saved.', 'fluent-smtp'), |
| 47 |
]); |
| 48 |
} |
| 49 |
|
| 50 |
public function sendTestMessage(Request $request) |
| 51 |
{ |
| 52 |
$this->verify(); |
| 53 |
|
| 54 |
// Let's update the notification status |
| 55 |
$settings = (new Settings())->notificationSettings(); |
| 56 |
|
| 57 |
if (Arr::get($settings, 'discord.status') != 'yes') { |
| 58 |
return $this->sendError([ |
| 59 |
'message' => __('Discord notifications are not enabled.', 'fluent-smtp') |
| 60 |
], 422); |
| 61 |
} |
| 62 |
|
| 63 |
$message = __('Test message from ', 'fluent-smtp') . site_url() . '. ' . __('If you can read this, the connection is working.', 'fluent-smtp'); |
| 64 |
|
| 65 |
$result = NotificationHelper::sendDiscordMessage($message, Arr::get($settings, 'discord.webhook_url')); |
| 66 |
|
| 67 |
if (is_wp_error($result)) { |
| 68 |
return $this->sendError([ |
| 69 |
'message' => $result->get_error_message(), |
| 70 |
'errors' => $result->get_error_data(), |
| 71 |
], 422); |
| 72 |
} |
| 73 |
|
| 74 |
return $this->sendSuccess([ |
| 75 |
'message' => __('Test message sent.', 'fluent-smtp'), |
| 76 |
'server_response' => $result |
| 77 |
]); |
| 78 |
} |
| 79 |
|
| 80 |
public function disconnect() |
| 81 |
{ |
| 82 |
$this->verify(); |
| 83 |
|
| 84 |
NotificationHelper::updateChannelSettings('discord', [ |
| 85 |
'status' => 'no', |
| 86 |
'webhook_url' => '', |
| 87 |
'channel_name' => '' |
| 88 |
]); |
| 89 |
|
| 90 |
return $this->sendSuccess([ |
| 91 |
'message' => __('Discord has been disconnected.', 'fluent-smtp') |
| 92 |
]); |
| 93 |
} |
| 94 |
} |
| 95 |
|