1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Craft Contact Form Extensions plugin for Craft CMS 3.x. |
4
|
|
|
* |
5
|
|
|
* Adds extensions to the Craft CMS contact form plugin. |
6
|
|
|
* |
7
|
|
|
* @link https://rias.be |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018 Rias |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace rias\contactformextensions; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Plugin; |
16
|
|
|
use craft\contactform\events\SendEvent; |
17
|
|
|
use craft\contactform\Mailer; |
18
|
|
|
use craft\contactform\models\Submission; |
19
|
|
|
use craft\events\RegisterUrlRulesEvent; |
20
|
|
|
use craft\events\TemplateEvent; |
21
|
|
|
use craft\mail\Message; |
22
|
|
|
use craft\services\Plugins; |
23
|
|
|
use craft\web\twig\variables\CraftVariable; |
24
|
|
|
use craft\web\UrlManager; |
25
|
|
|
use craft\web\View; |
|
|
|
|
26
|
|
|
use rias\contactformextensions\models\Settings; |
27
|
|
|
use rias\contactformextensions\services\ContactFormExtensionsService as ContactFormExtensionsServiceService; |
28
|
|
|
use rias\contactformextensions\variables\ContactFormExtensionsVariable; |
29
|
|
|
use yii\base\Event; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Craft plugins are very much like little applications in and of themselves. We’ve made |
33
|
|
|
* it as simple as we can, but the training wheels are off. A little prior knowledge is |
34
|
|
|
* going to be required to write a plugin. |
35
|
|
|
* |
36
|
|
|
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL, |
37
|
|
|
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces. |
38
|
|
|
* |
39
|
|
|
* https://craftcms.com/docs/plugins/introduction |
40
|
|
|
* |
41
|
|
|
* @author Rias |
42
|
|
|
* |
43
|
|
|
* @since 1.0.0 |
44
|
|
|
* |
45
|
|
|
* @property ContactFormExtensionsServiceService $contactFormExtensionsService |
46
|
|
|
* @property Settings $settings |
47
|
|
|
* |
48
|
|
|
* @method Settings getSettings() |
49
|
|
|
*/ |
50
|
|
|
class ContactFormExtensions extends Plugin |
51
|
|
|
{ |
52
|
|
|
// Static Properties |
53
|
|
|
// ========================================================================= |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Static property that is an instance of this plugin class so that it can be accessed via |
57
|
|
|
* ContactFormExtensions::$plugin. |
58
|
|
|
* |
59
|
|
|
* @var ContactFormExtensions |
60
|
|
|
*/ |
61
|
|
|
public static $plugin; |
62
|
|
|
|
63
|
|
|
public $name; |
64
|
|
|
|
65
|
|
|
// Public Methods |
66
|
|
|
// ========================================================================= |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set our $plugin static property to this class so that it can be accessed via |
70
|
|
|
* CraftContactFormExtensions::$plugin. |
71
|
|
|
* |
72
|
|
|
* Called after the plugin class is instantiated; do any one-time initialization |
73
|
|
|
* here such as hooks and events. |
74
|
|
|
* |
75
|
|
|
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; |
76
|
|
|
* you do not need to load it in your init() method. |
77
|
|
|
*/ |
78
|
|
|
public function init() |
79
|
|
|
{ |
80
|
|
|
parent::init(); |
81
|
|
|
self::$plugin = $this; |
82
|
|
|
|
83
|
|
|
if (!Craft::$app->plugins->isPluginInstalled('contact-form') && !Craft::$app->request->getIsConsoleRequest()) { |
84
|
|
|
Craft::$app->session->setNotice(Craft::t('contact-form-extensions', 'The Contact Form plugin is not installed or activated, Contact Form Extensions does not work without it.')); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
Event::on(View::class, View::EVENT_BEFORE_RENDER_TEMPLATE, function (TemplateEvent $e) { |
88
|
|
|
if ( |
89
|
|
|
$e->template === 'settings/plugins/_settings' && |
90
|
|
|
$e->variables['plugin'] === $this |
91
|
|
|
) { |
92
|
|
|
// Add the tabs |
93
|
|
|
$e->variables['tabs'] = [ |
94
|
|
|
['label' => 'Settings', 'url' => '#settings-tab-settings'], |
95
|
|
|
['label' => 'reCAPTCHA', 'url' => '#settings-tab-recaptcha'], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) { |
101
|
|
|
$event->rules = array_merge($event->rules, [ |
102
|
|
|
'contact-form-extensions/submissions/<submissionId:\d+>' => 'contact-form-extensions/submissions/show-submission', |
103
|
|
|
'contact-form-extensions/submissions/<submissionId:\d+>/<siteHandle:{handle}>' => 'contact-form-extensions/submissions/show-submission', |
104
|
|
|
]); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
Event::on(Mailer::class, Mailer::EVENT_BEFORE_SEND, function (SendEvent $e) { |
108
|
|
|
if ($e->isSpam) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($this->settings->recaptcha) { |
113
|
|
|
$recaptcha = $this->contactFormExtensionsService->recaptcha; |
|
|
|
|
114
|
|
|
$captchaResponse = Craft::$app->request->getParam('g-recaptcha-response'); |
115
|
|
|
|
116
|
|
|
if (!$recaptcha->verifyResponse($captchaResponse, $_SERVER['REMOTE_ADDR'])) { |
117
|
|
|
$e->isSpam = true; |
118
|
|
|
$e->handled = true; |
119
|
|
|
|
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$submission = $e->submission; |
125
|
|
|
if ($this->settings->enableDatabase) { |
126
|
|
|
$this->contactFormExtensionsService->saveSubmission($submission); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($this->settings->enableTemplateOverwrite) { |
130
|
|
|
// First set the template mode to the Site templates |
131
|
|
|
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
132
|
|
|
|
133
|
|
|
// Render the set template |
134
|
|
|
$html = Craft::$app->view->renderTemplate( |
135
|
|
|
$this->settings->notificationTemplate, |
136
|
|
|
['submission' => $e->submission] |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
// Update the message body |
140
|
|
|
$e->message->setHtmlBody($html); |
141
|
|
|
|
142
|
|
|
// Set the template mode back to Control Panel |
143
|
|
|
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
144
|
|
|
} |
145
|
|
|
}); |
146
|
|
|
|
147
|
|
|
Event::on(Mailer::class, Mailer::EVENT_AFTER_SEND, function (SendEvent $e) { |
148
|
|
|
if ($this->settings->enableConfirmationEmail) { |
149
|
|
|
// First set the template mode to the Site templates |
150
|
|
|
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
151
|
|
|
|
152
|
|
|
// Render the set template |
153
|
|
|
$html = Craft::$app->view->renderTemplate( |
154
|
|
|
$this->settings->confirmationTemplate, |
155
|
|
|
['submission' => $e->submission] |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
// Create the confirmation email |
159
|
|
|
$message = new Message(); |
160
|
|
|
$message->setTo($e->submission->fromEmail); |
161
|
|
|
$message->setFrom($e->message->getTo()); |
162
|
|
|
$message->setHtmlBody($html); |
163
|
|
|
$message->setSubject($this->settings->getConfirmationSubject()); |
164
|
|
|
|
165
|
|
|
// Send the mail |
166
|
|
|
Craft::$app->mailer->send($message); |
167
|
|
|
|
168
|
|
|
// Set the template mode back to Control Panel |
169
|
|
|
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
170
|
|
|
} |
171
|
|
|
}); |
172
|
|
|
|
173
|
|
|
Event::on( |
174
|
|
|
CraftVariable::class, |
175
|
|
|
CraftVariable::EVENT_INIT, |
176
|
|
|
function (Event $event) { |
177
|
|
|
/** @var CraftVariable $variable */ |
178
|
|
|
$variable = $event->sender; |
179
|
|
|
$variable->set('contactFormExtensions', ContactFormExtensionsVariable::class); |
180
|
|
|
} |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
|
|
public function getCpNavItem() |
188
|
|
|
{ |
189
|
|
|
if (!$this->settings->enableDatabase) { |
190
|
|
|
return; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$navItem = parent::getCpNavItem(); |
194
|
|
|
|
195
|
|
|
$navItem['label'] = Craft::t('contact-form-extensions', 'Form Submissions'); |
196
|
|
|
|
197
|
|
|
return $navItem; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// Protected Methods |
201
|
|
|
// ========================================================================= |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Creates and returns the model used to store the plugin’s settings. |
205
|
|
|
* |
206
|
|
|
* @return \craft\base\Model|null |
207
|
|
|
*/ |
208
|
|
|
protected function createSettingsModel() |
209
|
|
|
{ |
210
|
|
|
return new Settings(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the rendered settings HTML, which will be inserted into the content |
215
|
|
|
* block on the settings page. |
216
|
|
|
* |
217
|
|
|
* @throws \Twig_Error_Loader |
218
|
|
|
* @throws \yii\base\Exception |
219
|
|
|
* |
220
|
|
|
* @return string The rendered settings HTML |
221
|
|
|
*/ |
222
|
|
|
protected function settingsHtml(): string |
223
|
|
|
{ |
224
|
|
|
// Get and pre-validate the settings |
225
|
|
|
$settings = $this->getSettings(); |
226
|
|
|
$settings->validate(); |
227
|
|
|
|
228
|
|
|
// Get the settings that are being defined by the config file |
229
|
|
|
$overrides = Craft::$app->getConfig()->getConfigFromFile(strtolower($this->handle)); |
230
|
|
|
|
231
|
|
|
return Craft::$app->view->renderTemplate( |
232
|
|
|
'contact-form-extensions/settings', |
233
|
|
|
[ |
234
|
|
|
'settings' => $this->getSettings(), |
235
|
|
|
'overrides' => array_keys($overrides), |
236
|
|
|
] |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths