Compare commits
7 Commits
41f44b1200
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5345b86a25 | ||
|
|
f458be3293 | ||
|
|
a08d0527b4 | ||
|
|
458c216fea | ||
|
|
5828269a70 | ||
|
|
d4be70eb78 | ||
|
|
f1d0414f58 |
167
admin.php
Normal file
167
admin.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
function init_postode_admin()
|
||||
{
|
||||
add_action("admin_menu", "local_postcodes_admin_menu", 99);
|
||||
add_action("admin_head", "local_postcodes_admin_css");
|
||||
add_action("admin_init", "local_postcodes_register_settings"); // Correct
|
||||
}
|
||||
|
||||
function local_postcodes_admin_menu()
|
||||
{
|
||||
add_submenu_page(
|
||||
"woocommerce", // Parent menu slug
|
||||
"Lokale Postcodes", // Page title
|
||||
"Lokale Postcodes", // Menu title
|
||||
"manage_options", // Capability
|
||||
"local-postcodes", // Menu slug
|
||||
"generate_admin_page", // Callback function
|
||||
"dashicons-buddicons-pm", // Optional icon URL or dashicon
|
||||
);
|
||||
}
|
||||
|
||||
function local_postcodes_register_settings()
|
||||
{
|
||||
register_setting(
|
||||
"my_plugin_options",
|
||||
"local_postcodes_values",
|
||||
"local_postcodes_input_validate",
|
||||
);
|
||||
}
|
||||
|
||||
function generate_admin_page()
|
||||
{
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><span class="dashicons dashicons-admin-home"></span> Lokale Postcodereeksen</h1>
|
||||
<?php settings_errors("local_postcodes_error_messages"); ?>
|
||||
<div class="info">
|
||||
<p class="info">Vul hier de lokale postcodereeksen in.</p>
|
||||
<ul>
|
||||
<li>Het formaat voor een reeks is #### - #### (voorbeeld: 5000 - 5199).</li>
|
||||
<li>Plaats één reeks op één regel.</li>
|
||||
<li>Een reeks van 1 is geldig. (voorbeeld: 5000 - 5000)</li>
|
||||
<li>Om een reeks te verwijderen. Wis de reeks en sla vervolgens de wijzigen op.</li>
|
||||
</ul>
|
||||
</div>
|
||||
<form id="postcodes_form" method="post" action="options.php">
|
||||
<?php
|
||||
settings_fields("my_plugin_options");
|
||||
do_settings_sections("my_plugin_options");
|
||||
?>
|
||||
<textarea id="local_postcodes_values" name="local_postcodes_values"><?php echo esc_textarea(
|
||||
get_option("local_postcodes_values"),
|
||||
); ?></textarea>
|
||||
<?php submit_button(); ?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
function local_postcodes_admin_css()
|
||||
{
|
||||
?>
|
||||
<style>
|
||||
h1 .dashicons{
|
||||
font-size: 25px;
|
||||
|
||||
}
|
||||
|
||||
.info {
|
||||
font-size: 1.1rem;
|
||||
max-width: 60ch;
|
||||
|
||||
ul {
|
||||
list-style: square inside;
|
||||
}
|
||||
}
|
||||
|
||||
#postcodes_form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
max-width: 60ch;
|
||||
align-items: flex-end;
|
||||
font-size: 1.1rem;
|
||||
|
||||
#local_postcodes_values{
|
||||
width: 100% !important;
|
||||
height: 50vh !important;
|
||||
font-size: 1.1rem;
|
||||
font-family: "Consolas", "Monaco", "Courier New", monospace;
|
||||
}
|
||||
|
||||
|
||||
.submit{
|
||||
margin-top: .2rem;
|
||||
}
|
||||
}
|
||||
</style>';
|
||||
<?php
|
||||
}
|
||||
|
||||
function local_postcodes_input_validate($input)
|
||||
{
|
||||
$valid_input = "";
|
||||
$lines = preg_split("/\R/", $input, -1, PREG_SPLIT_NO_EMPTY);
|
||||
$pattern = '/^\d{4}\s*\-\s*\d{4}$/';
|
||||
$pc_arr = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if (preg_match($pattern, $line)) {
|
||||
$parts = explode("-", $line);
|
||||
$first = (int) $parts[0];
|
||||
$second = (int) $parts[1];
|
||||
|
||||
if ($first <= $second) {
|
||||
$pc_arr[] = [$first, $second];
|
||||
} else {
|
||||
$pc_arr[] = [$second, $first];
|
||||
}
|
||||
} else {
|
||||
add_settings_error(
|
||||
"local_postcodes_error_messages",
|
||||
"invalid_postcode_" . md5($line),
|
||||
'❌️ Ongeldige regel: "' .
|
||||
esc_html($line) .
|
||||
'". Gebruik het formaat #### - ####.(bijvoorbeeld: 5000 - 5199)',
|
||||
"error",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
usort($pc_arr, function ($a, $b) {
|
||||
return $a[0] <=> $b[0];
|
||||
});
|
||||
|
||||
$old_pc_arr_len = count($pc_arr);
|
||||
$pc_arr = array_map(
|
||||
"unserialize",
|
||||
array_unique(array_map("serialize", $pc_arr)),
|
||||
);
|
||||
$new_pc_arr_len = count($pc_arr);
|
||||
|
||||
if ($new_pc_arr_len < $old_pc_arr_len) {
|
||||
add_settings_error(
|
||||
"local_postcodes_error_messages",
|
||||
"Duplicaten",
|
||||
// @formatter:off
|
||||
"⚠️ " .
|
||||
$old_pc_arr_len -
|
||||
$new_pc_arr_len .
|
||||
" " .
|
||||
($old_pc_arr_len - $new_pc_arr_len > 1
|
||||
? "duplicaten"
|
||||
: "duplicaat") .
|
||||
" verwijderd.",
|
||||
// @formatter:on
|
||||
"warning",
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($pc_arr as $range) {
|
||||
$valid_input .= $range[0] . " - " . $range[1] . "\n";
|
||||
}
|
||||
|
||||
return $valid_input;
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
<?php
|
||||
|
||||
require_once plugin_dir_path(__FILE__) . "session_dialog.php";
|
||||
require_once plugin_dir_path(__FILE__) . "admin.php";
|
||||
/*
|
||||
* Plugin Name: prijzen per poscode range
|
||||
* Description: posctcodes in de 5000-5800 range krijgen een lokaal tarief aangeboden.
|
||||
* Author: Remo Zaros
|
||||
* Version: 0.9.3
|
||||
* Version: 0.9.5
|
||||
* Text Domeain: prijs-per-postcode
|
||||
*/
|
||||
|
||||
@@ -30,6 +31,8 @@ class PrijsPerPostcode
|
||||
}
|
||||
$uri = $_SERVER["REQUEST_URI"];
|
||||
init_postcode_handlers($uri);
|
||||
init_postode_admin();
|
||||
|
||||
add_filter("woocommerce_sale_flash", "__return_null");
|
||||
|
||||
//add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
session_start();
|
||||
function init_postcode_handlers($uri)
|
||||
{
|
||||
if (strpos($uri, "/winkel/") !== false) {
|
||||
@@ -72,14 +71,14 @@ function send_postcode_data()
|
||||
console.log("Data returnd", data);
|
||||
|
||||
if (data.status === "error"){
|
||||
const err = data.message;
|
||||
const err = data.code;
|
||||
let errmsg
|
||||
|
||||
switch (err) {
|
||||
case "Huisnummer not found":
|
||||
case "HUISNUMMER_NOT_FOUND":
|
||||
errmsg = "Adres niet gevonden.";
|
||||
break;
|
||||
case "Multiple addresses match this huisnummer; add huisletter and/or huisnummertoevoeging":
|
||||
case "HUISNUMMER_AMBIGUOUS":
|
||||
errmsg = "Huisnummertovoeging mist.";
|
||||
break;
|
||||
default:
|
||||
@@ -116,7 +115,7 @@ function render_dialog_html()
|
||||
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"
|
||||
placeholder="5010 AA"
|
||||
size="10"
|
||||
required
|
||||
autocomplete="off"
|
||||
@@ -272,6 +271,15 @@ function getStraatnaam($postcode, $huisnummer)
|
||||
|
||||
function postcode_in_range($postcode)
|
||||
{
|
||||
$vals = get_option("local_postcodes_values", "");
|
||||
$rows = preg_split("/\R/", $vals, -1, PREG_SPLIT_NO_EMPTY);
|
||||
$pc_arr = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$row = trim($row);
|
||||
$postcode_range = explode("-", $row);
|
||||
$pc_arr[] = [(int) $postcode_range[0], (int) $postcode_range[1]];
|
||||
}
|
||||
$cleanPostcode = strtoupper(preg_replace("/\s+/", "", $postcode));
|
||||
|
||||
if (!preg_match('/^\d{4}[A-Z]{2}$/', $cleanPostcode)) {
|
||||
@@ -280,10 +288,12 @@ function postcode_in_range($postcode)
|
||||
|
||||
$numberPart = (int) substr($cleanPostcode, 0, 4);
|
||||
|
||||
return ($numberPart >= 4800 && $numberPart <= 4899) ||
|
||||
($numberPart >= 5000 && $numberPart <= 5199) ||
|
||||
($numberPart >= 5260 && $numberPart <= 5268) ||
|
||||
($numberPart >= 5688 && $numberPart <= 5689);
|
||||
foreach ($pc_arr as $pc_to_check) {
|
||||
if ($numberPart >= $pc_to_check[0] && $numberPart <= $pc_to_check[1]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function modify_checkout_with_js()
|
||||
@@ -315,7 +325,7 @@ function modify_checkout_with_js()
|
||||
if (typeof wp !== 'undefined' && wp.data && wp.data.dispatch) {
|
||||
const store = 'wc/store/cart';
|
||||
|
||||
wp.data.dispatch(store).setShippingAddress({
|
||||
wp.data.dispatch(store).setBillingAddress({
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
address_1: '<?php echo esc_js($address); ?>',
|
||||
@@ -331,7 +341,7 @@ function modify_checkout_with_js()
|
||||
//make fields READONLY and ppstcode reset.
|
||||
setTimeout(() => {
|
||||
// make prefilled fiields readonly.
|
||||
$('#shipping-postcode, #shipping-city, #shipping-address_1')
|
||||
$('#billing-postcode, #billing-city, #billing-address_1')
|
||||
.prop('readonly', true)
|
||||
.css('background', '#f9f9f9');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user