Skip to content

Commit

Permalink
Draft of hreflang with blog_id
Browse files Browse the repository at this point in the history
  • Loading branch information
lloc committed Oct 28, 2023
1 parent 63798ce commit 2acfca5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
37 changes: 24 additions & 13 deletions includes/Map/HrefLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,40 @@
class HrefLang {

/**
* @var array
* @var string[]
*/
protected $map = [];

/**
* @param MslsBlogCollection $blogs
*/
public function __construct( MslsBlogCollection $blogs ) {
$map = [];
foreach ( $blogs->get_objects() as $blog ) {
$map[ $blog->get_alpha2() ][] = $blog->get_language();
}

foreach ( $map as $alpha2 => $languages ) {
foreach ( self::map_languages_from( $blogs ) as $alpha2 => $languages ) {
if ( 1 == count( $languages ) ) {
$this->map[ $languages[0] ] = $alpha2;
$this->map[ current( $languages) ][ key( $languages ) ] = $alpha2;
} else {
foreach ( $languages as $language ) {
$this->map[ $language ] = $this->get_hreflang( $language );
foreach ( $languages as $blog_id => $language ) {
$this->map[ $language ][ $blog_id ] = $this->get_hreflang( $language );
}
}
}
}

/**
* @param MslsBlogCollection $blogs
*
* @return array
*/
public static function map_languages_from( MslsBlogCollection $blogs ): array {
$map = [];

foreach ( $blogs->get_objects() as $blog ) {
$map[ $blog->get_alpha2() ][ $blog->userblog_id ] = $blog->get_language();
}

return $map;
}

/**
* @param string $language
*
Expand All @@ -51,19 +61,20 @@ protected function get_hreflang( string $language ): string {
*
* @return string
*/
public function get( string $language ): string {
public function get( string $language, int $blog_id ): string {
if ( ! has_filter( 'msls_head_hreflang' ) ) {
return $this->map[ $language ] ?? $language;
return $this->map[ $language ][ $blog_id ] ?? $language;
}

/**
* Overrides the hreflang value
*
* @param string $language
* @param int $blog_id
*
* @since 0.9.9
*/
return (string) apply_filters( 'msls_head_hreflang', $language );
return (string) apply_filters( 'msls_head_hreflang', $language, $blog_id );
}

}
2 changes: 1 addition & 1 deletion includes/MslsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function get_alternate_links() {
$default = sprintf( $format, 'x-default', $url, esc_attr( $description ) );
}

$arr[] = sprintf( $format, $hreflang->get( $blog->get_language() ), $url, esc_attr( $description ) );
$arr[] = sprintf( $format, $hreflang->get( $blog->get_language(), $blog->userblog_id ), $url, esc_attr( $description ) );
}

if ( 1 === count( $arr ) ) {
Expand Down
28 changes: 17 additions & 11 deletions tests/Map/test-hreflang.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public function get_sut() {
'en_GB' => 'en',
];

$i = 1;
foreach ( $map as $locale => $alpha2 ) {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( [
'get_alpha2' => $alpha2,
'get_language' => $locale,
'get_alpha2' => $alpha2,
] );
$blog->userblog_id = $i++;

$blogs[] = $blog;
}
Expand All @@ -41,22 +43,26 @@ public function get_sut() {
public function test_get() {
$obj = $this->get_sut();

$this->assertEquals( 'de-DE', $obj->get( 'de_DE' ) );
$this->assertEquals( 'de-DE', $obj->get( 'de_DE_formal' ) );
$this->assertEquals( 'fr', $obj->get( 'fr_FR' ) );
$this->assertEquals( 'es', $obj->get( 'es_ES' ) );
$this->assertEquals( 'cat', $obj->get( 'cat' ) );
$this->assertEquals( 'en-GB', $obj->get( 'en_GB' ) );
$this->assertEquals( 'en-US', $obj->get( 'en_US' ) );
Functions\expect( 'has_filter' )->with( 'msls_head_hreflang' )->times( 8 )->andReturn( false );

$this->assertEquals( 'de-DE', $obj->get( 'de_DE', 1 ) );
$this->assertEquals( 'de-DE', $obj->get( 'de_DE_formal', 2 ) );
$this->assertEquals( 'fr', $obj->get( 'fr_FR', 3 ) );
$this->assertEquals( 'es', $obj->get( 'es_ES', 4 ) );
$this->assertEquals( 'cat', $obj->get( 'cat', 5 ) );
$this->assertEquals( 'en-US', $obj->get( 'en_US', 6 ) );
$this->assertEquals( 'en-GB', $obj->get( 'en_GB', 7 ) );

$this->assertEquals( 'en_US', $obj->get( 'en_US', 8 ) );
}

public function test_get_has_filter() {
$obj = $this->get_sut();

Functions\when( 'has_filter' )->justReturn( true );
Filters\expectApplied('msls_head_hreflang')->once()->with( 'en_US')->andReturn( 'en-US' );
Functions\expect( 'has_filter' )->with( 'msls_head_hreflang' )->once()->andReturn( true );
Filters\expectApplied('msls_head_hreflang')->with( 'en_US')->once()->andReturn( 'en-US' );

$this->assertEquals( 'en-US', $obj->get( 'en_US' ) );
$this->assertEquals( 'en-US', $obj->get( 'en_US', 8 ) );
}

}

0 comments on commit 2acfca5

Please sign in to comment.