button_set.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: button_set
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_button_set' ) ) {
  11. class CSF_Field_button_set 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. 'multiple' => false,
  18. 'options' => array(),
  19. 'query_args' => array(),
  20. ) );
  21. $value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value );
  22. echo $this->field_before();
  23. if ( isset( $this->field['options'] ) ) {
  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 '<div class="csf-siblings csf--button-group" data-multiple="'. esc_attr( $args['multiple'] ) .'">';
  28. foreach ( $options as $key => $option ) {
  29. $type = ( $args['multiple'] ) ? 'checkbox' : 'radio';
  30. $extra = ( $args['multiple'] ) ? '[]' : '';
  31. $active = ( in_array( $key, $value ) || ( empty( $value ) && empty( $key ) ) ) ? ' csf--active' : '';
  32. $checked = ( in_array( $key, $value ) || ( empty( $value ) && empty( $key ) ) ) ? ' checked' : '';
  33. echo '<div class="csf--sibling csf--button'. esc_attr( $active ) .'">';
  34. echo '<input type="'. esc_attr( $type ) .'" name="'. esc_attr( $this->field_name( $extra ) ) .'" value="'. esc_attr( $key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  35. echo $option;
  36. echo '</div>';
  37. }
  38. echo '</div>';
  39. } else {
  40. echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'sakurairo_csf' );
  41. }
  42. }
  43. echo $this->field_after();
  44. }
  45. }
  46. }