Files
wp-postcode-plugin/session_dialog.php
2026-05-07 17:51:53 +02:00

154 lines
3.8 KiB
PHP

<?php
function if_needed_place_postcode_form($uri)
{
if (strpos($uri, "/shop") !== false || strpos($uri, "/winkel") !== false) {
render_dialog_html();
add_action("wp_enqueue_scripts", "dialog_styles");
add_action("wp_footer", "send_postcode_data");
if (!has_postcode()) {
add_action("wp_footer", "show_modal");
}
}
}
function dialog_styles()
{
wp_enqueue_style(
"prijs-per-postcode",
plugins_url("/assets/postcode_dialog.css", __FILE__),
);
}
function show_modal()
{
?>
<script>
const postcodeModal = document.querySelector("#postcode_modal");
postcodeModal.showModal();
</script>
<?php
}
function send_postcode_data()
{
?>
<script>
const submitBtn = document.querySelector("#postcode_modal_form");
submitBtn.addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const data = Object.fromEntries(formData.entries());
const json = JSON.stringify(data);
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
});
});
</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"
/>
<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"
/>
<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")) {
$resp = [
"status" => "succes",
"message" => $nonce,
];
} 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 verifyPostcode()
{
if (preg_match('/^[0-9]{4}\s?[A-Za-z]{2}$/', $postcode)) {
return true;
}
return false;
}
function verifyHuisnummer() {}
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;
}