This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
base.php
189 lines (172 loc) Β· 4.65 KB
/
base.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
// Include translation file
require_once __DIR__ . '/l10n.php';
// Base file to handle each page request
require get_template_directory().'/config.php';
require get_template_directory().'/strings.php';
$html = '';
// If news then include nothing
if(is_page('oc-news') || is_page('blogfeed')) {
ob_clean();
ob_start();
include roots_template_path();
$html .= ob_get_contents();
ob_end_clean();
} else {
/**
* Pages that still use the old layout and haven't been migrated
*/
$oldPages = [
// 'about',
'pr20160602',
'pr20160614',
'pr20160705',
'pr20160719',
'pr20160721',
'pr20160725',
'pr20160812',
'pr20160825',
'pr20160901',
'pr20160916',
'pr20160929',
'pr20161027',
'pr20161213',
'pr20170307',
'pr20170502',
'pr20170508',
'pr20170522',
'pr20170807',
'pr20170824',
'pr20170927',
'pr20171018',
'user',
];
function is_blog()
{
global $post;
$posttype = get_post_type($post);
return (((is_archive()) || (is_author()) || (is_category()) || (is_home()) || (is_single()) || (is_tag())) && ($posttype == 'post')) ? true : false;
}
$currentPage = strtolower(get_post()->post_name);
$oldPage = true;
if (in_array($currentPage, $oldPages)) {
// It's an old page, use the old template
ob_clean();
ob_start();
get_template_part('templates/head-old');
$html .= ob_get_contents();
ob_end_clean();
} else {
// It's a new page, use the new template
ob_clean();
ob_start();
get_template_part('templates/head');
$html .= ob_get_contents();
ob_end_clean();
$oldPage = false;
}
$html .= '<body ';
ob_clean();
ob_start();
body_class();
$html .= ob_get_contents() . '>';
ob_end_clean();
ob_clean();
ob_start();
get_template_part('templates/header-top-navbar');
$html .= ob_get_contents();
ob_end_clean();
// The basic translation for the files
$l = new L10N(get_post()->post_name);
if ($oldPage === true) {
$html .= '<div class="wrap container" role="document"><div class="content row">';
$html .= '<main class="main ' . roots_main_class() . '; ?>" role="main">';
ob_clean();
ob_start();
include roots_template_path();
$html .= ob_get_contents();
ob_end_clean();
$html .= '</main></div></div>';
} else {
ob_clean();
ob_start();
include roots_template_path();
$html .= ob_get_contents();
ob_end_clean();
}
ob_clean();
ob_start();
require get_template_directory() . '/templates/footer.php';
$html .= ob_get_contents();
ob_end_clean();
if (PIWIKTRACKING) {
ob_clean();
ob_start();
get_template_part('templates/piwik-tracking');
$html .= ob_get_contents();
ob_end_clean();
}
$html .= '</body>';
$html .= '</html>';
}
// Check if the page is completely translated, if so use the translated one. If not default to the cached value.
$storeToCache = false;
$echoed = false;
$hl = '';
$path = parse_url(site_url())['path'];
$language = explode('/', substr($_SERVER['REQUEST_URI'], strlen($path)));
if(isset($language[1])) { // check if language is set.
if (strlen($hl) == 2) {
$hl = strtolower((string)$language[1]); // lowercase two-character country codes
} else {
$hl = (string)$language[1];
}
// check if $hl is either a 2-character alphanumeric code or a regexp matched, 5-character xx_YY style code
if ( (ctype_alnum($hl) && strlen($hl) === 2) || (preg_match("/^[a-z]{2}_[A-Z]{2}/",$language[1]) && strlen($hl) == 5) ) {
$l10nFiles = [
get_post()->post_name,
'header-navbar',
'footer',
];
$storeToCache = true;
foreach($l10nFiles as $file) {
$translatedPath = __DIR__ . '/l10n/' . $hl . '/' . $file . '.json';
$originalPath = __DIR__ . '/l10n/base/' . $file . '.json';
if(file_exists($translatedPath) && file_exists($originalPath)) {
$translatedFile = json_decode(file_get_contents($translatedPath), true);
$originalFile = json_decode(file_get_contents($originalPath), true);
} else {
$translatedFile = null;
$originalFile = null;
}
if(!is_array($translatedFile) || !is_array($originalFile)) {
$storeToCache = false;
} else if (
is_array($translatedFile)
&& is_array($originalFile)
&& array_keys($translatedFile) !== array_keys($originalFile)
) {
$storeToCache = false;
}
}
$cacheFile = __DIR__ . '/l10n/cache/' . md5($_SERVER['REQUEST_URI']);
if(file_exists($cacheFile) && $storeToCache === false) {
echo file_get_contents($cacheFile);
$echoed = true;
}
# do not store submit forms to the cache
if(strpos(get_post()->post_name, 'submit') !== false) {
$storeToCache = false;
}
if($storeToCache === true) {
file_put_contents($cacheFile, $html);
if(file_exists($cacheFile)) {
echo file_get_contents($cacheFile);
$echoed = true;
}
}
}
}
if($echoed === false) {
echo $html;
}