/
Plugin.php
108 lines (98 loc) · 3.68 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
<?php namespace SerenityNow\Subscribe;
use Mailchimp;
use SerenityNow\Subscribe\Models\Settings as MailChimpSettings;
use System\Models\MailSetting;
use System\Models\MailTemplate;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public $require = ['RainLab.Blog'];
public function boot()
{
//include Mailchimp V2 loaded via composer
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/vendor/mailchimp/mailchimp/src');
\Event::listen(['eloquent.updating: RainLab\Blog\Models\Post',
'eloquent.creating: RainLab\Blog\Models\Post'], function ($post) {
//send email when the post status is changed from unpublished to published
$original = $post->getOriginal();
if (count($original)) { //updating
if (($original['published'] == 0) && ($post->published == 1)) {
$this->sendEmail($post);
}
} else { //creating
if ($post->published == 1) {
$this->sendEmail($post);
}
}
});
}
private function sendEmail($post)
{
$contentAndSubject = $this->getContentAndSubject('serenitynow.subscribe::mail.email', ['post' => $post]);
$options = [
'list_id' => MailChimpSettings::get('list_id'),
'subject' => $contentAndSubject['subject'],
'from_name' => MailSetting::get('sender_name'),
'from_email' => MailSetting::get('sender_email'),
];
/*
* Mailchimp API V2.0
*/
try {
$mailchimp = new Mailchimp(MailChimpSettings::get('api_key'));
$campaign = $mailchimp->campaigns->create('regular', $options, array('html' => $contentAndSubject['html']));
$mailchimp->campaigns->send($campaign['id']);
} catch (\MailChimp_Error $e) {
throw new \Exception('MailChimp returned the following error: ' . $e->getMessage());
}
}
/**
* @param $code
* @param $data
*
* @return array('html'=>,'subject'=>)
* @throws \Exception
* Helper function to extract content and subject from the backend mail template
*/
private function getContentAndSubject($code, $data)
{
try {
$template = MailTemplate::whereCode($code)->firstOrFail();
$html = \Twig::parse($template->content_html, $data);
if ($template->layout) {
$html = \Twig::parse($template->layout->content_html, [
'content' => $html,
'css' => $template->layout->content_css
] + (array)$data);
}
return ['html' => $html, 'subject' => $template->subject];
} catch (\Exception $ex) {
throw new \Exception("Error accessing Email template: $code. Please refresh plugin and retry");
}
}
public function registerMailTemplates()
{
return [
'serenitynow.subscribe::mail.email' => 'E-mail Sent To Subscribers',
];
}
public function registerComponents()
{
return [
'SerenityNow\Subscribe\Components\Signup' => 'Signup',
];
}
public function registerSettings()
{
return [
'settings' => [
'category' => 'Blog',
'label' => 'MailChimp Subscription',
'icon' => 'icon-envelope',
'description' => 'On Blog Publish, Send Email to All Subscribers',
'class' => 'SerenityNow\Subscribe\Models\Settings',
'order' => 500
]
];
}
}