| 1 |
<?php |
| 2 |
namespace HelloPlus\Modules\Forms\Classes; |
| 3 |
|
| 4 |
use Elementor\Utils; |
| 5 |
use Elementor\Widget_Base; |
| 6 |
use Elementor\Icons_Manager; |
| 7 |
use HelloPlus\Modules\Theme\Module as Theme_Module; |
| 8 |
use HelloPlus\Modules\Forms\Module; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
abstract class Form_Base extends Widget_Base { |
| 15 |
|
| 16 |
public function on_export( $element ) { |
| 17 |
/** @var \HelloPlus\Modules\Forms\Classes\Action_Base[] $actions */ |
| 18 |
$actions = Module::instance()->actions_registrar->get(); |
| 19 |
|
| 20 |
foreach ( $actions as $action ) { |
| 21 |
$new_element_data = $action->on_export( $element ); |
| 22 |
if ( null !== $new_element_data ) { |
| 23 |
$element = $new_element_data; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return $element; |
| 28 |
} |
| 29 |
|
| 30 |
public static function get_button_sizes(): array { |
| 31 |
return [ |
| 32 |
'xs' => esc_html__( 'Extra Small', 'hello-plus' ), |
| 33 |
'sm' => esc_html__( 'Small', 'hello-plus' ), |
| 34 |
'md' => esc_html__( 'Medium', 'hello-plus' ), |
| 35 |
'lg' => esc_html__( 'Large', 'hello-plus' ), |
| 36 |
'xl' => esc_html__( 'Extra Large', 'hello-plus' ), |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public function make_textarea_field( $item, $item_index, $instance ): string { |
| 41 |
$this->add_render_attribute( 'textarea' . $item_index, [ |
| 42 |
'class' => [ |
| 43 |
'elementor-field-textual', |
| 44 |
'ehp-form__field', |
| 45 |
'eph-form__textarea', |
| 46 |
esc_attr( $item['css_classes'] ), |
| 47 |
'elementor-size-' . $item['input_size'], |
| 48 |
], |
| 49 |
'name' => $this->get_attribute_name( $item ), |
| 50 |
'id' => $this->get_attribute_id( $item ), |
| 51 |
'rows' => $item['rows'], |
| 52 |
] ); |
| 53 |
|
| 54 |
if ( $item['placeholder'] ) { |
| 55 |
$this->add_render_attribute( 'textarea' . $item_index, 'placeholder', $item['placeholder'] ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( $item['required'] ) { |
| 59 |
$this->add_required_attribute( 'textarea' . $item_index ); |
| 60 |
} |
| 61 |
|
| 62 |
$value = empty( $item['field_value'] ) ? '' : $item['field_value']; |
| 63 |
|
| 64 |
return '<textarea ' . $this->get_render_attribute_string( 'textarea' . $item_index ) . '>' . $value . '</textarea>'; |
| 65 |
} |
| 66 |
|
| 67 |
public function make_select_field( $item, $i ) { |
| 68 |
$this->add_render_attribute( |
| 69 |
[ |
| 70 |
'select-wrapper' . $i => [ |
| 71 |
'class' => [ |
| 72 |
'ehp-form__select-wrapper', |
| 73 |
'remove-before', |
| 74 |
esc_attr( $item['css_classes'] ), |
| 75 |
], |
| 76 |
], |
| 77 |
'select' . $i => [ |
| 78 |
'name' => $this->get_attribute_name( $item ) . ( ! empty( $item['allow_multiple'] ) ? '[]' : '' ), |
| 79 |
'id' => $this->get_attribute_id( $item ), |
| 80 |
'class' => [ |
| 81 |
'ehp-form__field', |
| 82 |
'elementor-field', |
| 83 |
'elementor-field-textual', |
| 84 |
'ehp-form__select', |
| 85 |
'elementor-size-' . $item['input_size'], |
| 86 |
], |
| 87 |
], |
| 88 |
] |
| 89 |
); |
| 90 |
|
| 91 |
if ( $item['required'] ) { |
| 92 |
$this->add_required_attribute( 'select' . $i ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( $item['allow_multiple'] ) { |
| 96 |
$this->add_render_attribute( 'select' . $i, 'multiple' ); |
| 97 |
if ( ! empty( $item['select_size'] ) ) { |
| 98 |
$this->add_render_attribute( 'select' . $i, 'size', $item['select_size'] ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$options = preg_split( "/\\r\\n|\\r|\\n/", $item['field_options'] ); |
| 103 |
|
| 104 |
if ( ! $options ) { |
| 105 |
return ''; |
| 106 |
} |
| 107 |
|
| 108 |
ob_start(); |
| 109 |
?> |
| 110 |
<div <?php $this->print_render_attribute_string( 'select-wrapper' . $i ); ?>> |
| 111 |
<div class="select-caret-down-wrapper"> |
| 112 |
<?php |
| 113 |
if ( ! $item['allow_multiple'] ) { |
| 114 |
$icon = [ |
| 115 |
'library' => 'eicons', |
| 116 |
'value' => 'eicon-caret-down', |
| 117 |
'position' => 'right', |
| 118 |
]; |
| 119 |
Icons_Manager::render_icon( $icon, [ 'aria-hidden' => 'true' ] ); |
| 120 |
} |
| 121 |
?> |
| 122 |
</div> |
| 123 |
<select <?php $this->print_render_attribute_string( 'select' . $i ); ?>> |
| 124 |
<?php |
| 125 |
foreach ( $options as $key => $option ) { |
| 126 |
$option_id = esc_attr( $item['custom_id'] . $key ); |
| 127 |
$option_value = esc_attr( $option ); |
| 128 |
$option_label = $option; |
| 129 |
|
| 130 |
if ( false !== strpos( $option, '|' ) ) { |
| 131 |
list( $label, $value ) = explode( '|', $option ); |
| 132 |
$option_value = esc_attr( $value ); |
| 133 |
$option_label = $label; |
| 134 |
} |
| 135 |
|
| 136 |
$this->add_render_attribute( $option_id, 'value', $option_value ); |
| 137 |
|
| 138 |
// Support multiple selected values |
| 139 |
if ( ! empty( $item['field_value'] ) && in_array( $option_value, explode( ',', $item['field_value'] ), true ) ) { |
| 140 |
$this->add_render_attribute( $option_id, 'selected', 'selected' ); |
| 141 |
} ?> |
| 142 |
<option <?php $this->print_render_attribute_string( $option_id ); ?>><?php |
| 143 |
// PHPCS - $option_label is already escaped |
| 144 |
echo esc_html( $option_label ); ?></option> |
| 145 |
<?php } ?> |
| 146 |
</select> |
| 147 |
</div> |
| 148 |
<?php |
| 149 |
|
| 150 |
$select = ob_get_clean(); |
| 151 |
return $select; |
| 152 |
} |
| 153 |
|
| 154 |
protected function make_radio_checkbox_field( $item, $item_index, $type ): string { |
| 155 |
$options = preg_split( "/\\r\\n|\\r|\\n/", $item['field_options'] ); |
| 156 |
$html = ''; |
| 157 |
if ( $options ) { |
| 158 |
$html .= '<div class="elementor-field-subgroup ' . esc_attr( $item['css_classes'] ) . ' ' . esc_attr( $item['inline_list'] ) . '">'; |
| 159 |
foreach ( $options as $key => $option ) { |
| 160 |
$element_id = esc_attr( $item['custom_id'] ) . $key; |
| 161 |
$html_id = $this->get_attribute_id( $item ) . '-' . $key; |
| 162 |
$option_label = $option; |
| 163 |
$option_value = $option; |
| 164 |
if ( false !== strpos( $option, '|' ) ) { |
| 165 |
list( $option_label, $option_value ) = explode( '|', $option ); |
| 166 |
} |
| 167 |
|
| 168 |
$this->add_render_attribute( |
| 169 |
$element_id, |
| 170 |
[ |
| 171 |
'type' => $type, |
| 172 |
'value' => $option_value, |
| 173 |
'id' => $html_id, |
| 174 |
'name' => $this->get_attribute_name( $item ) . ( ( 'checkbox' === $type && count( $options ) > 1 ) ? '[]' : '' ), |
| 175 |
] |
| 176 |
); |
| 177 |
|
| 178 |
if ( ! empty( $item['field_value'] ) && $option_value === $item['field_value'] ) { |
| 179 |
$this->add_render_attribute( $element_id, 'checked', 'checked' ); |
| 180 |
} |
| 181 |
|
| 182 |
if ( $item['required'] && 'radio' === $type ) { |
| 183 |
$this->add_required_attribute( $element_id ); |
| 184 |
} |
| 185 |
|
| 186 |
$html .= '<span class="elementor-field-option"><input ' . $this->get_render_attribute_string( $element_id ) . '> <label for="' . $html_id . '">' . $option_label . '</label></span>'; |
| 187 |
} |
| 188 |
$html .= '</div>'; |
| 189 |
} |
| 190 |
|
| 191 |
return $html; |
| 192 |
} |
| 193 |
|
| 194 |
public function form_fields_render_attributes( $i, $instance, $item ) { |
| 195 |
$this->add_render_attribute( |
| 196 |
[ |
| 197 |
'field-group' . $i => [ |
| 198 |
'class' => [ |
| 199 |
'ehp-form__field-group', |
| 200 |
'is-field-type-' . $item['field_type'], |
| 201 |
'is-field-group-' . $item['custom_id'], |
| 202 |
], |
| 203 |
], |
| 204 |
'input' . $i => [ |
| 205 |
'type' => $item['field_type'], |
| 206 |
'name' => $this->get_attribute_name( $item ), |
| 207 |
'id' => $this->get_attribute_id( $item ), |
| 208 |
'class' => [ |
| 209 |
'ehp-form__field', |
| 210 |
'elementor-size-' . $item['input_size'], |
| 211 |
empty( $item['css_classes'] ) ? '' : esc_attr( $item['css_classes'] ), |
| 212 |
], |
| 213 |
], |
| 214 |
'label' . $i => [ |
| 215 |
'for' => $this->get_attribute_id( $item ), |
| 216 |
'class' => 'ehp-form__field-label', |
| 217 |
], |
| 218 |
] |
| 219 |
); |
| 220 |
|
| 221 |
if ( empty( $item['width'] ) ) { |
| 222 |
$item['width'] = '100'; |
| 223 |
} |
| 224 |
|
| 225 |
$this->add_render_attribute( 'field-group' . $i, 'class', 'has-width-' . $item['width'] ); |
| 226 |
|
| 227 |
if ( $item['allow_multiple'] ) { |
| 228 |
$this->add_render_attribute( 'field-group' . $i, 'class', 'elementor-field-type-' . $item['field_type'] . '-multiple' ); |
| 229 |
} |
| 230 |
|
| 231 |
if ( ! empty( $item['width_tablet'] ) ) { |
| 232 |
$this->add_render_attribute( 'field-group' . $i, 'class', 'has-width-md-' . $item['width_tablet'] ); |
| 233 |
} |
| 234 |
|
| 235 |
if ( ! empty( $item['width_mobile'] ) ) { |
| 236 |
$this->add_render_attribute( 'field-group' . $i, 'class', 'has-width-sm-' . $item['width_mobile'] ); |
| 237 |
} |
| 238 |
|
| 239 |
// Allow zero as placeholder. |
| 240 |
if ( ! Utils::is_empty( $item['placeholder'] ) ) { |
| 241 |
$this->add_render_attribute( 'input' . $i, 'placeholder', $item['placeholder'] ); |
| 242 |
} |
| 243 |
|
| 244 |
if ( ! empty( $item['field_value'] ) ) { |
| 245 |
$this->add_render_attribute( 'input' . $i, 'value', $item['field_value'] ); |
| 246 |
} |
| 247 |
|
| 248 |
if ( ! $instance['show_labels'] ) { |
| 249 |
$this->add_render_attribute( 'label' . $i, 'class', 'elementor-screen-only' ); |
| 250 |
} |
| 251 |
|
| 252 |
if ( ! empty( $item['required'] ) ) { |
| 253 |
$class = 'is-field-required'; |
| 254 |
if ( ! empty( $instance['mark_required'] ) ) { |
| 255 |
$class .= ' is-mark-required'; |
| 256 |
} |
| 257 |
$this->add_render_attribute( 'field-group' . $i, 'class', $class ); |
| 258 |
$this->add_required_attribute( 'input' . $i ); |
| 259 |
} |
| 260 |
|
| 261 |
if ( 'yes' === $instance['field_border_switcher'] ) { |
| 262 |
$this->add_render_attribute( 'field-group' . $i, 'class', 'has-border' ); |
| 263 |
} |
| 264 |
|
| 265 |
if ( ! empty( $instance['fields_shape'] ) ) { |
| 266 |
$this->add_render_attribute( 'field-group' . $i, 'class', 'has-shape-' . $instance['fields_shape'] ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
public function render_plain_content() {} |
| 271 |
|
| 272 |
public function get_attribute_name( $item ): string { |
| 273 |
return "form_fields[{$item['custom_id']}]"; |
| 274 |
} |
| 275 |
|
| 276 |
public function get_attribute_id( $item ): string { |
| 277 |
return 'form-field-' . esc_attr( $item['custom_id'] ); |
| 278 |
} |
| 279 |
|
| 280 |
private function add_required_attribute( $element ) { |
| 281 |
$this->add_render_attribute( $element, 'required', 'required' ); |
| 282 |
$this->add_render_attribute( $element, 'aria-required', 'true' ); |
| 283 |
} |
| 284 |
|
| 285 |
public function get_categories(): array { |
| 286 |
return [ Theme_Module::HELLOPLUS_EDITOR_CATEGORY_SLUG ]; |
| 287 |
} |
| 288 |
} |
| 289 |
|