checkbox.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. 'check_all' => false,
  20. 'check_all_text' => esc_html__( 'Check/Uncheck All' ),
  21. ) );
  22. $inline_class = ( $args['inline'] ) ? ' class="csf--inline-list"' : '';
  23. echo $this->field_before();
  24. if ( isset( $this->field['options'] ) ) {
  25. $value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value );
  26. $options = $this->field['options'];
  27. $options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) );
  28. if ( is_array( $options ) && ! empty( $options ) ) {
  29. echo '<ul'. $inline_class .'>';
  30. foreach ( $options as $option_key => $option_value ) {
  31. if ( is_array( $option_value ) && ! empty( $option_value ) ) {
  32. echo '<li>';
  33. echo '<ul>';
  34. echo '<li><strong>'. esc_attr( $option_key ) .'</strong></li>';
  35. foreach ( $option_value as $sub_key => $sub_value ) {
  36. $checked = ( in_array( $sub_key, $value ) ) ? ' checked' : '';
  37. echo '<li>';
  38. echo '<label>';
  39. echo '<input type="checkbox" name="'. esc_attr( $this->field_name( '[]' ) ) .'" value="'. esc_attr( $sub_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  40. echo '<span class="csf--text">'. esc_attr( $sub_value ) .'</span>';
  41. echo '</label>';
  42. echo '</li>';
  43. }
  44. echo '</ul>';
  45. echo '</li>';
  46. } else {
  47. $checked = ( in_array( $option_key, $value ) ) ? ' checked' : '';
  48. echo '<li>';
  49. echo '<label>';
  50. echo '<input type="checkbox" name="'. esc_attr( $this->field_name( '[]' ) ) .'" value="'. esc_attr( $option_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  51. echo '<span class="csf--text">'. esc_attr( $option_value ) .'</span>';
  52. echo '</label>';
  53. echo '</li>';
  54. }
  55. }
  56. echo '</ul>';
  57. if ( $args['check_all'] ) {
  58. echo '<div class="csf-checkbox-all">'. esc_html( $args['check_all_text'] ) .'</div>';
  59. }
  60. } else {
  61. echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'sakurairo_csf' );
  62. }
  63. } else {
  64. echo '<label class="csf-checkbox">';
  65. echo '<input type="hidden" name="'. esc_attr( $this->field_name() ) .'" value="'. $this->value .'" class="csf--input"'. $this->field_attributes() .'/>';
  66. echo '<input type="checkbox" name="_pseudo" class="csf--checkbox"'. esc_attr( checked( $this->value, 1, false ) ) . $this->field_attributes() .'/>';
  67. echo ( ! empty( $this->field['label'] ) ) ? '<span class="csf--text">'. esc_attr( $this->field['label'] ) .'</span>' : '';
  68. echo '</label>';
  69. }
  70. echo $this->field_after();
  71. }
  72. }
  73. }