1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/videos/ |
4
|
|
|
* @copyright Copyright (c) 2020, Dukt |
5
|
|
|
* @license https://github.com/dukt/videos/blob/v2/LICENSE.md |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\videos; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\events\RegisterCacheOptionsEvent; |
12
|
|
|
use craft\events\RegisterComponentTypesEvent; |
13
|
|
|
use craft\events\RegisterUrlRulesEvent; |
14
|
|
|
use craft\helpers\UrlHelper; |
15
|
|
|
use craft\services\Fields; |
16
|
|
|
use craft\utilities\ClearCaches; |
17
|
|
|
use craft\web\twig\variables\CraftVariable; |
18
|
|
|
use craft\web\UrlManager; |
19
|
|
|
use dukt\videos\base\PluginTrait; |
20
|
|
|
use dukt\videos\fields\Video as VideoField; |
21
|
|
|
use dukt\videos\models\Settings; |
22
|
|
|
use dukt\videos\web\twig\variables\VideosVariable; |
23
|
|
|
use yii\base\Event; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Videos plugin class. |
27
|
|
|
* |
28
|
|
|
* @author Dukt <[email protected]> |
29
|
|
|
* @since 1.0 |
30
|
|
|
*/ |
31
|
|
|
class Plugin extends \craft\base\Plugin |
32
|
|
|
{ |
33
|
|
|
// Traits |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
use PluginTrait; |
37
|
|
|
|
38
|
|
|
// Properties |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
public $schemaVersion = '1.0.3'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
public $hasCpSettings = true; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \dukt\videos\Plugin The plugin instance. |
53
|
|
|
*/ |
54
|
|
|
public static $plugin; |
55
|
|
|
|
56
|
|
|
// Public Methods |
57
|
|
|
// ========================================================================= |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function init() |
63
|
|
|
{ |
64
|
|
|
parent::init(); |
65
|
|
|
self::$plugin = $this; |
66
|
|
|
|
67
|
|
|
$this->_setPluginComponents(); |
68
|
|
|
$this->_registerCpRoutes(); |
69
|
|
|
$this->_registerFieldTypes(); |
70
|
|
|
$this->_registerCacheOptions(); |
71
|
|
|
$this->_registerVariable(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
|
public function getSettingsResponse() |
78
|
|
|
{ |
79
|
|
|
$url = UrlHelper::cpUrl('videos/settings'); |
80
|
|
|
|
81
|
|
|
return Craft::$app->controller->redirect($url); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get OAuth provider options. |
86
|
|
|
* |
87
|
|
|
* @param string $gatewayHandle |
88
|
|
|
* @return null|array |
89
|
|
|
* @throws \yii\base\InvalidConfigException |
90
|
|
|
*/ |
91
|
|
|
public function getOauthProviderOptions(string $gatewayHandle) |
92
|
|
|
{ |
93
|
|
|
$options = null; |
94
|
|
|
|
95
|
|
|
$configSettings = Craft::$app->config->getConfigFromFile($this->id); |
96
|
|
|
|
97
|
|
|
if (isset($configSettings['oauthProviderOptions'][$gatewayHandle])) { |
98
|
|
|
$options = $configSettings['oauthProviderOptions'][$gatewayHandle]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$storedSettings = Craft::$app->plugins->getStoredPluginInfo($this->id)['settings']; |
102
|
|
|
|
103
|
|
|
if ($options === null && isset($storedSettings['oauthProviderOptions'][$gatewayHandle])) { |
104
|
|
|
$options = $storedSettings['oauthProviderOptions'][$gatewayHandle]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!isset($options['redirectUri'])) { |
108
|
|
|
$gateway = $this->getGateways()->getGateway($gatewayHandle, false); |
109
|
|
|
$options['redirectUri'] = $gateway->getRedirectUri(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
return $options; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Protected Methods |
117
|
|
|
// ========================================================================= |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
|
|
protected function createSettingsModel() |
123
|
|
|
{ |
124
|
|
|
return new Settings(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Protected Methods |
128
|
|
|
// ========================================================================= |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set plugin components. |
132
|
|
|
*/ |
133
|
|
|
private function _setPluginComponents() |
134
|
|
|
{ |
135
|
|
|
$this->setComponents([ |
136
|
|
|
'videos' => \dukt\videos\services\Videos::class, |
137
|
|
|
'cache' => \dukt\videos\services\Cache::class, |
138
|
|
|
'gateways' => \dukt\videos\services\Gateways::class, |
139
|
|
|
'oauth' => \dukt\videos\services\Oauth::class, |
140
|
|
|
'tokens' => \dukt\videos\services\Tokens::class, |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Register CP routes. |
146
|
|
|
*/ |
147
|
|
|
private function _registerCpRoutes() |
148
|
|
|
{ |
149
|
|
|
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) { |
150
|
|
|
$rules = [ |
151
|
|
|
'videos/settings' => 'videos/settings/index', |
152
|
|
|
'videos/settings/<gatewayHandle:{handle}>' => 'videos/settings/gateway', |
153
|
|
|
'videos/settings/<gatewayHandle:{handle}>/oauth' => 'videos/settings/gateway-oauth', |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
$event->rules = array_merge($event->rules, $rules); |
157
|
|
|
}); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Register field types. |
162
|
|
|
*/ |
163
|
|
|
private function _registerFieldTypes() |
164
|
|
|
{ |
165
|
|
|
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) { |
166
|
|
|
$event->types[] = VideoField::class; |
167
|
|
|
}); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Register cache options. |
172
|
|
|
*/ |
173
|
|
|
private function _registerCacheOptions() |
174
|
|
|
{ |
175
|
|
|
Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, function(RegisterCacheOptionsEvent $event) { |
176
|
|
|
$event->options[] = [ |
177
|
|
|
'key' => 'videos-caches', |
178
|
|
|
'label' => Craft::t('videos', 'Videos caches'), |
179
|
|
|
'action' => Craft::$app->path->getRuntimePath().'/videos' |
180
|
|
|
]; |
181
|
|
|
}); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Register template variable. |
186
|
|
|
*/ |
187
|
|
|
private function _registerVariable() |
188
|
|
|
{ |
189
|
|
|
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) { |
190
|
|
|
/** @var CraftVariable $variable */ |
191
|
|
|
$variable = $event->sender; |
192
|
|
|
$variable->set('videos', VideosVariable::class); |
193
|
|
|
}); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|