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