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