Files
wp-plugin-registrations/includes/frontend.php
T

63 lines
6.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
if (!defined('ABSPATH')) exit;
function hgr_form_token($id,$issued=null,$uuid=null) {
$payload=$id.'.'.($issued??time()).'.'.($uuid??wp_generate_uuid4());
return $payload.'.'.hash_hmac('sha256',$payload,wp_salt('nonce'));
}
function hgr_check_token($token,$id) {
if (!is_string($token) || strlen($token)>200) return false;
$bits=explode('.',$token);
if (count($bits)!==4 || !ctype_digit($bits[0]) || !ctype_digit($bits[1]) || (int)$bits[0]!==$id || !preg_match('/^[a-f0-9-]{36}$/',$bits[2])) return false;
$age=time()-(int)$bits[1];
return $age>=3 && $age<=7200 && hash_equals(hash_hmac('sha256',implode('.',array_slice($bits,0,3)),wp_salt('nonce')),$bits[3]);
}
add_shortcode('hg_registrations',function($attrs) {
global $wpdb;
$attrs=shortcode_atts(['activity'=>'0'],$attrs,'hg_registrations');$activity=absint($attrs['activity']);
$sql=$wpdb->prepare('SELECT m.* FROM '.hgr_table('moments')." m INNER JOIN $wpdb->posts p ON p.ID=m.activity_id WHERE m.state='open' AND m.starts_at>%s AND p.post_status='publish' AND p.post_type='hgr_activity'",current_time('mysql',true));
if ($activity) $sql.=$wpdb->prepare(' AND m.activity_id=%d',$activity);
$rows=$wpdb->get_results($sql.' ORDER BY m.starts_at LIMIT 100');
wp_enqueue_style('hgr-frontend',HGR_URL.'assets/frontend.css',[],HGR_VERSION);
wp_enqueue_script('hgr-frontend',HGR_URL.'assets/frontend.js',[],HGR_VERSION,true);
if (!headers_sent()) { nocache_headers(); }
ob_start(); ?>
<section class="hgr" aria-label="<?php echo esc_attr(hgr_settings()['moment_label']); ?>">
<?php if (isset($_GET['hgr_reserved'])): ?><div class="hgr-notice" role="status"><strong>Je aanvraag is ontvangen.</strong> De plaatsen zijn gereserveerd. Je deelname is pas definitief nadat de organisatie deze heeft bevestigd.</div><?php endif; ?>
<?php if (isset($_GET['hgr_error']) && is_scalar($_GET['hgr_error'])): $message=get_transient('hgr_error_'.sanitize_key(wp_unslash($_GET['hgr_error'])));if($message): ?><div class="hgr-notice hgr-error" role="alert"><?php echo esc_html($message); ?></div><?php endif;endif; ?>
<?php if (!$rows): ?><p class="hgr-empty">Er zijn momenteel geen geplande momenten waarvoor je kunt inschrijven.</p><?php endif; ?>
<?php foreach($rows as $m): $cap=hgr_capacity($m->id);$title=get_the_title($m->activity_id);$max=min($cap['available'],(int)hgr_settings()['max_participants']); ?>
<article class="hgr-moment"><div class="hgr-moment-head"><div><h3><?php echo esc_html($title); ?></h3><p><?php echo esc_html(hgr_local_time($m->starts_at)); ?> – <?php echo esc_html(wp_date('H:i',strtotime($m->ends_at.' UTC'))); ?><br><?php echo esc_html($m->location); ?></p></div><div class="hgr-capacity"><strong><?php echo (int)$cap['available']; ?> beschikbaar</strong><span>van <?php echo (int)$cap['capacity']; ?> plaatsen</span></div></div>
<?php if (!$max): ?><p class="hgr-full">Volgeboekt</p><?php else: ?>
<details class="hgr-details"><summary>Plaatsen reserveren</summary>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="hgr-form" data-max="<?php echo $max; ?>">
<input type="hidden" name="action" value="hgr_reserve"><input type="hidden" name="moment_id" value="<?php echo (int)$m->id; ?>"><input type="hidden" name="hgr_token" value="<?php echo esc_attr(hgr_form_token((int)$m->id)); ?>"><input type="hidden" name="return_url" value="<?php echo esc_url(get_permalink()); ?>">
<?php wp_nonce_field('hgr_reserve','hgr_nonce'); ?>
<div class="hgr-honey" aria-hidden="true"><label>Website<input name="website" tabindex="-1" autocomplete="off"></label></div>
<div class="hgr-fields"><label>Naam contactpersoon <span aria-hidden="true">*</span><input name="contact_name" autocomplete="name" required maxlength="150"></label><label>E-mailadres <span aria-hidden="true">*</span><input type="email" name="contact_email" autocomplete="email" required maxlength="254"></label><label>Telefoonnummer<input type="tel" name="phone" autocomplete="tel" maxlength="60"></label><label>Organisatie<input name="organization" autocomplete="organization" maxlength="150"></label></div>
<fieldset class="hgr-participants"><legend>Deelnemers</legend><div class="hgr-person"><label>Naam deelnemer 1 <span aria-hidden="true">*</span><input name="participants[]" required maxlength="150"></label><button type="button" class="hgr-remove" hidden>Verwijderen</button></div></fieldset>
<button type="button" class="hgr-add">Deelnemer toevoegen</button><p class="hgr-count" aria-live="polite">1 deelnemer</p>
<label class="hgr-consent"><input type="checkbox" name="consent" value="1" required> Ik ga akkoord met verwerking van deze gegevens voor mijn inschrijving.</label>
<p class="hgr-help">Je reserveert plaatsen. De organisatie bevestigt je deelname afzonderlijk. Vul geen medische of andere gevoelige gegevens in.</p>
<button type="submit" class="hgr-submit">Reservering aanvragen</button>
</form></details><?php endif; ?></article><?php endforeach; ?></section>
<?php return ob_get_clean();
});
function hgr_submission() {
$input=wp_unslash($_POST);$id=absint($input['moment_id']??0);
$return=wp_validate_redirect(is_scalar($input['return_url']??null)?$input['return_url']: '',home_url('/'));
$return=remove_query_arg(['hgr_error','hgr_reserved'],$return);
$error=null;
if (!wp_verify_nonce(is_scalar($input['hgr_nonce']??null)?$input['hgr_nonce']:'','hgr_reserve') || !hgr_check_token($input['hgr_token']??null,$id) || !empty($input['website']) || ($input['consent']??'')!=='1') $error='Controleer het formulier. Vernieuw de pagina als deze lang open heeft gestaan.';
$ip=hash_hmac('sha256',$_SERVER['REMOTE_ADDR']??'',wp_salt('auth'));$rate='hgr_rate_'.$ip;
if (!$error && (int)get_transient($rate)>=10) $error='Er zijn te veel aanvragen verstuurd. Probeer het over enkele minuten opnieuw.';
if (!$error) {
$result=hgr_reserve($id,$input,hash('sha256',$input['hgr_token']));
if (is_wp_error($result)) $error=$result->get_error_message();
else { if ($result['new']) { set_transient($rate,(int)get_transient($rate)+1,10*MINUTE_IN_SECONDS);hgr_notify($result['id']); } wp_safe_redirect(add_query_arg('hgr_reserved','1',$return));exit; }
}
$key=str_replace('-','',wp_generate_uuid4());set_transient('hgr_error_'.$key,$error,5*MINUTE_IN_SECONDS);
wp_safe_redirect(add_query_arg('hgr_error',$key,$return));exit;
}
add_action('admin_post_hgr_reserve','hgr_submission');
add_action('admin_post_nopriv_hgr_reserve','hgr_submission');