PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.7.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.7.0
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / fields / dropdown-markup.php
dropdown-markup.php
211 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sureforms Dropdown Markup Class file.
4 *
5 * @package sureforms.
6 * @since 0.0.1
7 */
8
9 namespace SRFM\Inc\Fields;
10
11 require SRFM_DIR . 'modules/gutenberg/classes/class-spec-gb-helper.php';
12
13 use Spec_Gb_Helper;
14 use SRFM\Inc\Helper;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 /**
21 * Sureforms Dropdown Markup Class.
22 *
23 * @since 0.0.1
24 */
25 class Dropdown_Markup extends Base {
26 /**
27 * Unique instance identifier for this dropdown.
28 *
29 * @var string
30 * @since 1.10.0
31 */
32 protected $unique_slug;
33
34 /**
35 * Stores the multi select attribute value.
36 *
37 * @var string
38 * @since 0.0.7
39 */
40 protected $multi_select_attr;
41
42 /**
43 * Stores the search attribute value.
44 *
45 * @var string
46 * @since 0.0.7
47 */
48 protected $search_attr;
49
50 /**
51 * Flag indicating if the value should be shown.
52 *
53 * @var bool
54 * @since 1.5.0
55 */
56 protected $show_values;
57
58 /**
59 * Array of preselected option indices.
60 *
61 * @var array
62 * @since 2.3.0
63 */
64 protected $preselected_options;
65
66 /**
67 * Static counter for generating unique dropdown instances.
68 *
69 * @var int
70 * @since 1.10.0
71 */
72 private static $instance_counter = 0;
73
74 /**
75 * Initialize the properties based on block attributes.
76 *
77 * @param array<mixed> $attributes Block attributes.
78 * @since 0.0.2
79 */
80 public function __construct( $attributes ) {
81 $this->slug = 'dropdown';
82 $this->set_properties( $attributes );
83 $this->set_input_label( __( 'Dropdown', 'sureforms' ) );
84 $this->set_error_msg( $attributes, 'srfm_dropdown_block_required_text' );
85 $this->multi_select_attr = ! empty( $attributes['multiSelect'] ) ? 'true' : 'false';
86 $this->search_attr = ! empty( $attributes['searchable'] ) ? 'true' : 'false';
87 $this->preselected_options = $attributes['preselectedOptions'] ?? [];
88 $this->set_markup_properties();
89 $this->set_aria_described_by();
90
91 // Store the original placeholder before set_label_as_placeholder modifies placeholder_attr.
92 $original_placeholder = $this->placeholder;
93 $this->set_label_as_placeholder( $this->input_label );
94
95 // For dropdowns, use the appropriate placeholder text:
96 // 1. If label is being used as placeholder (label_markup is empty), use the label.
97 // 2. Otherwise, use the block's placeholder value.
98 if ( empty( $this->label_markup ) ) {
99 $this->placeholder = $this->label;
100 } elseif ( ! empty( $original_placeholder ) ) {
101 $this->placeholder = $original_placeholder;
102 }
103
104 // Translate the default placeholder text for frontend display.
105 if ( 'Select an option' === $this->placeholder ) {
106 $this->placeholder = __( 'Select an option', 'sureforms' );
107 }
108
109 $this->show_values = apply_filters( 'srfm_show_options_values', false, $attributes['showValues'] ?? false );
110
111 // Generate unique instance identifier.
112 self::$instance_counter++;
113 $this->unique_slug = $this->slug . '-' . self::$instance_counter;
114 }
115
116 /**
117 * Render the sureforms dropdown classic styling
118 *
119 * @since 0.0.2
120 * @return string|bool
121 */
122 public function markup() {
123 $this->class_name = $this->get_field_classes();
124
125 ob_start(); ?>
126 <div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $this->class_name ); ?>">
127 <fieldset>
128 <input class="srfm-input-<?php echo esc_attr( $this->slug ); ?>-hidden" data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" aria-required="<?php echo esc_attr( $this->data_require_attr ); ?>" <?php echo wp_kses_post( $this->data_attribute_markup() ); ?> name="srfm-<?php echo esc_attr( $this->unique_slug ); ?>-<?php echo esc_attr( $this->block_id ); ?><?php echo esc_attr( $this->field_name ); ?>" type="hidden" value=""/>
129 <legend class="srfm-block-legend">
130 <?php echo wp_kses_post( $this->label_markup ); ?>
131 <?php echo wp_kses_post( $this->help_markup ); ?>
132 </legend>
133 <div class="srfm-block-wrap srfm-dropdown-common-wrap">
134 <?php
135 if ( is_array( $this->options ) ) {
136 ?>
137 <select
138 class="srfm-dropdown-common srfm-<?php echo esc_attr( $this->slug ); ?>-input"
139 <?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?>
140 data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" aria-required="<?php echo esc_attr( $this->data_require_attr ); ?>" <?php echo wp_kses_post( $this->data_attribute_markup() ); ?> name="srfm-<?php echo esc_attr( $this->unique_slug ); ?>-<?php echo esc_attr( $this->block_id ); ?><?php echo esc_attr( $this->field_name ); ?>" data-multiple="<?php echo esc_attr( $this->multi_select_attr ); ?>" data-searchable="<?php echo esc_attr( $this->search_attr ); ?>"
141 <?php
142 if ( ! empty( $this->preselected_options ) ) {
143 $preselected_labels = array_map(
144 function( $i ) {
145 $option = $this->options[ $i ] ?? [];
146 return is_array( $option ) ? ( $option['label'] ?? '' ) : '';
147 },
148 $this->preselected_options
149 );
150 $json_encoded = wp_json_encode( $preselected_labels );
151 echo 'data-preselected="' . esc_attr( false !== $json_encoded ? $json_encoded : '' ) . '"';
152 }
153 ?>
154 tabindex="0" aria-hidden="true">
155 <option class="srfm-dropdown-placeholder" value="" disabled <?php echo empty( $this->preselected_options ) ? 'selected' : ''; ?>><?php echo esc_html( $this->placeholder ); ?></option>
156 <?php foreach ( $this->options as $i => $option ) { ?>
157 <?php
158 $icon_svg = Spec_Gb_Helper::render_svg_html( $option['icon'] ?? '', true );
159 $escaped_icon_svg = htmlspecialchars( Helper::get_string_value( $icon_svg ), ENT_QUOTES, 'UTF-8' );
160 $is_preselected = is_array( $this->preselected_options ) && in_array( $i, $this->preselected_options, true );
161 ?>
162 <option value="<?php echo isset( $option['label'] ) ? esc_html( $option['label'] ) : ''; ?>" data-icon="<?php echo ! empty( $escaped_icon_svg ) ? esc_attr( $escaped_icon_svg ) : ''; ?>" <?php echo $this->show_values && isset( $option['value'] ) ? 'option-value="' . esc_attr( $option['value'] ) . '"' : ''; ?> <?php echo $is_preselected ? 'selected' : ''; ?>><?php echo isset( $option['label'] ) ? esc_html( $option['label'] ) : ''; ?></option>
163 <?php
164 }
165 ?>
166 </select>
167 <?php } ?>
168 </div>
169 <div class="srfm-error-wrap">
170 <?php echo wp_kses_post( $this->error_msg_markup ); ?>
171 </div>
172 </fieldset>
173 </div>
174 <?php
175 $markup = ob_get_clean();
176
177 return apply_filters(
178 'srfm_block_field_markup',
179 $markup,
180 [
181 'slug' => $this->slug,
182 'field_name' => $this->field_name,
183 'is_editing' => $this->is_editing,
184 'attributes' => $this->attributes,
185 ]
186 );
187 }
188
189 /**
190 * Data attribute markup for min and max value
191 *
192 * @since 0.0.13
193 * @return string
194 */
195 protected function data_attribute_markup() {
196 $data_attr = '';
197 if ( 'false' === $this->multi_select_attr ) {
198 return '';
199 }
200
201 if ( $this->min_selection ) {
202 $data_attr .= 'data-min-selection="' . esc_attr( $this->min_selection ) . '"';
203 }
204 if ( $this->max_selection ) {
205 $data_attr .= 'data-max-selection="' . esc_attr( $this->max_selection ) . '"';
206 }
207
208 return $data_attr;
209 }
210 }
211