| 1 |
<?php |
| 2 |
namespace HelloPlus\Modules\Forms\Classes\Render; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
use HelloPlus\Modules\Forms\Widgets\Ehp_Form; |
| 9 |
use HelloPlus\Includes\Utils; |
| 10 |
use Elementor\Icons_Manager; |
| 11 |
use Elementor\Utils as Elementor_Utils; |
| 12 |
|
| 13 |
class Widget_Form_Render { |
| 14 |
protected Ehp_Form $widget; |
| 15 |
protected array $settings; |
| 16 |
|
| 17 |
public function render() { |
| 18 |
$form_name = $this->settings['form_name']; |
| 19 |
|
| 20 |
if ( ! empty( $form_name ) ) { |
| 21 |
$this->widget->add_render_attribute( 'form', 'name', $form_name ); |
| 22 |
} |
| 23 |
|
| 24 |
$this->widget->add_render_attribute( 'wrapper', [ |
| 25 |
'class' => 'ehp-form__wrapper', |
| 26 |
] ); |
| 27 |
|
| 28 |
$referer_title = trim( wp_title( '', false ) ); |
| 29 |
|
| 30 |
if ( ! $referer_title && is_home() ) { |
| 31 |
$referer_title = get_option( 'blogname' ); |
| 32 |
} |
| 33 |
|
| 34 |
?> |
| 35 |
<form class="ehp-form" method="post" <?php $this->widget->print_render_attribute_string( 'form' ); ?>> |
| 36 |
<?php $this->render_text_container(); ?> |
| 37 |
<input type="hidden" name="post_id" value="<?php echo (int) Utils::get_current_post_id(); ?>"/> |
| 38 |
<input type="hidden" name="form_id" value="<?php echo esc_attr( $this->widget->get_id() ); ?>"/> |
| 39 |
<input type="hidden" name="referer_title" value="<?php echo esc_attr( $referer_title ); ?>"/> |
| 40 |
|
| 41 |
<?php if ( is_singular() ) { |
| 42 |
// `queried_id` may be different from `post_id` on Single theme builder templates. |
| 43 |
?> |
| 44 |
<input type="hidden" name="queried_id" value="<?php echo (int) get_the_ID(); ?>"/> |
| 45 |
<?php } ?> |
| 46 |
|
| 47 |
<div <?php $this->widget->print_render_attribute_string( 'wrapper' ); ?>> |
| 48 |
<?php |
| 49 |
foreach ( $this->settings['form_fields'] as $item_index => $item ) : |
| 50 |
$item['input_size'] = $this->settings['input_size']; |
| 51 |
$this->widget->form_fields_render_attributes( $item_index, $this->settings, $item ); |
| 52 |
|
| 53 |
$field_type = $item['field_type']; |
| 54 |
|
| 55 |
/** |
| 56 |
* Render form field. |
| 57 |
* |
| 58 |
* Filters the field rendered by Elementor forms. |
| 59 |
* |
| 60 |
* @param array $item The field value. |
| 61 |
* @param int $item_index The field index. |
| 62 |
* @param Ehp_Form $this An instance of the form. |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
* |
| 66 |
*/ |
| 67 |
$item = apply_filters( 'hello_plus/forms/render/item', $item, $item_index, $this ); |
| 68 |
|
| 69 |
/** |
| 70 |
* Render form field. |
| 71 |
* |
| 72 |
* Filters the field rendered by Elementor forms. |
| 73 |
* |
| 74 |
* The dynamic portion of the hook name, `$field_type`, refers to the field type. |
| 75 |
* |
| 76 |
* @param array $item The field value. |
| 77 |
* @param int $item_index The field index. |
| 78 |
* @param Ehp_Form $this An instance of the form. |
| 79 |
* |
| 80 |
* @since 1.0.0 |
| 81 |
* |
| 82 |
*/ |
| 83 |
$item = apply_filters( "hello_plus/forms/render/item/{$field_type}", $item, $item_index, $this ); |
| 84 |
|
| 85 |
$print_label = ! in_array( $item['field_type'], [ 'hidden', 'html', 'step' ], true ); |
| 86 |
?> |
| 87 |
<div <?php $this->widget->print_render_attribute_string( 'field-group' . $item_index ); ?>> |
| 88 |
<?php |
| 89 |
if ( $print_label && $item['field_label'] ) { |
| 90 |
?> |
| 91 |
<label <?php $this->widget->print_render_attribute_string( 'label' . $item_index ); ?>> |
| 92 |
<?php |
| 93 |
echo esc_html( $item['field_label'] ); ?> |
| 94 |
</label> |
| 95 |
<?php |
| 96 |
} |
| 97 |
|
| 98 |
switch ( $item['field_type'] ) : |
| 99 |
case 'textarea': |
| 100 |
echo wp_kses( |
| 101 |
$this->widget->make_textarea_field( $item, $item_index, $this->settings ), |
| 102 |
[ |
| 103 |
'textarea' => [ |
| 104 |
'cols' => true, |
| 105 |
'rows' => true, |
| 106 |
'name' => true, |
| 107 |
'id' => true, |
| 108 |
'class' => true, |
| 109 |
'style' => true, |
| 110 |
'placeholder' => true, |
| 111 |
'maxlength' => true, |
| 112 |
'required' => true, |
| 113 |
'readonly' => true, |
| 114 |
'disabled' => true, |
| 115 |
], |
| 116 |
] |
| 117 |
); |
| 118 |
break; |
| 119 |
|
| 120 |
case 'select': |
| 121 |
echo wp_kses( $this->widget->make_select_field( $item, $item_index ), [ |
| 122 |
'select' => [ |
| 123 |
'name' => true, |
| 124 |
'id' => true, |
| 125 |
'class' => true, |
| 126 |
'style' => true, |
| 127 |
'required' => true, |
| 128 |
'disabled' => true, |
| 129 |
], |
| 130 |
'option' => [ |
| 131 |
'value' => true, |
| 132 |
'selected' => true, |
| 133 |
], |
| 134 |
] ); |
| 135 |
break; |
| 136 |
|
| 137 |
case 'text': |
| 138 |
case 'email': |
| 139 |
$this->widget->add_render_attribute( 'input' . $item_index, 'class', 'elementor-field-textual' ); |
| 140 |
?> |
| 141 |
<input size="1" <?php $this->widget->print_render_attribute_string( 'input' . $item_index ); ?>> |
| 142 |
<?php |
| 143 |
break; |
| 144 |
|
| 145 |
default: |
| 146 |
$field_type = $item['field_type']; |
| 147 |
|
| 148 |
/** |
| 149 |
* Hello+ form field render. |
| 150 |
* |
| 151 |
* Fires when a field is rendered in the frontend. This hook allows developers to |
| 152 |
* add functionality when from fields are rendered. |
| 153 |
* |
| 154 |
* The dynamic portion of the hook name, `$field_type`, refers to the field type. |
| 155 |
* |
| 156 |
* @param array $item The field value. |
| 157 |
* @param int $item_index The field index. |
| 158 |
* @param Ehp_Form $this An instance of the form. |
| 159 |
* |
| 160 |
* @since 1.0.0 |
| 161 |
* |
| 162 |
*/ |
| 163 |
do_action( "hello_plus/forms/render_field/{$field_type}", $item, $item_index, $this->widget ); |
| 164 |
endswitch; |
| 165 |
?> |
| 166 |
</div> |
| 167 |
<?php endforeach; ?> |
| 168 |
<?php $this->render_button(); ?> |
| 169 |
</div> |
| 170 |
</form> |
| 171 |
<?php |
| 172 |
} |
| 173 |
|
| 174 |
protected function render_button(): void { |
| 175 |
$button_icon = $this->settings['selected_button_icon']; |
| 176 |
$button_text = $this->settings['button_text']; |
| 177 |
$button_css_id = $this->settings['button_css_id']; |
| 178 |
$button_width = $this->settings['button_width']; |
| 179 |
$button_width_tablet = $this->settings['button_width_tablet']; |
| 180 |
$button_width_mobile = $this->settings['button_width_mobile']; |
| 181 |
$button_hover_animation = $this->settings['button_hover_animation']; |
| 182 |
$button_classnames = 'ehp-form__button'; |
| 183 |
$button_border = $this->settings['button_border_switcher']; |
| 184 |
$button_corner_shape = $this->settings['button_shape']; |
| 185 |
$button_type = $this->settings['button_type']; |
| 186 |
|
| 187 |
$submit_group_classnames = 'ehp-form__submit-group'; |
| 188 |
|
| 189 |
if ( ! empty( $button_width ) ) { |
| 190 |
$submit_group_classnames .= ' has-width-' . $button_width; |
| 191 |
} |
| 192 |
|
| 193 |
if ( ! empty( $button_width_tablet ) ) { |
| 194 |
$submit_group_classnames .= ' has-width-md-' . $button_width_tablet; |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! empty( $button_width_mobile ) ) { |
| 198 |
$submit_group_classnames .= ' has-width-sm-' . $button_width_mobile; |
| 199 |
} |
| 200 |
|
| 201 |
$this->widget->add_render_attribute( 'submit-group', [ |
| 202 |
'class' => $submit_group_classnames, |
| 203 |
] ); |
| 204 |
|
| 205 |
if ( 'yes' === $button_border ) { |
| 206 |
$button_classnames .= ' has-border'; |
| 207 |
} |
| 208 |
|
| 209 |
if ( ! empty( $button_corner_shape ) ) { |
| 210 |
$button_classnames .= ' has-shape-' . $button_corner_shape; |
| 211 |
} |
| 212 |
|
| 213 |
if ( ! empty( $button_type ) ) { |
| 214 |
$button_classnames .= ' is-type-' . $button_type; |
| 215 |
} |
| 216 |
|
| 217 |
$this->widget->add_render_attribute( 'button', [ |
| 218 |
'class' => $button_classnames, |
| 219 |
'type' => 'submit', |
| 220 |
] ); |
| 221 |
|
| 222 |
if ( $button_hover_animation ) { |
| 223 |
$this->widget->add_render_attribute( 'button', 'class', 'elementor-animation-' . $button_hover_animation ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( ! empty( $button_css_id ) ) { |
| 227 |
$this->widget->add_render_attribute( 'button', 'id', $button_css_id ); |
| 228 |
} |
| 229 |
|
| 230 |
$this->widget->add_render_attribute( 'button-text', [ |
| 231 |
'class' => 'ehp-form__button-text', |
| 232 |
] ); |
| 233 |
|
| 234 |
?> |
| 235 |
<div <?php $this->widget->print_render_attribute_string( 'submit-group' ); ?>> |
| 236 |
<button <?php $this->widget->print_render_attribute_string( 'button' ); ?>> |
| 237 |
<?php if ( ! empty( $button_icon ) || ! empty( $button_icon['value'] ) ) : ?> |
| 238 |
<?php |
| 239 |
Icons_Manager::render_icon( $button_icon, |
| 240 |
[ |
| 241 |
'aria-hidden' => 'true', |
| 242 |
'class' => 'ehp-form__button-icon', |
| 243 |
], |
| 244 |
); |
| 245 |
?> |
| 246 |
<?php endif; ?> |
| 247 |
|
| 248 |
<?php if ( ! empty( $button_text ) ) : ?> |
| 249 |
<span <?php $this->widget->print_render_attribute_string( 'button-text' ); ?>><?php $this->widget->print_unescaped_setting( 'button_text' ); ?></span> |
| 250 |
<?php endif; ?> |
| 251 |
</button> |
| 252 |
</div> |
| 253 |
<?php |
| 254 |
} |
| 255 |
|
| 256 |
protected function render_text_container(): void { |
| 257 |
$heading_text = $this->settings['text_heading']; |
| 258 |
$has_heading = ! empty( $this->settings['text_heading'] ); |
| 259 |
$heading_tag = $this->settings['text_heading_tag']; |
| 260 |
|
| 261 |
$description_text = $this->settings['text_description']; |
| 262 |
$has_description = ! empty( $description_text ); |
| 263 |
?> |
| 264 |
<div class="ehp-form__text-container"> |
| 265 |
<?php if ( $has_heading ) { |
| 266 |
$heading_output = sprintf( '<%1$s %2$s>%3$s</%1$s>', Elementor_Utils::validate_html_tag( $heading_tag ), 'class="ehp-form__heading"', esc_html( $heading_text ) ); |
| 267 |
// Escaped above |
| 268 |
Elementor_Utils::print_unescaped_internal_string( $heading_output ); |
| 269 |
} ?> |
| 270 |
|
| 271 |
<?php if ( $has_description ) { ?> |
| 272 |
<p class="ehp-form__description"><?php echo esc_html( $description_text ); ?></p> |
| 273 |
<?php } ?> |
| 274 |
</div> |
| 275 |
<?php |
| 276 |
} |
| 277 |
|
| 278 |
public function __construct( Ehp_Form $widget ) { |
| 279 |
$this->widget = $widget; |
| 280 |
$this->settings = $widget->get_settings_for_display(); |
| 281 |
} |
| 282 |
} |
| 283 |
|