← Back
Editing: form-base.php
<?php namespace ElementorPro\Modules\Forms\Classes; use Elementor\Modules\MarkdownRender\Html_To_Markdown; use Elementor\Icons_Manager; use Elementor\Utils; use ElementorPro\Base\Base_Widget; use ElementorPro\Base\Markdown_Utils; use ElementorPro\Modules\Forms\Module; if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } abstract class Form_Base extends Base_Widget { public function on_export( $element ) { /** @var \ElementorPro\Modules\Forms\Classes\Action_Base[] $actions */ $actions = Module::instance()->actions_registrar->get(); foreach ( $actions as $action ) { $new_element_data = $action->on_export( $element ); if ( null !== $new_element_data ) { $element = $new_element_data; } } return $element; } public static function get_button_sizes() { return [ 'xs' => esc_html__( 'Extra Small', 'elementor-pro' ), 'sm' => esc_html__( 'Small', 'elementor-pro' ), 'md' => esc_html__( 'Medium', 'elementor-pro' ), 'lg' => esc_html__( 'Large', 'elementor-pro' ), 'xl' => esc_html__( 'Extra Large', 'elementor-pro' ), ]; } protected function make_textarea_field( $item, $item_index ) { $this->add_render_attribute( 'textarea' . $item_index, [ 'class' => [ 'elementor-field-textual', 'elementor-field', esc_attr( $item['css_classes'] ), 'elementor-size-' . $item['input_size'], ], 'name' => $this->get_attribute_name( $item ), 'id' => $this->get_attribute_id( $item ), 'rows' => $item['rows'], ] ); if ( $item['placeholder'] ) { $this->add_render_attribute( 'textarea' . $item_index, 'placeholder', $item['placeholder'] ); } if ( $item['required'] ) { $this->add_render_attribute( 'textarea' . $item_index, 'required', 'required' ); } $value = empty( $item['field_value'] ) ? '' : $item['field_value']; return '<textarea ' . $this->get_render_attribute_string( 'textarea' . $item_index ) . '>' . $value . '</textarea>'; } protected function make_select_field( $item, $i ) { $this->add_render_attribute( [ 'select-wrapper' . $i => [ 'class' => [ 'elementor-field', 'elementor-select-wrapper', 'remove-before', esc_attr( $item['css_classes'] ), ], ], 'select' . $i => [ 'name' => $this->get_attribute_name( $item ) . ( ! empty( $item['allow_multiple'] ) ? '[]' : '' ), 'id' => $this->get_attribute_id( $item ), 'class' => [ 'elementor-field-textual', 'elementor-size-' . $item['input_size'], ], ], ] ); if ( $item['required'] ) { $this->add_render_attribute( 'select' . $i, 'required', 'required' ); } if ( $item['allow_multiple'] ) { $this->add_render_attribute( 'select' . $i, 'multiple' ); if ( ! empty( $item['select_size'] ) ) { $this->add_render_attribute( 'select' . $i, 'size', $item['select_size'] ); } } $options = preg_split( "/\\r\\n|\\r|\\n/", $item['field_options'] ); if ( ! $options ) { return ''; } ob_start(); ?> <div <?php $this->print_render_attribute_string( 'select-wrapper' . $i ); ?>> <div class="select-caret-down-wrapper"> <?php if ( ! $item['allow_multiple'] ) { $icon = [ 'library' => 'eicons', 'value' => 'eicon-caret-down', 'position' => 'right', ]; Icons_Manager::render_icon( $icon, [ 'aria-hidden' => 'true' ] ); } ?> </div> <select <?php $this->print_render_attribute_string( 'select' . $i ); ?>> <?php foreach ( $options as $key => $option ) { $option_id = esc_attr( $item['custom_id'] . $key ); $option_value = esc_attr( $option ); $option_label = esc_html( $option ); if ( false !== strpos( $option, '|' ) ) { list( $label, $value ) = explode( '|', $option ); $option_value = esc_attr( $value ); $option_label = esc_html( $label ); } $this->add_render_attribute( $option_id, 'value', $option_value ); // Support multiple selected values if ( ! empty( $item['field_value'] ) && in_array( $option_value, explode( ',', $item['field_value'] ) ) ) { $this->add_render_attribute( $option_id, 'selected', 'selected' ); } ?> <option <?php $this->print_render_attribute_string( $option_id ); ?>><?php // PHPCS - $option_label is already escaped echo $option_label; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></option> <?php } ?> </select> </div> <?php $select = ob_get_clean(); return $select; } protected function make_radio_checkbox_field( $item, $item_index, $type ) { $options = preg_split( "/\\r\\n|\\r|\\n/", $item['field_options'] ); $html = ''; if ( $options ) { $html .= '<div class="elementor-field-subgroup ' . esc_attr( $item['css_classes'] ) . ' ' . esc_attr( $item['inline_list'] ) . '">'; foreach ( $options as $key => $option ) { $element_id = esc_attr( $item['custom_id'] ) . $key; $html_id = $this->get_attribute_id( $item ) . '-' . $key; $option_label = $option; $option_value = $option; if ( false !== strpos( $option, '|' ) ) { list( $option_label, $option_value ) = explode( '|', $option ); } $this->add_render_attribute( $element_id, [ 'type' => $type, 'value' => $option_value, 'id' => $html_id, 'name' => $this->get_attribute_name( $item ) . ( ( 'checkbox' === $type && count( $options ) > 1 ) ? '[]' : '' ), ] ); if ( ! empty( $item['field_value'] ) && $option_value === $item['field_value'] ) { $this->add_render_attribute( $element_id, 'checked', 'checked' ); } if ( $item['required'] && 'radio' === $type ) { $this->add_render_attribute( $element_id, 'required', 'required' ); } $html .= '<span class="elementor-field-option"><input ' . $this->get_render_attribute_string( $element_id ) . '> <label for="' . $html_id . '">' . $option_label . '</label></span>'; } $html .= '</div>'; } return $html; } protected function form_fields_render_attributes( $i, $instance, $item ) { $this->add_render_attribute( [ 'field-group' . $i => [ 'class' => [ 'elementor-field-type-' . $item['field_type'], 'elementor-field-group', 'elementor-column', 'elementor-field-group-' . $item['custom_id'], ], ], 'input' . $i => [ 'type' => $item['field_type'], 'name' => $this->get_attribute_name( $item ), 'id' => $this->get_attribute_id( $item ), 'class' => [ 'elementor-field', 'elementor-size-' . $item['input_size'], empty( $item['css_classes'] ) ? '' : esc_attr( $item['css_classes'] ), ], ], 'label' . $i => [ 'for' => $this->get_attribute_id( $item ), 'class' => 'elementor-field-label', ], ] ); if ( empty( $item['width'] ) ) { $item['width'] = '100'; } $this->add_render_attribute( 'field-group' . $i, 'class', 'elementor-col-' . $item['width'] ); if ( ! empty( $item['width_tablet'] ) ) { $this->add_render_attribute( 'field-group' . $i, 'class', 'elementor-md-' . $item['width_tablet'] ); } if ( $item['allow_multiple'] ) { $this->add_render_attribute( 'field-group' . $i, 'class', 'elementor-field-type-' . $item['field_type'] . '-multiple' ); } if ( ! empty( $item['width_mobile'] ) ) { $this->add_render_attribute( 'field-group' . $i, 'class', 'elementor-sm-' . $item['width_mobile'] ); } // Allow zero as placeholder. if ( ! Utils::is_empty( $item['placeholder'] ) ) { $this->add_render_attribute( 'input' . $i, 'placeholder', $item['placeholder'] ); } if ( ! empty( $item['field_value'] ) ) { $this->add_render_attribute( 'input' . $i, 'value', $item['field_value'] ); } if ( ! $instance['show_labels'] ) { $this->add_render_attribute( 'label' . $i, 'class', 'elementor-screen-only' ); } if ( ! empty( $item['required'] ) ) { $class = 'elementor-field-required'; if ( ! empty( $instance['mark_required'] ) ) { $class .= ' elementor-mark-required'; } $this->add_render_attribute( 'field-group' . $i, 'class', $class ); $this->add_render_attribute( 'input' . $i, 'required', 'required' ); } } public function render_plain_content() {} public function render_markdown(): string { $settings = $this->get_settings_for_display(); $body_lines = $this->build_form_field_lines( $settings['form_fields'] ?? [] ); $button = Utils::html_to_plain_text( $settings['button_text'] ?? '' ); if ( empty( $body_lines ) && '' === $button ) { return ''; } $parts = []; $form_name = Utils::html_to_plain_text( $settings['form_name'] ?? '' ); if ( '' === $form_name ) { $form_name = Utils::html_to_plain_text( $this->get_title() ); } if ( '' !== $form_name ) { $parts[] = '## ' . $form_name; } if ( ! empty( $body_lines ) ) { $parts[] = implode( "\n\n", $body_lines ); } if ( '' !== $button ) { $parts[] = Markdown_Utils::button( $button ); } return implode( "\n\n", $parts ); } protected function build_form_field_lines( array $fields ): array { $lines = []; $bullet_buffer = []; foreach ( $fields as $field ) { $line = $this->get_form_field_markdown_line( $field ); if ( '' === $line ) { continue; } if ( 0 === strpos( $line, '### ' ) ) { if ( ! empty( $bullet_buffer ) ) { $lines[] = implode( "\n", $bullet_buffer ); $bullet_buffer = []; } $lines[] = $line; continue; } if ( 0 === strpos( $line, '- ' ) ) { $bullet_buffer[] = $line; continue; } if ( ! empty( $bullet_buffer ) ) { $lines[] = implode( "\n", $bullet_buffer ); $bullet_buffer = []; } $lines[] = $line; } if ( ! empty( $bullet_buffer ) ) { $lines[] = implode( "\n", $bullet_buffer ); } return $lines; } protected function get_form_field_markdown_line( array $field ): string { $type = $field['field_type'] ?? ''; if ( in_array( $type, $this->get_form_markdown_skip_field_types(), true ) ) { return ''; } if ( 'step' === $type ) { $label = $this->get_form_field_label( $field ); return '' !== $label ? '### ' . $label : ''; } if ( 'html' === $type ) { return trim( Html_To_Markdown::convert( $field['field_html'] ?? '' ) ); } $label = $this->get_form_field_label( $field ); if ( '' === $label ) { return ''; } $meta_parts = [ $type ]; if ( ! empty( $field['required'] ) ) { $meta_parts[] = 'required'; } $options = $this->get_form_field_options_summary( $field ); $meta = implode( ', ', $meta_parts ); if ( '' !== $options ) { $meta .= ': ' . $options; } return '- **' . $label . '** (' . $meta . ')'; } protected function get_form_field_label( array $field ): string { $label = Utils::html_to_plain_text( $field['field_label'] ?? '' ); if ( '' === $label ) { $label = Utils::html_to_plain_text( $field['placeholder'] ?? '' ); } if ( '' === $label ) { $label = (string) ( $field['field_type'] ?? '' ); } return $label; } protected function get_form_field_options_summary( array $field ): string { $type = $field['field_type'] ?? ''; if ( ! in_array( $type, [ 'select', 'radio', 'checkbox' ], true ) ) { return ''; } $options = preg_split( "/\r\n|\n|\r/", (string) ( $field['field_options'] ?? '' ) ); if ( empty( $options ) ) { return ''; } $labels = []; foreach ( $options as $option ) { $option_label = $option; if ( false !== strpos( $option, '|' ) ) { $option_label = explode( '|', $option )[0]; } $option_label = Utils::html_to_plain_text( $option_label ); if ( '' !== $option_label ) { $labels[] = $option_label; } } return implode( ', ', $labels ); } protected function get_form_markdown_skip_field_types(): array { return [ 'recaptcha', 'recaptcha_v3', 'hidden', 'honeypot' ]; } public function get_attribute_name( $item ) { return "form_fields[{$item['custom_id']}]"; } public function get_attribute_id( $item ) { return 'form-field-' . esc_attr( $item['custom_id'] ); } }
Save File
Cancel