| @@ -1,5 +1,6 @@ | ||
| 1 | -<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { | |
| 2 | + die; } // Cannot access directly. | |
| 2 | 3 | /** |
| 3 | 4 | * |
| 4 | 5 | * Field: radio |
| 5 | 6 | * |
| @@ -4,90 +5,80 @@ | ||
| 4 | 5 | * Field: radio |
| 5 | 6 | * |
| 6 | 7 | * @since 1.0.0 |
| 7 | 8 | * @version 1.0.0 |
| 8 | - * | |
| 9 | 9 | */ |
| 10 | 10 | if ( ! class_exists( 'ADMINIFY_Field_radio' ) ) { |
| 11 | - class ADMINIFY_Field_radio extends ADMINIFY_Fields { | |
| 11 | + class ADMINIFY_Field_radio extends ADMINIFY_Fields { | |
| 12 | 12 | |
| 13 | - public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { | |
| 14 | - parent::__construct( $field, $value, $unique, $where, $parent ); | |
| 15 | - } | |
| 13 | + public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { | |
| 14 | + parent::__construct( $field, $value, $unique, $where, $parent ); | |
| 15 | + } | |
| 16 | 16 | |
| 17 | - public function render() { | |
| 17 | + public function render() { | |
| 18 | + $args = wp_parse_args( | |
| 19 | + $this->field, | |
| 20 | + [ | |
| 21 | + 'inline' => false, | |
| 22 | + 'query_args' => [], | |
| 23 | + ] | |
| 24 | + ); | |
| 18 | 25 | |
| 19 | - $args = wp_parse_args( $this->field, array( | |
| 20 | - 'inline' => false, | |
| 21 | - 'query_args' => array(), | |
| 22 | - ) ); | |
| 26 | + $inline_class = ( $args['inline'] ) ? ' class="adminify--inline-list"' : ''; | |
| 23 | 27 | |
| 24 | - $inline_class = ( $args['inline'] ) ? ' class="adminify--inline-list"' : ''; | |
| 28 | + echo wp_kses_post( $this->field_before() ); | |
| 25 | 29 | |
| 26 | - echo wp_kses_post( $this->field_before() ); | |
| 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'] ) ); | |
| 27 | 33 | |
| 28 | - if ( isset( $this->field['options'] ) ) { | |
| 34 | + if ( is_array( $options ) && ! empty( $options ) ) { | |
| 35 | + echo '<ul' . esc_attr( $inline_class ) . '>'; | |
| 29 | 36 | |
| 30 | - $options = $this->field['options']; | |
| 31 | - $options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) ); | |
| 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' : ''; | |
| 32 | 55 | |
| 33 | - if ( is_array( $options ) && ! empty( $options ) ) { | |
| 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 | + } | |
| 34 | 64 | |
| 35 | - echo '<ul'. $inline_class .'>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- attribute value pre-escaped with esc_attr() where built. | |
| 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 | + } | |
| 36 | 79 | |
| 37 | - foreach ( $options as $option_key => $option_value ) { | |
| 80 | + echo wp_kses_post( $this->field_after() ); | |
| 81 | + } | |
| 38 | 82 | |
| 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 ) .'/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 49 | - echo '<span class="adminify--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 ) .'/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 63 | - echo '<span class="adminify--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.', 'adminify' ); | |
| 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 ) ) .'/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 83 | - echo ( ! empty( $this->field['label'] ) ) ? '<span class="adminify--text">'. esc_attr( $this->field['label'] ) .'</span>' : ''; | |
| 84 | - echo '</label>'; | |
| 85 | - | |
| 86 | - } | |
| 87 | - | |
| 88 | - echo wp_kses_post( $this->field_after() ); | |
| 89 | - | |
| 90 | - } | |
| 91 | - | |
| 92 | - } | |
| 83 | + } | |
| 93 | 84 | } |