| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. |
| 2 |
/** |
| 3 |
* |
| 4 |
* Field: radio |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
* |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'CSF_Field_radio' ) ) { |
| 11 |
class CSF_Field_radio extends CSF_Fields { |
| 12 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
|
| 19 |
$args = wp_parse_args( $this->field, array( |
| 20 |
'inline' => false, |
| 21 |
'query_args' => array(), |
| 22 |
) ); |
| 23 |
|
| 24 |
$inline_class = ( $args['inline'] ) ? ' class="csf--inline-list"' : ''; |
| 25 |
|
| 26 |
echo $this->field_before(); |
| 27 |
|
| 28 |
if ( isset( $this->field['options'] ) ) { |
| 29 |
|
| 30 |
$options = $this->field['options']; |
| 31 |
$options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) ); |
| 32 |
|
| 33 |
if ( is_array( $options ) && ! empty( $options ) ) { |
| 34 |
|
| 35 |
echo '<ul'. $inline_class .'>'; |
| 36 |
|
| 37 |
foreach ( $options as $option_key => $option_value ) { |
| 38 |
|
| 39 |
if ( is_array( $option_value ) && ! empty( $option_value ) ) { |
| 40 |
|
| 41 |
echo '<li>'; |
| 42 |
echo '<ul>'; |
| 43 |
echo '<li><strong>'. esc_attr( $option_key ) .'</strong></li>'; |
| 44 |
foreach ( $option_value as $sub_key => $sub_value ) { |
| 45 |
$checked = ( $sub_key == $this->value ) ? ' checked' : ''; |
| 46 |
echo '<li>'; |
| 47 |
echo '<label>'; |
| 48 |
echo '<input type="radio" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $sub_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>'; |
| 49 |
echo '<span class="csf--text">'. esc_attr( $sub_value ) .'</span>'; |
| 50 |
echo '</label>'; |
| 51 |
echo '</li>'; |
| 52 |
} |
| 53 |
echo '</ul>'; |
| 54 |
echo '</li>'; |
| 55 |
|
| 56 |
} else { |
| 57 |
|
| 58 |
$checked = ( $option_key == $this->value ) ? ' checked' : ''; |
| 59 |
|
| 60 |
echo '<li>'; |
| 61 |
echo '<label>'; |
| 62 |
echo '<input type="radio" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $option_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>'; |
| 63 |
echo '<span class="csf--text">'. esc_attr( $option_value ) .'</span>'; |
| 64 |
echo '</label>'; |
| 65 |
echo '</li>'; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
echo '</ul>'; |
| 72 |
|
| 73 |
} else { |
| 74 |
|
| 75 |
echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'csf' ); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
} else { |
| 80 |
|
| 81 |
$label = ( isset( $this->field['label'] ) ) ? $this->field['label'] : ''; |
| 82 |
echo '<label><input type="radio" name="'. esc_attr( $this->field_name() ) .'" value="1"'. $this->field_attributes() . esc_attr( checked( $this->value, 1, false ) ) .'/>'; |
| 83 |
echo ( ! empty( $this->field['label'] ) ) ? '<span class="csf--text">'. esc_attr( $this->field['label'] ) .'</span>' : ''; |
| 84 |
echo '</label>'; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
echo $this->field_after(); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
} |
| 94 |
|