$attributes Block attributes. * @since 0.0.2 */ public function __construct( $attributes ) { $this->slug = 'multi-choice'; $this->set_properties( $attributes ); $this->set_input_label( __( 'Multi Choice', 'sureforms' ) ); $this->set_error_msg( $attributes, 'srfm_multi_choice_block_required_text' ); $this->single_selection = $attributes['singleSelection'] ?? false; $this->choice_width = $attributes['choiceWidth'] ?? ''; $this->vertical_layout = $attributes['verticalLayout'] ?? false; $this->option_type = ! empty( $attributes['optionType'] ) && in_array( $attributes['optionType'], [ 'icon', 'image' ], true ) ? $attributes['optionType'] : 'icon'; $this->type_attr = $this->single_selection ? 'radio' : 'checkbox'; $this->svg_type = $this->single_selection ? 'circle' : 'square'; $this->name_attr = $this->single_selection ? 'name="srfm-input-' . esc_attr( $this->slug ) . '-' . esc_attr( $this->block_id ) . '"' : ''; $this->choice_width_attr = $this->choice_width ? 'srfm-choice-width-' . str_replace( '.', '-', $this->choice_width ) : ''; $this->show_values = apply_filters( 'srfm_show_options_values', false, $attributes['showValues'] ?? false ); $this->preselected_options = $attributes['preselectedOptions'] ?? []; $this->resolve_dynamic_default( $attributes ); $this->set_markup_properties(); $this->set_aria_described_by(); } /** * Render the sureforms Multichoice classic styling * * @since 0.0.2 * @return string|bool */ public function markup() { $check_svg = Helper::fetch_svg( $this->svg_type . '-checked', 'srfm-' . $this->slug . '-icon', 'aria-hidden="true"' ); $unchecked_svg = Helper::fetch_svg( $this->svg_type . '-unchecked', 'srfm-' . $this->slug . '-icon-unchecked', 'aria-hidden="true"' ); $this->class_name = $this->get_field_classes( [ "srfm-{$this->type_attr}-mode" ] ); $allowed_tags_svg = Helper::$allowed_tags_svg; ob_start(); ?>
data_attribute_markup() ); ?> name="srfm-input-slug ); ?>-block_id ); ?>field_name ); ?>" type="hidden" value=""/> label_markup ); ?> help_markup ); ?> options ) ) { ?>
options as $i => $option ) { ?>
name_attr ); ?> aria_described_by ) ? esc_attr( trim( $this->aria_described_by ) ) : '' ) . '"' : ''; ?> data_require_attr ) ? esc_attr( $this->data_require_attr ) : '' ) . '"' : ''; ?> show_values && isset( $option['value'] ) ? 'option-value="' . esc_attr( $option['value'] ) . '"' : ''; ?> preselected_options ) && in_array( $i, $this->preselected_options, true ) ? 'checked' : ''; ?> />
option_type && ! empty( $option['icon'] ) ) { ?> option_type && ! empty( $option['image'] ) ) { ?>
error_msg_markup ); ?>
$this->slug, 'is_editing' => $this->is_editing, 'field_name' => $this->field_name, 'attributes' => $this->attributes, ] ); } /** * Resolve dynamic default value from smart tags and override preselected options. * * @param array $attributes Block attributes. * @since 2.8.1 * @return void */ protected function resolve_dynamic_default( $attributes ) { $dynamic_default = is_string( $attributes['dynamicDefaultValue'] ?? '' ) ? $attributes['dynamicDefaultValue'] : ''; if ( empty( $dynamic_default ) || ! $this->single_selection ) { return; } $smart_tags = new Smart_Tags(); $resolved = $smart_tags->process_smart_tags( $dynamic_default ); if ( empty( $resolved ) || ! is_string( $resolved ) ) { return; } $resolved = trim( $resolved ); if ( is_array( $this->options ) ) { foreach ( $this->options as $i => $option ) { $option_title = $option['optionTitle'] ?? ''; if ( strcasecmp( $option_title, $resolved ) === 0 ) { $this->preselected_options = [ $i ]; return; } } } } /** * Data attribute markup for min and max value * * @since 0.0.13 * @return string */ protected function data_attribute_markup() { $data_attr = ''; if ( $this->single_selection ) { return ''; } if ( $this->min_selection ) { $data_attr .= 'data-min-selection="' . esc_attr( $this->min_selection ) . '"'; } if ( $this->max_selection ) { $data_attr .= 'data-max-selection="' . esc_attr( $this->max_selection ) . '"'; } return $data_attr; } }