146 lines
7.0 KiB
PHP
146 lines
7.0 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
class WMF_Form_Renderer {
|
|
|
|
public static function render($form_id,$args=array()) {
|
|
$form=get_post($form_id);
|
|
if(!$form||$form->post_type!=='wmf-form'||$form->post_status!=='publish')
|
|
return '<p class="wmf-error">Formular nicht gefunden.</p>';
|
|
|
|
if(!session_id()) session_start();
|
|
|
|
$meta = wmf_get_form_meta($form_id);
|
|
$fields = $meta['fields']??array();
|
|
$saved_values = $_SESSION['wmf_values_'.$form_id]??array();
|
|
$errors = $_SESSION['wmf_errors_'.$form_id]??array();
|
|
$success = $_SESSION['wmf_success_'.$form_id]??false;
|
|
|
|
unset($_SESSION['wmf_values_'.$form_id],$_SESSION['wmf_errors_'.$form_id],$_SESSION['wmf_success_'.$form_id]);
|
|
|
|
ob_start();
|
|
|
|
if($success) {
|
|
echo '<div class="wmf-notice wmf-notice-success" role="alert">'.esc_html($meta['success_message']).'</div>';
|
|
if(empty($args['always_show'])) return ob_get_clean();
|
|
}
|
|
if(!empty($errors['_global']))
|
|
echo '<div class="wmf-notice wmf-notice-error" role="alert">'.esc_html($errors['_global']).'</div>';
|
|
|
|
$multi_step=!empty($meta['multi_step'])&&$meta['multi_step']==='1';
|
|
// Schritte ermitteln
|
|
$steps=array();
|
|
foreach($fields as $f) { $s=intval($f['step']??0); if(!isset($steps[$s])) $steps[$s]=array(); $steps[$s][]=$f; }
|
|
ksort($steps);
|
|
$total_steps=count($steps);
|
|
if($total_steps<2) $multi_step=false;
|
|
|
|
$form_cls='wmf-form'.(!empty($meta['css_class'])?' '.esc_attr($meta['css_class']):'');
|
|
$field_json=wp_json_encode($fields);
|
|
?>
|
|
<div class="wmf-form-wrap" id="wmf-wrap-<?php echo intval($form_id); ?>">
|
|
|
|
<?php if($multi_step&&!empty($meta['show_progress'])&&$meta['show_progress']==='1'): ?>
|
|
<div class="wmf-progress-bar" data-steps="<?php echo $total_steps; ?>">
|
|
<?php $step_labels=$meta['step_labels']??array();
|
|
foreach(array_keys($steps) as $si=>$snum): $lbl=$step_labels[$si]??'Schritt '.($si+1); ?>
|
|
<div class="wmf-step-indicator <?php echo $si===0?'active':''; ?>" data-step="<?php echo $si; ?>">
|
|
<span class="wmf-step-num"><?php echo $si+1; ?></span>
|
|
<span class="wmf-step-lbl"><?php echo esc_html($lbl); ?></span>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form class="<?php echo $form_cls; ?>"
|
|
id="wmf-form-<?php echo intval($form_id); ?>"
|
|
method="post"
|
|
enctype="multipart/form-data"
|
|
novalidate
|
|
data-form-id="<?php echo intval($form_id); ?>"
|
|
data-multi-step="<?php echo $multi_step?'1':'0'; ?>"
|
|
data-total-steps="<?php echo $total_steps; ?>"
|
|
data-fields='<?php echo esc_attr($field_json); ?>'>
|
|
|
|
<?php wp_nonce_field('wmf_submit_'.$form_id,'wmf_nonce'); ?>
|
|
<input type="hidden" name="wmf_form_id" value="<?php echo intval($form_id); ?>">
|
|
<input type="hidden" name="wmf_action" value="submit">
|
|
|
|
<?php // Honeypot
|
|
if(!empty($meta['honeypot_enabled'])&&$meta['honeypot_enabled']==='1'):
|
|
$hp_name='wmf_hp_'.md5($form_id); ?>
|
|
<div class="wmf-hp-field" aria-hidden="true" style="position:absolute;left:-9999px;opacity:0;pointer-events:none;">
|
|
<label for="<?php echo esc_attr($hp_name); ?>">Bitte leer lassen</label>
|
|
<input type="text" id="<?php echo esc_attr($hp_name); ?>" name="<?php echo esc_attr($hp_name); ?>" value="" tabindex="-1" autocomplete="off">
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if($multi_step): ?>
|
|
<?php foreach(array_values($steps) as $si=>$step_fields): ?>
|
|
<div class="wmf-step" data-step="<?php echo $si; ?>" <?php echo $si>0?'style="display:none;"':''; ?>>
|
|
<div class="wmf-fields">
|
|
<?php self::render_fields($step_fields,$errors,$saved_values); ?>
|
|
</div>
|
|
<div class="wmf-step-nav">
|
|
<?php if($si>0): ?><button type="button" class="wmf-prev-step button button-secondary">← Zurück</button><?php endif; ?>
|
|
<?php if($si<$total_steps-1): ?>
|
|
<button type="button" class="wmf-next-step button button-primary">Weiter →</button>
|
|
<?php else: ?>
|
|
<?php self::render_submit($meta,$form_id); ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php else: ?>
|
|
<div class="wmf-fields">
|
|
<?php self::render_fields($fields,$errors,$saved_values); ?>
|
|
</div>
|
|
<?php self::render_submit($meta,$form_id); ?>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
private static function render_fields($fields,$errors,$saved_values) {
|
|
foreach($fields as $field) {
|
|
$obj=wmf_get_field($field['type']??'');
|
|
if(!$obj) continue;
|
|
$value=$saved_values[$field['id']]??'';
|
|
$err=$errors[$field['id']]??'';
|
|
$wrap_cls='wmf-field-wrap'.($err?' wmf-has-error':'');
|
|
echo '<div class="'.esc_attr($wrap_cls).'">';
|
|
$obj->render($field,$value);
|
|
if($err) echo '<p class="wmf-field-error">'.esc_html($err).'</p>';
|
|
echo '</div>';
|
|
}
|
|
}
|
|
|
|
private static function render_submit($meta,$form_id) {
|
|
// reCAPTCHA
|
|
$integrations=wmf_get_integrations();
|
|
if(!empty($meta['recaptcha_enabled'])&&$meta['recaptcha_enabled']==='1') {
|
|
$rv3=$integrations->get_service('recaptchav3');
|
|
$rv2=$integrations->get_service('recaptcha');
|
|
if($rv3&&$rv3->is_connected()) {
|
|
$creds=$rv3->get_credentials();
|
|
echo '<input type="hidden" name="wmf_recaptcha_token" id="wmf-rctoken-'.intval($form_id).'">';
|
|
echo '<script>if(typeof grecaptcha!=="undefined"){grecaptcha.ready(function(){grecaptcha.execute("'.esc_js($creds['site_key']).'",{action:"submit"}).then(function(t){document.getElementById("wmf-rctoken-'.intval($form_id).'").value=t;});});}</script>';
|
|
} elseif($rv2&&$rv2->is_connected()) {
|
|
$creds=$rv2->get_credentials();
|
|
echo '<div class="wmf-recaptcha g-recaptcha" data-sitekey="'.esc_attr($creds['site_key']).'"></div>';
|
|
echo '<script async defer src="https://www.google.com/recaptcha/api.js"></script>';
|
|
}
|
|
}
|
|
?>
|
|
<div class="wmf-submit-wrap">
|
|
<button type="submit" class="wmf-submit-button">
|
|
<?php echo esc_html($meta['submit_label']?:'Absenden'); ?>
|
|
</button>
|
|
<span class="wmf-spinner" aria-hidden="true"></span>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|