-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticPages.module
More file actions
162 lines (123 loc) · 4.48 KB
/
StaticPages.module
File metadata and controls
162 lines (123 loc) · 4.48 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
Static pages module
https://github.com/ryancramerdesign/Helloworld
AT
30.07.24
*/
namespace ProcessWire;
class StaticPages extends WireData implements Module, ConfigurableModule {
public function init() {
$this->addHookAfter('Modules::saveConfig', $this, 'hookAfterSaveConfig');
if( !$this->isEnabled || $this->isEnabled == '' ) return;
$this->addHookAfter('Page::render', $this, 'hookCache');
$this->pages->addHookAfter('save', $this, 'hookWipe');
$this->pages->addHookAfter('delete', $this, 'hookWipe');
}
public function hookCache($event) {
return $this->cachePage($event);
}
public function hookWipe($event) {
/*
ob_start();
echo 'page: ', var_dump( $event->arguments[0] );
$buffer = ob_get_clean();
$this->message($buffer);
*/
return $this->wipeCaches();
}
public function hookAfterSaveConfig($event) {
//should run in backend only!
//Only continue if settings of this module are being saved.
if( $event->arguments[0] !== $this->className ) return;
if( $_POST['forceWipeOnModuleSave'] != 1 ) return;
return $this->wipeCaches();
}
////
public function cachePage($event) {
//should run in frontend only!
$page = $event->object;
//skipping 404 page caching
if( $page->id == wire('config')->http404PageID ) return;
$html = $event->return;
// skip if template is deprecated
if( in_array($page->template->id, (array)$this->nonStaticTemplates) ) return;
//skip if get or post request
if( count(wire('input')->get) > 0 || count (wire('input')->post) > 0 ) return;
//caching url segments independently for non-404 pages
//if( $page->id != wire('config')->http404PageID )
$urlSegmentStr = wire('input')->urlSegmentStr;
$dir = "{$_SERVER['DOCUMENT_ROOT']}{$page->path}";
if( $urlSegmentStr !='' ) $dir.= "$urlSegmentStr/";
$path = $dir.$this->staticFile;
mkdir($dir, octdec($this->newDirPermissions), true);
file_put_contents($path,$html);
}
public function wipeCaches(){
//wipes every dir/index.html in non-deprecated dirs with no regard to template, page etc
//should run in backend
$start = microtime(true);
$deletedFiles = 0;
$deletedDirs = 0;
$deletedBytes = 0;
ob_start();
$nonStaticDirs = explode( "\n", $this->nonStaticDirs );
//echo '$nonStaticDirs: ', var_dump($nonStaticDirs);//
//echo '$_SERVER[DOCUMENT_ROOT]: ', var_dump($_SERVER['DOCUMENT_ROOT']);//
$rii = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($_SERVER['DOCUMENT_ROOT']) );
foreach ($rii as $entry) {
//skipping dirs
if ( $entry == $_SERVER['DOCUMENT_ROOT'].'/..' ) continue;
if ( $entry->isDir() ) continue;
$path = $entry->getPathname();
$pathInfo = pathinfo($path);
$dirPath = $pathInfo['dirname'];
//skipping files which should not be deleted
if( $pathInfo['basename'] != $this->staticFile ) continue;
//skipping $nonStaticDirs
$continueFlag = false;
foreach( $nonStaticDirs as $nonStaticDir ){
$sample = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $nonStaticDir;
if( strpos($dirPath, $sample) === false ) continue;
$continueFlag = true;
break;
}
if( $continueFlag ) continue;
//echo "deleting file $path...\n";//
$fileSize = filesize($path);
unlink($path);
$deletedFiles++;
$deletedBytes += $fileSize;
//trying to delete the dir if empty
$dirIsEmpty = !(new \FilesystemIterator($dirPath))->valid();
if( !$dirIsEmpty ) continue;
//echo "deleting dir $dirPath...\n";//
rmdir($dirPath);//
$deletedDirs++;
//trying to delete empty dirs up to the DOCUMENT_ROOT level
$parts = explode( DIRECTORY_SEPARATOR, $dirPath );
foreach( $parts as $part ){
array_pop($parts);
$parentDirPath = implode( DIRECTORY_SEPARATOR, $parts );
if( strlen($parentDirPath) <= strlen($_SERVER['DOCUMENT_ROOT']) ) continue;
if( in_array( $parentDirPath, ['', '.', '..'] ) ) continue;
$dirIsEmpty = !(new \FilesystemIterator($parentDirPath))->valid();
if( !$dirIsEmpty ) continue;
//echo "deleting dir $parentDirPath...\n";
rmdir($parentDirPath);//
$deletedDirs++;
}
}
$deletedBytes = $this->readableFilesize($deletedBytes);
$duration = round( microtime(true)-$start, 2 );
echo "$deletedFiles static file(s) ($deletedBytes) and $deletedDirs dir(s) deleted in $duration s.";
$buffer = ob_get_clean();
$this->message($buffer);
}
//privates
private function readableFilesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
}