Skip to content

Commit

Permalink
Delay language matching for directory router until wp_loaded action
Browse files Browse the repository at this point in the history
  • Loading branch information
goncaloasimoes committed Feb 16, 2023
1 parent 29dd39a commit aea9605
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions lib/Router/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ class Directory {
*/
public function init() : void {

// Trailing slash to handle cases like homepage when url/uri does not have / at the end.
$request_uri = trailingslashit( $this->clean_path( $_SERVER['REQUEST_URI'] ) );
$lang = $this->current_lang_from_uri( Options::get()['default_language'], $request_uri );
if ( $lang === Options::get()['default_language'] ) {
return;
}
// Delayed until `wp_loaded` to let `ubb_options` filter to be added before the directory language is found.
add_action( 'wp_loaded', function () {

// Trailing slash to handle cases like homepage when url/uri does not have / at the end.
$request_uri = trailingslashit( $this->clean_path( $_SERVER['REQUEST_URI'] ) );
$lang = $this->current_lang_from_uri( Options::get()['default_language'], $request_uri );
if ( $lang === Options::get()['default_language'] ) {
return;
}

$directory = $this->get_directory_name( $lang );
if ( str_starts_with( $request_uri, "/{$directory}/" ) ) {
$_GET['lang'] = $lang;
}
$directory = $this->get_directory_name( $lang );
if ( str_starts_with( $request_uri, "/{$directory}/" ) ) {
$_GET['lang'] = $lang;
}
} );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion unbabble.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Unbabble
* Plugin URI: https://github.com/26B/unbabble
* Description: A new and simple i18n system for WordPress
* Version: 0.0.1
* Version: 0.0.5
* Author: 26B
* Author URI: https://26b.io/
* License: GPL-2.0+
Expand Down

0 comments on commit aea9605

Please sign in to comment.