Completed
Push — master ( bd79fa...fc59c9 )
by Marin
04:34
created

Html_Field::to_json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php 
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * HTML field class.
7
 * Allows to create a field that displays any HTML in a container.
8
 */
9
class Html_Field extends Field {
10
	public $field_html;
11
12
	/**
13
	 * Set the field HTML or callback that returns the HTML.
14
	 * @param string|callable $callback_or_html HTML or callable that returns the HTML.
15
	 */
16
	public function set_html( $callback_or_html ) {
17
		if ( is_callable( $callback_or_html ) ) {
18
			$this->field_html = call_user_func( $callback_or_html );
19
		} else {
20
			$this->field_html = $callback_or_html;
21
		}
22
		
23
		return $this;
24
	}
25
26
	/**
27
	 * Returns an array that holds the field data, suitable for JSON representation.
28
	 * This data will be available in the Underscore template and the Backbone Model.
29
	 * 
30
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
31
	 * @return array
32
	 */
33 View Code Duplication
	public function to_json( $load ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
		$field_data = parent::to_json( $load );
35
36
		$field_data = array_merge( $field_data, array(
37
			'html' => $this->field_html,
38
		) );
39
40
		return $field_data;
41
	}
42
43
	/**
44
	 * Underscore template of this field
45
	 */
46
	public function template() {
47
		?>
48
		{{{ html }}}
49
		<?php
50
	}
51
52
	/**
53
	 * Whether this field is required.
54
	 * The HTML field is non-required by design.
55
	 * 
56
	 * @return false
57
	 */
58
	public function is_required() {
59
		return false;
60
	}
61
62
	/**
63
	 * Retrieve field label.
64
	 * The label for the HTML field is hidden by design.
65
	 * 
66
	 * @return string 
67
	 */
68
	public function get_label() {
69
		return '';
70
	}
71
72
	/**
73
	 * Load the field value.
74
	 * Skipped, no value to be loaded.
75
	 */
76
	public function load() {
77
		// skip;
78
	}
79
80
	/**
81
	 * Save the field value.
82
	 * Skipped, no value to be saved.
83
	 */
84
	public function save() {
85
		// skip;
86
	}
87
88
	/**
89
	 * Delete the field value.
90
	 * Skipped, no value to be deleted.
91
	 */
92
	public function delete() {
93
		// skip;
94
	}
95
}
96