Files
wp-postcode-plugin/session_dialog.php
2026-05-08 15:53:44 +02:00

206 lines
5.2 KiB
PHP

<?php
session_start();
function if_needed_place_postcode_form($uri)
{
if (strpos($uri, "/shop") !== false || strpos($uri, "/winkel") !== false) {
render_dialog_html();
add_action("wp_enqueue_scripts", "modal_styles");
add_action("wp_footer", "send_postcode_data");
if (!has_postcode()) {
add_action("wp_footer", "show_modal");
}
}
}
function modal_styles()
{
wp_enqueue_style(
"prijs-per-postcode",
plugins_url("/assets/postcode_modal.css", __FILE__),
);
}
function show_modal()
{
?>
<h1>sdfsdfsdfsdfsdfsdfsdf</h1>
<script>
const postcodeModal = document.querySelector("#postcode_modal");
postcodeModal.showModal();
</script>
<?php
}
function send_postcode_data()
{
?>
<script type="module">
const postcodeModal = document.querySelector("#postcode_modal");
const submitBtn = document.querySelector("#postcode_modal_form");
submitBtn.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const data = Object.fromEntries(formData.entries());
const json = JSON.stringify(data);
try {
const resp = await fetch('<? echo get_rest_url(null, "postcode-modal/v1/submit"); ?>', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': '<? echo wp_create_nonce("wp_rest"); ?>'
},
body: json
});
if(!resp.ok){
throw new Error(`HTTP Error! status: ${resp.status}` );
}
const data = await resp.json();
console.log("Data returnd", data);
if (data.status === "error"){
}
if (data.status === "success"){
postcodeModal.close();
}
}catch(err){
console.error("Fetch Failed:", err);
throw err;
}
});
</script>
<?php
}
function render_dialog_html()
{
?>
<dialog id="postcode_modal" class="postcode_modal" closedby="none">
<h2>Vul je postcode en huisnummer in.</h2>
<form id="postcode_modal_form" method="post" action="">
<input type="text" name="postcode"
title="Voer een geldige Nederlandse postcode in (bijv. 1234AB of 1234 AB)."
pattern="[1-9][0-9]{3} ?(?!sa|sd|ss)[a-zA-Z]{2}"
placeholder= "1010 AA"
required
/>
<input type="text" name="huisnummer"
pattern="\d+([-\s]?[a-zA-Z]+)?"
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
placeholder= "12a"
required
/>
<button id="postcode_modal_submit" type="submit">verzend</button>
<form>
</dialog>
<?php
}
function has_postcode()
{
if (isset($_SESSION["postcode"])) {
return true;
}
return false;
}
function handle_postcode_modal($data)
{
$params = $data->get_params();
$nonce = $data->get_header("X-WP-Nonce");
if (wp_verify_nonce($nonce, "wp_rest")) {
if (!verify_postcode($params["postcode"])) {
$resp = [
"status" => "error",
"message" => "postcode",
];
echo $resp;
exit();
}
if (!verify_huisnummer($params["huisnummer"])) {
$resp = [
"status" => "error",
"message" => "huisnummer",
];
echo $resp;
exit();
}
$_SESSION["postcode"] = $params["postcode"];
$_SESSION["huisnummer"] = $params["huisnummer"];
$resp = [
"status" => "success",
"message" => "all good",
];
} else {
$resp = [
"status" => "error",
"message" => "nononce",
];
}
echo json_encode($resp);
exit();
}
function register_modal_api()
{
register_rest_route("postcode-modal/v1", "submit", [
"methods" => "POST",
"callback" => "handle_postcode_modal",
]);
}
function verify_postcode($postcode)
{
if (!preg_match('/^[0-9]{4}\s?[A-Za-z]{2}$/', $postcode) === 1) {
return false;
}
return true;
}
function verify_huisnummer($huisnummer)
{
if (!preg_match('/^[0-9]{4}\s?[A-Za-z]{2}$/', $huisnummer) === 1) {
return false;
}
return true;
}
function check_data_at_openpostcode($oostcode, $huisnummer)
{
$urk =
"https//openpostcode.nl/api/v2/address?postcode=" .
$postcode .
"&" .
$huisnummer;
$options = [
"http" => [
"method" => "GET",
"header" => "Accept: application/json\r\n",
],
"ssl" => [
"verify_peer" => true,
"verify_peer_name" => true,
],
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false) {
// Handle error
} else {
$data = json_decode($response, true);
}
return data;
}