| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: button_set |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'DarkifyField_button_set' ) ) { |
| 11 |
class DarkifyField_button_set extends DarkifyFields { |
| 12 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
|
| 19 |
$args = wp_parse_args( |
| 20 |
$this->field, |
| 21 |
array( |
| 22 |
'multiple' => false, |
| 23 |
'options' => array(), |
| 24 |
'query_args' => array(), |
| 25 |
) |
| 26 |
); |
| 27 |
|
| 28 |
$value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value ); |
| 29 |
|
| 30 |
echo wp_kses_post( $this->field_before() ); |
| 31 |
|
| 32 |
if ( isset( $this->field['options'] ) ) { |
| 33 |
|
| 34 |
$options = $this->field['options']; |
| 35 |
$options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) ); |
| 36 |
|
| 37 |
if ( is_array( $options ) && ! empty( $options ) ) { |
| 38 |
|
| 39 |
echo '<div class="darkify-siblings darkify--button-group" data-multiple="' . esc_attr( $args['multiple'] ) . '">'; |
| 40 |
|
| 41 |
foreach ( $options as $key => $option ) { |
| 42 |
|
| 43 |
$type = ( $args['multiple'] ) ? 'checkbox' : 'radio'; |
| 44 |
$extra = ( $args['multiple'] ) ? '[]' : ''; |
| 45 |
$active = ( in_array( $key, $value ) || ( empty( $value ) && empty( $key ) ) ) ? ' darkify--active' : ''; |
| 46 |
$checked = ( in_array( $key, $value ) || ( empty( $value ) && empty( $key ) ) ) ? ' checked' : ''; |
| 47 |
|
| 48 |
$option_label = is_array( $option ) ? ( isset( $option['label'] ) ? $option['label'] : '' ) : $option; |
| 49 |
$option_disabled = is_array( $option ) && ! empty( $option['disabled'] ) ? ' disabled' : ''; |
| 50 |
$pro_only_class = $option_disabled ? ' darkify-pro-only' : ''; |
| 51 |
|
| 52 |
echo '<div class="darkify--sibling darkify--button' . esc_attr( $active ) . esc_attr( $pro_only_class ) . '">'; |
| 53 |
echo '<input' . esc_attr( $option_disabled ) . ' type="' . esc_attr( $type ) . '" name="' . esc_attr( $this->field_name( $extra ) ) . '" value="' . esc_attr( $key ) . '"' . wp_kses_post( $this->field_attributes() ) . esc_attr( $checked ) . '/>'; |
| 54 |
echo esc_html( $option_label ); |
| 55 |
echo '</div>'; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
echo '</div>'; |
| 60 |
|
| 61 |
} else { |
| 62 |
|
| 63 |
echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'darkify' ); |
| 64 |
|
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
echo wp_kses_post( $this->field_after() ); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|