Skip to content

Commit

Permalink
fix, function and tests
Browse files Browse the repository at this point in the history
changed cf7a_get_browser_language_array structure and name, was planning to refactor it for language only but it is used in the Activator, maybe will refactor
  • Loading branch information
gardenboi committed Dec 15, 2023
1 parent e03ddac commit d1b0c2f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/CF7_AntiSpam_Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ public function cf7a_spam_filter( $spam ) {
$spam_score += $score_detection;
$reason['browser_language'] = 'missing browser language';
} else {
$languages_locales = cf7a_get_browser_language_array( $languages['browser_language'] );
$languages_locales = cf7a_get_browser_languages_locales_array( $languages['browser_language'] );
$languages['browser'] = $languages_locales['languages'];
}

Expand Down
50 changes: 25 additions & 25 deletions core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,36 @@ function cf7a_get_real_ip() {
*
* @return array assoc array of languages and locales
*/
function cf7a_get_browser_language_array( $languages_locales ) {
function cf7a_get_browser_languages_locales_array( $languages_locales ) {
$result = array_reduce(
explode( ',', $languages_locales ),
function( $res, $el ) {
// trim spaces and removes the semicolon.
$el = trim( $el );
if ( strpos( $el, ';' ) !== false ) {
$el = substr( $el, 0, strpos( $el, ';' ) );
}
if ( strlen( $el ) >= 5 ) {
/* split into key: language , value: locale */
$l = explode( '-', $el );
$res[ 'languages' ][] = strtolower($l[0]) ;
$res[ 'locales' ][] = strtoupper($l[1]) ;
} else if ( strlen( $el ) === 2 && ctype_alpha( $el ) ) {
/* otherwise keep key:language, value: '' (any locale) */
if ( ctype_lower( $el ) ) {
$res[ 'languages' ][] = $el ;
} else {
$res[ 'locales' ][] = $el ;
}
explode( ',', $languages_locales ),
function( $res, $el ) {
// trim spaces and removes the semicolon.
$el = trim( $el );
if ( strpos( $el, ';' ) !== false ) {
$el = substr( $el, 0, strpos( $el, ';' ) );
}
if ( strlen( $el ) >= 5 ) {
/* split into key: language , value: locale */
$l = explode( '-', $el );
$res[ 'languages' ][] = $l[0] ;
$res[ 'locales' ][] = $l[1] ;
} else if ( strlen( $el ) === 2 && ctype_alpha( $el ) ) {
/* otherwise keep key:language, value: '' (any locale) */
if ( ctype_lower( $el ) ) {
$res[ 'languages' ][] = $el ;
} elseif ( ctype_upper( $el ) ) {
$res[ 'locales' ][] = $el ;
}
return $res;
},
array()
}
return $res;
},
array()
);

if (! empty($result)) {
$result['languages'] = array_unique($result['languages']);
$result['locales'] = array_unique($result['locales']);
$result['languages'] = array_values(array_unique($result['languages'])) ;
$result['locales'] = array_values(array_unique($result['locales'])) ;
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion engine/CF7_AntiSpam_Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static function init_vars() {
),
'languages_locales' => array(
'allowed' => isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] )
? cf7a_get_browser_language_array( sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) )
? cf7a_get_browser_languages_locales_array( sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) )
: array(),
'disallowed' => array(),
),
Expand Down
12 changes: 5 additions & 7 deletions tests/PhpUnit/Tests/CF7_AntiSpam_FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testCf7a_reverse_ipv4() {
$this->returnValue( '1.1.168.192' );
}

public function testCf7a_get_browser_language_array() {
public function testCf7a_get_browser_languages_locales_array() {

$tests = array(
array(
Expand All @@ -55,29 +55,27 @@ public function testCf7a_get_browser_language_array() {
),
array(
"string" => 'da,en-GB;q=0.8,en;q=0.7',
"expected" => array( 'languages' => array( 'da', 'en' ), 'locales' => array( 'GB', 'US' ) )
"expected" => array( 'languages' => array( 'da', 'en' ), 'locales' => array( 'GB' ) )
),
array(
"string" => 'zh-CN, zh-TW; q = 0.9, zh-HK; q = 0.8, zh; q = 0.7, en; q = 0.6',
"expected" => array( 'languages' => array( 'zh', 'en' ), 'locales' => array( 'CN', 'TW', 'HK', 'US' ) )
"expected" => array( 'languages' => array( 'zh', 'en' ), 'locales' => array( 'CN', 'TW', 'HK' ) )
),
array(
"string" => 'en-US,en;q=0.9,de;q=0.8,es;q=0.7,fr;q=0.6,it;q=0.5,pt;q=0.4,ru;q=0.3,ja;q=0.2,zh-CN;q=0.1,zh-TW;q=0.1',
"expected" => array(
'languages' => array( 'en', 'de', 'es', 'fr', 'it', 'pt', 'ru', 'ja', 'zh' ),
'locales' => array( 'US', 'US', 'US', 'US', 'US', 'US', 'US', 'US', 'CN' )
'locales' => array( 'US', 'CN', 'TW' )
)
),
//ISSUE TEST CASE
// be-BY is Belarussian Belarus, nl-BE, fr-BE, de-BE is Belgium, so we need to discriminate for the second argument
array(
"string" => 'ru-RU, be-BY;q=0.9, en-US;q=0.8, en;q=0.7',
"expected" => array( 'languages' => array( 'ru', 'be', 'en' ), 'locales' => array( 'RU', 'BY', 'US' ) )
)
);

foreach ( $tests as $test ) {
$result = cf7a_get_browser_language_array( $test['string'] );
$result = cf7a_get_browser_languages_locales_array( $test['string'] );
$this->assertEquals( $test['expected'], $result, 'error expected ' . print_r( $test, true ) . " result " . print_r( $result, true ) );
}
}
Expand Down

0 comments on commit d1b0c2f

Please sign in to comment.