Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Fix priority of language-only redirect matches. #234
Browse files Browse the repository at this point in the history
Close #234.
  • Loading branch information
tfrommen committed Oct 17, 2016
1 parent 26f6fb7 commit 9bff76b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
- Language API: Use fallback names for languages, see
[issue #224](https://github.com/inpsyde/multilingual-press/issues/224).

## 2.4.8
- Install/Uninstall: Prevent potential PHP notice due to deprecated `wp_get_sites()` function, see
[issue #229](https://github.com/inpsyde/multilingual-press/issues/229) and
[issue #236](https://github.com/inpsyde/multilingual-press/issues/236).
- Language Manager: Fix HTTP code of _Chinese (Singapore)_, see
[issue #232](https://github.com/inpsyde/multilingual-press/issues/232).
- Language Manager: Fix HTTP code of _French (Switzerland)_, see
[issue #233](https://github.com/inpsyde/multilingual-press/issues/233).
- Redirect: Fix priority of language-only redirect matches, see
[issue #234](https://github.com/inpsyde/multilingual-press/issues/234).

## 2.4.7
- CSS: Use font weight "600" instead of "bold", see
[Native Fonts in 4.6](https://make.wordpress.org/core/2016/07/07/native-fonts-in-4-6/).
Expand Down
34 changes: 28 additions & 6 deletions src/inc/redirect/Mlp_Language_Negotiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Mlp_Language_Negotiation implements Mlp_Language_Negotiation_Interface {
*/
private $language_api;

/**
* @var float
*/
private $language_only_priority_factor;

/**
* @var AcceptHeaderParser
*/
Expand All @@ -35,6 +40,20 @@ public function __construct( Mlp_Language_Api_Interface $language_api, AcceptHea
$this->language_api = $language_api;

$this->parser = $parser ?: new AcceptLanguageParser();

/**
* Filters the factor used to compute the priority of language-only matches. This has to be between 0 and 1.
*
* @see get_user_priority()
* @since 2.4.8
*
* @param float $factor The factor used to compute the priority of language-only matches.
*/
$factor = (float) apply_filters( 'multilingualpress.language_only_priority_factor', .7 );
$factor = min( 1, $factor );
$factor = max( 0, $factor );

$this->language_only_priority_factor = $factor;
}

/**
Expand Down Expand Up @@ -148,15 +167,17 @@ private function sort_priorities( $a, $b ) {
*/
private function get_user_priority( Language $language, array $user ) {

$lang_http = $language->name( 'http_name' );
$lang_http = strtolower( $language->name( 'http_name' ) );

if ( isset ( $user[ $lang_http ] ) )
if ( isset( $user[ $lang_http ] ) ) {
return $user[ $lang_http ];
}

$lang_short = $language->name( 'language_short' );
$lang_short = strtolower( $language->name( 'language_short' ) );

if ( isset ( $user[ $lang_short ] ) )
return $user[ $lang_short ];
if ( isset( $user[ $lang_short ] ) ) {
return $this->language_only_priority_factor * $user[ $lang_short ];
}

return 0;
}
Expand All @@ -177,12 +198,13 @@ private function parse_accept_header( $accept_header ) {
$out = [];

foreach ( $fields as $name => $priority ) {
$name = strtolower( $name );

$out[ $name ] = $priority;

$short = $this->get_short_form( $name );

if ( ( $short !== $name ) && ! isset ( $out[ $short ] ) )
if ( $short && ( $short !== $name ) && ! isset ( $out[ $short ] ) )
$out[ $short ] = $priority;
}

Expand Down

0 comments on commit 9bff76b

Please sign in to comment.