From b3843b2e27e38c466ffbf528a8748073dfc11f81 Mon Sep 17 00:00:00 2001
From: rickard <reekrd@gmail.com>
Date: Wed, 9 Jun 2021 13:37:35 +0200
Subject: [PATCH 1/3] Run a filter beforing outputting the hreflan link

---
 qtranslate_frontend.php | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/qtranslate_frontend.php b/qtranslate_frontend.php
index 15a7a382..c1ee1568 100644
--- a/qtranslate_frontend.php
+++ b/qtranslate_frontend.php
@@ -55,7 +55,9 @@ function qtranxf_wp_head() {
     $hreflangs['x-default'] = qtranxf_convertURL( '', $q_config['default_language'] );
 
     foreach ( $hreflangs as $hreflang => $href ) {
-        echo '<link hreflang="' . $hreflang . '" href="' . $href . '" rel="alternate" />' . PHP_EOL;
+        if( apply_filters( 'qtranslate_add_hreflang', $hreflang, $href ) ) {
+            echo '<link hreflang="' . $hreflang . '" href="' . $href . '" rel="alternate" />' . PHP_EOL;            
+        }
     }
 }
 

From 47052a215a14c77bbbb611b6095a592472144a09 Mon Sep 17 00:00:00 2001
From: rickard <reekrd@gmail.com>
Date: Sun, 13 Jun 2021 12:50:15 +0200
Subject: [PATCH 2/3] Renamed filter. It now passes the array  of languages and
 is only called once

---
 qtranslate_frontend.php | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/qtranslate_frontend.php b/qtranslate_frontend.php
index c1ee1568..78a84ffb 100644
--- a/qtranslate_frontend.php
+++ b/qtranslate_frontend.php
@@ -54,10 +54,10 @@ function qtranxf_wp_head() {
     // Fallback for unmatched language (default hreflang for SEO)
     $hreflangs['x-default'] = qtranxf_convertURL( '', $q_config['default_language'] );
 
+    $hreflangs = apply_filters( 'qtranslate_hreflangs', $hreflangs );
+
     foreach ( $hreflangs as $hreflang => $href ) {
-        if( apply_filters( 'qtranslate_add_hreflang', $hreflang, $href ) ) {
-            echo '<link hreflang="' . $hreflang . '" href="' . $href . '" rel="alternate" />' . PHP_EOL;            
-        }
+        echo '<link hreflang="' . $hreflang . '" href="' . $href . '" rel="alternate" />' . PHP_EOL;
     }
 }
 

From 2e964ed891f98fb06462b466e94ea049bd445a28 Mon Sep 17 00:00:00 2001
From: rickard <reekrd@gmail.com>
Date: Tue, 17 Jan 2023 17:55:48 +0100
Subject: [PATCH 3/3] Customized for VH

---
 qtranslate_core.php | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/qtranslate_core.php b/qtranslate_core.php
index 0100e114..5b5c38e8 100644
--- a/qtranslate_core.php
+++ b/qtranslate_core.php
@@ -87,8 +87,10 @@ function qtranxf_init_language() {
              * @return mixed A new URL to be redirected to instead of $url_lang or "false" to cancel redirection.
              */
             $target = apply_filters( 'qtranslate_language_detect_redirect', $url_lang, $url_orig, $url_info );
+
             if ( $target !== false && $target != $url_orig ) {
-                wp_redirect( $target );
+                // rickfix,, we want redirects to be "permanent" and not default 302
+                wp_redirect( $target, 301 );
                 nocache_headers(); // prevent browser from caching redirection
                 exit();
             } else {
@@ -221,7 +223,7 @@ function qtranxf_detect_language( &$url_info ) {
     $url_info['language'] = $lang;
 
     // REST calls should be deterministic (stateless), no special language detection e.g. based on cookie
-    $url_info['set_cookie'] = ! wp_doing_ajax() && ! qtranxf_is_rest_request_expected();
+    $url_info['set_cookie'] = ! wp_doing_ajax() && ! wp_doing_cron() && ! qtranxf_is_rest_request_expected(); 
 
     /**
      * Hook for possible other methods