2026-10-11 21:15:13 +02:00
<? php
/**
* Plugin Name: HG Contactformulier
2026-10-12 13:38:59 +02:00
* Description: Configureerbaar contactformulier met workflows en talen vanuit de website, zonder inzendingen op te slaan.
* Version: 1.2.0
2026-10-11 21:15:13 +02:00
* Requires at least: 6.5
* Requires PHP: 8.0
* Text Domain: hg-contactform
*/
if ( ! defined ( 'ABSPATH' )) { exit ; }
2026-10-12 13:38:59 +02:00
define ( 'HGCF_VERSION' , '1.2.0' );
define ( 'HGCF_FILE' , __FILE__ );
2026-10-11 21:15:13 +02:00
require_once __DIR__ . '/includes/settings.php' ;
require_once __DIR__ . '/includes/language.php' ;
2026-10-12 13:38:59 +02:00
require_once __DIR__ . '/includes/form.php' ;
2026-10-11 21:15:13 +02:00
add_action ( 'template_redirect' , function () {
if (( $_SERVER [ 'REQUEST_METHOD' ] ?? '' ) !== 'POST' || ( $_POST [ 'mlhc_action' ] ?? '' ) !== 'send' ) return ;
$id = get_queried_object_id (); $post = get_post ( $id );
if ( ! is_singular () || ! $post || ( ! has_shortcode ( $post -> post_content , 'hg_contactform' ) && ! has_shortcode ( $post -> post_content , 'mlh_contact' ))) wp_die ( 'Invalid form page' , '' , [ 'response' => 403 ]);
$lang = hgcf_language ( $post -> post_content );
$error = hgcf_text ( 'Het versturen is niet gelukt. Probeer het opnieuw of gebruik een andere contactmogelijkheid.' , 'Your message could not be sent. Please try again or use another contact option.' , $lang );
$nonce = $_POST [ 'mlhc_nonce' ] ?? '' ; $started = $_POST [ 'mlhc_started' ] ?? '' ; $signature = $_POST [ 'mlhc_signature' ] ?? '' ;
if ( ! is_string ( $nonce ) || ! wp_verify_nonce ( $nonce , 'hgcf_send' ) || ! is_string ( $started ) || ! ctype_digit ( $started ) || ! is_string ( $signature ) || ! hash_equals ( hash_hmac ( 'sha256' , $started , wp_salt ( 'nonce' )), $signature ) || time () - ( int ) $started < 2 || time () - ( int ) $started > DAY_IN_SECONDS || ! empty ( $_POST [ 'mlhc_website' ])) {
$GLOBALS [ 'mlhc_errors' ] = new WP_Error ( 'security' , $error ); return ;
}
$submitted = []; foreach ([ 'name' , 'email' , 'phone' , 'message' , 'mode' , 'massage' , 'preferred_date' , 'preferred_time' , 'consent' ] as $field ) { if ( isset ( $_POST [ 'mlhc_' . $field ])) $submitted [ $field ] = $_POST [ 'mlhc_' . $field ]; }
$data = hgcf_validate ( $submitted , $lang );
if ( is_wp_error ( $data )) { $GLOBALS [ 'mlhc_errors' ] = $data ; return ; }
$ip = $_SERVER [ 'REMOTE_ADDR' ] ?? 'unknown' ;
$rate_key = 'mlhc_rate_' . hash_hmac ( 'sha256' , $ip , wp_salt ( 'auth' ));
$rate = get_transient ( $rate_key );
if ( $rate && ( $rate [ 'count' ] >= 5 || time () - $rate [ 'last' ] < 30 )) { $GLOBALS [ 'mlhc_errors' ] = new WP_Error ( 'rate' , hgcf_text ( 'Even geduld: probeer het over een minuut opnieuw of gebruik een andere contactmogelijkheid.' , 'Please wait a minute before trying again, or use another contact option.' , $lang )); return ; }
$rate = $rate ?: [ 'count' => 0 , 'last' => 0 ]; $rate [ 'count' ] ++ ; $rate [ 'last' ] = time (); set_transient ( $rate_key , $rate , 15 * MINUTE_IN_SECONDS );
if ( ! hgcf_send ( $data , $lang )) { $GLOBALS [ 'mlhc_errors' ] = new WP_Error ( 'mail' , $error ); return ; }
$url = add_query_arg ([ 'contact_status' => 'received' , 'receipt' => wp_create_nonce ( 'mlhc_received_' . $id )], get_permalink ( $id ));
wp_safe_redirect ( $url . '#contact-form' , 303 ); exit ;
});
add_shortcode ( 'hg_contactform' , 'hgcf_render' );
add_shortcode ( 'mlh_contact' , 'hgcf_render' );
add_action ( 'wp_enqueue_scripts' , function (){
2026-10-12 13:38:59 +02:00
$post = get_post (); if ( is_singular () && $post && ( has_shortcode ( $post -> post_content , 'hg_contactform' ) || has_shortcode ( $post -> post_content , 'mlh_contact' ))) wp_enqueue_style ( 'hgcf' , plugins_url ( 'assets/contact.css' , __FILE__ ),[], HGCF_VERSION );
2026-10-11 21:15:13 +02:00
});