fix: step navigation back-click bug and add shortcode lang attribute

Track highestStep so navigating backward preserves completed dots and
allows clicking forward to any previously visited step. Add [umzugsliste
lang="de|en"] shortcode attribute that switches locale via
switch_to_locale() for per-page language control.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 22:54:55 +09:00
parent 39f94a6b2e
commit b9ae7d707d
3 changed files with 34 additions and 8 deletions

View File

@@ -13,6 +13,7 @@
var l10n = typeof umzugslisteL10n !== 'undefined' ? umzugslisteL10n : {};
var TOTAL_STEPS = 9;
var currentStep = 1;
var highestStep = 1;
// ===== Utility Helpers =====
@@ -57,6 +58,7 @@
if (target) target.classList.add('active');
currentStep = n;
if (n > highestStep) highestStep = n;
updateProgressBar();
updateNavButtons();
updateRunningTotalsVisibility();
@@ -90,7 +92,7 @@
dot.classList.remove('active', 'completed');
if (step === currentStep) {
dot.classList.add('active');
} else if (step < currentStep) {
} else if (step <= highestStep) {
dot.classList.add('completed');
}
});
@@ -98,7 +100,7 @@
// Update progress fill
var fill = qs('#progress-fill');
if (fill) {
var pct = ((currentStep - 1) / (TOTAL_STEPS - 1)) * 100;
var pct = ((highestStep - 1) / (TOTAL_STEPS - 1)) * 100;
fill.style.width = pct + '%';
}
}
@@ -510,11 +512,11 @@
});
}
// Progress dot click (backward navigation only)
// Progress dot click (navigate to any visited step)
qsa('.progress-dot').forEach(function(dot) {
dot.addEventListener('click', function() {
var step = parseInt(this.getAttribute('data-step'), 10);
if (step < currentStep) {
if (step <= highestStep && step !== currentStep) {
showStep(step);
}
});

View File

@@ -51,11 +51,26 @@ class Umzugsliste_Shortcode {
* @return string Form HTML
*/
public function render_form( $atts ) {
// Ensure assets are enqueued
$this->enqueue_assets();
$atts = shortcode_atts( array( 'lang' => '' ), $atts, 'umzugsliste' );
$switched = false;
// Render the form
return Umzugsliste_Form_Renderer::render();
if ( ! empty( $atts['lang'] ) ) {
$locale_map = array( 'de' => 'de_DE', 'en' => 'en_US' );
$locale = isset( $locale_map[ $atts['lang'] ] ) ? $locale_map[ $atts['lang'] ] : '';
if ( $locale && $locale !== get_locale() ) {
switch_to_locale( $locale );
$switched = true;
}
}
$this->enqueue_assets();
$html = Umzugsliste_Form_Renderer::render();
if ( $switched ) {
restore_previous_locale();
}
return $html;
}
/**

View File

@@ -126,6 +126,15 @@ class Umzugsliste {
}
if ( $use_standalone ) {
// Extract lang from shortcode if present and switch locale before template loads
$post = get_queried_object();
if ( $post && isset( $post->post_content ) && preg_match( '/\[umzugsliste[^\]]*lang=["\'](\w+)["\']/', $post->post_content, $m ) ) {
$locale_map = array( 'de' => 'de_DE', 'en' => 'en_US' );
if ( isset( $locale_map[ $m[1] ] ) && $locale_map[ $m[1] ] !== get_locale() ) {
switch_to_locale( $locale_map[ $m[1] ] );
}
}
$custom_template = UMZUGSLISTE_PLUGIN_DIR . 'templates/form-page.php';
if ( file_exists( $custom_template ) ) {
return $custom_template;