Add reusable NL/EN contact form with configurable admin settings
This commit is contained in:
commit
3e598b4bae
11 files changed
+633
No files matched your search
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
function hgcf_defaults() {
|
||||
return ['recipient'=>get_option('admin_email'),'site_name'=>get_bloginfo('name'),'contact_name'=>'','enable_requests'=>false,'show_phone'=>true,'opening_time'=>'09:00','closing_time'=>'21:00','services'=>[],
|
||||
'title_nl'=>'Waarmee kunnen we je helpen?','title_en'=>'How can we help?',
|
||||
'request_label_nl'=>'Een afspraak aanvragen','request_label_en'=>'Request an appointment',
|
||||
'service_label_nl'=>'Waarvoor wil je een afspraak?','service_label_en'=>'Which service would you like?',
|
||||
'consent_nl'=>'Mijn gegevens mogen worden gebruikt om op dit bericht te reageren.','consent_en'=>'My details may be used to reply to this message.',
|
||||
'note_nl'=>'Je bericht wordt per e-mail verstuurd en niet op deze website opgeslagen. Deel hier geen medische of andere gevoelige gegevens.','note_en'=>'Your message is sent by email and is not stored on this website. Please do not include medical or other sensitive details.',
|
||||
'success_nl'=>'Je bericht is aangeboden voor verzending. We nemen zo snel mogelijk contact met je op.','success_en'=>'Your message has been submitted for sending. We will get back to you as soon as possible.'];
|
||||
}
|
||||
function hgcf_settings() { $saved=get_option('hgcf_settings',[]); return array_replace(hgcf_defaults(),is_array($saved)?$saved:[]); }
|
||||
function hgcf_setting($key) { return hgcf_settings()[$key]??''; }
|
||||
function hgcf_local_setting($key,$lang) { return str_replace('{contact_name}',hgcf_setting('contact_name')?:hgcf_setting('site_name'),hgcf_setting($key.'_'.($lang==='en'?'en':'nl'))); }
|
||||
function hgcf_sanitize_settings($input) {
|
||||
$old=hgcf_settings(); if(!is_array($input))return $old;
|
||||
$out=$old;
|
||||
if(!isset($input['recipient']) || !is_string($input['recipient']) || !is_email($input['recipient']) || preg_match('/[\r\n]/',$input['recipient'])) {add_settings_error('hgcf_settings','recipient','Vul een geldig ontvangstadres in.');} else {$out['recipient']=sanitize_email($input['recipient']);}
|
||||
foreach(['site_name','contact_name','title_nl','title_en','request_label_nl','request_label_en','service_label_nl','service_label_en','consent_nl','consent_en','note_nl','note_en','success_nl','success_en'] as $key) {
|
||||
if(isset($input[$key]) && is_string($input[$key])) {$value=sanitize_textarea_field($input[$key]); if(strlen($value)<=1500 && ($key==='contact_name'||$value!==''))$out[$key]=$value;else add_settings_error('hgcf_settings',$key,'Controleer het veld '.$key.'.');}
|
||||
}
|
||||
foreach(['enable_requests','show_phone'] as $key)$out[$key]=isset($input[$key])&&$input[$key]==='1';
|
||||
$start=$input['opening_time']??'';$end=$input['closing_time']??'';
|
||||
if(is_string($start)&&is_string($end)&&preg_match('/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/',$start)&&preg_match('/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/',$end)&&$start<$end){$out['opening_time']=$start;$out['closing_time']=$end;}else add_settings_error('hgcf_settings','times','Vul geldige tijden in, met de begintijd vóór de eindtijd.');
|
||||
if(isset($input['services_text'])&&is_string($input['services_text'])) {
|
||||
$services=[];$valid=strlen($input['services_text'])<=12000;
|
||||
foreach(preg_split('/\r\n|\r|\n/',$input['services_text']) as $line) {
|
||||
if(trim($line)==='')continue;
|
||||
$parts=array_map('trim',explode('|',$line));
|
||||
if(count($parts)!==3 || !preg_match('/^[a-z][a-z0-9_-]{0,39}$/',$parts[0]) || $parts[0]==='unsure' || isset($services[$parts[0]]) || $parts[1]==='' || $parts[2]==='' || strlen($parts[1])>150 || strlen($parts[2])>150){$valid=false;break;}
|
||||
$services[$parts[0]]=[sanitize_text_field($parts[1]),sanitize_text_field($parts[2])];
|
||||
}
|
||||
if($valid&&count($services)<=50)$out['services']=$services;else add_settings_error('hgcf_settings','services','Gebruik maximaal 50 unieke regels: code|Nederlandse naam|Engelse naam. De code unsure is gereserveerd.');
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
add_action('admin_menu',function(){add_menu_page('HG Contactformulier','Contactformulier','manage_options','hg-contactform','hgcf_admin_page','dashicons-email-alt',58);});
|
||||
add_action('admin_init',function(){register_setting('hgcf','hgcf_settings',['type'=>'array','sanitize_callback'=>'hgcf_sanitize_settings','default'=>hgcf_defaults()]);});
|
||||
function hgcf_admin_page() {
|
||||
if(!current_user_can('manage_options'))wp_die('Geen toegang.', '', ['response'=>403]);
|
||||
$s=hgcf_settings();
|
||||
?>
|
||||
<div class="wrap"><h1>HG Contactformulier</h1><p>Gebruik <code>[hg_contactform]</code> op een pagina. De taal wordt automatisch gekozen; je kunt ook <code>[hg_contactform lang="nl"]</code> of <code>[hg_contactform lang="en"]</code> gebruiken.</p>
|
||||
<?php settings_errors('hgcf_settings'); ?><form action="options.php" method="post"><?php settings_fields('hgcf'); ?>
|
||||
<h2>Algemeen</h2><table class="form-table" role="presentation">
|
||||
<?php foreach(['recipient'=>['Ontvangstadres','email'],'site_name'=>['Naam in e-mailonderwerp','text'],'contact_name'=>['Contactnaam (optioneel)','text'],'opening_time'=>['Vroegste voorkeurtijd','time'],'closing_time'=>['Laatste voorkeurtijd','time']] as $key=>$field): ?>
|
||||
<tr><th scope="row"><label for="hgcf-<?php echo esc_attr($key); ?>"><?php echo esc_html($field[0]); ?></label></th><td><input id="hgcf-<?php echo esc_attr($key); ?>" class="regular-text" type="<?php echo esc_attr($field[1]); ?>" name="hgcf_settings[<?php echo esc_attr($key); ?>]" value="<?php echo esc_attr($s[$key]); ?>" <?php if($key==='recipient')echo 'required'; ?>></td></tr>
|
||||
<?php endforeach; ?>
|
||||
<?php foreach(['enable_requests'=>'Afspraakaanvragen inschakelen','show_phone'=>'Optioneel telefoonveld tonen'] as $key=>$label): ?><tr><th scope="row"><?php echo esc_html($label); ?></th><td><label><input type="checkbox" name="hgcf_settings[<?php echo esc_attr($key); ?>]" value="1" <?php checked($s[$key]); ?>> Ingeschakeld</label></td></tr><?php endforeach; ?>
|
||||
<tr><th scope="row"><label for="hgcf-services">Diensten / behandelingen</label></th><td><textarea id="hgcf-services" name="hgcf_settings[services_text]" class="large-text code" rows="8"><?php $lines=[];foreach($s['services'] as $key=>$names)$lines[]=$key.'|'.$names[0].'|'.$names[1];echo esc_textarea(implode("\n",$lines)); ?></textarea><p class="description">Eén dienst per regel: <code>code|Nederlandse naam|Engelse naam</code>. Bijvoorbeeld <code>consultation|Kennismaking|Introduction</code>. “Ik weet het nog niet” wordt automatisch toegevoegd.</p></td></tr>
|
||||
</table><h2>Teksten per taal</h2><p>Gebruik eventueel <code>{contact_name}</code> voor de ingestelde contactnaam. De aanvraag is een voorkeur, geen bevestigde reservering. Inzendingen worden niet als berichten in de database opgeslagen. Antispambeveiliging blijft actief.</p>
|
||||
<table class="form-table" role="presentation"><?php foreach(['title'=>'Formuliertitel','request_label'=>'Naam van aanvraagmodus','service_label'=>'Label dienstenkeuze','consent'=>'Toestemmingstekst','note'=>'Toelichting onder formulier','success'=>'Bevestigingstekst'] as $key=>$label): ?><tr><th scope="row"><?php echo esc_html($label); ?></th><td><?php foreach(['nl'=>'Nederlands','en'=>'English'] as $lang=>$name): $field=$key.'_'.$lang; ?><p><label for="hgcf-<?php echo esc_attr($field); ?>"><?php echo esc_html($name); ?></label><br><textarea id="hgcf-<?php echo esc_attr($field); ?>" name="hgcf_settings[<?php echo esc_attr($field); ?>]" class="large-text" rows="2" maxlength="1500"><?php echo esc_textarea($s[$field]); ?></textarea></p><?php endforeach; ?></td></tr><?php endforeach; ?></table>
|
||||
<?php submit_button(); ?></form><p>Verzending gebruikt de bestaande e-mailvoorziening van deze website. Een succesvolle verzending betekent niet automatisch dat de e-mail is afgeleverd.</p></div>
|
||||
<?php
|
||||
}
|
||||
Reference in new issue
Block a user