1 | <?php |
||
2 | /** |
||
3 | * Transcoder plugin for Craft CMS |
||
4 | * |
||
5 | * Transcode videos to various formats, and provide thumbnails of the video |
||
6 | * |
||
7 | * @link https://nystudio107.com |
||
8 | * @copyright Copyright (c) 2017 nystudio107 |
||
9 | */ |
||
10 | |||
11 | namespace nystudio107\transcoder; |
||
12 | |||
13 | use Craft; |
||
14 | use craft\base\Model; |
||
15 | use craft\base\Plugin; |
||
16 | use craft\console\Application as ConsoleApplication; |
||
17 | use craft\elements\Asset; |
||
18 | use craft\events\DefineAssetThumbUrlEvent; |
||
19 | use craft\events\PluginEvent; |
||
20 | use craft\events\RegisterCacheOptionsEvent; |
||
21 | use craft\events\RegisterUrlRulesEvent; |
||
22 | use craft\helpers\Assets as AssetsHelper; |
||
23 | use craft\helpers\FileHelper; |
||
24 | use craft\helpers\UrlHelper; |
||
25 | use craft\services\Assets; |
||
26 | use craft\services\Plugins; |
||
27 | use craft\utilities\ClearCaches; |
||
28 | use craft\web\twig\variables\CraftVariable; |
||
29 | use craft\web\UrlManager; |
||
30 | use nystudio107\transcoder\models\Settings; |
||
31 | use nystudio107\transcoder\services\ServicesTrait; |
||
32 | use nystudio107\transcoder\variables\TranscoderVariable; |
||
33 | use yii\base\ErrorException; |
||
34 | use yii\base\Event; |
||
35 | |||
36 | /** |
||
37 | * Class Transcode |
||
38 | * |
||
39 | * @author nystudio107 |
||
40 | * @package Transcode |
||
41 | * @since 1.0.0 |
||
42 | * @method Settings getSettings() |
||
43 | */ |
||
44 | class Transcoder extends Plugin |
||
45 | { |
||
46 | // Traits |
||
47 | // ========================================================================= |
||
48 | |||
49 | use ServicesTrait; |
||
50 | |||
51 | // Static Properties |
||
52 | // ========================================================================= |
||
53 | |||
54 | /** |
||
55 | * @var null|Transcoder |
||
56 | */ |
||
57 | public static ?Transcoder $plugin; |
||
58 | |||
59 | /** |
||
60 | * @var null|Settings |
||
61 | */ |
||
62 | public static ?Settings $settings; |
||
63 | |||
64 | // Public Properties |
||
65 | // ========================================================================= |
||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | public bool $hasCpSection = false; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | public bool $hasCpSettings = false; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | public string $schemaVersion = '1.0.0'; |
||
81 | |||
82 | // Public Methods |
||
83 | // ========================================================================= |
||
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | public function init(): void |
||
89 | { |
||
90 | parent::init(); |
||
91 | self::$plugin = $this; |
||
92 | // Initialize properties |
||
93 | self::$settings = self::$plugin->getSettings(); |
||
94 | // Handle console commands |
||
95 | if (Craft::$app instanceof ConsoleApplication) { |
||
96 | $this->controllerNamespace = 'nystudio107\transcoder\console\controllers'; |
||
97 | } |
||
98 | // Add in our Craft components |
||
99 | $this->addComponents(); |
||
100 | // Install our global event handlers |
||
101 | $this->installEventHandlers(); |
||
102 | // We've loaded! |
||
103 | Craft::info( |
||
104 | Craft::t( |
||
105 | 'transcoder', |
||
106 | '{name} plugin loaded', |
||
107 | ['name' => $this->name] |
||
108 | ), |
||
109 | __METHOD__ |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Clear all the caches! |
||
115 | */ |
||
116 | public function clearAllCaches(): void |
||
117 | { |
||
118 | $transcoderPaths = self::$plugin->getSettings()->transcoderPaths; |
||
119 | |||
120 | foreach ($transcoderPaths as $key => $value) { |
||
121 | $dir = Craft::parseEnv($value); |
||
0 ignored issues
–
show
|
|||
122 | try { |
||
123 | FileHelper::clearDirectory($dir); |
||
124 | Craft::info( |
||
125 | Craft::t( |
||
126 | 'transcoder', |
||
127 | '{name} cache directory cleared', |
||
128 | ['name' => $key] |
||
129 | ), |
||
130 | __METHOD__ |
||
131 | ); |
||
132 | } catch (ErrorException $e) { |
||
133 | // the directory doesn't exist |
||
134 | Craft::error($e->getMessage(), __METHOD__); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | // Protected Methods |
||
140 | // ========================================================================= |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | protected function createSettingsModel(): ?Model |
||
146 | { |
||
147 | return new Settings(); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Add in our Craft components |
||
152 | */ |
||
153 | protected function addComponents(): void |
||
154 | { |
||
155 | // Register our variables |
||
156 | Event::on( |
||
157 | CraftVariable::class, |
||
158 | CraftVariable::EVENT_INIT, |
||
159 | function(Event $event) { |
||
160 | /** @var CraftVariable $variable */ |
||
161 | $variable = $event->sender; |
||
162 | $variable->set('transcoder', [ |
||
163 | 'class' => TranscoderVariable::class, |
||
164 | 'viteService' => $this->vite, |
||
165 | ]); |
||
166 | } |
||
167 | ); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Install our event handlers |
||
172 | */ |
||
173 | protected function installEventHandlers(): void |
||
174 | { |
||
175 | $settings = $this->getSettings(); |
||
176 | // Handler: Assets::EVENT_GET_THUMB_PATH |
||
177 | Event::on( |
||
178 | Assets::class, |
||
179 | Assets::EVENT_DEFINE_THUMB_URL, |
||
180 | static function(DefineAssetThumbUrlEvent $event) { |
||
181 | Craft::debug( |
||
182 | 'Assets::EVENT_GET_THUMB_PATH', |
||
183 | __METHOD__ |
||
184 | ); |
||
185 | $asset = $event->asset; |
||
186 | if (AssetsHelper::getFileKindByExtension($asset->filename) === Asset::KIND_VIDEO) { |
||
187 | $path = Transcoder::$plugin->transcode->handleGetAssetThumbPath($event); |
||
188 | if (!empty($path)) { |
||
189 | $event->url = $path; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | ); |
||
194 | if ($settings->clearCaches) { |
||
195 | // Add the Transcoded path to the list of things the Clear Caches tool can delete. |
||
196 | Event::on( |
||
197 | ClearCaches::class, |
||
198 | ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
||
199 | function(RegisterCacheOptionsEvent $event) { |
||
200 | $event->options[] = [ |
||
201 | 'key' => 'transcoder', |
||
202 | 'label' => Craft::t('transcoder', 'Transcoder caches'), |
||
203 | 'action' => [$this, 'clearAllCaches'], |
||
204 | ]; |
||
205 | } |
||
206 | ); |
||
207 | } |
||
208 | // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
||
209 | Event::on( |
||
210 | Plugins::class, |
||
211 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
212 | function(PluginEvent $event) { |
||
213 | if ($event->plugin === $this) { |
||
214 | $request = Craft::$app->getRequest(); |
||
215 | if ($request->isCpRequest) { |
||
216 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('transcoder/welcome'))->send(); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | ); |
||
221 | $request = Craft::$app->getRequest(); |
||
222 | // Install only for non-console site requests |
||
223 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
224 | $this->installSiteEventListeners(); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Install site event listeners for site requests only |
||
230 | */ |
||
231 | protected function installSiteEventListeners(): void |
||
232 | { |
||
233 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
234 | Event::on( |
||
235 | UrlManager::class, |
||
236 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
237 | function(RegisterUrlRulesEvent $event) { |
||
238 | Craft::debug( |
||
239 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
240 | __METHOD__ |
||
241 | ); |
||
242 | // Register our Control Panel routes |
||
243 | $event->rules = array_merge( |
||
244 | $event->rules, |
||
245 | $this->customFrontendRoutes() |
||
246 | ); |
||
247 | } |
||
248 | ); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Return the custom frontend routes |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | protected function customFrontendRoutes(): array |
||
257 | { |
||
258 | return [ |
||
259 | ]; |
||
260 | } |
||
261 | } |
||
262 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.