initial commit!

This commit is contained in:
Remo Zaros
2026-05-07 16:35:15 +02:00
commit 179da8d2b2
6 changed files with 147 additions and 0 deletions

3
assets/index.php Normal file
View File

@@ -0,0 +1,3 @@
<?php
// where you at?

View File

@@ -0,0 +1,7 @@
.postcode_dialog {
background: green;
}
body {
background-color: firebrick !important;
}

View File

3
index.php Normal file
View File

@@ -0,0 +1,3 @@
<?php
// where you at?

32
prijs-per-postcode.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
require_once "session_dialog.php";
/*
* Plugin Name: Prijs per Postcode
* Description: veschillende prijzen per postdoce. een range posctcodes zijn "lokaal" andere zijn overig
* Author: Remo Zaros
* Version: 0.0.1
* Text Domeain: prijs-per-postcode
*/
if (!defined("ABSPATH")) {
echo "big bag of potatoes";
exit();
}
class PrijsPerPostcode
{
public function __construct()
{
add_action("init", [$this, "init"]);
add_action("rest_api_init", "register_modal_api");
}
public function init()
{
$uri = $_SERVER["REQUEST_URI"];
if_needed_place_postcode_form($uri);
}
}
new PrijsPerPostcode();

102
session_dialog.php Normal file
View File

@@ -0,0 +1,102 @@
<?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">
<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}"
/>
<input type="text" name="huisnummer"
pattern="\d+([-\s]?[a-zA-Z]+)?"
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
/>
<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)
{
$header = $data->get_headers();
$params = $data->get_params();
$nonce = $headers["x_wp_nonce"][0];
if (wp_verify_nonce($nonce, "wp_rest")) {
echo $nonce;
}
}
function register_modal_api()
{
register_rest_route("postcode-modal/v1", "submit", [
"methods" => "POST",
"callback" => "handle_postcode_modal",
]);
}