PluginProbe ʕ •ᴥ•ʔ
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF / trunk
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF vtrunk
2.0.5 trunk 1.3.7 1.4.0 1.4.1 1.4.2 1.4.6 1.5.0 1.5.3 1.5.6 1.5.8 1.6.5 1.6.6 1.6.9 1.7.0 1.7.4 1.8.1 1.8.2 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4
robin-image-optimizer / libs / factory / forms / includes / form-element.class.php
robin-image-optimizer / libs / factory / forms / includes Last commit date
providers 5 months ago complex-control.class.php 5 months ago control-holder.class.php 5 months ago control.class.php 5 months ago custom-element.class.php 5 months ago form-element.class.php 5 months ago form-layout.class.php 5 months ago form.class.php 5 months ago holder.class.php 5 months ago html-builder.class.php 5 months ago index.php 5 months ago
form-element.class.php
397 lines
1 <?php
2 /**
3 * The file contains the base class for all form element (controls, holders).
4 *
5 * @package factory-forms
6 * @since 1.0.0
7 */
8
9 // Exit if accessed directly
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 if ( ! class_exists( 'Wbcr_FactoryForms600_FormElement' ) ) {
15
16 /**
17 * The base class for all form element (controls, holders).
18 *
19 * Provides several methods to build html markup of an element.
20 *
21 * @since 1.0.0
22 */
23 abstract class Wbcr_FactoryForms600_FormElement {
24
25 /**
26 * A type of an elemnt.
27 *
28 * @since 1.0.0
29 * @var boolean
30 */
31 protected $type = null;
32
33 /**
34 * An html attribute builder.
35 *
36 * @since 1.0.0
37 * @var Wbcr_FactoryForms600_HtmlAttributeBuilder
38 */
39 private $html_builder;
40
41 /**
42 * Element options.
43 *
44 * @since 1.0.0
45 * @var array
46 */
47 public $options = [];
48
49 /**
50 * A parent form.
51 *
52 * @since 1.0.0
53 * @var Wbcr_FactoryForms600_Form
54 */
55 protected $form;
56
57 /**
58 * A form layout.
59 *
60 * @since 1.0.0
61 * @var Wbcr_FactoryForms600_FormLayout
62 */
63 protected $layout;
64
65 /**
66 * Is this element a control?
67 *
68 * @since 1.0.0
69 * @var bool
70 */
71 public $is_control = false;
72
73 /**
74 * Is this element a control holder?
75 *
76 * @since 1.0.0
77 * @var bool
78 */
79 public $is_holder = false;
80
81 /**
82 * Is this element a custom form element?
83 *
84 * @since 1.0.0
85 * @var bool
86 */
87 public $is_custom = false;
88
89 /**
90 * Creates a new instance of a form element.
91 *
92 * @since 1.0.0
93 * @param mixed[] $options A holder options.
94 * @param Wbcr_FactoryForms600_Form $form A parent form.
95 */
96 public function __construct( $options, $form ) {
97 $this->options = $options;
98 $this->form = $form;
99 $this->layout = $form->layout;
100
101 $this->html_builder = new Wbcr_FactoryForms600_HtmlAttributeBuilder();
102
103 if ( isset( $this->options['cssClass'] ) ) {
104 $this->html_builder->addCssClass( $this->options['cssClass'] );
105 }
106
107 if ( isset( $this->options['htmlData'] ) ) {
108 foreach ( $this->options['htmlData'] as $data_key => $data_value ) {
109 $this->html_builder->addHtmlData( $data_key, $data_value );
110 }
111 }
112
113 if ( isset( $this->options['htmlAttrs'] ) ) {
114 foreach ( $this->options['htmlAttrs'] as $attr_key => $attr_value ) {
115 $this->html_builder->addHtmlAttr( $attr_key, $attr_value );
116 }
117 }
118
119 $this->addCssClass( 'factory-' . $this->type );
120 }
121
122
123 /**
124 * Sets options for the control.
125 *
126 * @since 1.0.0
127 * @param mixed[] $options
128 * @return void
129 */
130 public function setOptions( $options ) {
131 $this->options = $options;
132 }
133
134 /**
135 * Gets options of the control.
136 *
137 * @since 1.0.0
138 * @return mixed[] $options
139 */
140 public function getOptions() {
141 return $this->options;
142 }
143
144 /**
145 * Sets a new value for a given option.
146 *
147 * @since 1.0.0
148 * @param string $name An option name to set.
149 * @param mixed $value A value to set.
150 * @return void
151 */
152 public function setOption( $name, $value ) {
153 $this->options[ $name ] = $value;
154 }
155
156 /**
157 * Gets an option value or default.
158 *
159 * @since 1.0.0
160 * @param string $name An option name to get.
161 * @param mixed $default A default value
162 * @return mixed|null
163 */
164 public function getOption( $name, $default = null ) {
165 return isset( $this->options[ $name ] )
166 ? $this->options[ $name ]
167 : $default;
168 }
169
170 /**
171 * Prints an option value or default.
172 *
173 * @since 1.0.0
174 * @param string $name An option name to get.
175 * @param mixed $default A default value
176 * @return void
177 */
178 public function option( $name, $default = null ) {
179 $value = $this->getOption( $name, $default );
180 echo $value;
181 }
182
183 /**
184 * Adds a new CSS class for the element.
185 *
186 * @since 1.0.0
187 * @return void
188 */
189 public function addCssClass( $class ) {
190 $this->html_builder->addCssClass( $class );
191 }
192
193 /**
194 * Prints CSS classes of the element.
195 *
196 * @since 1.0.0
197 * @return void
198 */
199 protected function cssClass() {
200 $this->html_builder->printCssClass();
201 }
202
203 /**
204 * Adds a new html attribute.
205 *
206 * @since 1.0.0
207 * @param string $data_key
208 * @param string $data_value
209 */
210 protected function addHtmlData( $data_key, $data_value ) {
211 $this->html_builder->addHtmlData( $data_key, $data_value );
212 }
213
214 /**
215 * Adds a new html attribute.
216 *
217 * @since 1.0.0
218 * @param string $attr_name
219 * @param string $attr_value
220 * @return void
221 */
222 protected function addHtmlAttr( $attr_name, $attr_value ) {
223 $this->html_builder->addHtmlAttr( $attr_name, $attr_value );
224 }
225
226 /**
227 * Prints all html attributes, including css classes and data.
228 *
229 * @since 1.0.0
230 * @return void
231 */
232 protected function attrs() {
233 $this->html_builder->printAttrs();
234 }
235
236 /**
237 * Returns an element title.
238 *
239 * @since 1.0.0
240 * @return string|bool
241 */
242 public function getTitle() {
243 if ( isset( $this->options['title'] ) ) {
244 return $this->options['title'];
245 }
246
247 return false;
248 }
249
250 /**
251 * Returns true if an element has title.
252 *
253 * @since 1.0.0
254 * @return bool
255 */
256 public function hasTitle() {
257 $title = $this->getTitle();
258
259 return ! empty( $title );
260 }
261
262 /**
263 * Prints an element title.
264 *
265 * @since 1.0.0
266 * @return void
267 */
268 public function title() {
269 echo $this->getTitle();
270 }
271
272 /**
273 * Returns an element hint.
274 *
275 * @since 1.0.0
276 * @return string
277 */
278 public function getHint() {
279 if ( isset( $this->options['hint'] ) ) {
280 return $this->options['hint'];
281 }
282
283 return false;
284 }
285
286 /**
287 * Returns true if an element has hint.
288 *
289 * @since 1.0.0
290 * @return bool
291 */
292 public function hasHint() {
293 $hint = $this->getHint();
294
295 return ! empty( $hint );
296 }
297
298 /**
299 * Prints an element hint.
300 *
301 * @since 1.0.0
302 * @return void
303 */
304 public function hint( $esc = false ) {
305 echo $esc
306 ? esc_html( $this->getHint() )
307 : $this->getHint();
308 }
309
310 /**
311 * Returns an element name.
312 *
313 * @since 1.0.0
314 * @return string
315 */
316 public function getName() {
317
318 if ( empty( $this->options['name'] ) && ! empty( $this->options['title'] ) ) {
319 $this->options['name'] = str_replace( ' ', '-', $this->options['title'] );
320 $this->options['name'] = strtolower( $this->options['name'] );
321 }
322
323 if ( ! isset( $this->options['name'] ) ) {
324 $this->options['name'] = $this->type . '-' . rand();
325 }
326
327 return $this->options['name'];
328 }
329
330 /**
331 * Prints an element name.
332 *
333 * @since 1.0.0
334 * @return void
335 */
336 public function name() {
337 echo $this->getName();
338 }
339
340 /**
341 * Returns a form name
342 *
343 * @since 1.0.0
344 * @return string
345 */
346 public function getFormName() {
347 return $this->form->name;
348 }
349
350 /**
351 * Returns an element type.
352 *
353 * @since 1.0.0
354 * @return string
355 */
356 public function getType() {
357 return $this->type;
358 }
359
360 /**
361 * Returns an element icon.
362 *
363 * @since 1.0.0
364 * @return string
365 */
366 public function getIcon() {
367 if ( isset( $this->options['icon'] ) ) {
368 return $this->options['icon'];
369 }
370
371 return false;
372 }
373
374 /**
375 * Returns true if an element has a icon.
376 *
377 * @since 1.0.0
378 * @return bool
379 */
380 public function hasIcon() {
381 $icon = $this->getIcon();
382
383 return ! empty( $icon );
384 }
385
386 /**
387 * Prints an element icon.
388 *
389 * @since 1.0.0
390 * @return void
391 */
392 public function icon() {
393 echo $this->getIcon();
394 }
395 }
396 }
397