-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplating.php
177 lines (141 loc) · 4.5 KB
/
Templating.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
<?php
/**
* Templating
*
* @author Whizark <devaloka@whizark.com>
* @see http://whizark.com
* @copyright Copyright (C) 2015 Whizark.
* @license MIT
* @license GPL-2.0
* @license GPL-3.0
*/
namespace Devaloka\Component\Templating;
/**
* Class Templating
*
* @package Devaloka\Component\Templating
*/
class Templating implements InjectableTemplatingInterface
{
protected $globals = [];
protected $variables = [];
/**
* @param string $slug
* @param string $name
* @param mixed[] $vars
*
* @return bool
*/
public function partial($slug, $name = '', array $vars = [])
{
$templateFile = $this->getTemplate($slug, $name);
if ($templateFile === '') {
return false;
}
$globals = $this->getWordPressGlobals();
$globals = array_merge($globals, $this->globals);
$defaultVars = $this->getWordPressVariables();
$defaultVars = array_merge($defaultVars, $this->variables);
$vars = array_merge($defaultVars, $vars);
$environment = $this->createEnvironment($slug, $name);
call_user_func($environment, $templateFile, $vars, $globals);
}
public function getTemplate($slug, $name = '')
{
do_action('get_template_part_' . $slug, $slug, $name);
$templates = [];
if ($name !== '') {
$templates[] = $slug . '-' . $name . '.php';
}
$templates[] = $slug . '.php';
$templateFile = locate_template($templates, false, false);
$templateFile = apply_filters('devaloka_tpl_partial', $templateFile);
$templateFile = apply_filters('devaloka_tpl_partial_' . $slug, $templateFile, $slug, $name);
return $templateFile;
}
/**
* @param string $slug
* @param string|null $name
* @param mixed[] $vars
*
* @return bool
*/
public function partialOnly($slug, $name = '', array $vars = [])
{
$templateFile = $this->getTemplate($slug, $name);
if ($templateFile === '') {
return false;
}
$vars = array_merge($this->variables, $vars);
$environment = $this->createEnvironment($slug, $name);
call_user_func($environment, $templateFile, $vars);
}
public function injectGlobals()
{
array_merge($GLOBALS, $this->globals);
}
public function registerGlobal($name, $value)
{
$this->globals[$name] = $value;
}
public function registerVariable($name, $value)
{
$this->variables[$name] = $value;
}
protected function getWordPressGlobals()
{
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
$globals = compact(
[
'posts',
'post',
'wp_did_header',
'wp_query',
'wp_rewrite',
'wpdb',
'wp_version',
'wp',
'id',
'comment',
'user_ID',
]
);
return $globals;
}
protected function getWordPressVariables()
{
$globals = $this->getWordPressGlobals();
$variables = [];
if (is_array($globals['wp_query']->query_vars)) {
foreach ($globals['wp_query']->query_vars as $name => $value) {
if (array_key_exists($name, $globals)) {
continue;
}
$variables[$name] = $value;
}
}
if (array_key_exists('s', $variables)) {
$variables['s'] = esc_attr($variables['s']);
}
return $variables;
}
protected function createEnvironment($slug, $name)
{
$_slug = $slug;
$_name = $name;
return function ($_template_file, array $_vars = [], array $_globals = []) use ($_slug, $_name) {
foreach ($_globals as $key => $value) {
if (array_key_exists($key, $GLOBALS)) {
${$key} = &$GLOBALS[$key];
}
}
unset($key, $value);
extract($_vars, EXTR_SKIP);
do_action('devaloka_tpl_partial_before');
do_action('devaloka_tpl_partial_before_' . $_slug, $_slug, $_name, $_template_file, $_vars);
require $_template_file;
do_action('devaloka_tpl_partial_after_' . $_slug, $_slug, $_name, $_template_file, $_vars);
do_action('devaloka_tpl_partial_after');
};
}
}