1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Undefined\Stash; |
4
|
|
|
|
5
|
|
|
use Timber\Timber; |
6
|
|
|
use Timber\Post as TimberPost; |
7
|
|
|
|
8
|
|
|
final class Controller |
9
|
|
|
{ |
10
|
|
|
private $parentPages; |
11
|
|
|
private $found; |
12
|
|
|
private $pages; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Controller constructor. |
16
|
|
|
* |
17
|
|
|
* Set context and fetch post id |
18
|
|
|
*/ |
19
|
|
|
function __construct() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$this->found = false; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* make singleton |
26
|
|
|
* |
27
|
|
|
* @return null|Controller |
28
|
|
|
*/ |
29
|
|
|
public static function Instance() |
30
|
|
|
{ |
31
|
|
|
static $inst = null; |
32
|
|
|
if ($inst === null) { |
33
|
|
|
$inst = new Controller(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $inst; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set all pages from within the functions.php->setControllers() |
41
|
|
|
* |
42
|
|
|
* @param $pages |
43
|
|
|
*/ |
44
|
|
|
public function setPages($pages) |
45
|
|
|
{ |
46
|
|
|
$this->pages = $pages; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set all pages from within the functions.php->setControllers() |
51
|
|
|
* |
52
|
|
|
* @param $pages |
53
|
|
|
*/ |
54
|
|
|
public function setParentPages($pages) |
55
|
|
|
{ |
56
|
|
|
$this->parentPages = $pages; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set context for the page |
61
|
|
|
*/ |
62
|
|
|
private function setContext() |
63
|
|
|
{ |
64
|
|
|
$this->postId = get_the_ID(); |
|
|
|
|
65
|
|
|
$this->context = Timber::get_context(); |
|
|
|
|
66
|
|
|
$this->context['post'] = new TimberPost(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function returnId($key) |
70
|
|
|
{ |
71
|
|
|
if (function_exists('pll_get_post')) { |
72
|
|
|
return pll_get_post($key); |
73
|
|
|
} else { |
74
|
|
|
return $key; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Find the correct controller for the single post object |
80
|
|
|
* |
81
|
|
|
* If none controller is found, fallback on teh default post |
82
|
|
|
*/ |
83
|
|
|
public function single() |
84
|
|
|
{ |
85
|
|
|
$this->setContext(); |
86
|
|
|
|
87
|
|
|
$context = $this->context; |
88
|
|
|
|
89
|
|
|
$file = get_template_directory() . '/controllers/single/' . strtolower($this->context['post']->post_type) . '.php'; |
90
|
|
|
|
91
|
|
View Code Duplication |
if (file_exists($file)) { |
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set context for included file |
94
|
|
|
*/ |
95
|
|
|
include($file); |
96
|
|
|
} else { |
97
|
|
|
Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function search() |
102
|
|
|
{ |
103
|
|
|
$this->setContext(); |
104
|
|
|
|
105
|
|
|
$context = $this->context; |
106
|
|
|
|
107
|
|
|
Timber::render(array('search.twig', 'archive.twig'), $context, Cache::getTimerTime()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function fourOFour() |
111
|
|
|
{ |
112
|
|
|
include(get_template_directory() . '/controllers/404.php'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function archive() |
116
|
|
|
{ |
117
|
|
|
$this->setContext(); |
118
|
|
|
|
119
|
|
|
$context = $this->context; |
120
|
|
|
|
121
|
|
|
if (is_category()) { |
122
|
|
|
$file = get_template_directory() . '/controllers/archive/category.php'; |
123
|
|
|
} else if (is_tax()) { |
124
|
|
|
$file = get_template_directory() . '/controllers/archive/tax.php'; |
125
|
|
|
} else { |
126
|
|
|
$file = get_template_directory() . '/controllers/archive/' . strtolower($this->context['post']->title) . '.php'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
if (file_exists($file)) { |
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set context for included file |
132
|
|
|
*/ |
133
|
|
|
include($file); |
134
|
|
|
} else { |
135
|
|
|
Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function page() |
140
|
|
|
{ |
141
|
|
|
$pageId = get_the_ID(); |
142
|
|
|
$parentId = wp_get_post_parent_id($pageId); |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Loop though set pages in functions.php->setControllers() |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
foreach ($this->parentPages as $key => $page) { |
|
|
|
|
148
|
|
|
if ($parentId == $this->returnId($key)) { |
149
|
|
|
/** |
150
|
|
|
* See if controller excists else fall back to default |
151
|
|
|
*/ |
152
|
|
|
$file = get_template_directory() . '/controllers/pages/' . $page . '.php'; |
153
|
|
|
$this->found = $page; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Loop though set pages in functions.php->setControllers() |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
foreach ($this->pages as $key => $page) { |
|
|
|
|
161
|
|
|
if ($pageId == $this->returnId($key)) { |
162
|
|
|
/** |
163
|
|
|
* See if controller excists else fall back to default |
164
|
|
|
*/ |
165
|
|
|
$file = get_template_directory() . '/controllers/pages/' . $page . '.php'; |
166
|
|
|
$this->found = $page; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ($file && file_exists($file)) { |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
$this->setContext(); |
174
|
|
|
/** |
175
|
|
|
* Set context for included file |
176
|
|
|
*/ |
177
|
|
|
$context = $this->context; |
178
|
|
|
|
179
|
|
|
include($file); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (!$this->found) { |
183
|
|
|
$this->found = 'default'; |
184
|
|
|
$this->returnDefault(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Fallback when no controller found or controller file isn't present |
190
|
|
|
*/ |
191
|
|
|
private function returnDefault() |
192
|
|
|
{ |
193
|
|
|
$this->setContext(); |
194
|
|
|
$context = $this->context; |
|
|
|
|
195
|
|
|
Timber::render(['page-' . strtolower($this->context['post']->post_name) . '.twig', 'page.twig'], $this->context, Cache::getTimerTime()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return name of the body class |
200
|
|
|
* |
201
|
|
|
* @return bool|string |
202
|
|
|
*/ |
203
|
|
|
public function getClass() |
204
|
|
|
{ |
205
|
|
|
if ($this->found) { |
|
|
|
|
206
|
|
|
return 'page-' . $this->found; |
207
|
|
|
} else { |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.