PluginProbe
FluentSMTP – WP Mail SMTP Plugin with Amazon SES, SendGrid, Mailgun, Postmark, Cloudflare, toSend, Gmail and Any SMTP / trunk
FluentSMTP – WP Mail SMTP Plugin with Amazon SES, SendGrid, Mailgun, Postmark, Cloudflare, toSend, Gmail and Any SMTP vtrunk
2.4.0 trunk 1.0.1 1.1.0 1.1.1 1.2.0 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.71 2.2.72 2.2.73 2.2.80 2.2.81 All 32 releases
fluent-smtp / app / Http / Controllers / DiscordController.php

DiscordController.php in FluentSMTP – WP Mail SMTP Plugin with Amazon SES, SendGrid, Mailgun, Postmark, Cloudflare, toSend, Gmail and Any SMTP trunk, at app/Http/Controllers/DiscordController.php

95 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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