diff --git a/classes/ColdTrick/WidgetManager/Widgets.php b/classes/ColdTrick/WidgetManager/Widgets.php index fa8f37a..0911602 100644 --- a/classes/ColdTrick/WidgetManager/Widgets.php +++ b/classes/ColdTrick/WidgetManager/Widgets.php @@ -139,7 +139,7 @@ public static function getContentFromCache(\Elgg\Event $event): ?array { } $result = $event->getValue(); - $result[\Elgg\ViewsService::OUTPUT_KEY] = $cached_data; + $result[\Elgg\ViewsService::OUTPUT_KEY] = $cached_data . 'from_cache'; return $result; } @@ -192,29 +192,6 @@ public static function saveContentInCache(\Elgg\Event $event): void { elgg_save_system_cache("widget_cache_{$widget->guid}_{$current_language}", $event->getValue()); } - /** - * Unsets the cached data for cacheable widgets - * - * @param \Elgg\Event $event 'update:after', 'object' - * - * @return void - */ - public static function clearWidgetCacheOnUpdate(\Elgg\Event $event): void { - $widget = $event->getObject(); - if (!$widget instanceof \ElggWidget) { - return; - } - - if (!self::isCacheableWidget($widget)) { - return; - } - - $languages = elgg()->translator->getAllowedLanguages(); - foreach ($languages as $language) { - elgg_delete_system_cache("widget_cache_{$widget->guid}_{$language}"); - } - } - /** * Checks if the provide widget is registered as a cacheable widget * @@ -222,7 +199,7 @@ public static function clearWidgetCacheOnUpdate(\Elgg\Event $event): void { * * @return bool */ - protected static function isCacheableWidget(\ElggWidget $widget): bool { + public static function isCacheableWidget(\ElggWidget $widget): bool { static $cacheable_handlers; if (!isset($cacheable_handlers)) { $cacheable_handlers = elgg_trigger_event_results('cacheable_handlers', 'widget_manager', [], []); diff --git a/classes/WidgetManagerWidget.php b/classes/WidgetManagerWidget.php index 71558c6..b27bdee 100644 --- a/classes/WidgetManagerWidget.php +++ b/classes/WidgetManagerWidget.php @@ -13,4 +13,24 @@ class WidgetManagerWidget extends \ElggWidget { public function getDisplayName(): string { return $this->widget_manager_custom_title ?: parent::getDisplayName(); } + + /** + * {@inheritdoc} + */ + public function invalidateCache(): void { + if (!$this->guid) { + return; + } + + parent::invalidateCache(); + + if (!\ColdTrick\WidgetManager\Widgets::isCacheableWidget($this)) { + return; + } + + $languages = elgg()->translator->getAllowedLanguages(); + foreach ($languages as $language) { + elgg_delete_system_cache("widget_cache_{$this->guid}_{$language}"); + } + } } diff --git a/elgg-plugin.php b/elgg-plugin.php index 069807e..d853379 100644 --- a/elgg-plugin.php +++ b/elgg-plugin.php @@ -199,11 +199,6 @@ '\ColdTrick\WidgetManager\Groups::updateGroupWidgets' => [], ], ], - 'update:after' => [ - 'object' => [ - '\ColdTrick\WidgetManager\Widgets::clearWidgetCacheOnUpdate' => [], - ], - ], 'view' => [ 'object/widget/body' => [ '\ColdTrick\WidgetManager\Widgets::saveContentInCache' => ['priority' => 9999],