Completed
Push — develop ( d972ca...3ad20e )
by David
04:13
created

control::set_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
/**
3
 * UIX Controls
4
 *
5
 * @package   controls
6
 * @author    David Cramer
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2016 David Cramer
10
 */
11
12
namespace uix\ui;
13
14
/**
15
 * Base UIX Control class.
16
 *
17
 * @since       1.0.0
18
 */
19
class control extends \uix\data\data {
20
21
	/**
22
	 * The type of object
23
	 *
24
	 * @since       1.0.0
25
	 * @access      public
26
	 * @var         string
27
	 */
28
	public $type = 'control';
29
30
	/**
31
	 * Register the UIX objects
32
	 *
33
	 * @since  1.0.0
34
	 * @access public
35
	 *
36
	 * @param string $slug Object slug
37
	 * @param array  $object object structure array
38
	 *
39
	 * @return object|\uix object instance
40
	 */
41 9
	public static function register( $slug, $object, $parent = null ) {
42
43 9
		$caller = get_called_class();
44
		// get the current instance
45 9
		if ( empty( $object['type'] ) || ! uix()->is_callable( 'control\\' . $object['type'] ) ) {
46 1
			$object['type'] = 'text';
47
		}
48
49 9
		$caller = $caller . '\\' . $object['type'];
50
51 9
		return new $caller( $slug, $object, $parent );
52
53
	}
54
55
	/**
56
	 * Sets the controls data
57
	 *
58
	 * @since  1.0.0
59
	 * @see    \uix\uix
60
	 * @access public
61
	 */
62 9
	public function setup() {
63
64
		// run parents to setup sanitization filters
65 9
		parent::setup();
66 9
		if ( isset( $this->struct['value'] ) ) {
67 9
			$this->set_value( $this->struct['value'] );
68 9
		}
69 5
		// base attributes defined
70
		$this->attributes['name'] = $this->name();
71 9
		$this->attributes['id']   = $this->id() . '-control';
72
	}
73
74 9
	/**
75
	 * Create and Return the control's input name
76 9
	 *
77 9
	 * @since  1.0.0
78 9
	 * @access public
79
	 * @return string The control name
80
	 */
81
	public function name() {
82
		return $this->id();
83
	}
84
85
	/**
86
	 * Sets the attributes for the control.
87 9
	 *
88 9
	 * @since  1.0.0
89
	 * @access public
90
	 */
91
	public function set_attributes() {
92
93
		if ( ! empty( $this->struct['config'] ) ) {
94
			$this->set_config();
95
		}
96
97 5
		$this->attributes['class'] += $this->classes();
98
99 5
		parent::set_attributes();
100
101
	}
102
103 5
	/**
104
	 * Handy method for setting data-* attributes using the setup parameter
105 5
	 *
106
	 * @since  1.0.0
107 5
	 * @access public
108
	 */
109
	public function set_config() {
110
111
		foreach ( $this->struct['config'] as $key => $setting ) {
112
			$this->attributes[ 'data-' . $key ] = $setting;
113
		}
114
115
	}
116
117
	/**
118
	 * Gets the classes for the control input
119
	 *
120
	 * @since  1.0.0
121
	 * @access public
122
	 * @return array
123
	 */
124
	public function classes() {
125
126
		return [
127
			'widefat',
128
		];
129
130 3
	}
131
132
	/**
133 3
	 * Define core page styles
134
	 *
135
	 * @since  1.0.0
136
	 * @access public
137
	 */
138
	public function set_assets() {
139
		$this->assets['style']['controls'] = $this->url . 'assets/css/control' . UIX_ASSET_DEBUG . '.css';
140
		parent::set_assets();
141
	}
142
143
	/**
144 9
	 * Render the Control
145 9
	 *
146 9
	 * @since  1.0.0
147 9
	 * @see    \uix\ui\uix
148
	 * @access public
149
	 * @return string HTML of rendered control
150
	 */
151
	public function render() {
152
153
		$output = '<div id="' . esc_attr( $this->id() ) . '" class="uix-control uix-control-' . esc_attr( $this->type ) . ' ' . esc_attr( $this->id() ) . '">';
154
155
		$output .= $this->label();
156
		$output .= '<div class="uix-control-input">';
157 5
		$output .= $this->input();
158
		$output .= '</div>';
159 5
		$output .= $this->description();
160
161 5
		$output .= '</div>';
162 5
163 5
		return $output;
164 5
	}
165 5
166
	/**
167 5
	 * Returns the label for the control
168
	 *
169 5
	 * @since  1.0.0
170
	 * @access public
171
	 * @return string label of control
172
	 */
173
	public function label() {
174
		$output = null;
175
		if ( isset( $this->struct['label'] ) ) {
176
			$output .= '<label for="' . esc_attr( $this->id() ) . '-control"><span class="uix-control-label">' . esc_html( $this->struct['label'] ) . '</span></label>';
177
		}
178
179 5
		return $output;
180 5
	}
181 5
182 2
	/**
183
	 * Returns the main input field for rendering
184
	 *
185 5
	 * @since  1.0.0
186
	 * @see    \uix\ui\uix
187
	 * @access public
188
	 * @return string Input field HTML striung
189
	 */
190
	public function input() {
191
192
		return '<input type="' . esc_attr( $this->type ) . '" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>';
193
	}
194
195
	/**
196 4
	 * Get this controls value.
197
	 *
198 4
	 * @since  1.0.0
199
	 * @access public
200
	 * @return mixed the controls value
201
	 */
202
	public function get_value() {
203
		$data  = $this->get_data();
204
		$value = null;
205
		if ( null !== $data && null !== $data[ $this->slug ] ) {
206
			$value = $data[ $this->slug ];
207
		}
208 5
209 5
		return $value;
210 5
	}
211
212 5
213 5
	/**
214
	 * Set this controls value.
215
	 *
216 5
	 * @since  1.0.0
217
	 * @access public
218
	 *
219
	 * @param mixed $value The value to set this control to.
220
	 *
221
	 * @return mixed the controls value
222
	 */
223
	public function set_value( $value ) {
224
		return $this->set_data( array( $this->slug => $value ) );
225
	}
226 5
227 5
	/**
228 5
	 * Returns the description for the control
229 1
	 *
230
	 * @since  1.0.0
231
	 * @access public
232 5
	 * @return string description string
233
	 */
234
	public function description() {
235
		$output = null;
236
		if ( isset( $this->struct['description'] ) ) {
237
			$output .= '<span class="uix-control-description">' . esc_html( $this->struct['description'] ) . '</span>';
238
		}
239
240
		return $output;
241
	}
242 3
243 3
	/**
244 1
	 * Checks if the current control is active.
245
	 *
246
	 * @since  1.0.0
247 3
	 * @access public
248
	 * @return bool
249
	 */
250
	public function is_active() {
251
		if ( ! empty( $this->parent ) ) {
252
			return $this->parent->is_active();
253
		}
254
255
		return parent::is_active();
256
	}
257
258
}
259