-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
179 lines (155 loc) · 5.77 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
class buttondown_newsletter extends Plugin {
public function init()
{
global $site;
// Fields and default values for the database of this plugin
$this->dbFields = array(
'apiKey'=>'', // APIKey form buttondown
'paused'=> false,
'startDate' => date('Y-m-d H:i:s'), // page creation date which newsletter will be send YYYY-MM-DD Hours:Minutes:Seconds
'sentEmails'=> json_encode(array()),
'includeCover' => false,
'subjectPrefix' => 'New post on '.$site->title().': ',
);
}
// Method/function to process the post settings.
public function post()
{
// Process the POST variables like in the parent function.
// Code from file: bludit/bl-kernel/abstract/plugin.class.php
$args = $_POST;
foreach ($this->dbFields as $key=>$value) {
if (isset($args[$key])) {
$value = Sanitize::html($args[$key]);
if ($value === 'false') {
$value = false;
} elseif ($value === 'true') {
$value = true;
}
settype($value, gettype($this->dbFields[$key]));
$this->db[$key] = $value;
}
}
// Save the plugin settings to the database.
return $this->save();
}
// Method called on the settings of the plugin on the admin area
public function form()
{
global $L;
$sentEmails = $this->getSentEmailsArray();
$html = '<div class="alert alert-primary" role="alert">';
$html .= $this->description();
$html .= '</div>';
$html .= '<div>';
// API key
$html .= '<label><strong>Buttondown API Key</strong></label>';
$html .= '<input id="apiKey" name="apiKey" type="text" value="'.$this->getValue('apiKey').'">';
$html .= '<span class="tip">Copy your API key on https://buttondown.email/settings/programming </span>';
// Sending paused
$html .= '<label>'.$L->get('paused').'</label>';
$html .= '<select id="paused" name="paused" type="text">';
$html .= '<option value="true" '.($this->getValue('paused')===true?'selected':'').'>'.$L->get('is-paused').'</option>';
$html .= '<option value="false" '.($this->getValue('paused')===false?'selected':'').'>'.$L->get('is-active').'</option>';
$html .= '</select>';
$html .= '<span class="tip">'.$L->get('paused-tip').'</span>';
// Start date
$html .= '<label>'.$L->get('send-after').'</label>';
$html .= '<input id="startDate" name="startDate" type="text" value="'.$this->getValue('startDate').'">';
$html .= '<span class="tip">'.$L->get('send-after-tip').'</span>';
// Subject Prefix
$html .= '<label>'.$L->get('subject-prefix').'</label>';
$html .= '<input id="subjectPrefix" name="subjectPrefix" type="text" value="'.$this->getValue('subjectPrefix').'">';
$html .= '<span class="tip">'.$L->get('subject-prefix-tip').'</span>';
// Sending paused
$html .= '<label>'.$L->get('include-cover').'</label>';
$html .= '<select id="includeCover" name="includeCover" type="text">';
$html .= '<option value="true" '.($this->getValue('includeCover')===true?'selected':'').'>'.$L->get('yes').'</option>';
$html .= '<option value="false" '.($this->getValue('includeCover')===false?'selected':'').'>'.$L->get('no').'</option>';
$html .= '</select>';
$html .= '<span class="tip">'.$L->get('include-cover-tip').'</span>';
$html .= '</div><hr>';
// List of page keys for which mail was sent
$html .= '<h4>'.$L->get('sent-list').'</h4>';
$html .= '<span class="tip">'.$L->get('sent-list-tip').'</span>';
$html .= '<div style="overflow-y: scroll; height:400px;"><ul>';
foreach ($sentEmails as $sentKey):
$html .= '<li>'.$sentKey.'</li>';
endforeach;
$html .= '</ul></div>';
$html .= '<div><a target="_blank" rel="noopener" href="https://buttondown.email/archive">Buttondown Archive</a></div>';
return $html;
}
// Get array of sent email keys
public function getSentEmailsArray()
{
$sentEmailsJSON = $this->getValue('sentEmails',false);
$sentEmails = json_decode($sentEmailsJSON, true);
return $sentEmails;
}
public function sendEmail($key)
{
if (!$this->getValue('paused')){
$apiKey = $this->getValue('apiKey');
$sentEmails = $this->getSentEmailsArray();
$url = 'https://api.buttondown.email/v1/emails';
$page = new Page($key);
$startDateTS = strtotime($this->getValue('startDate'));
$pageDateTS = strtotime($page->dateRaw());
global $syslog;
// Only if no newslettetr was sent and page is NOT static, ausosave, nonindex
if ($page->published() and
!$page->isStatic() and
!$page->autosave() and
!$page->noindex() and
$startDateTS <= $pageDateTS and
!in_array($key, $sentEmails)){
$body = '<h1>'.$page->title().'</h1>'.$page->content();
if($page->coverImage() and $this->getValue('includeCover')){
$body = '<img class="teaser" src="'.$page->coverImage().'">'.$body;
}
$data = array(
'body' => $body,
'secondary_id' => $page->position(),
'subject' => $this->getValue('subjectPrefix').$page->title(),
'external_url' => $page->permalink(),
'slug' => $page->slug(),
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Authorization: Token {$apiKey}\r\n".
"Content-Type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ } else {
// Add key to lsit of sent emails
array_push($sentEmails, $key);
$syslog->add(array(
'dictionaryKey'=>'buttondown-sent',
'notes'=>$key
));
$this->db['sentEmails'] = json_encode($sentEmails);
var_dump($result);
return $this->save();
}
var_dump($result);
}
}
return false;
}
public function afterPageCreate($key)
{
$this -> sendEmail($key);
}
public function afterPageModify($key)
{
$this -> sendEmail($key);
}
}
?>