143 lines
3.8 KiB
PHP
143 lines
3.8 KiB
PHP
<?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>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;
|
|
}
|
|
|
|
|
|
.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",
|
|
);
|
|
}
|
|
}
|
|
|
|
// Sorteer de VOLLEDIGE array NA de foreach-lus
|
|
usort($pc_arr, function ($a, $b) {
|
|
return $a[0] <=> $b[0];
|
|
});
|
|
|
|
// Bouw $valid_input op uit de gesorteerde $pc_arr
|
|
foreach ($pc_arr as $range) {
|
|
$valid_input .= $range[0] . " | " . $range[1] . "\n";
|
|
}
|
|
|
|
return $valid_input;
|
|
}
|