From c795ebf0eb42e1792e1732c5bf930f5c76dc8ef6 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 3 Nov 2020 17:44:59 +0000 Subject: [PATCH 01/10] Added options to disable draft preview caching xposedbones --- src/models/Settings.php | 3 ++- src/services/HtmlcacheService.php | 5 +++++ src/templates/_settings.twig | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/models/Settings.php b/src/models/Settings.php index 804197b..35c5b30 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -19,11 +19,12 @@ class Settings extends \craft\base\Model public $optimizeContent = 0; public $cacheDuration = 3600; public $purgeCache = 0; + public $disablePreviewCache = 1; public $excludedUrlPaths = []; public function rules() { return [ - [ ['enableGeneral', 'forceOn', 'optimizeContent', 'purgeCache' ], 'boolean' ], + [ ['enableGeneral', 'forceOn', 'optimizeContent', 'purgeCache', 'disablePreviewCache' ], 'boolean' ], [ ['cacheDuration' ], 'integer' ], ]; } diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index 6133219..e92a500 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -77,6 +77,11 @@ public function checkForCacheFile() */ public function canCreateCacheFile() { + // Skip if it's a preview url + if ($this->settings->disablePreviewCache && Craft::$app->request->getIsPreview()) { + return false; + } + // Skip if we're running in devMode and not in force mode if (\Craft::$app->config->general->devMode === true && $this->settings->forceOn == false) { return false; diff --git a/src/templates/_settings.twig b/src/templates/_settings.twig index 165f0bb..90c1202 100755 --- a/src/templates/_settings.twig +++ b/src/templates/_settings.twig @@ -45,6 +45,15 @@ errors: settings.getErrors('honeypotField') }) }} +{{ forms.lightswitchField({ + label: "Disable Preview Cache" | t, + id: 'disablePreviewCache', + name: 'disablePreviewCache', + instructions: "Disable the caching of a preview url, this prevents a draft from being publicly accessible" | t, + errors: settings.getErrors('disablePreviewCache'), + on: settings.disablePreviewCache +}) }} + {{ forms.lightswitchField({ label: "Purge Cache now?" | t, id: 'purgeCache', From d335ef007c62a4f166f5f5bba5c4af36533b6e3b Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 3 Nov 2020 17:46:39 +0000 Subject: [PATCH 02/10] changed cache identifier from param to internal uri thisisablock --- src/HtmlCache.php | 6 +++++- src/services/HtmlcacheService.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/HtmlCache.php b/src/HtmlCache.php index 7bf2200..c8d0ec8 100644 --- a/src/HtmlCache.php +++ b/src/HtmlCache.php @@ -142,9 +142,13 @@ public function init() if ($this->htmlcacheService->canCreateCacheFile()) { $elementClass = get_class($event->element); if (!in_array($elementClass, [User::class, GlobalSet::class])) { - $uri = \Craft::$app->request->getParam('p', ''); + $uri = \Craft::$app->request->getPathInfo() ?: $event->element->uri; $siteId = \Craft::$app->getSites()->getCurrentSite()->id; $elementId = $event->element->id; + + if (!$uri || strlen($uri) === 0) { + return; + } // check if cache entry already exits otherwise create it $cacheEntry = HtmlCacheCache::findOne(['uri' => $uri, 'siteId' => $siteId]); diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index e92a500..9b38cc1 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -38,7 +38,7 @@ class HtmlcacheService extends Component */ public function __construct() { - $this->uri = \Craft::$app->request->getParam('p', ''); + $this->uri = \Craft::$app->request->getPathInfo() ?: '__HOME__'; $this->siteId = \Craft::$app->getSites()->getCurrentSite()->id; $this->settings = HtmlCache::getInstance()->getSettings(); } From fa9aeff4074a0cd5bc3aba0cf8561d895b4666c4 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 3 Nov 2020 17:50:04 +0000 Subject: [PATCH 03/10] app->request to app->getRequest() loqus --- src/HtmlCache.php | 6 +++--- src/services/HtmlcacheService.php | 16 ++++++++-------- src/templates/_settings.twig | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/HtmlCache.php b/src/HtmlCache.php index c8d0ec8..8903e50 100644 --- a/src/HtmlCache.php +++ b/src/HtmlCache.php @@ -117,7 +117,7 @@ public function init() self::$plugin = $this; // ignore console requests - if ($this->isInstalled && !\Craft::$app->request->getIsConsoleRequest()) { + if ($this->isInstalled && !\Craft::$app->getRequest()->getIsConsoleRequest()) { $this->setComponents( [ 'htmlcacheService' => HtmlcacheService::class, @@ -142,14 +142,14 @@ public function init() if ($this->htmlcacheService->canCreateCacheFile()) { $elementClass = get_class($event->element); if (!in_array($elementClass, [User::class, GlobalSet::class])) { - $uri = \Craft::$app->request->getPathInfo() ?: $event->element->uri; + $uri = \Craft::$app->getRequest()->getPathInfo() ?: $event->element->uri; $siteId = \Craft::$app->getSites()->getCurrentSite()->id; $elementId = $event->element->id; if (!$uri || strlen($uri) === 0) { return; } - + // check if cache entry already exits otherwise create it $cacheEntry = HtmlCacheCache::findOne(['uri' => $uri, 'siteId' => $siteId]); if (!$cacheEntry) { diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index 9b38cc1..a7b02c0 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -38,7 +38,7 @@ class HtmlcacheService extends Component */ public function __construct() { - $this->uri = \Craft::$app->request->getPathInfo() ?: '__HOME__'; + $this->uri = \Craft::$app->getRequest()->getPathInfo() ?: '__HOME__'; $this->siteId = \Craft::$app->getSites()->getCurrentSite()->id; $this->settings = HtmlCache::getInstance()->getSettings(); } @@ -78,7 +78,7 @@ public function checkForCacheFile() public function canCreateCacheFile() { // Skip if it's a preview url - if ($this->settings->disablePreviewCache && Craft::$app->request->getIsPreview()) { + if ($this->settings->disablePreviewCache && Craft::$app->getRequest()->getIsPreview()) { return false; } @@ -98,25 +98,25 @@ public function canCreateCacheFile() } // Skip if it's a CP Request - if (\Craft::$app->request->getIsCpRequest()) { + if (\Craft::$app->getRequest()->getIsCpRequest()) { return false; } // Skip if it's an action Request - if (\Craft::$app->request->getIsActionRequest()) { + if (\Craft::$app->getRequest()->getIsActionRequest()) { return false; } // Skip if it's a preview request - if (\Craft::$app->request->getIsLivePreview()) { + if (\Craft::$app->getRequest()->getIsLivePreview()) { return false; } // Skip if it's a post request - if (!\Craft::$app->request->getIsGet()) { + if (!\Craft::$app->getRequest()->getIsGet()) { return false; } // Skip if it's an ajax request - if (\Craft::$app->request->getIsAjax()) { + if (\Craft::$app->getRequest()->getIsAjax()) { return false; } // Skip if route from element api @@ -162,7 +162,7 @@ private function isElementApiRoute() private function isPathExcluded() { // determine currently requested URL path and the multi-site ID - $requestedPath = \Craft::$app->request->getFullPath(); + $requestedPath = \Craft::$app->getRequest()->getFullPath(); $requestedSiteId = \Craft::$app->getSites()->getCurrentSite()->id; // compare with excluded paths and sites from the settings diff --git a/src/templates/_settings.twig b/src/templates/_settings.twig index 90c1202..8130b12 100755 --- a/src/templates/_settings.twig +++ b/src/templates/_settings.twig @@ -81,7 +81,7 @@ cols: [ { heading: 'Excluded paths' | t, - info: 'Enter precise URL paths or regular expressions without the siteURL part as they are returned by \Craft::$app->request->getFullPath(), like "any/page" insteadof "https://example.com/site-specific/path/any/page".' | t, + info: 'Enter precise URL paths or regular expressions without the siteURL part as they are returned by \Craft::$app->getRequest()->getFullPath(), like "any/page" insteadof "https://example.com/site-specific/path/any/page".' | t, type: 'singleline', placeholder: 'path/to/excluded/page OR regex like path/.*/excluded/.*', }, From f2709f5d4c061f10f3ab07e4fd208d37f01c983d Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 3 Nov 2020 17:52:52 +0000 Subject: [PATCH 04/10] composer 2 compatibility --- src/records/{HtmlCacheCachesRecord.php => HtmlCacheCache.php} | 0 src/records/{HtmlCacheElementRecord.php => HtmlCacheElement.php} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/records/{HtmlCacheCachesRecord.php => HtmlCacheCache.php} (100%) rename src/records/{HtmlCacheElementRecord.php => HtmlCacheElement.php} (100%) diff --git a/src/records/HtmlCacheCachesRecord.php b/src/records/HtmlCacheCache.php similarity index 100% rename from src/records/HtmlCacheCachesRecord.php rename to src/records/HtmlCacheCache.php diff --git a/src/records/HtmlCacheElementRecord.php b/src/records/HtmlCacheElement.php similarity index 100% rename from src/records/HtmlCacheElementRecord.php rename to src/records/HtmlCacheElement.php From c5e77c71f12bf48773e72893dc4149ab9111525b Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 8 Dec 2020 17:29:57 +0000 Subject: [PATCH 05/10] Create Clear Cache Utility Add a utility to clear the cache in production environments that have settings disabled. --- src/HtmlCache.php | 15 +++++++++ src/controllers/CacheController.php | 35 +++++++++++++++++++ src/icon-mask.svg | 1 + src/templates/_utility.twig | 12 +++++++ src/utilities/CacheUtility.php | 52 +++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 src/controllers/CacheController.php create mode 100644 src/icon-mask.svg create mode 100644 src/templates/_utility.twig create mode 100644 src/utilities/CacheUtility.php diff --git a/src/HtmlCache.php b/src/HtmlCache.php index 8903e50..997e52c 100644 --- a/src/HtmlCache.php +++ b/src/HtmlCache.php @@ -19,12 +19,15 @@ use craft\services\Plugins; use craft\events\PluginEvent; use craft\services\Elements; +use craft\services\Utilities; use craft\web\UrlManager; use craft\helpers\FileHelper; use craft\helpers\UrlHelper; use craft\events\RegisterUrlRulesEvent; use bolden\htmlcache\services\HtmlcacheService; use bolden\htmlcache\models\Settings; +use bolden\htmlcache\utilities\CacheUtility; + use yii\base\Event; use craft\elements\db\ElementQuery; @@ -35,6 +38,7 @@ use bolden\htmlcache\records\HtmlCacheElement; use craft\elements\User; use craft\elements\GlobalSet; +use craft\events\RegisterComponentTypesEvent; /** * Craft plugins are very much like little applications in and of themselves. We’ve made @@ -221,6 +225,17 @@ function (PluginEvent $event) { } } ); + + if (Craft::$app->getRequest()->getIsCpRequest()) { + Event::on( + Utilities::class, + Utilities::EVENT_REGISTER_UTILITY_TYPES, + function(RegisterComponentTypesEvent $event) { + $event->types[] = CacheUtility::class; + } + ); + } + parent::init(); } diff --git a/src/controllers/CacheController.php b/src/controllers/CacheController.php new file mode 100644 index 0000000..ecd6e04 --- /dev/null +++ b/src/controllers/CacheController.php @@ -0,0 +1,35 @@ +requireLogin(); + $this->requirePostRequest(); + try { + Plugin::$plugin->htmlcacheService->clearCacheFiles(); + Craft::$app->getSession()->setNotice(Craft::t('html-cache', 'Caches Cleared')); + } catch (\Throwable $th) { + Craft::$app->getSession()->setNotice(Craft::t('html-cache', 'Unable to Clear Caches')); + } + return; + } + +} diff --git a/src/icon-mask.svg b/src/icon-mask.svg new file mode 100644 index 0000000..b55d4a9 --- /dev/null +++ b/src/icon-mask.svg @@ -0,0 +1 @@ +Artboard 1 \ No newline at end of file diff --git a/src/templates/_utility.twig b/src/templates/_utility.twig new file mode 100644 index 0000000..e3d5e67 --- /dev/null +++ b/src/templates/_utility.twig @@ -0,0 +1,12 @@ +{% import "_includes/forms" as forms %} + +

{{ "Clear Caches"|t('html-cache') }}

+ +
+ {{ actionInput('html-cache/cache/clear-caches') }} + {{ csrfInput() }} +
+ +
+
+
diff --git a/src/utilities/CacheUtility.php b/src/utilities/CacheUtility.php new file mode 100644 index 0000000..e7eceda --- /dev/null +++ b/src/utilities/CacheUtility.php @@ -0,0 +1,52 @@ +getView()->renderTemplate('html-cache/_utility'); + } +} + From d1b222f9cb1dd8d2b6b975645ec1759619915b2c Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 10 Mar 2021 09:47:41 +0000 Subject: [PATCH 06/10] allow infinite cache duration --- src/services/HtmlcacheService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index a7b02c0..ecf9f2b 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -296,9 +296,9 @@ private function loadCache($file) } elseif (!empty($this->settings->cacheDuration)) { $settings = ['cacheDuration' => $this->settings->cacheDuration]; } else { - $settings = ['cacheDuration' => 3600]; + $settings = ['cacheDuration' => 0]; } - if (time() - ($fmt = filemtime($file)) >= $settings['cacheDuration']) { + if ($settings['cacheDuration'] > 0 && time() - ($fmt = filemtime($file)) >= $settings['cacheDuration']) { unlink($file); return false; } From 722254d1cebe55e7113c6e42a6771edb001230cd Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 10 Mar 2021 09:56:34 +0000 Subject: [PATCH 07/10] add option to exclude urls with query strings --- src/models/Settings.php | 1 + src/services/HtmlcacheService.php | 5 +++++ src/templates/_settings.twig | 9 +++++++++ 3 files changed, 15 insertions(+) diff --git a/src/models/Settings.php b/src/models/Settings.php index 35c5b30..3d383fb 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -21,6 +21,7 @@ class Settings extends \craft\base\Model public $purgeCache = 0; public $disablePreviewCache = 1; public $excludedUrlPaths = []; + public $queryStringCaching = false; public function rules() { return [ diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index ecf9f2b..cf73904 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -81,6 +81,11 @@ public function canCreateCacheFile() if ($this->settings->disablePreviewCache && Craft::$app->getRequest()->getIsPreview()) { return false; } + + // Skip if it has a query string and plugin is set to ignore urls with query strings + if ($this->settings->queryStringCaching && Craft::$app->getRequest()->getQueryString()) { + return false; + } // Skip if we're running in devMode and not in force mode if (\Craft::$app->config->general->devMode === true && $this->settings->forceOn == false) { diff --git a/src/templates/_settings.twig b/src/templates/_settings.twig index 8130b12..c959a7b 100755 --- a/src/templates/_settings.twig +++ b/src/templates/_settings.twig @@ -45,6 +45,15 @@ errors: settings.getErrors('honeypotField') }) }} +{{ forms.lightswitchField({ + label: "Disable Query String Caching" | t, + id: 'queryStringCaching', + name: 'queryStringCaching', + instructions: "Disable the caching of a urls with query strings" | t, + errors: settings.getErrors('queryStringCaching'), + on: settings.queryStringCaching +}) }} + {{ forms.lightswitchField({ label: "Disable Preview Cache" | t, id: 'disablePreviewCache', From e5377b8d7d6441e4746f299c9905c3da53fa4df7 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 10 Mar 2021 10:22:29 +0000 Subject: [PATCH 08/10] Add debug text to cached pages in devmode --- src/services/HtmlcacheService.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index cf73904..5e8edb1 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -307,7 +307,16 @@ private function loadCache($file) unlink($file); return false; } - \Craft::$app->response->data = file_get_contents($file); + + $page = file_get_contents($file); + + if (Craft::$app->config->general->devMode) { + $filename = array_slice(explode('/',$file),-1)[0]; + $uri = Craft::$app->getRequest()->getFullUri(); + $page .= ""; + } + + \Craft::$app->response->data = $page; return true; } From e6b6804f1cc37f068275dc8f30ec50900f4fa161 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 10 Mar 2021 10:36:59 +0000 Subject: [PATCH 09/10] More detailed debugging message --- src/services/HtmlcacheService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index 5e8edb1..b315fe3 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -313,7 +313,8 @@ private function loadCache($file) if (Craft::$app->config->general->devMode) { $filename = array_slice(explode('/',$file),-1)[0]; $uri = Craft::$app->getRequest()->getFullUri(); - $page .= ""; + $date = date("F d Y H:i:s",filemtime($file)); + $page .= "\n\n"; } \Craft::$app->response->data = $page; From b245d7f747dfa9a05fcb167d552314e13f9a455b Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 10 Mar 2021 17:08:11 +0000 Subject: [PATCH 10/10] Typo fixes --- src/services/HtmlcacheService.php | 2 +- src/templates/_settings.twig | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/HtmlcacheService.php b/src/services/HtmlcacheService.php index b315fe3..24c7692 100755 --- a/src/services/HtmlcacheService.php +++ b/src/services/HtmlcacheService.php @@ -83,7 +83,7 @@ public function canCreateCacheFile() } // Skip if it has a query string and plugin is set to ignore urls with query strings - if ($this->settings->queryStringCaching && Craft::$app->getRequest()->getQueryString()) { + if (!$this->settings->queryStringCaching && Craft::$app->getRequest()->getQueryString()) { return false; } diff --git a/src/templates/_settings.twig b/src/templates/_settings.twig index c959a7b..54e2197 100755 --- a/src/templates/_settings.twig +++ b/src/templates/_settings.twig @@ -46,10 +46,10 @@ }) }} {{ forms.lightswitchField({ - label: "Disable Query String Caching" | t, + label: "Enable Query String Caching" | t, id: 'queryStringCaching', name: 'queryStringCaching', - instructions: "Disable the caching of a urls with query strings" | t, + instructions: "Enable the caching of a urls with query strings" | t, errors: settings.getErrors('queryStringCaching'), on: settings.queryStringCaching }) }}