register_block_from_metadata( 'select' );
}
public function render( $atts ) {
if ( ! $this->allow_render_now( $atts ) ) { return ''; }
$label = isset( $atts['label'] ) ? $atts['label'] : 'Select';
$name = isset( $atts['name'] ) ? $atts['name'] : 'select_field';
$name = sanitize_key( $name );
$required = ! empty( $atts['required'] );
$reqAttr = $this->get_field_boolean_attr( $atts, 'required', 'required' );
$disabled= $this->get_field_boolean_attr( $atts, 'disabled', 'disabled' );
$labelPos= $this->get_field_label_position( $atts );
$multiple= ! empty( $atts['multiple'] );
$size = isset( $atts['size'] ) && is_numeric( $atts['size'] ) ? intval( $atts['size'] ) : 0;
$options = array();
if ( ! empty( $atts['options'] ) && is_array( $atts['options'] ) ) {
foreach ( $atts['options'] as $opt ) {
$ol = isset( $opt['label'] ) ? trim( (string)$opt['label'] ) : '';
$ov = isset( $opt['value'] ) ? (string)$opt['value'] : '';
if ( $ol === '' ) continue;
$options[] = array(
'label' => $ol,
'value' => $ov !== '' ? $ov : sanitize_title( $ol ),
'disabled' => ! empty( $opt['disabled'] ),
'selected' => ! empty( $opt['selected'] ),
);
}
}
$class = $this->build_field_wrapper_class( 'select', $atts );
$style_rules = $this->get_field_wrapper_style_rules( $atts );
$help_context = $this->get_field_help_context( $atts, $name );
$ariaLabel = $this->get_field_aria_label_attr( $atts );
$ariaDesc = $help_context['aria_describedby'];
$id = 'stacklar-select-' . $name . '-' . wp_generate_password( 6, false, false );
$input_rules = $this->get_input_style_rules( $atts );
$inputStyle = $this->build_style_attribute( $input_rules );
$labelHtml = $this->render_field_label( $id, $label, $required, $labelPos, $atts );
$nameAttr = $multiple ? $name . '[]' : $name;
$multipleAttr = $multiple ? 'multiple' : '';
$sizeAttr = ($multiple && $size > 0) ? ' size="'. intval($size) .'"' : '';
// Build options HTML
$optsHtml = '';
$hasAnySelected = false;
foreach ( $options as $o ) { if ( $o['selected'] ) { $hasAnySelected = true; break; } }
if ( $atts['placeholder'] !== '' ) {
$selectedPlaceholder = !$hasAnySelected ? ' selected' : '';
$optsHtml .= '';
}
foreach ( $options as $o ) {
$sel = $o['selected'] ? ' selected' : '';
$dis = $o['disabled'] ? ' disabled' : '';
$optsHtml .= '';
}
$inputHtml = $this->render_input_wrap(
''
);
$contentHtml = $this->render_field_content( $labelHtml, $inputHtml, $labelPos, $help_context['help_html'] );
$html = $this->render_wrapper_open(
$atts,
$class,
$style_rules,
array(
'label' => '> label, .stacklar__inline > label',
'input' => 'select',
)
);
$html .= $contentHtml;
$html .= $this->render_wrapper_close();
return $html;
}
}