This repository was archived by the owner on Jan 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclicksend-form-notifier.php
More file actions
185 lines (148 loc) · 5.3 KB
/
clicksend-form-notifier.php
File metadata and controls
185 lines (148 loc) · 5.3 KB
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
180
181
182
183
184
185
<?php
namespace Grav\Plugin;
use Grav\Common\Config\Config;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
/**
* Class ClicksendFormNotifyPlugin
*
* @package Grav\Plugin
*/
class ClicksendFormNotifierPlugin extends Plugin
{
private $client;
/**
* Constructor.
*
* @param string $name
* @param Grav $grav
* @param Config $config
*/
public function __construct($name, Grav $grav, Config $config = null)
{
$this->client = new Client([
'base_uri' => 'http://rest.clicksend.com',
'timeout' => 5,
]);
parent::__construct($name, $grav, $config);
}
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin() OR ! $this->isFormTriggered()) {
return;
}
// Enable the main event we are interested in
$this->enable([
'onFormProcessed' => ['onFormProcessed', 0],
]);
}
public function onFormProcessed()
{
$referer = $this->getReferrer($this->grav['uri']);
$enable_notification = $this->config->get('plugins.clicksend-form-notifier.enable_notification');
$username = $this->config->get('plugins.clicksend-form-notifier.username');
$apiKey = $this->config->get('plugins.clicksend-form-notifier.api_key');
$from = $this->config->get('plugins.clicksend-form-notifier.from');
$to = $this->config->get('plugins.clicksend-form-notifier.to');
$body = $this->config->get('plugins.clicksend-form-notifier.body');
$enable_auto_response = $this->config->get('plugins.clicksend-form-notifier.enable_auto_response');
$phone_field = $this->config->get('plugins.clicksend-form-notifier.phone_field');
$auto_response_msg = $this->config->get('plugins.clicksend-form-notifier.auto_response_msg');
$messagesPayload = [];
$formName = $this->getFormName();
// Replace placeholders.
$body = trim(str_replace('{{FORM_NAME}}', $formName, $body));
$auto_response_msg = trim(str_replace('{{FORM_NAME}}', $formName, $auto_response_msg));
// Phone field value.
$phone = (isset($_POST['data'][$phone_field]) AND ! empty($_POST['data'][$phone_field])) ? $_POST['data'][$phone_field] : '';
// Prepare message payload.
if ($enable_notification AND $to AND $body) {
$messagesPayload[] = [
'source' => 'grav',
'from' => $from,
'to' => $to,
'body' => $body,
];
}
if ($enable_auto_response AND $phone AND $auto_response_msg) {
$messagesPayload[] = [
'source' => 'grav',
'from' => $from,
'to' => $phone,
'body' => $auto_response_msg,
];
}
// Send SMS notification message.
try {
$response = $this->client->post('v3/sms/send', [
'auth' => [$username, $apiKey],
'json' => [
'messages' => $messagesPayload,
],
]);
$result = \GuzzleHttp\json_decode($response->getBody());
if ($response->getStatusCode() == 200) {
return $this->grav->redirect($referer.'?'.http_build_query(['submitted' => 'sent']));
}
return $this->grav->redirect($referer.'?'.http_build_query(['submitted' => 'not_sent', 'error' => $result->response_code]));
} catch (ClientException $e) {
$result = \GuzzleHttp\json_decode($e->getResponse()->getBody());
return $this->grav->redirect($referer.'?'.http_build_query(['submitted' => 'not_sent', 'error' => $result->response_code]));
}
}
/**
* Check if this is triggered by a form submit.
*
* @return bool
*/
private function isFormTriggered()
{
return isset($_POST['__form-name__']) ? true : false;
}
/**
* Get form name.
*
* @return string
*/
private function getFormName()
{
return isset($_POST['__form-name__']) ? trim(strip_tags($_POST['__form-name__'])) : '';
}
/**
* Get base referrer.
*
* @param Uri $uri
*
* @return array|string
*/
private function getReferrer(Uri $uri)
{
$referer = $uri->referrer('/', null);
$referer = explode('?', $referer);
$referer = isset($referer[0]) ? trim($referer[0]) : '/';
return $referer;
}
}