1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Rich Variables plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Allows you to easily use Craft Globals as variables in Rich Text fields |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\richvariables; |
12
|
|
|
|
13
|
|
|
use nystudio107\richvariables\models\Settings; |
14
|
|
|
use nystudio107\richvariables\assetbundles\richvariables\RichVariablesAsset; |
15
|
|
|
use nystudio107\richvariables\variables\RichVariablesVariable; |
16
|
|
|
|
17
|
|
|
use nystudio107\pluginmanifest\services\ManifestService; |
18
|
|
|
|
19
|
|
|
use Craft; |
20
|
|
|
use craft\base\Plugin; |
21
|
|
|
use craft\events\PluginEvent; |
22
|
|
|
use craft\events\RegisterUrlRulesEvent; |
23
|
|
|
use craft\helpers\UrlHelper; |
24
|
|
|
use craft\redactor\events\RegisterPluginPathsEvent; |
|
|
|
|
25
|
|
|
use craft\redactor\Field as RichText; |
|
|
|
|
26
|
|
|
use craft\services\Plugins; |
27
|
|
|
use craft\web\twig\variables\CraftVariable; |
28
|
|
|
use craft\web\UrlManager; |
29
|
|
|
|
30
|
|
|
use yii\base\Event; |
31
|
|
|
use yii\base\InvalidConfigException; |
32
|
|
|
|
33
|
|
|
use Composer\Semver\Comparator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class RichVariables |
37
|
|
|
* |
38
|
|
|
* @author nystudio107 |
|
|
|
|
39
|
|
|
* @package RichVariables |
|
|
|
|
40
|
|
|
* @since 1.0.0 |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @property ManifestService $manifest |
43
|
|
|
*/ |
|
|
|
|
44
|
|
|
class RichVariables extends Plugin |
45
|
|
|
{ |
46
|
|
|
// Static Properties |
47
|
|
|
// ========================================================================= |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @var RichVariables |
51
|
|
|
*/ |
52
|
|
|
public static $plugin; |
53
|
|
|
|
54
|
|
|
// Static Methods |
55
|
|
|
// ========================================================================= |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function __construct($id, $parent = null, array $config = []) |
61
|
|
|
{ |
62
|
|
|
$config['components'] = [ |
63
|
|
|
// Register the manifest service |
64
|
|
|
'manifest' => [ |
65
|
|
|
'class' => ManifestService::class, |
66
|
|
|
'assetClass' => RichVariablesAsset::class, |
67
|
|
|
'devServerManifestPath' => 'http://craft-richvariables-buildchain:8080/', |
68
|
|
|
'devServerPublicPath' => 'http://craft-richvariables-buildchain:8080/', |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
parent::__construct($id, $parent, $config); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Public Properties |
76
|
|
|
// ========================================================================= |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
public $schemaVersion = '1.0.0'; |
82
|
|
|
|
83
|
|
|
/** |
|
|
|
|
84
|
|
|
* @var bool |
85
|
|
|
*/ |
86
|
|
|
public $hasCpSection = false; |
87
|
|
|
|
88
|
|
|
/** |
|
|
|
|
89
|
|
|
* @var bool |
90
|
|
|
*/ |
91
|
|
|
public $hasCpSettings = true; |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
// Public Methods |
95
|
|
|
// ========================================================================= |
96
|
|
|
|
97
|
|
|
/** |
|
|
|
|
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
|
|
|
|
100
|
|
|
public function init() |
101
|
|
|
{ |
102
|
|
|
parent::init(); |
103
|
|
|
self::$plugin = $this; |
104
|
|
|
|
105
|
|
|
// Add in our event listeners that are needed for every request |
106
|
|
|
$this->installEventListeners(); |
107
|
|
|
// We're loaded! |
108
|
|
|
Craft::info( |
109
|
|
|
Craft::t( |
110
|
|
|
'rich-variables', |
111
|
|
|
'{name} plugin loaded', |
112
|
|
|
['name' => $this->name] |
113
|
|
|
), |
114
|
|
|
__METHOD__ |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Protected Methods |
119
|
|
|
// ========================================================================= |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Install our event listeners |
123
|
|
|
*/ |
|
|
|
|
124
|
|
|
protected function installEventListeners() |
125
|
|
|
{ |
126
|
|
|
Event::on( |
127
|
|
|
CraftVariable::class, |
128
|
|
|
CraftVariable::EVENT_INIT, |
129
|
|
|
function (Event $event) { |
130
|
|
|
/** @var CraftVariable $variable */ |
|
|
|
|
131
|
|
|
$variable = $event->sender; |
132
|
|
|
$variable->set('richVariables', [ |
|
|
|
|
133
|
|
|
'class' => RichVariablesVariable::class, |
134
|
|
|
'manifestService' => $this->manifest, |
135
|
|
|
]); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
); |
138
|
|
|
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
139
|
|
|
Event::on( |
140
|
|
|
Plugins::class, |
141
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
142
|
|
|
function (PluginEvent $event) { |
143
|
|
|
if ($event->plugin === $this) { |
144
|
|
|
$request = Craft::$app->getRequest(); |
145
|
|
|
if ($request->isCpRequest) { |
146
|
|
|
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('rich-variables/welcome'))->send(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
); |
151
|
|
|
$request = Craft::$app->getRequest(); |
152
|
|
|
// Install only for non-console site requests |
153
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
154
|
|
|
$this->installSiteEventListeners(); |
155
|
|
|
} |
156
|
|
|
// Install only for non-console Control Panel requests |
157
|
|
|
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
158
|
|
|
$this->installCpEventListeners(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Install site event listeners for site requests only |
164
|
|
|
*/ |
|
|
|
|
165
|
|
|
protected function installSiteEventListeners() |
166
|
|
|
{ |
167
|
|
|
// Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
168
|
|
|
Event::on( |
169
|
|
|
UrlManager::class, |
170
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
171
|
|
|
function (RegisterUrlRulesEvent $event) { |
172
|
|
|
Craft::debug( |
173
|
|
|
'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
174
|
|
|
__METHOD__ |
175
|
|
|
); |
176
|
|
|
// Register our Control Panel routes |
177
|
|
|
$event->rules = array_merge( |
178
|
|
|
$event->rules, |
179
|
|
|
$this->customFrontendRoutes() |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Install site event listeners for Control Panel requests only |
187
|
|
|
*/ |
|
|
|
|
188
|
|
|
protected function installCpEventListeners() |
189
|
|
|
{ |
190
|
|
|
// Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
191
|
|
|
Event::on( |
192
|
|
|
Plugins::class, |
193
|
|
|
Plugins::EVENT_AFTER_LOAD_PLUGINS, |
194
|
|
|
function () { |
195
|
|
|
$this->installRedactorPlugin(); |
196
|
|
|
} |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Return the custom frontend routes |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
|
|
protected function customFrontendRoutes(): array |
206
|
|
|
{ |
207
|
|
|
return [ |
208
|
|
|
]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Install our Redactor plugin |
213
|
|
|
*/ |
|
|
|
|
214
|
|
|
protected function installRedactorPlugin() |
215
|
|
|
{ |
216
|
|
|
// Make sure the Redactor plugin is installed |
217
|
|
|
$redactor = Craft::$app->getPlugins()->getPlugin('redactor'); |
218
|
|
|
if ($redactor) { |
219
|
|
|
// Event handler: RichText::EVENT_REGISTER_PLUGIN_PATHS |
220
|
|
|
Event::on( |
221
|
|
|
RichText::class, |
222
|
|
|
RichText::EVENT_REGISTER_PLUGIN_PATHS, |
223
|
|
|
function (RegisterPluginPathsEvent $event) { |
224
|
|
|
/** @var Plugin $redactor */ |
|
|
|
|
225
|
|
|
$redactor = Craft::$app->getPlugins()->getPlugin('redactor'); |
226
|
|
|
$versionDir = 'v1/'; |
227
|
|
|
if (Comparator::greaterThanOrEqualTo($redactor->version, '2.0.0')) { |
228
|
|
|
$versionDir = 'v2/'; |
229
|
|
|
} |
230
|
|
|
// Add the path to our Redactor plugin |
231
|
|
|
$src = Craft::getAlias('@nystudio107/richvariables/redactor/plugins/'.$versionDir); |
232
|
|
|
$event->paths[] = $src; |
233
|
|
|
} |
234
|
|
|
); |
235
|
|
|
// Register our asset bundle |
236
|
|
|
try { |
237
|
|
|
Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class); |
238
|
|
|
} catch (InvalidConfigException $e) { |
239
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
|
|
|
|
245
|
|
|
* @inheritdoc |
246
|
|
|
*/ |
|
|
|
|
247
|
|
|
protected function createSettingsModel() |
248
|
|
|
{ |
249
|
|
|
return new Settings(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
|
|
|
|
253
|
|
|
* @inheritdoc |
254
|
|
|
*/ |
|
|
|
|
255
|
|
|
protected function settingsHtml(): string |
256
|
|
|
{ |
257
|
|
|
// Get all of the globals sets |
258
|
|
|
$globalsHandles = []; |
259
|
|
|
$allGlobalsSets = Craft::$app->getGlobals()->getAllSets(); |
260
|
|
|
foreach ($allGlobalsSets as $globalsSet) { |
261
|
|
|
$globalsHandles[$globalsSet->handle] = $globalsSet->name; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// Render our settings template |
265
|
|
|
try { |
266
|
|
|
return Craft::$app->view->renderTemplate( |
267
|
|
|
'rich-variables/settings', |
268
|
|
|
[ |
269
|
|
|
'settings' => $this->getSettings(), |
270
|
|
|
'globalsSets' => $globalsHandles, |
271
|
|
|
] |
272
|
|
|
); |
273
|
|
|
} catch (\Twig\Error\LoaderError $e) { |
274
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
275
|
|
|
} catch (\Exception $e) { |
276
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return ''; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|