/
class-radio.php
77 lines (66 loc) · 1.7 KB
/
class-radio.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* AnsPress Radio type field object.
*
* @package AnsPress
* @subpackage Fields
* @since 4.1.0
* @author Rahul Aryan<support@anspress.net>
* @copyright Copyright (c) 2017, Rahul Aryan
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License
*/
namespace AnsPress\Form\Field;
use AnsPress\Form\Field;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The Radio type field object.
*
* @since 4.1.0
*/
class Radio extends Field {
/**
* The field type.
*
* @var string
*/
public $type = 'radio';
/**
* Prepare field.
*
* @return void
*/
protected function prepare() {
$this->args = wp_parse_args(
$this->args,
array(
'label' => __( 'AnsPress Radio Field', 'anspress-question-answer' ),
)
);
// Call parent prepare().
parent::prepare();
// Make sure checkbox value are sanitized.
$this->sanitize_cb = array_merge( array( 'text_field' ), $this->sanitize_cb );
}
/**
* Field markup.
*
* @return void
*/
public function field_markup() {
parent::field_markup();
$value = $this->value();
if ( $this->get( 'options' ) ) {
foreach ( $this->get( 'options' ) as $val => $label ) {
$checked = checked( $value, $val, false );
$this->add_html( '<label>' );
$this->add_html( '<input type="radio" value="' . esc_attr( $val ) . '" name="' . esc_attr( $this->field_name ) . '" id="' . sanitize_html_class( $this->field_name . $val ) . '" class="ap-form-control" ' . $checked . $this->custom_attr() . '/>' );
$this->add_html( $label . '</label>' );
}
}
/** This action is documented in lib/form/class-input.php */
do_action_ref_array( 'ap_after_field_markup', array( &$this ) );
}
}