1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Templater; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Handles the underscore templates rendering |
7
|
|
|
* |
8
|
|
|
**/ |
9
|
|
|
class Templater { |
10
|
|
|
|
11
|
|
|
protected $templates = array(); |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Hook all templates to the administration footer. |
15
|
|
|
*/ |
16
|
|
|
public function boot() { |
17
|
|
|
add_action( 'admin_footer', array( $this, 'render_templates' ), 999 ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Register a new template. |
22
|
|
|
* |
23
|
|
|
* @param string $name Template name |
24
|
|
|
* @param string $html Template content |
25
|
|
|
*/ |
26
|
5 |
|
public function add_template( $name, $html ) { |
27
|
|
|
// Check if the template is already added |
28
|
5 |
|
if ( isset( $this->templates[ $name ] ) ) { |
29
|
1 |
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Add the template to the stack |
33
|
5 |
|
$this->templates[ $name ] = $html; |
34
|
5 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get a template by name. |
38
|
|
|
* |
39
|
|
|
* @param string $name Template name |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
3 |
|
public function get_template( $name ) { |
43
|
3 |
|
if ( ! isset( $this->templates[ $name ] ) ) { |
44
|
1 |
|
return null; |
45
|
|
|
} |
46
|
2 |
|
return $this->templates[ $name ]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get all registered templaes. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
2 |
|
public function get_templates() { |
55
|
2 |
|
return $this->templates; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Render all registered templates. |
60
|
|
|
*/ |
61
|
1 |
|
public function render_templates() { |
62
|
1 |
|
$output = array(); |
63
|
1 |
|
foreach ( $this->templates as $name => $html ) { |
64
|
1 |
|
$id = 'crb-tmpl-' . $name; |
65
|
1 |
|
$html = apply_filters( 'carbon_template_' . $name, $html ); |
66
|
1 |
|
$html = apply_filters( 'carbon_template', $html, $name ); |
67
|
|
|
|
68
|
1 |
|
$id_attr = esc_attr( $id ); |
69
|
1 |
|
$output[] = <<<EOT |
70
|
|
|
<script type="text/html" id="$id_attr"> |
71
|
1 |
|
$html |
72
|
1 |
|
</script> |
73
|
|
|
EOT; |
74
|
1 |
|
} |
75
|
1 |
|
echo implode( PHP_EOL, $output ); |
|
|
|
|
76
|
1 |
|
} |
77
|
|
|
} |
78
|
|
|
|