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