swap session with cookie with hash

This commit is contained in:
Remo Zaros
2026-06-22 09:34:14 +02:00
parent ba63181e6a
commit e5e17cbafc
4 changed files with 224 additions and 64 deletions

View File

@@ -5,7 +5,7 @@ function init_postcode_handlers($uri)
if (strpos($uri, "/winkel/") !== false) {
add_action("wp_enqueue_scripts", "modal_styles");
add_action("wp_footer", "send_postcode_data");
if (!has_postcode()) {
if (!verify_pppr_cookie_string()) {
if (!is_admin()) {
WC()->cart->empty_cart();
}
@@ -33,9 +33,9 @@ function show_modal()
{
echo <<<'HTML'
<script id="postcode_modal_open">
const postcodeModal = document.querySelector("#postcode_modal");
postcodeModal.showModal();
</script>
const postcodeModal = document.querySelector("#postcode_modal");
postcodeModal.showModal();
</script>
HTML;
}
@@ -185,14 +185,6 @@ function render_dialog_html()
HTML;
}
function has_postcode()
{
if (isset($_SESSION["postcode"])) {
return true;
}
return false;
}
function handle_postcode_modal($data)
{
$params = $data->get_params();
@@ -276,23 +268,13 @@ function handle_postcode_modal($data)
]);
exit();
}
$cookie_val = postcode_in_range($params["postcode"]).'|'.$params["postcode"].'|'.$straatnaam.'|'.$params["huisnummer"].'|'.$woonplaats.'|';
// 6. Succes: Sessie opslaan & response
$_SESSION["postcode"] = $params["postcode"];
$_SESSION["huisnummer"] = $params["huisnummer"];
$_SESSION["straatnaam"] = $straatnaam;
$_SESSION["woonplaats"] = $woonplaats;
$_SESSION["postcode_is_local"] = postcode_in_range($params["postcode"]);
setcookie("PPPR", $cookie_val, [
'expires' => time() + 3600,
'path' => '/',
'domain' => $_SERVER['SERVER_NAME'],
'secure' => true, // Required for cross-origin fetch
'httponly' => true, // Prevents JavaScript access (XSS protection)
'samesite' => 'Lax' // Use 'None' for cross-origin, requires secure=true
]);
set_pppr_cookie(
postcode_in_range($params["postcode"]),
$straatnaam,
$params["huisnummer"],
$params["postcode"],
$woonplaats
);
echo json_encode([
"status" => "success",
@@ -403,15 +385,16 @@ function modify_checkout_with_js()
) {
return;
}
$data = get_PPPR_data();
$woonplaats = $_SESSION["woonplaats"];
$city = $data["city"];
$postcode = $formatted_postcode = preg_replace(
"/(\d+)([A-Z]+)/",
'$1 $2',
strtoupper($_SESSION["postcode"]),
strtoupper($data["postcode"]),
);
$address =
$_SESSION["straatnaam"] . " " . strtoupper($_SESSION["huisnummer"]);
$data["street"] . " " . strtoupper($data["house_number"]);
echo <<<HTML
<script type="text/javascript" id="fill_address_fields">
@@ -429,7 +412,7 @@ function modify_checkout_with_js()
last_name: '',
address_1: '{$address}',
address_2: '',
city: '{$woonplaats}',
city: '{$city}',
state: '',
postcode: '{$postcode}',
country: 'NL',
@@ -465,10 +448,6 @@ function modify_checkout_with_js()
HTML;
}
function load_assets_reset_postcode_on_checkout()
{
if (is_checkout() && !is_wc_endpoint_url()) {
@@ -501,11 +480,7 @@ function handle_unset_session_fetch()
if (!wp_verify_nonce($_POST["nonce"], "reset_postcode_nonce")) {
wp_die("Security check failed.");
}
// Unset the specific session variable
if (isset($_SESSION["postcode"])) {
$_SESSION = [];
}
unset_pppr_cookie();
// Send a JSON response
wp_send_json_success();