checkbox.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: checkbox
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_checkbox' ) ) {
  11. class CSF_Field_checkbox 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. $value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value );
  24. $options = $this->field['options'];
  25. $options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) );
  26. if ( is_array( $options ) && ! empty( $options ) ) {
  27. echo '<ul'. $inline_class .'>';
  28. foreach ( $options as $option_key => $option_value ) {
  29. if ( is_array( $option_value ) && ! empty( $option_value ) ) {
  30. echo '<li>';
  31. echo '<ul>';
  32. echo '<li><strong>'. esc_attr( $option_key ) .'</strong></li>';
  33. foreach ( $option_value as $sub_key => $sub_value ) {
  34. $checked = ( in_array( $sub_key, $value ) ) ? ' checked' : '';
  35. echo '<li>';
  36. echo '<label>';
  37. echo '<input type="checkbox" name="'. esc_attr( $this->field_name( '[]' ) ) .'" value="'. esc_attr( $sub_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  38. echo '<span class="csf--text">'. esc_attr( $sub_value ) .'</span>';
  39. echo '</label>';
  40. echo '</li>';
  41. }
  42. echo '</ul>';
  43. echo '</li>';
  44. } else {
  45. $checked = ( in_array( $option_key, $value ) ) ? ' checked' : '';
  46. echo '<li>';
  47. echo '<label>';
  48. echo '<input type="checkbox" name="'. esc_attr( $this->field_name( '[]' ) ) .'" value="'. esc_attr( $option_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  49. echo '<span class="csf--text">'. esc_attr( $option_value ) .'</span>';
  50. echo '</label>';
  51. echo '</li>';
  52. }
  53. }
  54. echo '</ul>';
  55. } else {
  56. echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'sakurairo_csf' );
  57. }
  58. } else {
  59. echo '<label class="csf-checkbox">';
  60. echo '<input type="hidden" name="'. esc_attr( $this->field_name() ) .'" value="'. $this->value .'" class="csf--input"'. $this->field_attributes() .'/>';
  61. echo '<input type="checkbox" name="_pseudo" class="csf--checkbox"'. esc_attr( checked( $this->value, 1, false ) ) . $this->field_attributes() .'/>';
  62. echo ( ! empty( $this->field['label'] ) ) ? '<span class="csf--text">'. esc_attr( $this->field['label'] ) .'</span>' : '';
  63. echo '</label>';
  64. }
  65. echo $this->field_after();
  66. }
  67. }
  68. }