block-template.php
140 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 | // Add auto-update data attributes if configured |
| 32 | if ( ! empty( $args['_jfb_data_attrs'] ) ) { |
| 33 | foreach ( $args['_jfb_data_attrs'] as $attr_key => $attr_value ) { |
| 34 | $this->add_attribute( $attr_key, $attr_value ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | $placeholder = $args['placeholder'] ?? false; |
| 39 | $default = $args['default'] ?? false; |
| 40 | $has_dynamic_default = false; |
| 41 | $is_array_like_string = static function ( $value ) { |
| 42 | if ( ! is_string( $value ) ) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | $value = trim( $value ); |
| 47 | |
| 48 | return '[' === substr( $value, 0, 1 ) && ']' === substr( $value, -1 ); |
| 49 | }; |
| 50 | |
| 51 | if ( |
| 52 | is_string( $default ) && ( |
| 53 | jet_form_builder()->regexp->has_macro( $default ) || |
| 54 | $is_array_like_string( $default ) |
| 55 | ) |
| 56 | ) { |
| 57 | $has_dynamic_default = true; |
| 58 | wp_enqueue_script( \Jet_Form_Builder\Blocks\Dynamic_Value::HANDLE ); |
| 59 | $this->add_attribute( 'data-default-val', $default ); |
| 60 | } elseif ( is_array( $default ) ) { |
| 61 | $normalized_default = array_values( $default ); |
| 62 | $single_default = $normalized_default[0] ?? ''; |
| 63 | |
| 64 | if ( |
| 65 | 1 === count( $normalized_default ) && |
| 66 | is_string( $single_default ) && |
| 67 | '[' === substr( trim( $single_default ), 0, 1 ) |
| 68 | ) { |
| 69 | $dynamic_default = $single_default; |
| 70 | } else { |
| 71 | $dynamic_default = wp_json_encode( $normalized_default ); |
| 72 | } |
| 73 | |
| 74 | if ( |
| 75 | is_string( $dynamic_default ) && ( |
| 76 | jet_form_builder()->regexp->has_macro( $dynamic_default ) || |
| 77 | $is_array_like_string( $dynamic_default ) |
| 78 | ) |
| 79 | ) { |
| 80 | $has_dynamic_default = true; |
| 81 | wp_enqueue_script( \Jet_Form_Builder\Blocks\Dynamic_Value::HANDLE ); |
| 82 | $this->add_attribute( 'data-default-val', $dynamic_default ); |
| 83 | } |
| 84 | } |
| 85 | ?> |
| 86 | <div class="jet-form-builder__field-wrap"> |
| 87 | <select <?php $this->render_attributes_string(); ?>> |
| 88 | <?php |
| 89 | |
| 90 | if ( $placeholder ) { |
| 91 | $additional_attrs = ( $args['is_disabled_placeholder'] ?? false ) ? 'disabled' : ''; |
| 92 | |
| 93 | if ( |
| 94 | ! $default || |
| 95 | ( |
| 96 | $has_dynamic_default && |
| 97 | ! $this->block_type->is_multiple() |
| 98 | ) |
| 99 | ) { |
| 100 | $additional_attrs .= ' selected'; |
| 101 | } |
| 102 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 103 | printf( '<option value="" %1$s>%2$s</option>', $additional_attrs, $placeholder ); |
| 104 | } |
| 105 | |
| 106 | if ( ! empty( $args['field_options'] ) ) { |
| 107 | |
| 108 | foreach ( $args['field_options'] as $value => $option ) { |
| 109 | |
| 110 | $selected = ''; |
| 111 | $calc = ''; |
| 112 | |
| 113 | if ( is_array( $option ) ) { |
| 114 | $val = $option['value']; |
| 115 | $label = $option['label']; |
| 116 | } else { |
| 117 | $val = $value; |
| 118 | $label = $option; |
| 119 | } |
| 120 | |
| 121 | $this->set_checked_option( $option ); |
| 122 | |
| 123 | if ( ! empty( $option['selected'] ) ) { |
| 124 | $selected = 'selected="selected"'; |
| 125 | } |
| 126 | |
| 127 | if ( is_array( $option ) && isset( $option['calculate'] ) ) { |
| 128 | $calc = ' data-calculate="' . esc_attr( $option['calculate'] ) . '"'; |
| 129 | } |
| 130 | |
| 131 | printf( '<option value="%1$s" %3$s%4$s>%2$s</option>', $val, $label, $selected, $calc ); |
| 132 | |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | ?> |
| 137 | </select> |
| 138 | </div> |
| 139 | <?php // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 140 |