dropdown-markup.php
| 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 | * Static counter for generating unique dropdown instances. |
| 60 | * |
| 61 | * @var int |
| 62 | * @since 1.10.0 |
| 63 | */ |
| 64 | private static $instance_counter = 0; |
| 65 | |
| 66 | /** |
| 67 | * Initialize the properties based on block attributes. |
| 68 | * |
| 69 | * @param array<mixed> $attributes Block attributes. |
| 70 | * @since 0.0.2 |
| 71 | */ |
| 72 | public function __construct( $attributes ) { |
| 73 | $this->slug = 'dropdown'; |
| 74 | $this->set_properties( $attributes ); |
| 75 | $this->set_input_label( __( 'Dropdown', 'sureforms' ) ); |
| 76 | $this->set_error_msg( $attributes, 'srfm_dropdown_block_required_text' ); |
| 77 | $this->multi_select_attr = ! empty( $attributes['multiSelect'] ) ? 'true' : 'false'; |
| 78 | $this->search_attr = ! empty( $attributes['searchable'] ) ? 'true' : 'false'; |
| 79 | $this->set_markup_properties(); |
| 80 | $this->set_aria_described_by(); |
| 81 | $this->set_label_as_placeholder( $this->input_label ); |
| 82 | $this->placeholder = ! empty( $this->placeholder_attr ) ? $this->label : __( 'Select an option', 'sureforms' ); |
| 83 | $this->show_values = apply_filters( 'srfm_show_options_values', false, $attributes['showValues'] ?? false ); |
| 84 | |
| 85 | // Generate unique instance identifier. |
| 86 | self::$instance_counter++; |
| 87 | $this->unique_slug = $this->slug . '-' . self::$instance_counter; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Render the sureforms dropdown classic styling |
| 92 | * |
| 93 | * @since 0.0.2 |
| 94 | * @return string|bool |
| 95 | */ |
| 96 | public function markup() { |
| 97 | $this->class_name = $this->get_field_classes(); |
| 98 | |
| 99 | ob_start(); ?> |
| 100 | <div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $this->class_name ); ?>"> |
| 101 | <fieldset> |
| 102 | <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=""/> |
| 103 | <legend class="srfm-block-legend"> |
| 104 | <?php echo wp_kses_post( $this->label_markup ); ?> |
| 105 | <?php echo wp_kses_post( $this->help_markup ); ?> |
| 106 | </legend> |
| 107 | <div class="srfm-block-wrap srfm-dropdown-common-wrap"> |
| 108 | <?php |
| 109 | if ( is_array( $this->options ) ) { |
| 110 | ?> |
| 111 | <select |
| 112 | class="srfm-dropdown-common srfm-<?php echo esc_attr( $this->slug ); ?>-input" |
| 113 | <?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?> |
| 114 | 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 ); ?>" tabindex="0" aria-hidden="true"> |
| 115 | <option class="srfm-dropdown-placeholder" value="" disabled selected><?php echo esc_html( $this->placeholder ); ?></option> |
| 116 | <?php foreach ( $this->options as $option ) { ?> |
| 117 | <?php |
| 118 | $icon_svg = Spec_Gb_Helper::render_svg_html( $option['icon'] ?? '', true ); |
| 119 | $escaped_icon_svg = htmlspecialchars( Helper::get_string_value( $icon_svg ), ENT_QUOTES, 'UTF-8' ); |
| 120 | ?> |
| 121 | <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 isset( $option['label'] ) ? esc_html( $option['label'] ) : ''; ?></option> |
| 122 | <?php |
| 123 | } |
| 124 | ?> |
| 125 | </select> |
| 126 | <?php } ?> |
| 127 | </div> |
| 128 | <div class="srfm-error-wrap"> |
| 129 | <?php echo wp_kses_post( $this->error_msg_markup ); ?> |
| 130 | </div> |
| 131 | </fieldset> |
| 132 | </div> |
| 133 | <?php |
| 134 | $markup = ob_get_clean(); |
| 135 | |
| 136 | return apply_filters( |
| 137 | 'srfm_block_field_markup', |
| 138 | $markup, |
| 139 | [ |
| 140 | 'slug' => $this->slug, |
| 141 | 'field_name' => $this->field_name, |
| 142 | 'is_editing' => $this->is_editing, |
| 143 | 'attributes' => $this->attributes, |
| 144 | ] |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Data attribute markup for min and max value |
| 150 | * |
| 151 | * @since 0.0.13 |
| 152 | * @return string |
| 153 | */ |
| 154 | protected function data_attribute_markup() { |
| 155 | $data_attr = ''; |
| 156 | if ( 'false' === $this->multi_select_attr ) { |
| 157 | return ''; |
| 158 | } |
| 159 | |
| 160 | if ( $this->min_selection ) { |
| 161 | $data_attr .= 'data-min-selection="' . esc_attr( $this->min_selection ) . '"'; |
| 162 | } |
| 163 | if ( $this->max_selection ) { |
| 164 | $data_attr .= 'data-max-selection="' . esc_attr( $this->max_selection ) . '"'; |
| 165 | } |
| 166 | |
| 167 | return $data_attr; |
| 168 | } |
| 169 | } |
| 170 |