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 | * Stores the multi select attribute value. |
| 28 | * |
| 29 | * @var string |
| 30 | * @since 0.0.7 |
| 31 | */ |
| 32 | protected $multi_select_attr; |
| 33 | |
| 34 | /** |
| 35 | * Stores the search attribute value. |
| 36 | * |
| 37 | * @var string |
| 38 | * @since 0.0.7 |
| 39 | */ |
| 40 | protected $search_attr; |
| 41 | |
| 42 | /** |
| 43 | * Flag indicating if the value should be shown. |
| 44 | * |
| 45 | * @var bool |
| 46 | * @since 1.5.0 |
| 47 | */ |
| 48 | protected $show_values; |
| 49 | |
| 50 | /** |
| 51 | * Initialize the properties based on block attributes. |
| 52 | * |
| 53 | * @param array<mixed> $attributes Block attributes. |
| 54 | * @since 0.0.2 |
| 55 | */ |
| 56 | public function __construct( $attributes ) { |
| 57 | $this->slug = 'dropdown'; |
| 58 | $this->set_properties( $attributes ); |
| 59 | $this->set_input_label( __( 'Dropdown', 'sureforms' ) ); |
| 60 | $this->set_error_msg( $attributes, 'srfm_dropdown_block_required_text' ); |
| 61 | $this->multi_select_attr = ! empty( $attributes['multiSelect'] ) ? 'true' : 'false'; |
| 62 | $this->search_attr = ! empty( $attributes['searchable'] ) ? 'true' : 'false'; |
| 63 | $this->set_markup_properties(); |
| 64 | $this->set_aria_described_by(); |
| 65 | $this->set_label_as_placeholder( $this->input_label ); |
| 66 | $this->placeholder = ! empty( $this->placeholder_attr ) ? $this->label : __( 'Select an option', 'sureforms' ); |
| 67 | $this->show_values = apply_filters( 'srfm_show_options_values', false, $attributes['showValues'] ?? false ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Render the sureforms dropdown classic styling |
| 72 | * |
| 73 | * @since 0.0.2 |
| 74 | * @return string|bool |
| 75 | */ |
| 76 | public function markup() { |
| 77 | $this->class_name = $this->get_field_classes(); |
| 78 | |
| 79 | ob_start(); ?> |
| 80 | <div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $this->class_name ); ?>"> |
| 81 | <fieldset> |
| 82 | <input class="srfm-input-<?php echo esc_attr( $this->slug ); ?>-hidden" data-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->slug ); ?>-<?php echo esc_attr( $this->block_id ); ?><?php echo esc_attr( $this->field_name ); ?>" type="hidden" value=""/> |
| 83 | <legend class="srfm-block-legend"> |
| 84 | <?php echo wp_kses_post( $this->label_markup ); ?> |
| 85 | <?php echo wp_kses_post( $this->help_markup ); ?> |
| 86 | </legend> |
| 87 | <div class="srfm-block-wrap srfm-dropdown-common-wrap"> |
| 88 | <?php |
| 89 | if ( is_array( $this->options ) ) { |
| 90 | ?> |
| 91 | <select |
| 92 | class="srfm-dropdown-common srfm-<?php echo esc_attr( $this->slug ); ?>-input" |
| 93 | <?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?> |
| 94 | data-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->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"> |
| 95 | <option class="srfm-dropdown-placeholder" value="" disabled selected><?php echo esc_html( $this->placeholder ); ?></option> |
| 96 | <?php foreach ( $this->options as $option ) { ?> |
| 97 | <?php |
| 98 | $icon_svg = Spec_Gb_Helper::render_svg_html( $option['icon'] ?? '', true ); |
| 99 | $escaped_icon_svg = htmlspecialchars( Helper::get_string_value( $icon_svg ), ENT_QUOTES, 'UTF-8' ); |
| 100 | ?> |
| 101 | <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> |
| 102 | <?php |
| 103 | } |
| 104 | ?> |
| 105 | </select> |
| 106 | <?php } ?> |
| 107 | </div> |
| 108 | <div class="srfm-error-wrap"> |
| 109 | <?php echo wp_kses_post( $this->error_msg_markup ); ?> |
| 110 | </div> |
| 111 | </fieldset> |
| 112 | </div> |
| 113 | <?php |
| 114 | $markup = ob_get_clean(); |
| 115 | |
| 116 | return apply_filters( |
| 117 | 'srfm_block_field_markup', |
| 118 | $markup, |
| 119 | [ |
| 120 | 'slug' => $this->slug, |
| 121 | 'field_name' => $this->field_name, |
| 122 | 'is_editing' => $this->is_editing, |
| 123 | 'attributes' => $this->attributes, |
| 124 | ] |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Data attribute markup for min and max value |
| 130 | * |
| 131 | * @since 0.0.13 |
| 132 | * @return string |
| 133 | */ |
| 134 | protected function data_attribute_markup() { |
| 135 | $data_attr = ''; |
| 136 | if ( 'false' === $this->multi_select_attr ) { |
| 137 | return ''; |
| 138 | } |
| 139 | |
| 140 | if ( $this->min_selection ) { |
| 141 | $data_attr .= 'data-min-selection="' . esc_attr( $this->min_selection ) . '"'; |
| 142 | } |
| 143 | if ( $this->max_selection ) { |
| 144 | $data_attr .= 'data-max-selection="' . esc_attr( $this->max_selection ) . '"'; |
| 145 | } |
| 146 | |
| 147 | return $data_attr; |
| 148 | } |
| 149 | } |
| 150 |