-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
91 lines (77 loc) · 3.17 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
<?php namespace Winter\DriverSparkPost;
use App;
use Event;
use System\Classes\PluginBase;
use System\Models\MailSetting;
use GuzzleHttp\Client;
use Vemcogroup\SparkPostDriver\Transport\SparkPostTransport;
/**
* Sparkpost Plugin Information File
*/
class Plugin extends PluginBase
{
public $elevated = true;
const MODE_SPARKPOST = 'sparkpost';
public function pluginDetails()
{
return [
'name' => 'winter.driversparkpost::lang.plugin.name',
'description' => 'winter.driversparkpost::lang.plugin.description',
'homepage' => 'https://github.com/wintercms/wn-driversparkpost-plugin',
'author' => 'Winter CMS',
'icon' => 'icon-leaf',
];
}
public function register()
{
Event::listen('mailer.beforeRegister', function ($mailManager) {
$mailManager->extend(self::MODE_SPARKPOST, function ($config) {
if (!isset($config['secret'])) {
$config = $this->app['config']->get('services.sparkpost', []);
}
$sparkpostOptions = $config['options'] ?? [];
$guzzleOptions = $config['guzzle'] ?? [];
$client = $this->app->make(Client::class, $guzzleOptions);
return new SparkPostTransport($client, $config['secret'], $sparkpostOptions);
});
$settings = MailSetting::instance();
if ($settings->send_mode === self::MODE_SPARKPOST) {
$config = App::make('config');
$config->set('mail.mailers.sparkpost.transport', self::MODE_SPARKPOST);
$config->set('services.sparkpost.secret', $settings->sparkpost_secret);
}
});
}
public function boot()
{
MailSetting::extend(function ($model) {
$model->bindEvent('model.beforeValidate', function () use ($model) {
$model->rules['sparkpost_secret'] = 'required_if:send_mode,' . self::MODE_SPARKPOST;
});
$model->sparkpost_secret = config('services.sparkpost.secret', env('SPARKPOST_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_SPARKPOST => 'Sparkpost']));
$widget->addTabFields([
'sparkpost_secret' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.driversparkpost::lang.sparkpost_secret',
'type' => 'sensitive',
'commentAbove' => 'winter.driversparkpost::lang.sparkpost_secret_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[sparkpost]'
],
],
]);
});
}
}