Completed
Push — milestone/2.0 ( d49b56...77f3ad )
by
unknown
03:12
created

Templater   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 69
ccs 24
cts 27
cp 0.8889
rs 10
wmc 8
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 3 1
A add_template() 0 9 2
A get_template() 0 6 2
A get_templates() 0 3 1
A render_templates() 0 16 2
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 );
1 ignored issue
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
76 1
	}
77
}
78