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

80 lines
16 KiB
PHP

<?php
if (!defined('ABSPATH')) exit;
HG_Plugin_Admin::register(['slug'=>'hg-registrations','title'=>'HG Inschrijvingen','label'=>'Inschrijvingen','callback'=>'hgr_admin','version'=>HGR_VERSION,'description'=>'Activiteiten, geplande momenten, capaciteit en inschrijvingen beheren.']);
add_action('admin_enqueue_scripts',function($hook) { if ($hook===HG_Plugin_Admin::hook('hg-registrations')) wp_enqueue_style('hgr-admin',HGR_URL.'assets/admin.css',[],HGR_VERSION); });
add_filter('parent_file',function($parent) { return get_current_screen()?->post_type==='hgr_activity' ? 'hg-plugins' : $parent; });
add_filter('submenu_file',function($submenu) { return get_current_screen()?->post_type==='hgr_activity' ? 'hg-registrations' : $submenu; });
function hgr_admin_url($tab,$args=[]) { return add_query_arg(array_merge(['page'=>'hg-registrations','tab'=>$tab],$args),admin_url('admin.php')); }
function hgr_admin_field($label,$name,$value='',$type='text',$extra='') {
?><label class="hgr-admin-field"><span><?php echo esc_html($label); ?></span><input type="<?php echo esc_attr($type); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>" <?php echo $extra; ?>></label><?php
}
function hgr_admin_form($operation) { ?><form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>"><input type="hidden" name="action" value="hgr_admin"><input type="hidden" name="operation" value="<?php echo esc_attr($operation); ?>"><?php wp_nonce_field('hgr_admin_'.$operation); }
function hgr_capacity_text($cap) { return "$cap[capacity] plaatsen · $cap[reserved] gereserveerd · $cap[confirmed] bevestigd · $cap[available] beschikbaar"; }
function hgr_admin() {
if (!current_user_can('manage_options')) return;
global $wpdb;
$tabs=['activities'=>'Activiteiten','moments'=>'Momenten','registrations'=>'Inschrijvingen','settings'=>'Instellingen','info'=>'Over deze plugin'];
$tab=sanitize_key($_GET['tab']??'activities');if (!isset($tabs[$tab])) $tab='activities';
?>
<div class="wrap hgr-admin"><header class="hgr-admin-header"><div><h1>HG Inschrijvingen</h1><p>Beheer activiteiten, geplande momenten en deelnemers.</p></div><span class="hgr-language-status">Reserveren → handmatig bevestigen</span></header>
<?php if (isset($_GET['notice'])) { $msg=get_transient('hgr_admin_notice_'.get_current_user_id().'_'.sanitize_key($_GET['notice']));if($msg): ?><div class="notice notice-<?php echo $msg['error']?'error':'success'; ?>"><p><?php echo esc_html($msg['message']); ?></p></div><?php endif; } ?>
<nav class="hgr-admin-tabs" aria-label="Pluginonderdelen"><?php foreach($tabs as $key=>$label): ?><a <?php if($key===$tab) echo 'class="is-active" aria-current="page"'; ?> href="<?php echo esc_url(hgr_admin_url($key)); ?>"><?php echo esc_html($label); ?></a><?php endforeach; ?></nav>
<?php
if ($tab==='activities') {
$activities=get_posts(['post_type'=>'hgr_activity','post_status'=>['publish','draft','pending','private'],'numberposts'=>-1]);
?><div class="hgr-panel-heading"><div><h2>Activiteiten</h2><p>Een activiteit beschrijft wat je aanbiedt. Plan de datums onder Momenten.</p></div><a class="button button-secondary" href="<?php echo esc_url(admin_url('post-new.php?post_type=hgr_activity')); ?>">Activiteit toevoegen</a></div><div class="hgr-admin-card"><table class="widefat striped"><thead><tr><th>Activiteit</th><th>Publicatie</th><th>Agenda op een pagina plaatsen</th></tr></thead><tbody><?php foreach($activities as $a): ?><tr><td><a href="<?php echo esc_url(get_edit_post_link($a->ID)); ?>"><?php echo esc_html($a->post_title); ?></a></td><td><?php echo esc_html(get_post_status_object($a->post_status)->label); ?></td><td><code>[hg_registrations activity="<?php echo (int)$a->ID; ?>"]</code></td></tr><?php endforeach; if(!$activities): ?><tr><td colspan="3">Er zijn nog geen activiteiten. Voeg eerst een activiteit toe.</td></tr><?php endif; ?></tbody></table><p class="hgr-admin-note">Plaats <code>[hg_registrations]</code> voor een overzicht van alle open momenten. Alleen gepubliceerde activiteiten zijn zichtbaar voor bezoekers.</p></div><?php
} elseif ($tab==='moments') {
$rows=$wpdb->get_results('SELECT * FROM '.hgr_table('moments').' ORDER BY starts_at DESC LIMIT 500');
?><div class="hgr-admin-card hgr-table"><h2>Geplande momenten</h2><table class="widefat striped"><thead><tr><th>Activiteit en datum</th><th>Locatie</th><th>Capaciteit</th><th>Status</th><th></th></tr></thead><tbody><?php foreach($rows as $m): ?><tr><td><?php echo esc_html(get_the_title($m->activity_id)); ?><br><?php echo esc_html(hgr_local_time($m->starts_at)); ?></td><td><?php echo esc_html($m->location); ?></td><td><?php echo esc_html(hgr_capacity_text(hgr_capacity($m->id))); ?></td><td><?php echo esc_html(['draft'=>'Concept','open'=>'Open','closed'=>'Gesloten'][$m->state]??$m->state); ?></td><td><a href="<?php echo esc_url(hgr_admin_url('moments',['moment_id'=>$m->id])); ?>">Bewerken</a></td></tr><?php endforeach;if(!$rows): ?><tr><td colspan="5">Er zijn nog geen geplande momenten.</td></tr><?php endif; ?></tbody></table></div><?php
$edit=isset($_GET['moment_id'])?hgr_moment(absint($_GET['moment_id'])):null;
$activities=get_posts(['post_type'=>'hgr_activity','post_status'=>['publish','draft'],'numberposts'=>-1]);
hgr_admin_form('moment'); ?>
<div class="hgr-admin-card"><h2><?php echo $edit?'Moment bewerken':'Moment toevoegen'; ?></h2><input type="hidden" name="id" value="<?php echo (int)($edit->id??0); ?>"><div class="hgr-admin-grid">
<label class="hgr-admin-field"><span>Activiteit</span><select name="activity_id" required><option value="">Kies een activiteit</option><?php foreach($activities as $a): ?><option value="<?php echo $a->ID; ?>" <?php selected($edit->activity_id??0,$a->ID); ?>><?php echo esc_html($a->post_title); ?></option><?php endforeach; ?></select></label>
<?php hgr_admin_field('Locatie','location',$edit->location??'','text','required maxlength="250"');
foreach(['starts_at'=>'Begindatum en tijd','ends_at'=>'Einddatum en tijd'] as $name=>$label) hgr_admin_field($label,$name,$edit?wp_date('Y-m-d\TH:i',strtotime($edit->$name.' UTC')):'','datetime-local','required');
hgr_admin_field('Aantal plaatsen','capacity',$edit->capacity??12,'number','min="1" max="10000" required'); ?>
<label class="hgr-admin-field"><span>Status</span><select name="state"><?php foreach(['draft'=>'Concept','open'=>'Open voor inschrijving','closed'=>'Gesloten'] as $key=>$label): ?><option value="<?php echo $key; ?>" <?php selected($edit->state??'draft',$key); ?>><?php echo esc_html($label); ?></option><?php endforeach; ?></select></label></div>
<p class="hgr-admin-note">Datums gebruiken de tijdzone van deze website: <?php echo esc_html(wp_timezone_string()); ?>. Gereserveerde en bevestigde deelnemers tellen beide mee. Met bestaande aanvragen kun je de datum, activiteit en locatie niet wijzigen.</p></div>
<div class="hgr-save-bar"><?php submit_button('Moment opslaan','primary','submit',false); ?><a href="<?php echo esc_url(hgr_admin_url('moments')); ?>">Nieuw moment</a><span>Wijzigingen worden pas actief na opslaan.</span></div></form><?php
} elseif ($tab==='registrations') {
$rows=$wpdb->get_results('SELECT * FROM '.hgr_table('registrations').' ORDER BY created_at DESC LIMIT 500');
?><div class="hgr-panel-heading"><div><h2>Inschrijvingen</h2><p>Reserveringen blijven staan totdat je ze behandelt. Er is geen automatisch verloop.</p></div><a class="button button-secondary" href="<?php echo esc_url(wp_nonce_url(admin_url('admin-post.php?action=hgr_export'),'hgr_export')); ?>">Deelnemers exporteren</a></div><div class="hgr-admin-card hgr-table"><table class="widefat striped"><thead><tr><th>Aanvraag</th><th>Contactpersoon</th><th>Activiteit en moment</th><th>Deelnemers</th><th>Status</th><th>E-mail</th></tr></thead><tbody><?php foreach($rows as $r): $m=hgr_moment($r->moment_id); ?><tr><td><a href="<?php echo esc_url(hgr_admin_url('registrations',['registration_id'=>$r->id])); ?>">#<?php echo (int)$r->id; ?></a><br><?php echo esc_html(hgr_local_time($r->created_at)); ?></td><td><?php echo esc_html($r->contact_name); ?><br><?php echo esc_html($r->organization); ?></td><td><?php echo esc_html($m?get_the_title($m->activity_id):''); ?><br><?php echo esc_html($m?hgr_local_time($m->starts_at):''); ?></td><td><?php echo (int)$r->seats; ?></td><td><span class="hgr-badge hgr-<?php echo esc_attr($r->status); ?>"><?php echo esc_html(hgr_statuses()[$r->status]??$r->status); ?></span></td><td><?php echo esc_html(['pending'=>'Nog versturen','sent'=>'Verstuurd','failed'=>'Verzending mislukt'][$r->notice_state]??$r->notice_state); ?></td></tr><?php endforeach;if(!$rows): ?><tr><td colspan="6">Er zijn nog geen inschrijvingen.</td></tr><?php endif; ?></tbody></table></div><?php
if (isset($_GET['registration_id']) && ($r=hgr_registration(absint($_GET['registration_id'])))) {
$m=hgr_moment($r->moment_id);hgr_admin_form('status'); ?>
<div class="hgr-admin-card"><h2>Inschrijving #<?php echo (int)$r->id; ?></h2><p><?php echo esc_html(get_the_title($m->activity_id).' · '.hgr_local_time($m->starts_at).' · '.$m->location); ?></p><p class="hgr-admin-note"><?php echo esc_html(hgr_capacity_text(hgr_capacity($m->id))); ?></p>
<div class="hgr-admin-grid"><div><h3>Contactpersoon</h3><p><?php echo esc_html($r->contact_name); ?><br><?php echo esc_html($r->contact_email); ?><br><?php echo esc_html($r->phone); ?><br><?php echo esc_html($r->organization); ?></p></div><div><h3>Deelnemers</h3><ol><?php foreach(json_decode($r->participants,true)?:[] as $name): ?><li><?php echo esc_html($name); ?></li><?php endforeach; ?></ol></div></div>
<input type="hidden" name="id" value="<?php echo (int)$r->id; ?>"><label class="hgr-admin-field"><span>Status</span><select name="status"><?php foreach(hgr_statuses() as $key=>$label): ?><option value="<?php echo $key; ?>" <?php selected($r->status,$key); ?>><?php echo esc_html($label); ?></option><?php endforeach; ?></select></label><p>Bij een statuswijziging ontvangt de contactpersoon een bericht. Afwijzen of annuleren geeft de plaatsen vrij.</p></div><div class="hgr-save-bar"><?php submit_button('Status opslaan en berichten versturen','primary','submit',false); ?></div></form><?php
if ($r->notice_state!=='sent') { hgr_admin_form('resend'); ?><input type="hidden" name="id" value="<?php echo (int)$r->id; ?>"><p><?php submit_button('Mislukte meldingen opnieuw versturen','secondary','submit',false); ?></p></form><?php }
}
} elseif ($tab==='settings') {
$s=hgr_settings();hgr_admin_form('settings'); ?><div class="hgr-admin-card"><h2>Algemeen en e-mail</h2><div class="hgr-admin-grid"><?php hgr_admin_field('Ontvangstadres organisatie','recipient',$s['recipient'],'email','required');hgr_admin_field('Maximaal deelnemers per aanvraag','max_participants',$s['max_participants'],'number','min="1" max="100" required');hgr_admin_field('Naam voor activiteit','activity_label',$s['activity_label'],'text','required maxlength="60"');hgr_admin_field('Naam voor gepland moment','moment_label',$s['moment_label'],'text','required maxlength="60"'); ?></div><p class="hgr-admin-note">Mailaflevering volgt de mailvoorziening van de website. Een gereserveerde plaats is nog geen bevestigde deelname. Zorg dat er een passende privacyverklaring is voor de gegevens die je verzamelt.</p></div><div class="hgr-save-bar"><?php submit_button('Wijzigingen opslaan','primary','submit',false); ?><span>Wijzigingen worden pas actief na opslaan.</span></div></form><?php
} else { ?><div class="hgr-admin-card"><h2>HG Inschrijvingen</h2><p>Versie <?php echo esc_html(HGR_VERSION); ?> · WordPress en PHP 8.1+</p><p>Herbruikbaar voor trainingen, workshops en andere activiteiten. Klantinstellingen staan uitsluitend in de website. Reserveringen en bevestigingen delen dezelfde capaciteitscontrole.</p><h3>Onderhoud en gegevens</h3><p>Gebruik databaseback-ups. Deactiveren verwijdert geen inschrijvingen. Er is geen automatische verwijdering of bewaartermijn; spreek deze vóór livegang af. Exports bevatten persoonsgegevens en zijn alleen beschikbaar voor beheerders.</p><p>Basis-antispam: honeypot, ondertekend formulier, wachttijd en verzendlimiet. Cache de inschrijfpagina niet langdurig. Er is geen online betaling of wachtlijst.</p></div><?php }
?></div><?php
}
function hgr_admin_action() {
if (!current_user_can('manage_options')) wp_die('Geen toegang.', '', ['response'=>403]);
$input=wp_unslash($_POST);$op=sanitize_key($input['operation']??'');check_admin_referer('hgr_admin_'.$op);
$tab='registrations';$args=[];$error=false;$message='Wijzigingen opgeslagen.';
if ($op==='moment') { $tab='moments';$result=hgr_save_moment($input);if(!is_wp_error($result))$args['moment_id']=$result; }
elseif ($op==='status') { $args['registration_id']=absint($input['id']??0);$result=hgr_set_status($args['registration_id'],$input['status']??'');if($result===true && !hgr_notify($args['registration_id'])) $message='Status opgeslagen, maar niet alle e-mails zijn verstuurd. Probeer opnieuw vanuit de aanvraag.'; }
elseif ($op==='resend') { $args['registration_id']=absint($input['id']??0);$result=hgr_notify($args['registration_id']);$message=$result?'Meldingen verstuurd.':'Verzending is niet gelukt. Controleer de mailvoorziening.';$error=!$result; }
elseif ($op==='settings') {
$tab='settings';$s=hgr_settings();$recipient=sanitize_email($input['recipient']??'');$max=filter_var($input['max_participants']??null,FILTER_VALIDATE_INT,['options'=>['min_range'=>1,'max_range'=>100]]);
$activity=sanitize_text_field($input['activity_label']??'');$moment=sanitize_text_field($input['moment_label']??'');
if (!is_email($recipient)||!$max||!$activity||!$moment||strlen($activity)>60||strlen($moment)>60) $result=new WP_Error('settings','Controleer de instellingen.');
else { update_option('hgr_settings',array_merge($s,['recipient'=>$recipient,'max_participants'=>$max,'activity_label'=>$activity,'moment_label'=>$moment]));$result=true; }
} else $result=new WP_Error('operation','Ongeldige actie.');
if (is_wp_error($result)) { $error=true;$message=$result->get_error_message(); }
$key=str_replace('-','',wp_generate_uuid4());set_transient('hgr_admin_notice_'.get_current_user_id().'_'.$key,['error'=>$error,'message'=>$message],5*MINUTE_IN_SECONDS);
wp_safe_redirect(hgr_admin_url($tab,array_merge($args,['notice'=>$key])));exit;
}
add_action('admin_post_hgr_admin','hgr_admin_action');
add_action('admin_post_hgr_export',function() {
if (!current_user_can('manage_options')) wp_die('Geen toegang.','',['response'=>403]);check_admin_referer('hgr_export');
global $wpdb;nocache_headers();header('Content-Type: text/csv; charset=utf-8');header('Content-Disposition: attachment; filename="inschrijvingen-'.gmdate('Y-m-d').'.csv"');
$out=fopen('php://output','w');fwrite($out,"\xEF\xBB\xBF");fputcsv($out,['Nummer','Activiteit','Datum','Locatie','Status','Deelnemer','Contactpersoon','E-mail','Telefoon','Organisatie'],';', '"','');
// One participant per row; neutralize spreadsheet formulas in user supplied values.
$offset=0;do { $rows=$wpdb->get_results($wpdb->prepare('SELECT * FROM '.hgr_table('registrations').' ORDER BY id LIMIT 200 OFFSET %d',$offset));foreach($rows as $r) { $m=hgr_moment($r->moment_id);foreach(json_decode($r->participants,true)?:[] as $person) { $row=[$r->id,get_the_title($m->activity_id),hgr_local_time($m->starts_at),$m->location,hgr_statuses()[$r->status],$person,$r->contact_name,$r->contact_email,$r->phone,$r->organization];$row=array_map(fn($v)=>preg_match('/^[\s]*[=+@-]/u',(string)$v)?"'".$v:$v,$row);fputcsv($out,$row,';', '"',''); } }$offset+=200; } while(count($rows)===200);fclose($out);exit;
});