action-button-render.php
2 years ago
base-select-radio-check.php
2 years ago
base.php
2 years ago
calculated-field-render.php
2 years ago
checkbox-field-render.php
2 years ago
date-field-render.php
2 years ago
datetime-field-render.php
2 years ago
form-builder.php
2 years ago
form-hidden-fields.php
2 years ago
group-break-field-render.php
2 years ago
heading-field-render.php
2 years ago
media-field-render.php
2 years ago
number-field-render.php
2 years ago
radio-field-render.php
2 years ago
range-field-render.php
2 years ago
repeater-field-render.php
2 years ago
select-field-render.php
2 years ago
text-field-render.php
2 years ago
textarea-field-render.php
2 years ago
time-field-render.php
2 years ago
wysiwyg-field-render.php
2 years ago
checkbox-field-render.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Blocks\Render; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | use Jet_Form_Builder\Classes\Builder_Helper; |
| 7 | |
| 8 | if ( ! defined( 'WPINC' ) ) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Define text field renderer class |
| 14 | */ |
| 15 | class Checkbox_Field_Render extends Base_Select_Radio_Check { |
| 16 | |
| 17 | public function __construct( $block_type ) { |
| 18 | parent::__construct( $block_type ); |
| 19 | |
| 20 | $this->set_options(); |
| 21 | } |
| 22 | |
| 23 | public function get_name() { |
| 24 | return 'checkbox-field'; |
| 25 | } |
| 26 | |
| 27 | public function render_without_layout( $template = null, $args = array() ) { |
| 28 | parent::render_without_layout( $template, $args ); |
| 29 | |
| 30 | return $this->render_options(); |
| 31 | } |
| 32 | |
| 33 | public function render_options(): string { |
| 34 | $required = $this->block_type->get_required_val(); |
| 35 | |
| 36 | $this->add_attribute( 'class', 'jet-form-builder__field checkboxes-field checkradio-field' ); |
| 37 | $this->add_attribute( 'class', $this->args['class_name'] ); |
| 38 | $this->add_attribute( 'required', $required ); |
| 39 | |
| 40 | $html = '<div class="jet-form-builder__fields-group checkradio-wrap" data-jfb-sync>'; |
| 41 | |
| 42 | if ( ! empty( $this->args['field_options'] ) ) { |
| 43 | foreach ( $this->args['field_options'] as $value => $option ) { |
| 44 | $html .= $this->render_option( $value, $option ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | $html .= '</div>'; |
| 49 | $this->reset_attributes(); |
| 50 | |
| 51 | return $html; |
| 52 | } |
| 53 | |
| 54 | public function render_option( $value, $option ): string { |
| 55 | $default = $this->args['default'] ?? array(); |
| 56 | |
| 57 | if ( is_array( $option ) ) { |
| 58 | $val = $option['value'] ?? $value; |
| 59 | $label = $option['label'] ?? $val; |
| 60 | } else { |
| 61 | $val = $value; |
| 62 | $label = $option; |
| 63 | } |
| 64 | |
| 65 | $html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap">'; |
| 66 | |
| 67 | $item = sprintf( |
| 68 | '<label class="jet-form-builder__field-label for-checkbox"> |
| 69 | <input %1$s %2$s><span>%3$s</span></label>', |
| 70 | Builder_Helper::attrs( |
| 71 | array( |
| 72 | array( 'type', 'checkbox' ), |
| 73 | array( 'name', esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() ) ), |
| 74 | array( 'value', esc_attr( $val ) ), |
| 75 | array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 76 | array( 'checked', in_array( (string) $val, $default, true ) ? 'checked' : '' ), |
| 77 | array( |
| 78 | 'data-calculate', |
| 79 | ( is_array( $option ) && isset( $option['calculate'] ) && '' !== $option['calculate'] ) |
| 80 | ? esc_attr( $option['calculate'] ) |
| 81 | : '', |
| 82 | ), |
| 83 | ) |
| 84 | ), |
| 85 | $this->get_attributes_string_save(), |
| 86 | wp_kses_post( $label ) |
| 87 | ); |
| 88 | |
| 89 | $html .= apply_filters( |
| 90 | 'jet-form-builder/render/checkbox-field/option', |
| 91 | $item, |
| 92 | $val, |
| 93 | $option, |
| 94 | $this |
| 95 | ); |
| 96 | |
| 97 | $html .= '</div>'; |
| 98 | |
| 99 | return $html; |
| 100 | } |
| 101 | |
| 102 | public function get_name_suffix(): string { |
| 103 | return count( $this->args['field_options'] ) > 1 ? '[]' : ''; |
| 104 | } |
| 105 | |
| 106 | } |
| 107 |