PluginProbe
Disable Comments & Delete All Comments / trunk
Disable Comments & Delete All Comments vtrunk
1.3.3 1.3.2 trunk 1.0.0 1.0.4 1.0.5 1.0.8 1.0.9 1.1.1 1.1.3 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.3.0 1.3.1
comments-plus / libs / factory / forms / includes / control.class.php

control.class.php in Disable Comments & Delete All Comments trunk, at libs/factory/forms/includes/control.class.php

422 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file contains the base class for all controls.
4 *
5 * @author Alex Kovalev <alex.kovalevv@gmail.com>
6 * @copyright (c) 2018, Webcraftic Ltd
7 *
8 * @package factory-forms
9 * @since 1.0.0
10 */
11
12 // Exit if accessed directly
13 if( !defined('ABSPATH') ) {
14 exit;
15 }
16
17 if( !class_exists('Wbcr_FactoryForms480_Control') ) {
18
19 /**
20 * The base class for all controls.
21 *
22 * @since 1.0.0
23 */
24 abstract class Wbcr_FactoryForms480_Control extends Wbcr_FactoryForms480_FormElement {
25
26 /**
27 * Is this element a control?
28 *
29 * @since 1.0.0
30 * @var bool
31 */
32 public $is_control = true;
33
34 /**
35 * Is this element a complex control?
36 *
37 * @since 1.0.0
38 * @var bool
39 */
40 public $is_complex_control = false;
41
42 /**
43 * A provider that is used to get values.
44 *
45 * @since 1.0.0
46 * @var Wbcr_IFactoryForms480_ValueProvider
47 */
48 protected $provider = null;
49
50 /**
51 * Create a new instance of the control.
52 *
53 * @param mixed[] $options
54 * @param FactoryForms480_Form $form
55 * @param null $provider
56 * @since 1.0.0
57 * @return void
58 */
59 public function __construct($options, $form, $provider = null)
60 {
61 parent::__construct($options, $form);
62 $this->provider = $provider;
63 }
64
65 /**
66 * Sets a provider for the control.
67 *
68 * @since 1.0.0
69 * @param IFactoryForms480_ValueProvider $provider
70 * @return void
71 */
72 public function setProvider($provider)
73 {
74 $this->provider = $provider;
75 }
76
77 /**
78 * Returns a control name used to save data with a provider.
79 *
80 * The method can return if the control have several elements.
81 *
82 * @since 1.0.0
83 * @return string[]|string|null A control name.
84 */
85 public function getName()
86 {
87 return isset($this->options['name'])
88 ? $this->options['name']
89 : null;
90 }
91
92 /**
93 * Prints a control name used to save data with a provider.
94 *
95 * @since 1.0.0
96 * @return void
97 */
98 protected function printName()
99 {
100 $name = $this->getName();
101 if( is_array($name) ) {
102 echo $name[0];
103 } else echo $name;
104 }
105
106 /**
107 * Returns a control scope.
108 *
109 * @since 1.0.0
110 * @return string|null A control scope.
111 */
112 public function getScope()
113 {
114 return isset($this->options['scope'])
115 ? $this->options['scope']
116 : null;
117 }
118
119 /**
120 * Prints a control scope.
121 *
122 * @since 1.0.0
123 * @return void
124 */
125 protected function printScope()
126 {
127 echo $this->getScope();
128 }
129
130 /**
131 * Returns a name of control on a form (scope + _ + name)
132 *
133 * @since 1.0.0
134 * @param null|string $name
135 * @return array|null|string|string[]
136 */
137 public function getNameOnForm($name = null)
138 {
139 $scope = $this->getScope();
140 $name = !$name
141 ? $this->getName()
142 : $name;
143
144 if( is_array($name) ) {
145 $names = array();
146 foreach($name as $item) {
147 $names[] = empty($scope)
148 ? $item
149 : $scope . '_' . $item;
150 }
151
152 return $names;
153 }
154
155 if( empty($scope) ) {
156 return $name;
157 }
158 if( empty($name) ) {
159 return null;
160 }
161
162 return $scope . '_' . $name;
163 }
164
165 /**
166 * Prints a control name on a form.
167 *
168 * @since 1.0.0
169 * @return void
170 */
171 public function printNameOnForm()
172 {
173 $name = $this->getNameOnForm();
174
175 if( is_array($name) ) {
176 echo $name[0];
177 } else {
178 echo $name;
179 }
180 }
181
182 /**
183 * Returns a submit value of the control by a given name.
184 *
185 * @since 1.0.0
186 * @param string $name
187 * @param string $sub_name
188 * @return string
189 */
190 public function getSubmitValue($name, $sub_name)
191 {
192 $name_on_form = $this->getNameOnForm($name);
193
194 $raw_value = isset($_POST[$name_on_form])
195 ? $_POST[$name_on_form]
196 : null;
197
198 $value = $raw_value;
199
200 if( is_array($value) ) {
201 $value = array_map('sanitize_text_field', $value);
202 $value = implode(',', $value);
203 } else {
204 $value = sanitize_text_field($value);
205 }
206
207 return $this->filterValue($value, $raw_value);
208 }
209
210 /**
211 * @param $value
212 * @param $raw_value
213 * @return mixed
214 */
215 protected function filterValue($value, $raw_value)
216 {
217 $sanitize_func = $this->getOption('filter_value');
218
219 // if the data options is a valid callback for an object method
220 if( !empty($sanitize_func) && ((is_array($sanitize_func) && count($sanitize_func) == 2 && gettype($sanitize_func[0]) == 'object') || function_exists($sanitize_func)) ) {
221 return call_user_func_array($sanitize_func, array($value, $raw_value));
222 }
223
224 return $value;
225 }
226
227
228 /**
229 * Returns an array of value to save received after submission of a form.
230 *
231 * @see getSubmitValue
232 *
233 * The array has the following format:
234 * array(
235 * 'control-name1' => 'value1',
236 * 'control-name2__sub-name1' => 'value2'
237 * 'control-name2__sub-name2' => 'value3'
238 * )
239 *
240 * @since 3.1.0
241 * @return mixed[]
242 */
243 public function getValuesToSave()
244 {
245 $values = array();
246 $name = $this->getName();
247
248 if( is_array($name) ) {
249 $i = 0;
250
251 foreach($name as $single_name) {
252 $sub_name = $this->getSubName($single_name);
253 if( !$sub_name ) {
254 $sub_name = $i;
255 $i++;
256 }
257 $values[$single_name] = $this->getSubmitValue($single_name, $sub_name);
258 }
259
260 return $values;
261 }
262
263 $values[$name] = $this->getSubmitValue($name, null);
264
265 return $values;
266 }
267
268 /**
269 * Returns an initial value of control that is used to render the control first time.
270 *
271 * @since 1.0.0
272 * @return mixed;
273 */
274 public function getValue($index = null, $multiple = false)
275 {
276 if( isset($this->options['value']) ) {
277 if( is_array($this->options['value']) ) {
278 if( $index !== null ) {
279 return $this->options['value'][$index];
280 } else return $this->options['value'];
281 } else {
282 return $this->options['value'];
283 }
284 }
285
286 $default = null;
287 if( isset($this->options['default']) ) {
288 if( is_array($this->options['default']) ) {
289 if( $index !== null ) {
290 $default = $this->options['default'][$index];
291 } else $default = $this->options['default'];
292 } else {
293 $default = $this->options['default'];
294 }
295 }
296
297 if( $this->provider ) {
298 $name = $this->getName();
299
300 if( is_array($name) ) {
301
302 $values = array();
303 $i = 0;
304
305 foreach($name as $single_name) {
306 $sub_name = $this->getSubName($single_name);
307 if( !$sub_name ) {
308 $sub_name = $i;
309 $i++;
310 }
311 $values[$sub_name] = $this->provider->getValue($single_name, isset($default[$sub_name])
312 ? $default[$sub_name]
313 : null);
314 }
315
316 if( $index !== null ) {
317 return $values[$index];
318 }
319
320 return $values;
321 } else {
322 return $this->provider->getValue($this->getName(), $default, $multiple);
323 }
324 }
325
326 return $default;
327 }
328
329 /**
330 * Shows the control.
331 *
332 * @since 1.0.0
333 * @return void
334 */
335 public function render()
336 {
337 $this->addCssClass('factory-from-control-' . $this->type);
338
339 // if the control is off, then ignore it
340 $off = $this->getOption('off', false);
341
342 if( $off ) {
343 return;
344 }
345
346 $this->beforeHtml();
347 $this->html();
348 $this->afterHtml();
349 }
350
351 /**
352 * A virtual method that is executed before rendering html markup of the control.
353 *
354 * @since 1.0.0
355 * @return void
356 */
357 protected function beforeHtml()
358 {
359 }
360
361 /**
362 * A virtual method that is executed after rendering html markup of the control.
363 *
364 * @since 1.0.0
365 * @return void
366 */
367 protected function afterHtml()
368 {
369 }
370
371 /**
372 * Renders the html markup for the control.
373 *
374 * @since 1.0.0
375 * @return void
376 */
377 public function html()
378 {
379 }
380
381 /**
382 * Returns a layout option.
383 *
384 * @since 1.0.0
385 * @param string $option_name A layout option to return.
386 * @param mixed $default A default value to return if the option doesn't exist.
387 * @return mixed
388 */
389 public function getLayoutOption($option_name, $default)
390 {
391 if( !isset($this->options['layout']) ) {
392 return $default;
393 }
394 if( !isset($this->options['layout'][$option_name]) ) {
395 return $default;
396 }
397
398 return $this->options['layout'][$option_name];
399 }
400
401 /**
402 * Splits the control name by '__' and return the right part.
403 *
404 * For example, if the $control_name is 'control__color', then returns 'color'.
405 * Throws an error if the control name cannot be splitted.
406 *
407 * @since 3.1.0
408 * @param string $control_name
409 * @return string
410 */
411 protected function getSubName($control_name)
412 {
413
414 $parts = explode('__', $control_name, 2);
415 if( !isset($parts[1]) ) {
416 return null;
417 }
418
419 return $parts[1];
420 }
421 }
422 }