register_block_from_metadata( 'checkbox' ); } public function render( $atts ) { if ( ! $this->allow_render_now( $atts ) ) { return ''; } $name = isset( $atts['name'] ) ? sanitize_key( $atts['name'] ) : 'checkbox_field'; $id_base = 'stacklar-checkbox-' . $name . '-' . wp_generate_password( 6, false, false ); $required= ! empty( $atts['required'] ); $disabled= ! empty( $atts['disabled'] ) ? 'disabled' : ''; $pos = isset( $atts['labelPosition'] ) ? $atts['labelPosition'] : 'right'; $layout = isset( $atts['layout'] ) && $atts['layout'] === 'vertical' ? 'vertical' : 'horizontal'; $help = isset( $atts['helpText'] ) ? esc_html( $atts['helpText'] ) : ''; $textCol = isset( $atts['textColor'] ) ? trim( $atts['textColor'] ) : ''; $labelCol= isset( $atts['labelColor'] ) ? trim( $atts['labelColor'] ) : ''; $options = array(); if ( ! empty( $atts['options'] ) && is_array( $atts['options'] ) ) { foreach ( $atts['options'] as $opt ) { $opt_label = isset( $opt['label'] ) ? trim( (string) $opt['label'] ) : ''; $opt_value = isset( $opt['value'] ) ? (string) $opt['value'] : ''; if ( $opt_label === '' ) continue; $options[] = array( 'label' => $opt_label, 'value' => $opt_value !== '' ? $opt_value : sanitize_title( $opt_label ), 'checked' => ! empty( $opt['checked'] ), 'disabled' => ! empty( $opt['disabled'] ), ); } } $class = 'stacklar-field stacklar-field--checkbox'; 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;'; } $labelStyle = $this->get_label_style( $atts ); $input_rules = $this->get_input_style_rules( $atts ); if ( $textCol !== '' ) { $input_rules[] = 'accent-color:' . stacklar_sanitize_css_value( $textCol ) . ';'; } $inputStyleAttr = $this->build_style_attribute( $input_rules ); // Render if ( empty( $options ) ) { $options = array( array( 'label' => 'Option 1', 'value' => 'opt_1', 'checked' => false, 'disabled' => false ), array( 'label' => 'Option 2', 'value' => 'opt_2', 'checked' => false, 'disabled' => false ), ); } // Multiple options: use name[] to capture multiple values $name_attr = $name . '[]'; $required_attrs = $required ? ' data-stacklar-checkbox-required="1" aria-required="true"' : ''; $items = array(); foreach ( $options as $idx => $opt ) { $oid = $id_base . '-' . $idx; $chk = $opt['checked'] ? 'checked' : ''; $dis = $opt['disabled'] ? 'disabled' : ''; $input = ''; $lbl = ''; $items[] = ( $pos === 'left' ) ? ( $lbl . ' ' . $input ) : ( $input . ' ' . $lbl ); } $content = $layout === 'vertical' ? '
' . implode( '', array_map( function($it){ return '
'. $it .'
'; }, $items ) ) . '
' : '
' . implode( '', array_map( function($it){ return ''. $it .''; }, $items ) ) . '
'; if ( $help !== '' ) { $content .= ''. $help .''; } $contentHtml = '
'. $content .'
'; $html = $this->render_wrapper_open( $atts, $class, $style_rules, array( 'label' => 'label', ), array( array( 'selector' => 'input[type="checkbox"]', 'attr' => 'fieldTypography', 'type' => 'typography', 'include_desktop' => true, 'important' => true, ), array( 'selector' => 'input[type="checkbox"]', 'attr' => 'padding', 'type' => 'box', 'property' => 'padding', 'include_desktop' => true, 'important' => true, ), array( 'selector' => 'input[type="checkbox"]', 'attr' => 'margin', 'type' => 'box', 'property' => 'margin', 'include_desktop' => true, 'important' => true, ), array( 'selector' => 'input[type="checkbox"]', 'attr' => 'textColor', 'type' => 'string', 'property' => 'accent-color', 'include_desktop' => true, 'important' => true, ), ) ); $html .= $contentHtml; $html .= $this->render_wrapper_close(); return $html; } }