|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\DefaultsManager; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Field; |
|
10
|
|
|
|
|
11
|
|
|
class Template |
|
12
|
|
|
{ |
|
13
|
7 |
|
/** |
|
14
|
|
|
* @param string $templatePath |
|
15
|
7 |
|
* @return void|string |
|
16
|
7 |
|
*/ |
|
17
|
7 |
|
public function build( $templatePath, array $data = [] ) |
|
18
|
|
|
{ |
|
19
|
|
|
$data = $this->normalize( $data ); |
|
20
|
|
|
ob_start(); |
|
21
|
7 |
|
glsr()->render( $templatePath, $data ); |
|
22
|
7 |
|
$template = ob_get_clean(); |
|
23
|
7 |
|
return $this->interpolate( $template, $data['context'] ); |
|
24
|
7 |
|
} |
|
25
|
7 |
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Interpolate context values into template placeholders |
|
28
|
|
|
* @param string $template |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function interpolate( $template, array $context = [] ) |
|
32
|
|
|
{ |
|
33
|
7 |
|
$context = $this->normalizeContext( $context ); |
|
34
|
|
|
foreach( $context as $key => $value ) { |
|
35
|
7 |
|
$template = strtr( |
|
36
|
7 |
|
$template, |
|
37
|
1 |
|
array_fill_keys( ['{'.$key.'}', '{{ '.$key.' }}'], $value ) |
|
38
|
1 |
|
); |
|
39
|
1 |
|
} |
|
40
|
|
|
return trim( $template ); |
|
41
|
|
|
} |
|
42
|
7 |
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $templatePath |
|
45
|
|
|
* @return void|string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function render( $templatePath, array $data = [] ) |
|
48
|
|
|
{ |
|
49
|
|
|
echo $this->build( $templatePath, $data ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $id |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
|
|
public function renderSettingFields( $id ) |
|
57
|
7 |
|
{ |
|
58
|
|
|
$fields = $this->getSettingFields( $this->normalizeSettingPath( $id )); |
|
59
|
7 |
|
$rows = ''; |
|
60
|
7 |
|
foreach( $fields as $name => $field ) { |
|
61
|
7 |
|
$field = wp_parse_args( $field, [ |
|
62
|
|
|
'name' => $name, |
|
63
|
|
|
'table' => true, |
|
64
|
7 |
|
]); |
|
65
|
|
|
$rows.= (new Field( $field ))->build(); |
|
66
|
|
|
} |
|
67
|
|
|
$this->render( 'pages/settings/'.$id, [ |
|
68
|
|
|
'context' => [ |
|
69
|
|
|
'rows' => $rows, |
|
70
|
|
|
], |
|
71
|
|
|
]); |
|
72
|
7 |
|
} |
|
73
|
1 |
|
|
|
74
|
7 |
|
/** |
|
75
|
7 |
|
* @return array |
|
76
|
1 |
|
*/ |
|
77
|
7 |
|
protected function getSettingFields( $path ) |
|
78
|
|
|
{ |
|
79
|
|
|
$settings = glsr( DefaultsManager::class )->settings(); |
|
80
|
|
|
return array_filter( $settings, function( $key ) use( $path ) { |
|
81
|
|
|
return glsr( Helper::class )->startsWith( $path, $key ); |
|
82
|
|
|
}, ARRAY_FILTER_USE_KEY ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function normalize( array $data ) |
|
89
|
|
|
{ |
|
90
|
|
|
$data = wp_parse_args( $data, array_fill_keys( ['context', 'globals'], [] )); |
|
91
|
|
|
foreach( $data as $key => $value ) { |
|
92
|
|
|
if( is_array( $value ))continue; |
|
93
|
|
|
$data[$key] = []; |
|
94
|
|
|
} |
|
95
|
|
|
$data['template'] = $this; |
|
96
|
|
|
$data['render'] = glsr( Html::class )->render( $data['globals'] ); |
|
97
|
|
|
return $data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function normalizeContext( array $context ) |
|
104
|
|
|
{ |
|
105
|
|
|
$context = array_filter( $context, function( $value ) { |
|
106
|
|
|
return !is_array( $value ) && !is_object( $value ); |
|
107
|
|
|
}); |
|
108
|
|
|
return array_map( function( $value ) { |
|
109
|
|
|
return esc_attr( (string)$value ); |
|
110
|
|
|
}, $context ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function normalizeSettingPath( $path ) |
|
117
|
|
|
{ |
|
118
|
|
|
return glsr( Helper::class )->prefixString( rtrim( $path, '.' ), 'settings.' ); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|