|
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
|
|
|
public function page() |
|
64
|
|
|
{ |
|
65
|
|
|
/** |
|
66
|
|
|
* Loop though set pages in functions.php->setControllers() |
|
67
|
|
|
*/ |
|
68
|
|
|
foreach ($this->pages as $key => $page) { |
|
69
|
|
|
|
|
70
|
|
|
if (get_the_ID() == $this->returnId($key)) { |
|
71
|
|
|
/** |
|
72
|
|
|
* See if controller excists else fall back to default |
|
73
|
|
|
*/ |
|
74
|
|
|
$file = get_template_directory() . '/controllers/' . $page . '.php'; |
|
75
|
|
|
if (file_exists($file)) { |
|
76
|
|
|
$this->found = $page; |
|
77
|
|
|
|
|
78
|
|
|
$this->setContext(); |
|
79
|
|
|
/** |
|
80
|
|
|
* Set context for included file |
|
81
|
|
|
*/ |
|
82
|
|
|
$context = $this->context; |
|
83
|
|
|
|
|
84
|
|
|
include($file); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!$this->found) { |
|
90
|
|
|
$this->found = 'default'; |
|
91
|
|
|
$this->returnDefault(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Fallback when no controller found or controller file isn't present |
|
97
|
|
|
*/ |
|
98
|
|
|
private function returnDefault() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->setContext(); |
|
101
|
|
|
$context = $this->context; |
|
|
|
|
|
|
102
|
|
|
Timber::render(['page-' . $this->context['post']->post_name . '.twig', 'page.twig'], $this->context, Cache::getTimerTime()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return name of the body class |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool|string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getClass() |
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->found) { |
|
113
|
|
|
return 'page-' . $this->found; |
|
114
|
|
|
} else { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
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.