|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\PluginContract; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Contracts\TemplateContract; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
8
|
|
|
|
|
9
|
|
|
class Template implements TemplateContract |
|
10
|
|
|
{ |
|
11
|
104 |
|
public function app(): PluginContract |
|
12
|
|
|
{ |
|
13
|
104 |
|
return glsr(); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
104 |
|
public function build(string $templatePath, array $data = [], bool $minify = false): string |
|
17
|
|
|
{ |
|
18
|
104 |
|
$data = $this->normalize($data); |
|
19
|
104 |
|
$path = str_replace('templates/', '', $templatePath); |
|
20
|
104 |
|
$template = $this->app()->build($templatePath, $data); |
|
21
|
104 |
|
$template = $this->app()->filterString("build/template/{$path}", $template, $data); |
|
22
|
104 |
|
$template = $this->interpolate($template, $path, $data); |
|
23
|
104 |
|
$template = $this->app()->filterString('rendered/template', $template, $templatePath, $data); |
|
24
|
104 |
|
$template = $this->app()->filterString("rendered/template/{$path}", $template, $data); |
|
25
|
104 |
|
if ($minify) { |
|
26
|
|
|
return $this->minify($template); |
|
27
|
|
|
} |
|
28
|
104 |
|
return $template; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
104 |
|
public function interpolate(string $template, string $templatePath, array $data = []): string |
|
32
|
|
|
{ |
|
33
|
104 |
|
$context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
34
|
104 |
|
$context = $this->app()->filterArray("interpolate/{$templatePath}", $context, $template, $data); |
|
35
|
104 |
|
return $this->interpolateContext($template, $context); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
104 |
|
public function interpolateContext(string $text, array $context = []): string |
|
39
|
|
|
{ |
|
40
|
104 |
|
foreach ($context as $key => $value) { |
|
41
|
104 |
|
$text = strtr( |
|
42
|
104 |
|
$text, |
|
43
|
104 |
|
array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
44
|
104 |
|
); |
|
45
|
|
|
} |
|
46
|
104 |
|
return trim($text); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* This provides support for :empty in CSS rules |
|
51
|
|
|
*/ |
|
52
|
|
|
public function minify(string $html): string |
|
53
|
|
|
{ |
|
54
|
|
|
$html = trim($html); |
|
55
|
|
|
$html = preg_replace('/\v+/u', '', $html); |
|
56
|
|
|
$html = preg_replace('/>\s+</u', '><', $html); |
|
57
|
|
|
return $html; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function render(string $templatePath, array $data = []): void |
|
61
|
|
|
{ |
|
62
|
|
|
echo $this->build($templatePath, $data); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function renderMultiple(string $templatePath, array $dataArr): void |
|
66
|
|
|
{ |
|
67
|
|
|
foreach ($dataArr as $data) { |
|
68
|
|
|
if (is_array($data)) { |
|
69
|
|
|
$this->render($templatePath, $data); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
104 |
|
protected function normalize(array $data): array |
|
75
|
|
|
{ |
|
76
|
104 |
|
$arrayKeys = ['context', 'globals']; |
|
77
|
104 |
|
$data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
78
|
104 |
|
foreach ($arrayKeys as $key) { |
|
79
|
104 |
|
if (!is_array($data[$key])) { |
|
80
|
|
|
$data[$key] = []; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
104 |
|
return $data; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
104 |
|
protected function normalizeContext(array $context): array |
|
87
|
|
|
{ |
|
88
|
104 |
|
$context = Arr::flatten($context); |
|
89
|
104 |
|
$context = array_filter($context, fn ($value) => !is_array($value) && !is_object($value)); |
|
90
|
104 |
|
return array_map(fn ($value) => (string) $value, $context); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|