__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
/**
3
 * Main breadcrumb administration settings field registration.
4
 *
5
 * @package carbon-breadcrumbs
6
 */
7
8
/**
9
 * Manages breadcrumb administration settings field registration.
10
 * Should be extended for each field type.
11
 *
12
 * @abstract
13
 */
14
abstract class Carbon_Breadcrumb_Admin_Settings_Field extends Carbon_Breadcrumb_Factory {
15
16
	/**
17
	 * Field title.
18
	 *
19
	 * @access protected
20
	 * @var string
21
	 */
22
	protected $title;
23
24
	/**
25
	 * Field ID.
26
	 *
27
	 * @access protected
28
	 * @var string
29
	 */
30
	protected $id;
31
32
33
	/**
34
	 * Constructor.
35
	 *
36
	 * Register an administration breadcrumb settings field.
37
	 *
38
	 * @access public
39
	 *
40
	 * @param string $id The ID of the field.
41
	 * @param string $title The title of the field.
42
	 * @param string $section The name of the section.
43
	 * @param array  $args Additional args.
44
	 */
45
	public function __construct( $id, $title, $section = '', $args = array() ) {
46
47
		$this->set_id( $id );
48
		$this->set_title( $title );
49
50
		add_settings_field(
51
			$id,
52
			$title,
53
			array( $this, 'render' ),
54
			Carbon_Breadcrumb_Admin_Settings::get_page_name(),
55
			$section,
56
			$args
57
		);
58
59
		register_setting( Carbon_Breadcrumb_Admin_Settings::get_page_name(), $id );
60
	}
61
62
	/**
63
	 * Register a new administration breadcrumb settings field of a certain type.
64
	 *
65
	 * @static
66
	 * @access public
67
	 *
68
	 * @param string $type Type of the field.
69
	 * @param string $id The ID of the field.
70
	 * @param string $title The title of the field.
71
	 * @param string $section The name of the section.
72
	 * @param array  $args Additional args.
73
	 * @return Carbon_Breadcrumb_Admin_Settings_Field $field
74
	 */
75
	public static function factory( $type, $id, $title, $section = '', $args = array() ) {
76
		$class = self::verify_class_name( __CLASS__ . '_' . $type, 'Unknown settings field type "' . $type . '".' );
77
		$field = new $class( $id, $title, $section, $args );
78
79
		return $field;
80
	}
81
82
	/**
83
	 * Retrieve the field title.
84
	 *
85
	 * @access public
86
	 *
87
	 * @return string $title The title of this field.
88
	 */
89
	public function get_title() {
90
		return $this->title;
91
	}
92
93
	/**
94
	 * Modify the title of this field.
95
	 *
96
	 * @access public
97
	 *
98
	 * @param string $title The new title.
99
	 */
100
	public function set_title( $title ) {
101
		$this->title = $title;
102
	}
103
104
	/**
105
	 * Retrieve the field ID.
106
	 *
107
	 * @access public
108
	 *
109
	 * @return string $id The ID of this field.
110
	 */
111
	public function get_id() {
112
		return $this->id;
113
	}
114
115
	/**
116
	 * Modify the ID of this field.
117
	 *
118
	 * @access public
119
	 *
120
	 * @param string $id The new ID.
121
	 */
122
	public function set_id( $id ) {
123
		$this->id = $id;
124
	}
125
126
	/**
127
	 * Retrieve the field value. If there is no value, use the default one.
128
	 *
129
	 * @access public
130
	 *
131
	 * @return mixed $value The value of this field.
132
	 */
133
	public function get_value() {
134
		$original_name = str_replace( 'carbon_breadcrumbs_', '', $this->get_id() );
135
		$field_data    = Carbon_Breadcrumb_Admin_Settings::get_field_data();
136
		$default       = ! empty( $field_data[ $original_name ]['default'] ) ? $field_data[ $original_name ]['default'] : '';
137
138
		$value = get_option( $this->get_id() );
139
		if ( false === $value ) {
140
			$value = $default;
141
		}
142
143
		return $value;
144
	}
145
146
	/**
147
	 * Render the help description of this field.
148
	 *
149
	 * @access public
150
	 */
151
	public function render_help() {
152
		$field_data    = Carbon_Breadcrumb_Admin_Settings::get_field_data();
153
		$original_name = str_replace( 'carbon_breadcrumbs_', '', $this->get_id() );
154
		$help          = ! empty( $field_data[ $original_name ]['help'] ) ? $field_data[ $original_name ]['help'] : '';
155
		if ( ! $help ) {
156
			return;
157
		}
158
		?>
159
		<p class="description"><?php echo esc_html( $help ); ?></p>
160
		<?php
161
	}
162
163
	/**
164
	 * Render this field.
165
	 *
166
	 * @access public
167
	 * @abstract
168
	 */
169
	abstract public function render();
170
171
}
172