Skip to content

Commit

Permalink
SLUGS: fix language autodetection
Browse files Browse the repository at this point in the history
  • Loading branch information
spleen1981 committed May 8, 2023
1 parent 9816c76 commit 171e2be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function qtranxf_init_language(): void {
$url_info['language'] = qtranxf_detect_language( $url_info );
$q_config['language'] = apply_filters( 'qtranslate_language', $url_info['language'], $url_info );

QTX_Module_Loader::load_active_modules_early_hooks();

assert( isset( $q_config['url_info']['doing_front_end'] ) );
if ( $q_config['url_info']['doing_front_end'] && qtranxf_can_redirect() ) {
qtranxf_check_url_maybe_redirect( $url_info );
Expand Down
20 changes: 20 additions & 0 deletions src/modules/module_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,24 @@ public static function load_active_modules(): void {
}
}
}

/**
* Loads early hooks from modules previously activated in the options.
*
* Attention! This assumes the current states stored in the options are valid.
* This doesn't perform any check, neither on the plugin conditions nor the folder structure.
* In the worst case the state can be refreshed by reactivating the plugin.
*
* Note also the modules should be loaded before "qtranslate_init_language" is triggered.
*/
public static function load_active_modules_early_hooks(): void {
$modules_state = get_option( QTX_OPTIONS_MODULES_STATE, array() );

foreach ( $modules_state as $module_id => $state ) {
$target = QTRANSLATE_DIR . '/src/modules/' . $module_id . '/' . 'early_hooks.php';
if ( $state === QTX_MODULE_STATE_ACTIVE && file_exists( $target ) ) {
require_once( $target );
}
}
}
}
27 changes: 27 additions & 0 deletions src/modules/slugs/early_hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

add_filter( 'qtranslate_language_detect_redirect', 'qtranxf_slugs_language_detect_redirect', 600, 3 );

/**
* Allows url redirection due to language auto detection only if site url has been requested
*
* @see qtranxf_check_url_maybe_redirect
* @param string $url_lang proposed target URL for the active language to redirect to.
* @param string $url_orig original URL supplied to browser, which needs to be standardized.
* @param array $url_info a hash of various information parsed from original URL, cookies and other site configuration. The key names should be self-explanatory.
*
* @return string resulting redirection url
*/
function qtranxf_slugs_language_detect_redirect($url_lang, $url_orig, $url_info): string {
global $q_config;
if (site_url().'/' === $url_orig)
return $url_lang;
else {
if ( empty( $url_info['lang_url'] ) ){
return qtranxf_convertURL( $url_orig, $q_config['default_language'],false,true );
} else {
return $url_orig;
}
}
}

0 comments on commit 171e2be

Please sign in to comment.