| 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 |
use SRFM\Inc\Smart_Tags; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Sureforms Dropdown Markup Class. |
| 23 |
* |
| 24 |
* @since 0.0.1 |
| 25 |
*/ |
| 26 |
class Dropdown_Markup extends Base { |
| 27 |
/** |
| 28 |
* Unique instance identifier for this dropdown. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
* @since 1.10.0 |
| 32 |
*/ |
| 33 |
protected $unique_slug; |
| 34 |
|
| 35 |
/** |
| 36 |
* Stores the multi select attribute value. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
* @since 0.0.7 |
| 40 |
*/ |
| 41 |
protected $multi_select_attr; |
| 42 |
|
| 43 |
/** |
| 44 |
* Stores the search attribute value. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
* @since 0.0.7 |
| 48 |
*/ |
| 49 |
protected $search_attr; |
| 50 |
|
| 51 |
/** |
| 52 |
* Flag indicating if the value should be shown. |
| 53 |
* |
| 54 |
* @var bool |
| 55 |
* @since 1.5.0 |
| 56 |
*/ |
| 57 |
protected $show_values; |
| 58 |
|
| 59 |
/** |
| 60 |
* Array of preselected option indices. |
| 61 |
* |
| 62 |
* @var array |
| 63 |
* @since 2.3.0 |
| 64 |
*/ |
| 65 |
protected $preselected_options; |
| 66 |
|
| 67 |
/** |
| 68 |
* Static counter for generating unique dropdown instances. |
| 69 |
* |
| 70 |
* @var int |
| 71 |
* @since 1.10.0 |
| 72 |
*/ |
| 73 |
private static $instance_counter = 0; |
| 74 |
|
| 75 |
/** |
| 76 |
* Initialize the properties based on block attributes. |
| 77 |
* |
| 78 |
* @param array<mixed> $attributes Block attributes. |
| 79 |
* @since 0.0.2 |
| 80 |
*/ |
| 81 |
public function __construct( $attributes ) { |
| 82 |
$this->slug = 'dropdown'; |
| 83 |
$this->set_properties( $attributes ); |
| 84 |
$this->set_input_label( __( 'Dropdown', 'sureforms' ) ); |
| 85 |
$this->set_error_msg( $attributes, 'srfm_dropdown_block_required_text' ); |
| 86 |
$this->multi_select_attr = ! empty( $attributes['multiSelect'] ) ? 'true' : 'false'; |
| 87 |
$this->search_attr = ! empty( $attributes['searchable'] ) ? 'true' : 'false'; |
| 88 |
$this->preselected_options = $attributes['preselectedOptions'] ?? []; |
| 89 |
$this->resolve_dynamic_default( $attributes ); |
| 90 |
$this->set_markup_properties(); |
| 91 |
$this->set_aria_described_by(); |
| 92 |
|
| 93 |
// Store the original placeholder before set_label_as_placeholder modifies placeholder_attr. |
| 94 |
$original_placeholder = $this->placeholder; |
| 95 |
$this->set_label_as_placeholder( $this->input_label ); |
| 96 |
|
| 97 |
// For dropdowns, use the appropriate placeholder text: |
| 98 |
// 1. If label is being used as placeholder (label_markup is empty), use the label. |
| 99 |
// 2. Otherwise, use the block's placeholder value. |
| 100 |
if ( empty( $this->label_markup ) ) { |
| 101 |
$this->placeholder = $this->label; |
| 102 |
} elseif ( ! empty( $original_placeholder ) ) { |
| 103 |
$this->placeholder = $original_placeholder; |
| 104 |
} |
| 105 |
|
| 106 |
// Translate the default placeholder text for frontend display. |
| 107 |
if ( 'Select an option' === $this->placeholder ) { |
| 108 |
$this->placeholder = __( 'Select an option', 'sureforms' ); |
| 109 |
} |
| 110 |
|
| 111 |
$this->show_values = apply_filters( 'srfm_show_options_values', false, $attributes['showValues'] ?? false ); |
| 112 |
|
| 113 |
// Generate unique instance identifier. |
| 114 |
self::$instance_counter++; |
| 115 |
$this->unique_slug = $this->slug . '-' . self::$instance_counter; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Render the sureforms dropdown classic styling |
| 120 |
* |
| 121 |
* @since 0.0.2 |
| 122 |
* @return string|bool |
| 123 |
*/ |
| 124 |
public function markup() { |
| 125 |
$this->class_name = $this->get_field_classes(); |
| 126 |
|
| 127 |
ob_start(); ?> |
| 128 |
<div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $this->class_name ); ?>"> |
| 129 |
<fieldset> |
| 130 |
<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=""/> |
| 131 |
<legend class="srfm-block-legend"> |
| 132 |
<?php echo wp_kses_post( $this->label_markup ); ?> |
| 133 |
<?php echo wp_kses_post( $this->help_markup ); ?> |
| 134 |
</legend> |
| 135 |
<div class="srfm-block-wrap srfm-dropdown-common-wrap"> |
| 136 |
<?php |
| 137 |
if ( is_array( $this->options ) ) { |
| 138 |
?> |
| 139 |
<select |
| 140 |
class="srfm-dropdown-common srfm-<?php echo esc_attr( $this->slug ); ?>-input" |
| 141 |
<?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?> |
| 142 |
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 ); ?>" |
| 143 |
<?php |
| 144 |
if ( ! empty( $this->preselected_options ) ) { |
| 145 |
$preselected_labels = array_map( |
| 146 |
function( $i ) { |
| 147 |
$option = $this->options[ $i ] ?? []; |
| 148 |
return is_array( $option ) ? ( $option['label'] ?? '' ) : ''; |
| 149 |
}, |
| 150 |
$this->preselected_options |
| 151 |
); |
| 152 |
$json_encoded = wp_json_encode( $preselected_labels ); |
| 153 |
echo 'data-preselected="' . esc_attr( false !== $json_encoded ? $json_encoded : '' ) . '"'; |
| 154 |
} |
| 155 |
?> |
| 156 |
tabindex="0" aria-hidden="true"> |
| 157 |
<option class="srfm-dropdown-placeholder" value="" disabled <?php echo empty( $this->preselected_options ) ? 'selected' : ''; ?>><?php echo esc_html( $this->placeholder ); ?></option> |
| 158 |
<?php foreach ( $this->options as $i => $option ) { ?> |
| 159 |
<?php |
| 160 |
$icon_svg = Spec_Gb_Helper::render_svg_html( $option['icon'] ?? '', true ); |
| 161 |
$escaped_icon_svg = htmlspecialchars( Helper::get_string_value( $icon_svg ), ENT_QUOTES, 'UTF-8' ); |
| 162 |
$is_preselected = is_array( $this->preselected_options ) && in_array( $i, $this->preselected_options, true ); |
| 163 |
?> |
| 164 |
<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> |
| 165 |
<?php |
| 166 |
} |
| 167 |
?> |
| 168 |
</select> |
| 169 |
<?php } ?> |
| 170 |
</div> |
| 171 |
<div class="srfm-error-wrap"> |
| 172 |
<?php echo wp_kses_post( $this->error_msg_markup ); ?> |
| 173 |
</div> |
| 174 |
</fieldset> |
| 175 |
</div> |
| 176 |
<?php |
| 177 |
$markup = ob_get_clean(); |
| 178 |
|
| 179 |
return apply_filters( |
| 180 |
'srfm_block_field_markup', |
| 181 |
$markup, |
| 182 |
[ |
| 183 |
'slug' => $this->slug, |
| 184 |
'field_name' => $this->field_name, |
| 185 |
'is_editing' => $this->is_editing, |
| 186 |
'attributes' => $this->attributes, |
| 187 |
] |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Resolve dynamic default value from smart tags and override preselected options. |
| 193 |
* |
| 194 |
* @param array<mixed> $attributes Block attributes. |
| 195 |
* @since 2.8.1 |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
protected function resolve_dynamic_default( $attributes ) { |
| 199 |
$dynamic_default = is_string( $attributes['dynamicDefaultValue'] ?? '' ) ? $attributes['dynamicDefaultValue'] : ''; |
| 200 |
if ( empty( $dynamic_default ) || ! empty( $attributes['multiSelect'] ) ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
$smart_tags = new Smart_Tags(); |
| 205 |
$resolved = $smart_tags->process_smart_tags( $dynamic_default ); |
| 206 |
|
| 207 |
if ( empty( $resolved ) || ! is_string( $resolved ) ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
$resolved = trim( $resolved ); |
| 212 |
|
| 213 |
if ( is_array( $this->options ) ) { |
| 214 |
foreach ( $this->options as $i => $option ) { |
| 215 |
$option_label = is_array( $option ) ? ( $option['label'] ?? '' ) : ''; |
| 216 |
if ( strcasecmp( $option_label, $resolved ) === 0 ) { |
| 217 |
$this->preselected_options = [ $i ]; |
| 218 |
return; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Data attribute markup for min and max value |
| 226 |
* |
| 227 |
* @since 0.0.13 |
| 228 |
* @return string |
| 229 |
*/ |
| 230 |
protected function data_attribute_markup() { |
| 231 |
$data_attr = ''; |
| 232 |
if ( 'false' === $this->multi_select_attr ) { |
| 233 |
return ''; |
| 234 |
} |
| 235 |
|
| 236 |
if ( $this->min_selection ) { |
| 237 |
$data_attr .= 'data-min-selection="' . esc_attr( $this->min_selection ) . '"'; |
| 238 |
} |
| 239 |
if ( $this->max_selection ) { |
| 240 |
$data_attr .= 'data-max-selection="' . esc_attr( $this->max_selection ) . '"'; |
| 241 |
} |
| 242 |
|
| 243 |
return $data_attr; |
| 244 |
} |
| 245 |
} |
| 246 |
|