Skip to content

Commit 58ce013

Browse files
committed
Selectively add the Clear Caches command to the CP
Signed-off-by: Andrew Welch <andrew@nystudio107.com>
1 parent c35d731 commit 58ce013

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

src/Transcoder.php

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
* @since 1.0.0
4444
*
4545
* @property Transcode $transcode
46+
* @property Settings $settings
47+
* @method Settings getSettings()
4648
*/
4749
class Transcoder extends Plugin
4850
{
@@ -89,28 +91,24 @@ public function init()
8991
public function clearAllCaches()
9092
{
9193
$transcoderPaths = Transcoder::$plugin->getSettings()->transcoderPaths;
92-
$clearCaches = Transcoder::$plugin->getSettings()->clearCaches;
93-
94-
if($clearCaches) {
95-
foreach ($transcoderPaths as $key => $value) {
96-
$dir = Craft::getAlias($value);
97-
try {
98-
FileHelper::clearDirectory($dir);
99-
Craft::info(
100-
Craft::t(
101-
'transcoder',
102-
'{name} cache directory cleared',
103-
['name' => $key]
104-
),
105-
__METHOD__
106-
);
107-
} catch (ErrorException $e) {
108-
// the directory doesn't exist
109-
Craft::error($e->getMessage(), __METHOD__);
110-
}
94+
95+
foreach ($transcoderPaths as $key => $value) {
96+
$dir = Craft::getAlias($value);
97+
try {
98+
FileHelper::clearDirectory($dir);
99+
Craft::info(
100+
Craft::t(
101+
'transcoder',
102+
'{name} cache directory cleared',
103+
['name' => $key]
104+
),
105+
__METHOD__
106+
);
107+
} catch (ErrorException $e) {
108+
// the directory doesn't exist
109+
Craft::error($e->getMessage(), __METHOD__);
111110
}
112111
}
113-
114112
}
115113

116114
// Protected Methods
@@ -146,6 +144,7 @@ function (Event $event) {
146144
*/
147145
protected function installEventHandlers()
148146
{
147+
$settings = $this->getSettings();
149148
// Handler: Assets::EVENT_GET_THUMB_PATH
150149
Event::on(
151150
Assets::class,
@@ -162,18 +161,20 @@ function (AssetThumbEvent $event) {
162161
}
163162
}
164163
);
165-
// Add the Transcode path to the list of things the Clear Caches tool can delete.
166-
Event::on(
167-
ClearCaches::class,
168-
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
169-
function (RegisterCacheOptionsEvent $event) {
170-
$event->options[] = [
171-
'key' => 'transcoder',
172-
'label' => Craft::t('transcoder', 'Transcoder caches'),
173-
'action' => [$this, 'clearAllCaches']
174-
];
175-
}
176-
);
164+
if ($settings->clearCaches) {
165+
// Add the Transcode path to the list of things the Clear Caches tool can delete.
166+
Event::on(
167+
ClearCaches::class,
168+
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
169+
function (RegisterCacheOptionsEvent $event) {
170+
$event->options[] = [
171+
'key' => 'transcoder',
172+
'label' => Craft::t('transcoder', 'Transcoder caches'),
173+
'action' => [$this, 'clearAllCaches'],
174+
];
175+
}
176+
);
177+
}
177178
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
178179
Event::on(
179180
Plugins::class,

src/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
// if a upload location has a subfolder defined, add this to the transcoder paths too
6161
'createSubfolders' => true,
6262

63-
// clear caches when somebody clears all caches from the CP?
63+
// Add the Clear Caches utility to the CP?
6464
'clearCaches' => false,
6565

6666
// Preset video encoders

0 commit comments

Comments
 (0)