block-template.php
84 lines
| 1 | <?php |
| 2 | /** |
| 3 | * input[type="hidden"] template |
| 4 | * |
| 5 | * @var Block_Render $this |
| 6 | * @var array $args |
| 7 | */ |
| 8 | |
| 9 | use JFB_Modules\Option_Field\Blocks\Select\Block_Render; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | $this->add_attribute( 'class', 'jet-form-builder__field select-field' ); |
| 17 | $this->add_attribute( 'class', $args['class_name'] ); |
| 18 | $this->add_attribute( 'required', $this->block_type->get_required_val() ); |
| 19 | $this->add_attribute( 'name', $this->block_type->get_field_name() ); |
| 20 | $this->add_attribute( 'data-field-name', $args['name'] ); |
| 21 | $this->add_attribute( 'id', $this->block_type->get_field_id( $args ) ); |
| 22 | $this->add_attribute( 'multiple', $this->block_type->is_multiple() ? 1 : '' ); |
| 23 | $this->add_attribute( |
| 24 | 'size', |
| 25 | $this->block_type->is_multiple() |
| 26 | ? $this->block_type->get_multiple_size() |
| 27 | : '' |
| 28 | ); |
| 29 | $this->add_attribute( 'data-jfb-sync' ); |
| 30 | |
| 31 | $placeholder = $args['placeholder'] ?? false; |
| 32 | $default = $args['default'] ?? false; |
| 33 | |
| 34 | $this->add_attribute( 'data-default-val', $default ); |
| 35 | ?> |
| 36 | <div class="jet-form-builder__field-wrap"> |
| 37 | <select <?php $this->render_attributes_string(); ?>> |
| 38 | <?php |
| 39 | |
| 40 | if ( $placeholder ) { |
| 41 | $additional_attrs = ( $args['is_disabled_placeholder'] ?? false ) ? 'disabled' : ''; |
| 42 | |
| 43 | if ( ! $default ) { |
| 44 | $additional_attrs .= ' selected'; |
| 45 | } |
| 46 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 47 | printf( '<option value="" %1$s>%2$s</option>', $additional_attrs, $placeholder ); |
| 48 | } |
| 49 | |
| 50 | if ( ! empty( $args['field_options'] ) ) { |
| 51 | |
| 52 | foreach ( $args['field_options'] as $value => $option ) { |
| 53 | |
| 54 | $selected = ''; |
| 55 | $calc = ''; |
| 56 | |
| 57 | if ( is_array( $option ) ) { |
| 58 | $val = $option['value']; |
| 59 | $label = $option['label']; |
| 60 | } else { |
| 61 | $val = $value; |
| 62 | $label = $option; |
| 63 | } |
| 64 | |
| 65 | $this->set_checked_option( $option ); |
| 66 | |
| 67 | if ( ! empty( $option['selected'] ) ) { |
| 68 | $selected = 'selected="selected"'; |
| 69 | } |
| 70 | |
| 71 | if ( is_array( $option ) && isset( $option['calculate'] ) ) { |
| 72 | $calc = ' data-calculate="' . esc_attr( $option['calculate'] ) . '"'; |
| 73 | } |
| 74 | |
| 75 | printf( '<option value="%1$s" %3$s%4$s>%2$s</option>', $val, $label, $selected, $calc ); |
| 76 | |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | ?> |
| 81 | </select> |
| 82 | </div> |
| 83 | <?php // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 84 |