Skip to content

Commit

Permalink
FIX: various bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnysideup committed May 16, 2024
1 parent fe986ea commit c3bca16
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Page:
PageController:
extensions:
- Sunnysideup\SimpleTemplateCaching\Extensions\PageControllerExtension
- Sunnysideup\SimpleTemplateCaching\Extensions\ControllerExtension

SilverStripe\CMS\Model\SiteTree:
extensions:
Expand Down
36 changes: 18 additions & 18 deletions src/Extensions/ControllerExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Sunnysideup\SimpleTemplateCaching\Extensions;

use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Core\Extension;
Expand Down Expand Up @@ -29,44 +31,42 @@ public function onBeforeInit()
if($extend) {
return;
}
if($owner->param('Action')) {
$sc = SiteConfig::current_site_config();
if(! $sc->HasCaching) {
return;
}
if($owner->param('ID')) {
$request = $owner->getRequest();
if($request->param('Action')) {
return;
}
if($owner->request->isAjax()) {
if($request->param('ID')) {
return;
}
if($owner->request->getVar('flush')) {
if($request->isAjax()) {
return;
}
if($owner->request->requestVars()) {
if($request->getVar('flush')) {
return;
}
$dataRecord = $owner->dataRecord;
if (empty($dataRecord)) {
if($request->requestVars()) {
return;
}
if($dataRecord->NeverCachePublicly) {
HTTPCacheControlMiddleware::singleton()
->disableCache()
;
$dataRecord = $owner->data();
if (empty($dataRecord)) {
return;
}
if($dataRecord->PublicCacheDurationInSeconds === -1 || $dataRecord->PublicCacheDurationInSeconds === 0) {
if($dataRecord->NeverCachePublicly) {
HTTPCacheControlMiddleware::singleton()
->disableCache()
->disableCache()
;
return;
}
$sc = SiteConfig::current_site_config();
if($sc->HasCaching) {
$cacheTime = $dataRecord->PublicCacheDurationInSecond ?: $sc->PublicCacheDurationInSeconds;
HTTPCacheControlMiddleware::singleton()
$cacheTime = (int) ($dataRecord->PublicCacheDurationInSeconds ?: $sc->PublicCacheDurationInSeconds);
if($cacheTime > 0) {
return HTTPCacheControlMiddleware::singleton()
->enableCache()
->publicCache(true)
->setMaxAge($cacheTime)
->publicCache(true)
;
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/Extensions/PageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public function updateSettingsFields(FieldList $fields)
[
CheckboxField::create(
'NeverCachePublicly',
'
Never cache this page.
This should be checked if the page can be different for different users.'
'Never cache this page.
This should be checked if the page can be different for different users.'
),
]
);
Expand All @@ -41,17 +40,16 @@ public function updateSettingsFields(FieldList $fields)
[
NumericField::create(
'PublicCacheDurationInSeconds',
'Number of caching for page'
'In seconds, how long can this be cached for?'
)
->setDescription(
'Use with care!<br /><br />
Leave empty or zero to use the default value for the site<br /><br />
This should only be used on pages that should be the same for all users and that should be accessible publicly.<br /><br />
You can also set this value <a href="/admin/settings#Root_Caching">for the whole site</a>.<br /><br />
The current value for the whole site is ' . SiteConfig::current_site_config()->PublicCacheDurationInSeconds . ' seconds.<br /><br />
Caching is ' . (SiteConfig::current_site_config()->HasCaching ? '' : 'NOT') . ' turned on for this site.
'
Use with care!<br /><br />
Leave empty or zero to use the default value for the site<br /><br />
This should only be used on pages that should be the same for all users and that should be accessible publicly.<br /><br />
You can also set this value <a href="/admin/settings#Root_Caching">for the whole site</a>.<br /><br />
The current value for the whole site is ' . SiteConfig::current_site_config()->PublicCacheDurationInSeconds . ' seconds.<br /><br />
Caching is ' . (SiteConfig::current_site_config()->HasCaching ? '' : 'NOT') . ' turned on for this site.
'
),

]
Expand Down

0 comments on commit c3bca16

Please sign in to comment.