1
|
|
|
<?php |
2
|
|
|
namespace Undefined\Stash; |
3
|
|
|
|
4
|
|
|
use Timber\Timber; |
5
|
|
|
use Timber\Post as TimberPost; |
6
|
|
|
|
7
|
|
|
final class Controller |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Controller constructor. |
11
|
|
|
* |
12
|
|
|
* Set context and fetch post id |
13
|
|
|
*/ |
14
|
|
|
function __construct() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$this->found = false; |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* make singleton |
21
|
|
|
* |
22
|
|
|
* @return null|Controller |
23
|
|
|
*/ |
24
|
|
|
public static function Instance() |
25
|
|
|
{ |
26
|
|
|
static $inst = null; |
27
|
|
|
if ($inst === null) { |
28
|
|
|
$inst = new Controller(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $inst; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set all pages from within the functions.php->setControllers() |
36
|
|
|
* |
37
|
|
|
* @param $pages |
38
|
|
|
*/ |
39
|
|
|
public function setPages($pages) |
40
|
|
|
{ |
41
|
|
|
$this->pages = $pages; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set context for the page |
46
|
|
|
*/ |
47
|
|
|
private function setContext() |
48
|
|
|
{ |
49
|
|
|
$this->postId = get_the_ID(); |
|
|
|
|
50
|
|
|
$this->context = Timber::get_context(); |
|
|
|
|
51
|
|
|
$this->context['post'] = new TimberPost(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function returnId($key) |
55
|
|
|
{ |
56
|
|
|
if (function_exists('pll_get_post')) { |
57
|
|
|
return pll_get_post($key); |
58
|
|
|
} else { |
59
|
|
|
return $key; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Find the correct controller for the single post object |
65
|
|
|
* |
66
|
|
|
* If none controller is found, fallback on teh default post |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function single() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->setContext(); |
71
|
|
|
|
72
|
|
|
$context = $this->context; |
73
|
|
|
|
74
|
|
|
$file = get_template_directory() . '/controllers/single/' . $this->context['post']->post_type . '.php'; |
75
|
|
|
|
76
|
|
|
if (file_exists($file)) { |
77
|
|
|
/** |
78
|
|
|
* Set context for included file |
79
|
|
|
*/ |
80
|
|
|
include($file); |
81
|
|
|
} else { |
82
|
|
|
Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function fourOFour() |
87
|
|
|
{ |
88
|
|
|
include(get_template_directory() . '/controllers/404.php'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function archive() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$this->setContext(); |
94
|
|
|
|
95
|
|
|
$context = $this->context; |
96
|
|
|
|
97
|
|
|
$file = get_template_directory() . '/controllers/archive/' . $this->context['post']->post_type . '.php'; |
98
|
|
|
|
99
|
|
|
if (file_exists($file)) { |
100
|
|
|
/** |
101
|
|
|
* Set context for included file |
102
|
|
|
*/ |
103
|
|
|
include($file); |
104
|
|
|
} else { |
105
|
|
|
Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function page() |
110
|
|
|
{ |
111
|
|
|
/** |
112
|
|
|
* Loop though set pages in functions.php->setControllers() |
113
|
|
|
*/ |
114
|
|
|
foreach ($this->pages as $key => $page) { |
115
|
|
|
if (get_the_ID() == $this->returnId($key)) { |
116
|
|
|
/** |
117
|
|
|
* See if controller excists else fall back to default |
118
|
|
|
*/ |
119
|
|
|
$file = get_template_directory() . '/controllers/pages/' . $page . '.php'; |
120
|
|
|
if (file_exists($file)) { |
121
|
|
|
$this->found = $page; |
122
|
|
|
|
123
|
|
|
$this->setContext(); |
124
|
|
|
/** |
125
|
|
|
* Set context for included file |
126
|
|
|
*/ |
127
|
|
|
$context = $this->context; |
128
|
|
|
|
129
|
|
|
include($file); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!$this->found) { |
135
|
|
|
$this->found = 'default'; |
136
|
|
|
$this->returnDefault(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Fallback when no controller found or controller file isn't present |
142
|
|
|
*/ |
143
|
|
|
private function returnDefault() |
144
|
|
|
{ |
145
|
|
|
$this->setContext(); |
146
|
|
|
$context = $this->context; |
|
|
|
|
147
|
|
|
Timber::render(['page-' . $this->context['post']->post_name . '.twig', 'page.twig'], $this->context, Cache::getTimerTime()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return name of the body class |
152
|
|
|
* |
153
|
|
|
* @return bool|string |
154
|
|
|
*/ |
155
|
|
|
public function getClass() |
156
|
|
|
{ |
157
|
|
|
if ($this->found) { |
158
|
|
|
return 'page-' . $this->found; |
159
|
|
|
} else { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
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.