register_block_from_metadata( 'radio' ); } public function render( $atts ) { if ( ! $this->allow_render_now( $atts ) ) { return ''; } $groupLabel = isset( $atts['label'] ) ? esc_html( $atts['label'] ) : 'Choose one'; $name = isset( $atts['name'] ) ? sanitize_key( $atts['name'] ) : 'radio_field'; $required= ! empty( $atts['required'] ); $disabled= ! empty( $atts['disabled'] ); $pos = isset( $atts['labelPosition'] ) ? $atts['labelPosition'] : 'top'; $layout = isset( $atts['layout'] ) && $atts['layout'] === 'vertical' ? 'vertical' : 'horizontal'; $help = isset( $atts['helpText'] ) ? esc_html( $atts['helpText'] ) : ''; $accent = isset( $atts['textColor'] ) ? trim( $atts['textColor'] ) : ''; $labelCol= isset( $atts['labelColor'] ) ? trim( $atts['labelColor'] ) : ''; $opts = 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; $opts[] = array( 'label' => $ol, 'value' => $ov !== '' ? $ov : sanitize_title( $ol ), 'disabled' => ! empty( $opt['disabled'] ), 'checked' => ! empty( $opt['checked'] ), ); } } $class = 'stacklar-field stacklar-field--radio'; if ( ! empty( $atts['className'] ) ) { $class .= ' ' . sanitize_html_class( $atts['className'] ); } $width = isset( $atts['width'] ) ? intval( $atts['width'] ) : 100; $style_rules = array(); $style_rules[] = '--stacklar-field-width:' . max(1, min(100, $width)) . '%;'; if ( ! empty( $atts['hidden'] ) ) { $style_rules[] = 'display:none;'; } $groupLabelStyle = $this->get_label_style( $atts ); $input_rules = $this->get_input_style_rules( $atts ); if ( $accent !== '' ) { $input_rules[] = 'accent-color:' . stacklar_sanitize_css_value( $accent ) . ';'; } $accentStyle = $this->build_style_attribute( $input_rules ); // Build options markup $items = array(); $first = true; foreach ( $opts as $idx => $opt ) { $oid = 'stacklar-radio-' . $name . '-' . $idx . '-' . wp_generate_password( 4, false, false ); $chk = $opt['checked'] ? 'checked' : ''; $dis = ( $opt['disabled'] || $disabled ) ? 'disabled' : ''; // HTML5: assigning required to one radio in the group is sufficient $req = ( $required && $first ) ? 'required' : ''; $first = false; $input = ''; $lbl = ''; $items[] = ( $pos === 'left' ) ? ( $lbl . ' ' . $input ) : ( $input . ' ' . $lbl ); } // If no options exist yet, render two sample choices if ( empty( $items ) ) { $oid1 = 'stacklar-radio-' . $name . '-demo-1'; $oid2 = 'stacklar-radio-' . $name . '-demo-2'; $req = $required ? 'required' : ''; $items[] = ( $pos === 'left' ) ? ' ' : ' '; $items[] = ( $pos === 'left' ) ? ' ' : ' '; } $container = $layout === 'vertical' ? '
' : ''; $labelHtml = ''; if ( $pos !== 'hidden' ) { $labelHtml = ''; } $contentHtml = $labelHtml . $container; if ( $pos === 'left' || $pos === 'right' ) { $contentHtml = '