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