Files
wp-plugin-contactform/includes/settings.php
T
parkel e1df255b0b Make consent checkbox and form note configurable
Add independent admin controls for the consent checkbox and explanatory note. Keep both enabled by default so existing installations retain their current behavior.

Render the optional elements conditionally and require consent in server validation only when the checkbox is enabled. Bump the plugin and stable release version to 1.3.4, document the controls, and cover enabled/disabled rendering, validation, and invalid settings in the integration suite.

Validation: PHP syntax and NL/EN integration checks passed on Hephaestus, with mail intercepted during tests. Staged whitespace checks passed. No changes to antispam behavior or other website configurations.
2026-11-09 23:41:55 +01:00

109 lines
9.8 KiB
PHP

<?php
if (!defined('ABSPATH')) exit;
function hgcf_defaults() {
return ['recipient'=>get_option('admin_email'),'site_name'=>get_bloginfo('name'),'contact_name'=>'','sender_email'=>'','sender_name'=>'','altcha_enabled'=>false,'show_consent'=>true,'show_note'=>true,
'title_nl'=>'Waarmee kunnen we je helpen?','title_en'=>'How can we help?',
'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_workflow_defaults($id='question') {
return ['id'=>$id,'enabled'=>true,'recipient'=>'','message_required'=>true,'phone'=>'optional','service'=>'hidden','date'=>'hidden','time'=>'hidden','opening_time'=>'09:00','closing_time'=>'21:00','services'=>[],
'label'=>['nl'=>'Een vraag stellen','en'=>'Ask a question'],'subject'=>['nl'=>'vraag','en'=>'question'],
'message_label'=>['nl'=>'Je vraag','en'=>'Your question'],'service_label'=>['nl'=>'Welke dienst?','en'=>'Which service?'],
'hint'=>['nl'=>'','en'=>'']];
}
function hgcf_normalize_settings($saved) {
$s=array_replace(hgcf_defaults(),is_array($saved)?$saved:[]);
if(!array_key_exists('workflows',$s)) {
$question=hgcf_workflow_defaults();$question['phone']=($s['show_phone']??true)?'optional':'hidden';$flows=['question'=>$question];
if($s['enable_requests']??false) {
$request=hgcf_workflow_defaults('request');$request['message_required']=false;$request['phone']=$question['phone'];$request['service']='optional';$request['date']='optional';$request['time']='optional';
$request['opening_time']=$s['opening_time']??'09:00';$request['closing_time']=$s['closing_time']??'21:00';
foreach(($s['services']??[]) as $id=>$names)$request['services'][$id]=['nl'=>$names[0],'en'=>$names[1]];
foreach(['nl','en'] as $lang){$request['label'][$lang]=$s['request_label_'.$lang]??($lang==='nl'?'Een afspraak aanvragen':'Request an appointment');$request['subject'][$lang]=$lang==='nl'?'afspraakaanvraag':'appointment request';$request['service_label'][$lang]=$s['service_label_'.$lang]??($lang==='nl'?'Welke dienst?':'Which service?');}
$request['message_label']=['nl'=>'Toelichting','en'=>'Message'];$request['hint']=['nl'=>'Dit is een voorkeur, geen reservering. We stemmen de datum en tijd met je af.','en'=>'This is a preference, not a reservation. We will confirm the date and time with you.'];$flows['request']=$request;
}
$s['workflows']=$flows;
}
return $s;
}
function hgcf_settings(){return hgcf_normalize_settings(get_option('hgcf_settings',[]));}
function hgcf_setting($key){return hgcf_settings()[$key]??'';}
function hgcf_translated($values,$lang,$fallback='') {
$text=$values[$lang]??$values[hgcf_default_language()]??$values['en']??$values['nl']??$fallback;
return str_replace('{contact_name}',hgcf_setting('contact_name')?:hgcf_setting('site_name'),is_string($text)?$text:$fallback);
}
function hgcf_raw_local_setting($key,$lang) {
$s=hgcf_settings();return $s[$key.'_'.$lang]??$s[$key.'_'.hgcf_default_language()]??$s[$key.'_en']??$s[$key.'_nl']??'';
}
function hgcf_local_setting($key,$lang) {
return str_replace('{contact_name}',hgcf_setting('contact_name')?:hgcf_setting('site_name'),hgcf_raw_local_setting($key,$lang));
}
function hgcf_workflows($active=true) {
$flows=hgcf_setting('workflows');return $active?array_filter($flows,fn($f)=>!empty($f['enabled'])):$flows;
}
function hgcf_workflow($id){$flows=hgcf_workflows();return $flows[$id]??null;}
function hgcf_valid_email($value,$empty=false){return is_string($value)&&(($empty&&$value==='')||(is_email($value)&&!preg_match('/[\r\n]/',$value)));}
function hgcf_valid_times($start,$end){return 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;}
function hgcf_sanitize_workflows($rows,$old,$languages) {
if(!is_array($rows)||count($rows)>20)return new WP_Error('flows','Gebruik één tot twintig workflows.');
$flows=[];
foreach($rows as $row) {
if(!is_array($row))return new WP_Error('flows','Een workflow is ongeldig.');
$id=$row['id']??'';
if(!is_string($id)||!preg_match('/^[a-z][a-z0-9_-]{0,39}$/',$id)||isset($flows[$id]))return new WP_Error('flows','Elke workflow moet een unieke geldige code hebben.');
$flow=array_replace(hgcf_workflow_defaults($id),$old[$id]??[]);
$flow['enabled']=($row['enabled']??'')==='1';$flow['message_required']=($row['message_required']??'')==='1';
$recipient=$row['recipient']??'';if(!hgcf_valid_email($recipient,true))return new WP_Error('flows','Controleer het ontvangstadres van de workflow.');$flow['recipient']=$recipient;
foreach(['phone','service','date','time'] as $field){if(!in_array($row[$field]??'', ['hidden','optional','required'],true))return new WP_Error('flows','Controleer de veldinstellingen.');$flow[$field]=$row[$field];}
if(!hgcf_valid_times($row['opening_time']??'',$row['closing_time']??''))return new WP_Error('flows','Controleer de begintijd en eindtijd van de workflow.');
$flow['opening_time']=$row['opening_time'];$flow['closing_time']=$row['closing_time'];
foreach(['label','subject','message_label','service_label','hint'] as $field) {if(!isset($row[$field])||!is_array($row[$field]))return new WP_Error('flows','Controleer de teksten van de workflow.');}
foreach(['label','subject','message_label','service_label','hint'] as $field)foreach($languages as $lang=>$name){
$value=$row[$field][$lang]??null;
if(!is_string($value)||strlen($value)>1500||($field!=='hint'&&trim($value)===''))return new WP_Error('flows','Vul de namen en labels van iedere actieve taal in.');
$flow[$field][$lang]=$field==='hint'?sanitize_textarea_field($value):sanitize_text_field($value);
}
$services=[];$service_rows=$row['services']??[];
if(!is_array($service_rows)||count($service_rows)>50)return new WP_Error('flows','Gebruik maximaal vijftig keuzemogelijkheden per workflow.');
foreach($service_rows as $entry){
if(!is_array($entry)||!isset($entry['labels'])||!is_array($entry['labels']))return new WP_Error('flows','Een keuzemogelijkheid is ongeldig.');
$key=$entry['id']??'';if(!is_string($key)||!preg_match('/^[a-z][a-z0-9_-]{0,39}$/',$key)||$key==='unsure'||isset($services[$key]))return new WP_Error('flows','Keuzemogelijkheden moeten unieke geldige codes hebben.');
$names=$flow['services'][$key]??[];
foreach($languages as $lang=>$name){$value=$entry['labels'][$lang]??'';if(!is_string($value)||trim($value)===''||strlen($value)>150)return new WP_Error('flows','Vul de naam van iedere keuzemogelijkheid in.');$names[$lang]=sanitize_text_field($value);}
$services[$key]=$names;
}
$flow['services']=$services;
if($flow['service']==='required'&&!$services)return new WP_Error('flows','Voeg keuzemogelijkheden toe wanneer de dienstenkeuze verplicht is.');
$flows[$id]=$flow;
}
if(!array_filter($flows,fn($flow)=>$flow['enabled']))return new WP_Error('flows','Houd minimaal één workflow ingeschakeld.');
return $flows;
}
function hgcf_sanitize_settings($input) {
$old=hgcf_settings();if(!is_array($input))return $old;
if(($_POST['option_page']??'')==='hgcf'&&($input['_complete']??'')!=='1'){add_settings_error('hgcf_settings','incomplete','Niet alle velden zijn ontvangen. Je instellingen zijn niet gewijzigd.');return $old;}
$out=$old;
foreach(['show_consent','show_note'] as $key)if(array_key_exists($key,$input)){if(in_array($input[$key],['0','1'],true))$out[$key]=$input[$key]==='1';else add_settings_error('hgcf_settings',$key,'Controleer de weergave-instelling.');}
if(array_key_exists('altcha_enabled',$input)) {
if(in_array($input['altcha_enabled'],['0','1'],true))$out['altcha_enabled']=$input['altcha_enabled']==='1';
else add_settings_error('hgcf_settings','altcha_enabled','Controleer de antispaminstelling.');
}
foreach(['recipient'=>false,'sender_email'=>true] as $key=>$empty){if(array_key_exists($key,$input)){if(hgcf_valid_email($input[$key],$empty))$out[$key]=$input[$key]===''?'':sanitize_email($input[$key]);else add_settings_error('hgcf_settings',$key,'Controleer het e-mailadres.');}}
foreach(['sender_name','site_name','contact_name'] as $key)if(array_key_exists($key,$input)) {
$value=$input[$key];if(is_string($value)&&strlen($value)<=150&&!preg_match('/[\r\n]/',$value)&&($key!=='site_name'||trim($value)!==''))$out[$key]=sanitize_text_field($value);else add_settings_error('hgcf_settings',$key,'Controleer de naam.');
}
foreach(hgcf_languages() as $lang=>$name)foreach(['title','consent','note','success'] as $key){$field=$key.'_'.$lang;if(array_key_exists($field,$input)){if(is_string($input[$field])&&strlen($input[$field])<=1500&&trim($input[$field])!=='')$out[$field]=sanitize_textarea_field($input[$field]);else add_settings_error('hgcf_settings',$field,'Vul de formulierteksten in.');}}
if(array_key_exists('workflows',$input)) {
$flows=hgcf_sanitize_workflows($input['workflows'],$old['workflows'],hgcf_languages());
if(is_wp_error($flows))add_settings_error('hgcf_settings','workflows',$flows->get_error_message());else $out['workflows']=$flows;
}
$out['schema_version']=2;
return $out;
}
HG_Plugin_Admin::register(['slug'=>'hg-contactform','title'=>'HG Contactformulier','label'=>'Contactformulier','callback'=>'hgcf_admin_page','version'=>HGCF_VERSION,'description'=>'Contactformulieren, workflows en e-mailinstellingen beheren.']);
add_action('admin_init',function(){register_setting('hgcf','hgcf_settings',['type'=>'array','sanitize_callback'=>'hgcf_sanitize_settings']);});
require_once __DIR__.'/admin.php';