| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Radio Control |
| 5 |
* |
| 6 |
* Main options: |
| 7 |
* name => a name of the control |
| 8 |
* value => a value to show in the control |
| 9 |
* default => a default value of the control if the "value" option is not specified |
| 10 |
* items => a callback to return items or an array of items to select |
| 11 |
* |
| 12 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 13 |
* @copyright (c) 2018, Webcraftic Ltd |
| 14 |
* |
| 15 |
* @package factory-forms |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
|
| 19 |
// Exit if accessed directly |
| 20 |
if( !defined('ABSPATH') ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
if( !class_exists('Wbcr_FactoryForms420_RadioControl') ) { |
| 25 |
|
| 26 |
class Wbcr_FactoryForms420_RadioControl extends Wbcr_FactoryForms420_Control { |
| 27 |
|
| 28 |
public $type = 'radio'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns a set of available items for the radio. |
| 32 |
* |
| 33 |
* @since 1.0.0 |
| 34 |
* @return mixed[] |
| 35 |
*/ |
| 36 |
private function getItems() |
| 37 |
{ |
| 38 |
$data = $this->getOption('data', array()); |
| 39 |
|
| 40 |
// if the data options is a valid callback for an object method |
| 41 |
if( (is_array($data) && count($data) == 2 && is_object($data[0])) || is_string($data) ) { |
| 42 |
|
| 43 |
return call_user_func($data); |
| 44 |
} |
| 45 |
|
| 46 |
// if the data options is an array of values |
| 47 |
return $data; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Preparing html attributes before rendering html of the control. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
protected function beforeHtml() |
| 57 |
{ |
| 58 |
$name_on_form = $this->getNameOnForm(); |
| 59 |
$this->addHtmlAttr('name', $name_on_form); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Shows the html markup of the control. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function html() |
| 69 |
{ |
| 70 |
$items = $this->getItems(); |
| 71 |
$value = $this->getValue(); |
| 72 |
?> |
| 73 |
<?php foreach($items as $item) { |
| 74 |
$checked = ($item[0] == $value) |
| 75 |
? 'checked="checked"' |
| 76 |
: ''; |
| 77 |
?> |
| 78 |
<span class="factory-form-radio-item"> |
| 79 |
<lable class="factory-from-radio-label"><?= esc_html($item[1]); ?></lable> |
| 80 |
<input type="radio" <?php $this->attrs() ?> value="<?php echo esc_attr($item[0]) ?>" <?php echo $checked ?>/> |
| 81 |
</span> |
| 82 |
<?php } |
| 83 |
} |
| 84 |
} |
| 85 |
} |