This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoCachingExcludesListener.php
executable file
·94 lines (80 loc) · 2.93 KB
/
AutoCachingExcludesListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Statamic\Addons\AutoCachingExcludes;
use Statamic\API\File;
use Statamic\API\Cache;
use Statamic\API\Collection;
use Statamic\API\Stache;
use Statamic\API\YAML;
use Statamic\Extend\Listener;
class AutoCachingExcludesListener extends Listener
{
/**
* The events to be listened for, and the methods to call.
*
* @var array
*/
public $events = [
'cp.page.published' => 'checkStaticCachingExclude',
];
private $entry;
private $caching_config;
/**
* Update caching.yaml so the page get's excluded from static caching if it contains a contact form.
*/
public function checkStaticCachingExclude($entry)
{
$this->entry = $entry;
$content_field = $this->entry->get($this->getConfig('content_field'));
$forms = $this->getConfig('forms');
$form_included = false;
$url = $this->entry->url();
$locales = $this->entry->locales();
$this->loadCachingConfig();
if($content_field) {
foreach ($content_field as $content)
{
if(in_array($content['type'], $forms)) {
$form_included = true;
}
}
if($form_included) {
foreach($locales as $locale) {
$this->addToCachingConfig($this->entry->in($locale)->uri());
}
} else {
foreach($locales as $locale) {
$this->removeFromCachingConfig($this->entry->in($locale)->uri());
}
}
} else {
foreach($locales as $locale) {
$this->removeFromCachingConfig($this->entry->in($locale)->uri());
}
}
$this->saveCachingConfig();
}
private function addToCachingConfig($url) {
$static_caching_exclude = $this->caching_config['static_caching_exclude'];
if (in_array($url, $static_caching_exclude) == false) {
$static_caching_exclude[] = $url;
}
$this->caching_config['static_caching_exclude'] = $static_caching_exclude;
}
private function removeFromCachingConfig($url) {
$static_caching_exclude = $this->caching_config['static_caching_exclude'];
if (in_array($url, $static_caching_exclude) == true) {
unset($static_caching_exclude[array_search($url, $static_caching_exclude)]);
}
$this->caching_config['static_caching_exclude'] = $static_caching_exclude;
}
private function loadCachingConfig() {
$caching_config_file = settings_path('caching.yaml');
$caching_config = YAML::parse(File::get($caching_config_file));
$this->caching_config = $caching_config;
}
private function saveCachingConfig() {
$caching_config_file = settings_path('caching.yaml');
$caching_config = YAML::dump($this->caching_config);
File::put($caching_config_file, $caching_config);
}
}