-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.php
127 lines (115 loc) · 5.16 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php namespace Winter\DriverMailgun;
use App;
use Event;
use System\Classes\PluginBase;
use System\Models\MailSetting;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
/**
* DriverMailgun Plugin Information File
*/
class Plugin extends PluginBase
{
public $elevated = true;
const MODE_MAILGUN = 'mailgun';
public function pluginDetails()
{
return [
'name' => 'winter.drivermailgun::lang.plugin_name',
'description' => 'winter.drivermailgun::lang.plugin_description',
'homepage' => 'https://github.com/wintercms/wn-drivermailgun-plugin',
'author' => 'Winter CMS',
'icon' => 'icon-leaf',
];
}
public function register()
{
Event::listen('mailer.beforeRegister', function ($mailManager) {
$mailManager->extend(self::MODE_MAILGUN, function ($config) {
$factory = new MailgunTransportFactory();
if (!isset($config['secret'])) {
$config = $this->app['config']->get('services.mailgun', []);
}
return $factory->create(new Dsn(
'mailgun+'.($config['scheme'] ?? 'https'),
$config['endpoint'] ?? 'default',
$config['secret'],
$config['domain']
));
});
$settings = MailSetting::instance();
if ($settings->send_mode === self::MODE_MAILGUN) {
$config = App::make('config');
$config->set('mail.mailers.mailgun.transport', self::MODE_MAILGUN);
$config->set('services.mailgun.domain', $settings->mailgun_domain);
$config->set('services.mailgun.endpoint', $settings->mailgun_endpoint);
$config->set('services.mailgun.secret', $settings->mailgun_secret);
}
});
}
public function boot()
{
MailSetting::extend(function ($model) {
$model->bindEvent('model.beforeValidate', function () use ($model) {
$model->rules['mailgun_endpoint'] = 'required_if:send_mode,' . self::MODE_MAILGUN;
$model->rules['mailgun_domain'] = 'required_if:send_mode,' . self::MODE_MAILGUN;
$model->rules['mailgun_secret'] = 'required_if:send_mode,' . self::MODE_MAILGUN;
});
$model->mailgun_endpoint = config('services.mailgun.endpoint', env('MAILGUN_ENDPOINT'));
$model->mailgun_domain = config('services.mailgun.domain', env('MAILGUN_DOMAIN'));
$model->mailgun_secret = config('services.mailgun.secret', env('MAILGUN_SECRET'));
});
Event::listen('backend.form.extendFields', function ($widget) {
if (!$widget->getController() instanceof \System\Controllers\Settings) {
return;
}
if (!$widget->model instanceof MailSetting) {
return;
}
$field = $widget->getField('send_mode');
$field->options(array_merge($field->options(), [self::MODE_MAILGUN => 'Mailgun']));
$widget->addTabFields([
'mailgun_endpoint' => [
'tab' => 'system::lang.mail.general',
'type' => 'dropdown',
'label' => 'winter.drivermailgun::lang.mailgun_endpoint',
'options' => [
'api.mailgun.net' => 'US - api.mailgun.net',
'api.eu.mailgun.net' => 'EU - api.eu.mailgun.net',
],
'commentAbove' => 'winter.drivermailgun::lang.mailgun_endpoint_comment',
'span' => 'full',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[mailgun]',
],
'default' => config('services.mailgun.endpoint'),
],
'mailgun_domain' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.drivermailgun::lang.mailgun_domain',
'commentAbove' => 'winter.drivermailgun::lang.mailgun_domain_comment',
'span' => 'left',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[mailgun]',
],
],
'mailgun_secret' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.drivermailgun::lang.mailgun_secret',
'commentAbove' => 'winter.drivermailgun::lang.mailgun_secret_comment',
'span' => 'right',
'type' => 'sensitive',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[mailgun]',
],
],
]);
});
}
}