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
|
|
|
|
16
|
|
|
use Craft; |
17
|
|
|
use craft\base\Plugin; |
18
|
|
|
use craft\events\PluginEvent; |
19
|
|
|
use craft\events\RegisterUrlRulesEvent; |
20
|
|
|
use craft\helpers\UrlHelper; |
21
|
|
|
use craft\redactor\events\RegisterPluginPathsEvent; |
|
|
|
|
22
|
|
|
use craft\redactor\Field as RichText; |
|
|
|
|
23
|
|
|
use craft\services\Plugins; |
24
|
|
|
use craft\web\UrlManager; |
25
|
|
|
|
26
|
|
|
use yii\base\Event; |
27
|
|
|
use yii\base\Exception; |
28
|
|
|
use yii\base\InvalidConfigException; |
29
|
|
|
|
30
|
|
|
use Composer\Semver\Comparator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class RichVariables |
34
|
|
|
* |
35
|
|
|
* @author nystudio107 |
|
|
|
|
36
|
|
|
* @package RichVariables |
|
|
|
|
37
|
|
|
* @since 1.0.0 |
|
|
|
|
38
|
|
|
*/ |
|
|
|
|
39
|
|
|
class RichVariables extends Plugin |
40
|
|
|
{ |
41
|
|
|
// Static Properties |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* @var RichVariables |
46
|
|
|
*/ |
47
|
|
|
public static $plugin; |
48
|
|
|
|
49
|
|
|
// Public Methods |
50
|
|
|
// ========================================================================= |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
|
|
|
|
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
parent::init(); |
58
|
|
|
self::$plugin = $this; |
59
|
|
|
|
60
|
|
|
// Add in our event listeners that are needed for every request |
61
|
|
|
$this->installEventListeners(); |
62
|
|
|
// We're loaded! |
63
|
|
|
Craft::info( |
64
|
|
|
Craft::t( |
65
|
|
|
'rich-variables', |
66
|
|
|
'{name} plugin loaded', |
67
|
|
|
['name' => $this->name] |
68
|
|
|
), |
69
|
|
|
__METHOD__ |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Protected Methods |
74
|
|
|
// ========================================================================= |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Install our event listeners |
78
|
|
|
*/ |
|
|
|
|
79
|
|
|
protected function installEventListeners() |
80
|
|
|
{ |
81
|
|
|
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
82
|
|
|
Event::on( |
83
|
|
|
Plugins::class, |
84
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
85
|
|
|
function (PluginEvent $event) { |
86
|
|
|
if ($event->plugin === $this) { |
87
|
|
|
$request = Craft::$app->getRequest(); |
88
|
|
|
if ($request->isCpRequest) { |
89
|
|
|
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('rich-variables/welcome'))->send(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
$request = Craft::$app->getRequest(); |
95
|
|
|
// Install only for non-console site requests |
96
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
97
|
|
|
$this->installSiteEventListeners(); |
98
|
|
|
} |
99
|
|
|
// Install only for non-console AdminCP requests |
100
|
|
|
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
101
|
|
|
$this->installCpEventListeners(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Install site event listeners for site requests only |
107
|
|
|
*/ |
|
|
|
|
108
|
|
|
protected function installSiteEventListeners() |
109
|
|
|
{ |
110
|
|
|
// Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
111
|
|
|
Event::on( |
112
|
|
|
UrlManager::class, |
113
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
114
|
|
|
function (RegisterUrlRulesEvent $event) { |
115
|
|
|
Craft::debug( |
116
|
|
|
'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
117
|
|
|
__METHOD__ |
118
|
|
|
); |
119
|
|
|
// Register our AdminCP routes |
120
|
|
|
$event->rules = array_merge( |
121
|
|
|
$event->rules, |
122
|
|
|
$this->customFrontendRoutes() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Install site event listeners for AdminCP requests only |
130
|
|
|
*/ |
|
|
|
|
131
|
|
|
protected function installCpEventListeners() |
132
|
|
|
{ |
133
|
|
|
// Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
134
|
|
|
Event::on( |
135
|
|
|
Plugins::class, |
136
|
|
|
Plugins::EVENT_AFTER_LOAD_PLUGINS, |
137
|
|
|
function () { |
138
|
|
|
$this->installRedactorPlugin(); |
139
|
|
|
} |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Return the custom frontend routes |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
protected function customFrontendRoutes(): array |
149
|
|
|
{ |
150
|
|
|
return [ |
151
|
|
|
// Make webpack async bundle loading work out of published AssetBundles |
152
|
|
|
'/cpresources/rich-variables/<resourceType:{handle}>/<fileName>' => 'rich-variables/cp-nav/resource', |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Install our Redactor plugin |
158
|
|
|
*/ |
|
|
|
|
159
|
|
|
protected function installRedactorPlugin() |
160
|
|
|
{ |
161
|
|
|
// Make sure the Redactor plugin is installed |
162
|
|
|
$redactor = Craft::$app->getPlugins()->getPlugin('redactor'); |
163
|
|
|
if ($redactor) { |
164
|
|
|
// Event handler: RichText::EVENT_REGISTER_PLUGIN_PATHS |
165
|
|
|
Event::on( |
166
|
|
|
RichText::class, |
167
|
|
|
RichText::EVENT_REGISTER_PLUGIN_PATHS, |
168
|
|
|
function (RegisterPluginPathsEvent $event) { |
169
|
|
|
/** @var Plugin $redactor */ |
|
|
|
|
170
|
|
|
$redactor = Craft::$app->getPlugins()->getPlugin('redactor'); |
171
|
|
|
$versionDir = 'v1/'; |
172
|
|
|
if (Comparator::greaterThanOrEqualTo($redactor->version, '2.0.0')) { |
173
|
|
|
$versionDir = 'v2/'; |
174
|
|
|
} |
175
|
|
|
// Add the path to our Redactor plugin |
176
|
|
|
$src = Craft::getAlias('@nystudio107/richvariables/redactor/plugins/'.$versionDir); |
177
|
|
|
$event->paths[] = $src; |
178
|
|
|
} |
179
|
|
|
); |
180
|
|
|
// Register our asset bundle |
181
|
|
|
try { |
182
|
|
|
Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class); |
183
|
|
|
} catch (InvalidConfigException $e) { |
184
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
|
|
|
|
190
|
|
|
* @inheritdoc |
191
|
|
|
*/ |
|
|
|
|
192
|
|
|
protected function createSettingsModel() |
193
|
|
|
{ |
194
|
|
|
return new Settings(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
|
|
|
|
198
|
|
|
* @inheritdoc |
199
|
|
|
*/ |
|
|
|
|
200
|
|
|
protected function settingsHtml(): string |
201
|
|
|
{ |
202
|
|
|
// Get all of the globals sets |
203
|
|
|
$globalsHandles = []; |
204
|
|
|
$allGlobalsSets = Craft::$app->getGlobals()->getAllSets(); |
205
|
|
|
foreach ($allGlobalsSets as $globalsSet) { |
206
|
|
|
$globalsHandles[$globalsSet->handle] = $globalsSet->name; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// Render our settings template |
210
|
|
|
try { |
211
|
|
|
return Craft::$app->view->renderTemplate( |
212
|
|
|
'rich-variables/settings', |
213
|
|
|
[ |
214
|
|
|
'settings' => $this->getSettings(), |
215
|
|
|
'globalsSets' => $globalsHandles, |
216
|
|
|
] |
217
|
|
|
); |
218
|
|
|
} catch (\Twig_Error_Loader $e) { |
219
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
220
|
|
|
} catch (Exception $e) { |
221
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return ''; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|