|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
6
|
|
|
|
|
7
|
|
|
class Template |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param string $templatePath |
|
11
|
|
|
* @return void|string |
|
12
|
|
|
*/ |
|
13
|
7 |
|
public function build($templatePath, array $data = []) |
|
14
|
|
|
{ |
|
15
|
7 |
|
$data = $this->normalize($data); |
|
16
|
7 |
|
ob_start(); |
|
17
|
7 |
|
glsr()->render($templatePath, $data); |
|
18
|
7 |
|
$template = ob_get_clean(); |
|
19
|
7 |
|
$path = glsr(Helper::class)->removePrefix('templates/', $templatePath); |
|
20
|
7 |
|
$template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
21
|
7 |
|
$template = $this->interpolate($template, $data['context'], $path); |
|
22
|
7 |
|
$template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
23
|
7 |
|
$template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
24
|
7 |
|
return $template; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Interpolate context values into template placeholders. |
|
29
|
|
|
* @param string $template |
|
30
|
|
|
* @param string $templatePath |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
7 |
|
public function interpolate($template, array $context = [], $templatePath) |
|
34
|
|
|
{ |
|
35
|
7 |
|
$context = $this->normalizeContext($context); |
|
36
|
7 |
|
$context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template); |
|
37
|
7 |
|
foreach ($context as $key => $value) { |
|
38
|
1 |
|
$template = strtr( |
|
39
|
1 |
|
$template, |
|
40
|
1 |
|
array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
7 |
|
return trim($template); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $templatePath |
|
48
|
|
|
* @return void|string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function render($templatePath, array $data = []) |
|
51
|
|
|
{ |
|
52
|
|
|
echo $this->build($templatePath, $data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
7 |
|
protected function normalize(array $data) |
|
59
|
|
|
{ |
|
60
|
7 |
|
$arrayKeys = ['context', 'globals']; |
|
61
|
7 |
|
$data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
62
|
7 |
|
foreach ($arrayKeys as $key) { |
|
63
|
7 |
|
if (is_array($data[$key])) { |
|
64
|
7 |
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
$data[$key] = []; |
|
67
|
|
|
} |
|
68
|
7 |
|
return $data; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function normalizeContext(array $context) |
|
75
|
|
|
{ |
|
76
|
7 |
|
$context = array_filter($context, function ($value) { |
|
77
|
1 |
|
return !is_array($value) && !is_object($value); |
|
78
|
7 |
|
}); |
|
79
|
7 |
|
return array_map(function ($value) { |
|
80
|
1 |
|
return (string) $value; |
|
81
|
7 |
|
}, $context); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|