diff --git a/includes/functions.php b/includes/functions.php index 18f0e38..fd0fef5 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,101 +1,105 @@ . function license($code) { return Licenses::instance()->get($code); } function request_uri() { return URL_protocol() . URL_domain() . ROOT . $_SERVER['REQUEST_URI']; } /** * Generate a stupid MySQL COALESCE() clause to select the most appropriate i18n database column * * @param string $name Base virtual name of the column * @param mixed $i18n Associative array of languages and column names, or a generic column name like 'column_%s' * @return string */ function i18n_coalesce( $name, $i18n ) { // current language (e.g. 'en') $lang = latest_language()->getISO(); // default language of the website $default = 'it'; // allow a generic string if( is_string( $i18n ) ) { $generic = $i18n; $i18n = []; $i18n[ $lang ] = sprintf( $generic, $lang ); $i18n[ $default ] = sprintf( $generic, $default ); } // base priority column name $base_priority = $i18n[ $default ]; // try to catch the more specific column, then the default if( $lang !== $default && isset( $i18n[ $lang ] ) ) { $more_priority = $i18n[ $lang ]; return "COALESCE( `$more_priority`, `$base_priority` ) AS $name"; } // just pick the default return "`$base_priority` AS $name"; } /** * Eventually redirect the request to this permalink * * It's allowed the 'lang' parameter to be present or not. * * @param string $permalink URL of the incoming request (absolute or relative) * @return string */ function force_permalink( $permalink ) { // incoming URL $from = BASE_URL . $_SERVER['REQUEST_URI']; // force absolute $permalink = site_page( $permalink, true ); // if it's the same request, do nothing if( $from === $permalink ) { return; } + // do not send a 301 Moved Permanently if the language was missing (maybe the user was just redirected to his language) + $status = isset( $_GET['l'] ) + ? 301 // 301 Moved Permanently + : 300; // 300 Multiple Choices + // redirect to the correct location - http_redirect( $permalink, 301 ); + http_redirect( $permalink, $status ); } /** * Force an URL to have the language argument it it requested the page with it * * @return string */ -function keep_url_in_language( $url, $force = false ) { +function keep_url_in_language( $url ) { - if( isset( $_GET['l'] ) || $force ) { - $url = http_build_get_query( $url, [ - 'l' => latest_language()->getCode(), - ] ); - } + $code = latest_language()->getCode(); + $url = http_build_get_query( $url, [ + 'l' => latest_language()->getISO(), + ] ); return $url; }