radio.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
  13. parent::__construct( $field, $value, $unique, $where, $parent );
  14. }
  15. public function render() {
  16. $args = wp_parse_args( $this->field, array(
  17. 'inline' => false,
  18. 'query_args' => array(),
  19. ) );
  20. $inline_class = ( $args['inline'] ) ? ' class="csf--inline-list"' : '';
  21. echo $this->field_before();
  22. if ( isset( $this->field['options'] ) ) {
  23. $options = $this->field['options'];
  24. $options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) );
  25. if ( is_array( $options ) && ! empty( $options ) ) {
  26. echo '<ul'. $inline_class .'>';
  27. foreach ( $options as $option_key => $option_value ) {
  28. if ( is_array( $option_value ) && ! empty( $option_value ) ) {
  29. echo '<li>';
  30. echo '<ul>';
  31. echo '<li><strong>'. esc_attr( $option_key ) .'</strong></li>';
  32. foreach ( $option_value as $sub_key => $sub_value ) {
  33. $checked = ( $sub_key == $this->value ) ? ' checked' : '';
  34. echo '<li>';
  35. echo '<label>';
  36. echo '<input type="radio" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $sub_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  37. echo '<span class="csf--text">'. esc_attr( $sub_value ) .'</span>';
  38. echo '</label>';
  39. echo '</li>';
  40. }
  41. echo '</ul>';
  42. echo '</li>';
  43. } else {
  44. $checked = ( $option_key == $this->value ) ? ' checked' : '';
  45. echo '<li>';
  46. echo '<label>';
  47. echo '<input type="radio" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $option_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  48. echo '<span class="csf--text">'. esc_attr( $option_value ) .'</span>';
  49. echo '</label>';
  50. echo '</li>';
  51. }
  52. }
  53. echo '</ul>';
  54. } else {
  55. echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'sakurairo_csf' );
  56. }
  57. } else {
  58. $label = ( isset( $this->field['label'] ) ) ? $this->field['label'] : '';
  59. echo '<label><input type="radio" name="'. esc_attr( $this->field_name() ) .'" value="1"'. $this->field_attributes() . esc_attr( checked( $this->value, 1, false ) ) .'/>';
  60. echo ( ! empty( $this->field['label'] ) ) ? '<span class="csf--text">'. esc_attr( $this->field['label'] ) .'</span>' : '';
  61. echo '</label>';
  62. }
  63. echo $this->field_after();
  64. }
  65. }
  66. }