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 Craft; |
14
|
|
|
use craft\base\Plugin; |
15
|
|
|
use nystudio107\templatecomments\models\Settings; |
16
|
|
|
use nystudio107\templatecomments\web\twig\CommentsTwigExtension; |
17
|
|
|
use nystudio107\templatecomments\web\twig\CommentTemplateLoader; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class TemplateComments |
21
|
|
|
* |
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package TemplateComments |
|
|
|
|
24
|
|
|
* @since 1.0.0 |
|
|
|
|
25
|
|
|
* |
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class TemplateComments extends Plugin |
28
|
|
|
{ |
29
|
|
|
// Static Properties |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var TemplateComments |
34
|
|
|
*/ |
35
|
|
|
public static $plugin; |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var Settings $settings |
39
|
|
|
*/ |
40
|
|
|
public static $settings; |
41
|
|
|
|
42
|
|
|
// Public Properties |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $schemaVersion = '1.0.0'; |
49
|
|
|
|
50
|
|
|
// Public Methods |
51
|
|
|
// ========================================================================= |
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
|
|
|
|
56
|
|
|
public function init() |
57
|
|
|
{ |
58
|
|
|
parent::init(); |
59
|
|
|
// Initialize properties |
60
|
|
|
self::$plugin = $this; |
61
|
|
|
self::$settings = $this->getSettings(); |
62
|
|
|
// Add in our Craft components |
63
|
|
|
$this->addComponents(); |
64
|
|
|
|
65
|
|
|
Craft::info( |
66
|
|
|
Craft::t( |
67
|
|
|
'templatecomments', |
68
|
|
|
'{name} plugin loaded', |
69
|
|
|
['name' => $this->name] |
70
|
|
|
), |
71
|
|
|
__METHOD__ |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Protected Methods |
76
|
|
|
// ========================================================================= |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add in our Craft components |
80
|
|
|
*/ |
|
|
|
|
81
|
|
|
protected function addComponents() |
82
|
|
|
{ |
83
|
|
|
$request = Craft::$app->getRequest(); |
84
|
|
|
if (!$request->getIsConsoleRequest()) { |
85
|
|
|
// Do nothing at all on AJAX requests |
86
|
|
|
if ($request->getIsAjax()) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
// Install only for site requests |
90
|
|
|
if ($request->getIsSiteRequest()) { |
91
|
|
|
$this->installSiteComponents(); |
92
|
|
|
} |
93
|
|
|
// Install only for Control Panel requests |
94
|
|
|
if ($request->getIsCpRequest()) { |
95
|
|
|
$this->installCpComponents(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Install components for site requests only |
102
|
|
|
*/ |
|
|
|
|
103
|
|
|
protected function installSiteComponents() |
104
|
|
|
{ |
105
|
|
|
if (self::$settings->siteTemplateComments) { |
106
|
|
|
$this->installTemplateComponents(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Install components for Control Panel requests only |
112
|
|
|
*/ |
|
|
|
|
113
|
|
|
protected function installCpComponents() |
114
|
|
|
{ |
115
|
|
|
if (self::$settings->cpTemplateComments) { |
116
|
|
|
$this->installTemplateComponents(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
|
|
|
|
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
|
|
|
|
123
|
|
|
protected function createSettingsModel() |
124
|
|
|
{ |
125
|
|
|
return new Settings(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Private Methods |
129
|
|
|
// ========================================================================= |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Install our template components |
133
|
|
|
*/ |
|
|
|
|
134
|
|
|
private function installTemplateComponents() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
137
|
|
|
if (!self::$settings->onlyCommentsInDevMode |
138
|
|
|
|| (self::$settings->onlyCommentsInDevMode && $devMode)) { |
|
|
|
|
139
|
|
|
Craft::$app->view->registerTwigExtension(new CommentsTwigExtension()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|