1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Template Comments plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Adds a HTML comment to demarcate each Twig template that is included or extended. |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\templatecomments; |
12
|
|
|
|
13
|
|
|
use nystudio107\templatecomments\models\Settings; |
14
|
|
|
use nystudio107\templatecomments\web\twig\CommentsTwigExtension; |
15
|
|
|
use nystudio107\templatecomments\web\twig\CommentTemplateLoader; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
use craft\base\Plugin; |
19
|
|
|
use craft\events\TemplateEvent; |
20
|
|
|
use craft\web\View; |
21
|
|
|
|
22
|
|
|
use yii\base\Event; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class TemplateComments |
26
|
|
|
* |
27
|
|
|
* @author nystudio107 |
|
|
|
|
28
|
|
|
* @package TemplateComments |
|
|
|
|
29
|
|
|
* @since 1.0.0 |
|
|
|
|
30
|
|
|
* |
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
class TemplateComments extends Plugin |
33
|
|
|
{ |
34
|
|
|
// Static Properties |
35
|
|
|
// ========================================================================= |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var TemplateComments |
39
|
|
|
*/ |
40
|
|
|
public static $plugin; |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var Settings $settings |
44
|
|
|
*/ |
45
|
|
|
public static $settings; |
46
|
|
|
|
47
|
|
|
// Public Properties |
48
|
|
|
// ========================================================================= |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $schemaVersion = '1.0.0'; |
54
|
|
|
|
55
|
|
|
// Public Methods |
56
|
|
|
// ========================================================================= |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
|
|
|
|
61
|
|
|
public function init() |
62
|
|
|
{ |
63
|
|
|
parent::init(); |
64
|
|
|
// Initialize properties |
65
|
|
|
self::$plugin = $this; |
66
|
|
|
self::$settings = $this->getSettings(); |
67
|
|
|
// Add in our Craft components |
68
|
|
|
$this->addComponents(); |
69
|
|
|
// Install our global event handlers |
70
|
|
|
$this->installEventListeners(); |
71
|
|
|
|
72
|
|
|
Craft::info( |
73
|
|
|
Craft::t( |
74
|
|
|
'templatecomments', |
75
|
|
|
'{name} plugin loaded', |
76
|
|
|
['name' => $this->name] |
77
|
|
|
), |
78
|
|
|
__METHOD__ |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Protected Methods |
83
|
|
|
// ========================================================================= |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add in our Craft components |
87
|
|
|
*/ |
|
|
|
|
88
|
|
|
protected function addComponents() |
89
|
|
|
{ |
90
|
|
|
$request = Craft::$app->getRequest(); |
91
|
|
|
// Install only for non-console site requests |
92
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
93
|
|
|
$this->installSiteComponents(); |
94
|
|
|
} |
95
|
|
|
// Install only for non-console AdminCP requests |
96
|
|
|
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
97
|
|
|
$this->installCpComponents(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Install components for site requests only |
104
|
|
|
*/ |
|
|
|
|
105
|
|
|
protected function installSiteComponents() |
106
|
|
|
{ |
107
|
|
|
if (self::$settings->siteTemplateComments) { |
108
|
|
|
$this->installTemplateComponents(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Install components for AdminCP requests only |
114
|
|
|
*/ |
|
|
|
|
115
|
|
|
protected function installCpComponents() |
116
|
|
|
{ |
117
|
|
|
if (self::$settings->cpTemplateComments) { |
118
|
|
|
$this->installTemplateComponents(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Install our event listeners |
124
|
|
|
*/ |
|
|
|
|
125
|
|
|
protected function installEventListeners() |
126
|
|
|
{ |
127
|
|
|
$request = Craft::$app->getRequest(); |
128
|
|
|
// Install only for non-console site requests |
129
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
130
|
|
|
$this->installSiteEventListeners(); |
131
|
|
|
} |
132
|
|
|
// Install only for non-console AdminCP requests |
133
|
|
|
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
134
|
|
|
$this->installCpEventListeners(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Install site event listeners for site requests only |
140
|
|
|
*/ |
|
|
|
|
141
|
|
|
protected function installSiteEventListeners() |
142
|
|
|
{ |
143
|
|
|
if (self::$settings->siteTemplateComments) { |
144
|
|
|
$this->installTemplateEventListeners(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Install site event listeners for AdminCP requests only |
150
|
|
|
*/ |
|
|
|
|
151
|
|
|
protected function installCpEventListeners() |
152
|
|
|
{ |
153
|
|
|
if (self::$settings->cpTemplateComments) { |
154
|
|
|
$this->installTemplateEventListeners(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
|
|
|
|
159
|
|
|
* @inheritdoc |
160
|
|
|
*/ |
|
|
|
|
161
|
|
|
protected function createSettingsModel() |
162
|
|
|
{ |
163
|
|
|
return new Settings(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Private Methods |
167
|
|
|
// ========================================================================= |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Install our template components |
171
|
|
|
*/ |
|
|
|
|
172
|
|
|
private function installTemplateComponents() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
175
|
|
|
if (!self::$settings->onlyCommentsInDevMode |
176
|
|
|
|| (self::$settings->onlyCommentsInDevMode && $devMode)) { |
|
|
|
|
177
|
|
|
Craft::$app->view->registerTwigExtension(new CommentsTwigExtension()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Install our template event listeners |
183
|
|
|
*/ |
|
|
|
|
184
|
|
|
private function installTemplateEventListeners() |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
187
|
|
|
if (!self::$settings->onlyCommentsInDevMode |
188
|
|
|
|| (self::$settings->onlyCommentsInDevMode && $devMode)) { |
|
|
|
|
189
|
|
|
// Remember the name of the currently rendering template |
190
|
|
|
Event::on( |
191
|
|
|
View::class, |
192
|
|
|
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
193
|
|
|
function (TemplateEvent $event) { |
194
|
|
|
$view = Craft::$app->getView(); |
195
|
|
|
if ($this->enabledForTemplate($event->template)) { |
196
|
|
|
$view->getTwig()->setLoader(new CommentTemplateLoader($view)); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Is template parsing enabled for this template? |
205
|
|
|
* |
206
|
|
|
* @param string $templateName |
|
|
|
|
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
private function enabledForTemplate(string $templateName): bool |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$ext = pathinfo($templateName, PATHINFO_EXTENSION); |
213
|
|
|
return (self::$settings->templateCommentsEnabled |
214
|
|
|
&& \in_array($ext, self::$settings->allowedTemplateSuffixes, false)); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|