From b9ae7d707d3aebf5eb3210c2cec3648a635c751d Mon Sep 17 00:00:00 2001 From: Viktor Miller Date: Sat, 7 Feb 2026 22:54:55 +0900 Subject: [PATCH] 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 --- assets/js/form.js | 10 ++++++---- includes/class-shortcode.php | 23 +++++++++++++++++++---- umzugsliste.php | 9 +++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/assets/js/form.js b/assets/js/form.js index 3ee47d5..687990f 100644 --- a/assets/js/form.js +++ b/assets/js/form.js @@ -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); } }); diff --git a/includes/class-shortcode.php b/includes/class-shortcode.php index e0ead90..c51eed1 100644 --- a/includes/class-shortcode.php +++ b/includes/class-shortcode.php @@ -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; } /** diff --git a/umzugsliste.php b/umzugsliste.php index 562fa70..88ed162 100644 --- a/umzugsliste.php +++ b/umzugsliste.php @@ -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;